The following code adds user data, consisting of X and Z coords
of a polyline, to that polyline for later use in other commands
//instantiate the user data object and add to the curve.
//strProfile is the list of coords
RS2023_6_UserData uData = new RS2023_6_UserData(1, strProf);
uData.StringData = strProf;
obj_ref.Object().UserData.Add(uData);
This code purports to retrieve that user data. However, obj never has user data, even when the curve in question is examined
foreach (var obj in rh_objects)
{
if (obj.HasUserData)
{
uData = obj.UserData.Find(typeof (RS2023_6_UserData)) as RS2023_6_UserData;
}
}
Although it seems like you can, you cannot attach user data to the runtime RhinoObject-inherited objects. User data must be attached to a runtime object’s geometry or attributes.
Also, if you expect undo to work, you should follow this pattern:
Get the object.
Copy the object’s geometry or attributes.
Attached you user data to one of the above.
Call either ObjectTable.ReplaceObject or ObjectTable.ModifyAttributes.
protected override Result RunCommand(RhinoDoc doc, RunMode mode)
{
// Pick some curve (for example)
var rc = Rhino.Input.RhinoGet.GetOneObject("Select curve", false, ObjectType.Curve, out var objref);
if (rc != Result.Success || null == objref)
return Result.Cancel;
// Get the runtime Rhino object
var rhObject = objref.Object();
if (null == rhObject)
return Result.Failure;
// See if the object's attributes has our user data
var userData = rhObject.Attributes.UserData.Find(typeof(MyUserData)) as MyUserData;
if (null == userData)
{
// Create and initialize our user data
userData = new MyUserData();
// Make a copy of the object's attributes
var attributes = rhObject.Attributes.Duplicate();
// Attach our user data
attributes.UserData.Add(userData);
// Modify the object's attributes
doc.Objects.ModifyAttributes(rhObject, attributes, false);
}
return Result.Success;
}