Find an InstanceDefinitionObject's parent

Good evening,
I’m trying to read a user string from an object’s instance reference.
I’ve found rhinoobject.Attributes.IsInstanceDefinitionObject to check if the object belongs to a block, but I haven’t found a way to find said instanceObject.
I’ve tried looping though every blocks in the model but it is extremely slow and it needs recursivity because I my usertexts are stored on the higher level of instance objects.

Is there any simple way to do this?
Thanks

Hi @Matthieu_from_NAVINN,

An instance definition object does not have a back pointer to its owning instance definition. You’ll need to dig through each instance definition’s object list to find which one owns the object.

– Dale

Sounds like another important feature missing from Rhino’s “not ready for prime time” block support. The vibe I get from this forum is that there’s plenty of Rhino users who want the real thing.

Hi @dale,
Thanks for the answer. I hope a back pointer will be added in Rhino7.

Hi @Matthieu_from_NAVINN,

How are you getting those objects into memory?

I’m asking because first you said:

But then you want to use:

Which are two different things.

If you instead search for InstanceObject you can use the InstanceDefinition property to get the parent definition

Hi @lando.schumpich,
Thank you for helping.
I am working on RhinoObjects gotten from a custom DisplayConduit.
In the overriden sub PreDrawObject(), e.RhinoObject gives me the model objects, but when they are sub objects of a block instance I need to read the block instance usertexts instead of the object usertexts.

Hi @Matthieu_from_NAVINN,

Could you not ignore the sub-objects, as you will also get the InstanceObject for every blockinstance in scene?

Hi @dale,
I’ve looked a bit more in details, and it seems even digging through each instance definition’s object list to find which one owns the object will be a challenge, because every subobjects IDs are empty :sob:. Is there anything other than the ID to identify an object inside an instance reference?

This is an example of my code, where I tried to build a proper dictionary to be able to retrieve the instance def from the sub-object ID:

 <Extension>
Public Function GetInstanceobjectsParents(ByVal doc As RhinoDoc) As Dictionary(Of Guid, InstanceObject)
    RhinoApp.Write("Reading blocks instances... ")
    Dim rslt As New Dictionary(Of Guid, InstanceObject)
    For Each item As RhinoObject In doc.Objects.FindByObjectType(ObjectType.InstanceReference)
        Dim instRef As InstanceObject = TryCast(item, InstanceObject)
        For Each ObjID As Guid In instRef.GetAllSubObjects
            If Not rslt.ContainsKey(ObjID) Then rslt.Add(ObjID, instRef)
        Next
    Next
    RhinoApp.WriteLine("Done.")
    Return rslt
End Function

<Extension>
Public Function GetAllSubObjects(ByVal instRef As InstanceObject) As List(Of Guid)
    Dim rslt As New List(Of Guid)
    For Each obj As RhinoObject In instRef.GetSubObjects
        If obj.ObjectType = ObjectType.InstanceReference Then
            Dim SubInstRef As InstanceObject = TryCast(obj, InstanceObject)
            For Each item As Guid In SubInstRef.GetAllSubObjects
                rslt.Add(item)
            Next
        Else
            rslt.Add(obj.Id)
        End If
    Next
    Return rslt
End Function

Hi @lando.schumpich,
That would be great but somehow if I ignore the sub objects, the block instance is not drawn either:

'ignore sub objects
If e.RhinoObject.IsInstanceDefinitionGeometry Then
         e.DrawObject = False
         Exit Sub
End If

I meant something more along the lines of:

  • continue when the current object is IsInstanceDefinitonGeometry,
  • rhObj as InstanceObject .InstanceDefinition and get usertext from definition when InstanceObject
  • get usertext regularly on all other cases

See if the attached sample is of any help.

TestVbMatthieu.vb (2.3 KB)

– Dale

Hi @dale,
Unfortunately this is not what I’m trying to do.
I need to read some user texts from the instance reference (not the instance definition).

The issue is that objects belonging to block will return the same ID for two instances of that block.

Thanks for your help

Matthieu

Do you store your user text on the geometry or on the attributes of an InstanceReference ? @Matthieu_from_NAVINN

Well, that’s the whole idea instances. I’m not sure how to help, sorry.

– Dale