Rhino 6 for Windows / RhinoCommon V6 / C#
I wrote a simple plugin to test this:
RhinoTestUserData.zip (381.1 KB)
This simple plugin has 2 commands:
- UDSetUserData to give user the ability to select an object and to “attach” to its UserDictionary a simple information: the center of its bounding box.
- UDGetUserData to give user the ability to select an object and to read (if present) the coordinates previously saved in the UserDictionary
Please note that i’m not reading and writing the UserDictionary of the RhinoObject but the UserDictionary of its GeometryBase object.
The plugin also intercepts the BeforeTransformObjects event and updates the coordinates saved in the UserDictionary accordingly to the transformation applied.
So far so good…
That’s what looks like a bug.
- Create a simple object with center in (0, 0, 0)
- Execute UDSetUserData and select the object
- Execute UDGetUserData, select the object and note that the center coordinates are right (0, 0, 0)
- Translate the object by a know value (i.e. +2 along X axis)
- Execute UDGetUserData, select the object and note that the center coordinates are updated (2, 0, 0)
- Undo the transformation
- Execute UDGetUserData, select the object and note that the center coordinates are still (2, 0, 0)
It looks like the GeometryObject.UserDictionary is not restored at its previous state.
Thanks to you all.
@dale Has this something to do with our recent conversation?
Sorry but I don’t recall our recent conversation.
Never modify a Rhino object during an event watcher. Rather, set a flag indicating that something interesting happened and then handle the event in a
RhinoApp.Idle event handler.
I’ve attached a sample for you to review.
RhinoTestUserDataPlugIn.cs (2.6 KB)
I just tried your suggestions and they work fine: user dictionary is updated correctly and is correctly restored after an undo command.
Due to this suggestion, even if i don’t know the reason why i have to do it, i tried to modify my code, beacuse i actually do modify Rhino objects in my RhinoDoc.BeforeTransformObjects event handler.
This is how it works now (roughly)
- Inside RhinoDoc.BeforeTransformObjects event hander, i save the list of objects that are going to be transformed, along with their transformations matrices
- Register an event handler for RhinoApp.OnIdle event
- In RhinoApp.OnIdle event handler, unregister the event hanlder itself
- Cycle in the list of objects previously saved
- Apply the same transformation to other rhino objects, these objects were not edited by user
- Clear the list of objects saved in RhinoDoc.BeforeTransformObjects event hander
What i want to do, basically, is this: when the user transforn a rhino object, i apply the same transformation to other rhino objects.
Everything seems to work fine but, when try to undo, only the object edited by the user is restored. All the object transformed by code remain in the last state.
Does this happens beacuse i’m editing objects outside a command? If yes, why it works if i change user dictionaries?
Thanks again, @dale!