How to modify the Userdata Using C++

Hi guys,

I have encounter a problem of modification of the usedata. The sample case of [cmdSampleAttributeUserData.cpp]is really helpful for me with link (rhino-developer-samples/cmdSampleAttributeUserData.cpp at 7 · mcneel/rhino-developer-samples · GitHub) . I make a small change with rand() to test whether the userdata can be modfied after added as follows, I have test to run twice with the command, the result is the same. What can I do to modify the userdata.
Original
ref.SelectionPoint(ud->m_my_point);
Changed
ud->m_my_point = ON_3dPoint(rand(), rand(), rand());
RhinoApp().Print(L"Point = %f,%f,%f\n", ud->m_my_point.x, ud->m_my_point.y, ud->m_my_point.z);

Many thanks
Qingxiang Meng


Modifying user data in Rhino C++ SDK can be done by first obtaining a reference to the object whose user data you wish to modify. This can be done by using the RhinoDoc::Objects() method to get a collection of all objects in the document, and then searching through that collection for the specific object. Once you have a reference to the object, you can access its user data by using the CRhinoObject::Attributes() method.

To modify the user data, you will need to create a new instance of the class CRhinoUserData and set the appropriate properties. Then you can use the CRhinoObject::Attributes().SetUserData() method to set the new user data on the object.

More or less this, I havent checked the documentation and assumed the methods are similar to C#

// Get the object whose user data we want to modify
CRhinoObject* pObject = RhinoDoc::ActiveDoc()->Objects().Find(object_id);
if (pObject)
{
  // Create a new instance of CRhinoUserData
  CRhinoUserData userData;
  userData.SetString("My custom data");
  
  // Set the new user data on the object
  pObject->Attributes().SetUserData(userData);
}

@farouk.serragedine Thanks for your help. However, I cannot find the method of SetUserData in CRhinoObject::Attributes(). The screen shoot is as follows. Many thanks.

Hi @Qingxiang,

I’ve added a sample that shows how to update user data:

Let me know if this helps.

– Dale

@dale

Perfect! You are the King of Rhino development :crazy_face:
Many many thanks!

Qingxiang