InstanceDefinition.getObjects() does not work as expected

Hello all,

I am writing a plug-in related to block definitions. In a nutshell, the plug-in listens to an InstanceDefinitionTableEvent, and when a new definition is added, it traverses through it to find the constituent elements. Here is the logic:

private void onInstanceDefinitionTableEvent(object sender, InstanceDefinitionTableEventArgs e)
{
    var t = e.EventType;
    InstanceDefinition newDef = e.NewState;
    switch (t)
    {
        case InstanceDefinitionTableEventType.Added:
            RhinoObject[] defObjects = newDef.GetObjects();
            foreach (RhinoObject defObject in defObjects)
            {
                //Here is the application logic
            }
    }
}

It works fine when opening a new Rhino file and creating block definitions with the plug-in already in place. For each new definition created, after invoking the .GetObjects() method, I get an array of objects constituting the definition. However, the behavior changes when opening an already started file.

Once opened, I first see the InstanceDefinitionTableEvents fired and then the InstanceReferences added to the documents (I also listen to the AddRhinoObject event), which makes perfect sense. What does not make sense to me is that while populating the InstanceDefinitionTable for a non-empty file that I just opened, the .GetObjects() method returns empty arrays for all block definitions.

Can anyone help me understand why this is happening and how to resolve it?

Hi @Konstantin_Loshkov,

Rhino .3dm files are structured where the table objects (e.g. layers, linetypes, instance definitions) are stored towards the top of the file, and geometric objects are stored towards the bottom. When reading .3dm files, the Rhino document is populated in this order.

In your case, its probably best to ignore any instance definition table events while Rhino is in the process of opening a file. When finished, then you can plow through the instance definition table and do what you need.

– Dale