Hi @dale Dale. I was able to figure it out that you are supposed to access the data with the find method like:
GeometrySegment test= list.Find(typeof(GeometrySegment)) as GeometrySegment;
Now I’m having a different problem with. When I add the customdata to my object I define some properties to the instance before adding it to the object. After I retrieve the data back All my previously defined properties are gone. Any ideas what i’m doing wrong. the code I used is below.
class:
[System.Runtime.InteropServices.Guid("D11BBCBB-37B5-4C31-BF70-97A498D12514")]
public class GeometrySegment : Rhino.DocObjects.Custom.UserData
{
private Curve _baseCurve;
private AreaMassProperties _areaMassProp;
public Curve baseCurve
{
get
{ return _baseCurve; }
set
{
_baseCurve = value;
_areaMassProp = AreaMassProperties.Compute(value);
}
}
public AreaMassProperties areaMassProp { get; set; }
// This class information will be written to the .3dm file
public override bool ShouldWrite
{
get
{
return true;
}
}
protected override bool Read(Rhino.FileIO.BinaryArchiveReader archive)
{
Rhino.Collections.ArchivableDictionary dict = archive.ReadDictionary();
_baseCurve = (Curve)dict["Curve"];
_areaMassProp = AreaMassProperties.Compute(_baseCurve);
return true;
}
protected override bool Write(Rhino.FileIO.BinaryArchiveWriter archive)
{
var dict = new Rhino.Collections.ArchivableDictionary(20171031, "Values");
dict.Set("Curve", baseCurve);
archive.WriteDictionary(dict);
return true;
}
Where I add the user data:
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
ObjRef sRef;
Result r = RhinoGet.GetOneObject("Pick closed curve", false, ObjectType.Curve, out sRef);
if (r != Result.Success) return r;
Curve selectedCurve = sRef.Curve();
//if the curve is not closed do nothing
if (!selectedCurve.IsClosed)
{
Rhino.RhinoApp.WriteLine("The curve was not closed!!");
return Result.Success;
}
List<Curve> cuttedCurves = CurveManipulation.cutCurve(selectedCurve, Plane.WorldXY, CurveManipulation.Axis.XAxis);
drawAndSaveUserAttr(cuttedCurves, doc);
return Result.Success;
}
//this method draws the curves and saves the useraddributes into them
private void drawAndSaveUserAttr(List<Curve> cuttedCurves, RhinoDoc doc)
{
foreach (Curve curve in cuttedCurves)
{
GeometrySegment seg = new GeometrySegment();
seg.baseCurve = curve;
ObjectAttributes attr = new ObjectAttributes();
attr.UserData.Add(seg);
attr.SetUserString("Name", "Concrete");
doc.Objects.AddCurve(curve, attr);
}
Where I get the userdata back:
private void button1_Click(object sender, EventArgs e)
{
Rhino.RhinoDoc doc = Rhino.RhinoDoc.ActiveDoc;
RhinoObject[] objects = doc.Objects.FindByUserString("Name", "Concrete",true);
foreach (RhinoObject o in objects)
{
Rhino.DocObjects.Custom.UserDataList list = o.Attributes.UserData;
GeometrySegment joo = list.Find(typeof(GeometrySegment)) as GeometrySegment;
if (joo.baseCurve != null)
Rhino.RhinoApp.WriteLine(joo.baseCurve.ToString());
else
Rhino.RhinoApp.WriteLine("doesnt work.");
}
}