Find nested blocks when selecting SubObject

Hi there,

I am pretty sure I found answers to this question somewhere, but I cannot recalll where, so I will ask this question again…

Let’s say I am working with nested blocks and with a GetObject I wanted to select one of the nested blocks (not necessarily in the first level of the block).
Then I would try something like this:

 go.SetCommandPrompt("Select items");
 go.SubObjectSelect = true;
 go.GetMultiple(1, 0);

 foreach (var obj in go.Objects())
 {
     string nestedBlock = null;
     if (obj.InstanceDefinitionPart() != null)
     {
         Guid nestedId = obj.InstanceDefinitionPart().Id;
         foreach (var item in doc.InstanceDefinitions)
         {
             if (item.GetObjectIds().Contains(nestedId))
             {
                 nestedBlock = item.Name;
                 break;
             }
         }
     }
}

Rhino.RhinoApp.WriteLine(nestedBlock);

This will only return the first level’s block to me, not the block the object is located in.
I fear this will not be possible in any other way, correct?

Would then an appropriate way be, to check if the selections point and to get all nested definitions in place and check if any object in there has a connection to this point that is 0?

Point3d pickPoint = obj.SelectionPoint();

Or would there be more elegant ways?

Thanks,
T.

I believe @lars has been working on something related. Not sure if that API work is already finished or if he is still working on it.

1 Like

@tobias.stoltmann

You could try something like this:

  foreach (var obj in go.Objects())
  {
    var instanceObject = obj.Object() as InstanceObject;
    if (instanceObject != null)
    {
      if (!obj.GeometryComponentIndex.IsUnset())
      {
        var subObject = instanceObject.SubObjectFromComponentIndex(obj.GeometryComponentIndex);
      }
    }
  }

It is correct that the object returned from GetObject will be an instance of a top level block. You can use the SubObjectFromComponentIndex to get to a selected object inside that instance.

@lars
Top notch!
Thanks!