What happens to Custom Objects on Save?

Hello i have another question regarding Custom Objects.

When a 3dm file containing custom objects gets saved, all of them get converted in some way (CustomBrepObject becomes BrepObject, CustomCurveObject becomes CurveObject etc.) Can someone explain how this process is happening and what can be changed about it?

For example in my current case i have UserData attached to my CustomObjects and while this userdata survives saving and reopening on “normal” objects, the custom object gets stripped of the userdata. I am following this example which works up to the point where custom objects come into play.

Maybe there is something to override or an event to subscribe to, so i can attach my userdata to those automatic replacement objects and get it saved in the doc.

Thanks for any help

Hi @lando.schumpich,

Custom objects are saved as their inherited types. For example, a CustomCurveObject is saved as a CurveObject. Any extra data on your derived object should be saved as user data.

When Rhino opens a document, you should watch for objects with your user data and convert them back to your custom object.

– Dale

Hey,

I finally got it to work, the problem was i was adding my UserData to the customObject itself when i should have been doing something like:

this.Geometry.UserData.Add(MyCustomUserData);

Subscribing to RhinoDoc.EndOpenDocument now gives me the ability to re-create all my custom objects after saving and reopening.

Thanks a lot!

Hi @lando.schumpich,

Would it be possible for you to provide one quick example demonstrating how to re-create the custom objects after saving and reopening?
Thanks in advance! :slight_smile:

Hi @jeffoulet,

For re-creation i typically do this inside of the plugIn class:

  1. Subscribe to the EndOpenDocument event
        protected override LoadReturnCode OnLoad(ref string errorMessage)
        {
            // Add an event handler so we know when documents are opened
            RhinoDoc.EndOpenDocument += RhinoDoc_EndOpenDocument;

            return LoadReturnCode.Success;
        }
  1. Iterate over all objects in the doc and search for your user data, re-create if found.
        private void RhinoDoc_EndOpenDocument(object sender, DocumentOpenEventArgs e)
        {
            RhinoDoc doc = e.Document;

            foreach (var docObject in doc.Objects)
            {
                // try to find our user data on a given doc object
                var data = docObject.Attributes.UserData.Find(typeof(MyCustomUserData)) as MyCustomUserData;
                if (data is null) continue;

                // Update data to restore auto implemented properties
                data.Update();

                // create a shiny new custom Object from the stored data
                var customObject = data.CreateCustomObject();

                // replace original object in doc
                doc.Objects.Replace(new ObjRef(docObject), customObject);
            }

        }

In this example I defined a method inside of MyCustomUserData to create a custom object.

You can also take a look at this Repository Where i implemented Read Write for the RectangleData class as an example

1 Like

Hi Lando,

Many thanks for your help!
By using the sample you provided here: Custom object for storing internal meta-information

And with the help of your Octopus example, I eventually get the CustomObjects to be re-created on document load time. :slight_smile:
I’m not sure to have fully/deeply understood the events/handlers stories yet, but at least, it works. I’ve now to wrap my mind around all this!