User data issue

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;
   	     }
}

Hi @cestes001,

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:

  1. Get the object.
  2. Copy the object’s geometry or attributes.
  3. Attached you user data to one of the above.
  4. Call either ObjectTable.ReplaceObject or ObjectTable.ModifyAttributes.

The SDK samples demonstrates this.

– Dale

Thx Dale. Are you ever not working? :grinning:

I think i must be missing something. I cant call either ReplaceObject() or ModifyAttributes() on ObjectTable object.

Hi @cestes001,

How about something like this?

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;
}

– Dale

Much appreciated.

One last (I hope) question:

is MyUserData and instance of the user data class as detailed here?

If so, I don’t see how to extract the data, once it’s saved to the object.

MyUserData is equivalent to your RS2023_6_UserData class.

– Dale