C# - Get Brep from Block

Hello.

I’ve been struggling all day long with this issue.

I need to get the list of vertices from each object I import in my scene as a .step or .3dm file.
Eventually, the goal is to compare the model I import with a set of models to find which one it matches.
Elefront lets me do that by exploding the block and analysing the Brep, but I need to automize with a C# script so that as soon as I import a file, the matching gets done.

I’ve tried this :


By adapting it in C#, I got this :

List<GeometryBase> GeoOutput = new List<GeometryBase>();

List<Rhino.DocObjects.RhinoObject> BlockInstanceList = new List<Rhino.DocObjects.RhinoObject>();
BlockInstanceList = Rhino.RhinoDoc.ActiveDoc.Objects.FindByObjectType(ObjectType.InstanceReference).ToList();

Rhino.DocObjects.Tables.InstanceDefinitionTable BlockDefinitionList = Rhino.RhinoDoc.ActiveDoc.InstanceDefinitions;

for(int i = 0; i < BlockDefinitionList.Count(); i++)
{
  List<Rhino.DocObjects.RhinoObject> BlockObjects = new List<Rhino.DocObjects.RhinoObject>();
  BlockObjects = BlockDefinitionList[i].GetObjects().ToList();

  for(int j = 0; j < BlockInstanceList.Count(); j++)
  {
    //Rhino.DocObjects.RhinoObject Block = BlockInstanceList[j];
    Rhino.DocObjects.InstanceObject Block = BlockInstanceList[j];
    Transform XForm = Block.InstanceXForm;

    for(int k = 0; k < BlockObjects.Count(); k++)
    {
      GeometryBase objX = BlockObjects[k].DuplicateGeometry();
      objX.Transform(XForm);
      GeoOutput.Add(objX);
    }
  }
}

But I get the following error message :

Am I missing something?
How can I convert a RhinoObject into an InstanceObject so that I can use InstanceXForm?

Thanks for your feedback!

Hi @valentin.bonneau,

RhinoObject rhinoObject = ...;

if (rhinoObject is InstanceObject instanceObject)
{
  Transform xform = instanceObject.InstanceXform;
  // TODO...
}

– Dale

Thanks for your reply Dale.
But unless I misunderstand what you’re saying, this is not a conversion, but merely a check that the RhinoObject is of type InstanceObject.
In line 4 of my code, am I not filtering the RhinoObjects I get being of InstanceReference type?
In that case, why am I getting the error I mention when I try to use it in line 16?

Thanks

Hello @dale .

To go further on this topic, could you help me understand why, in this case, I’ve got twice the same InstanceObject, but when I look at the subobjects, one is made of a Brep, and the other one is just made of the InstanceObject?

Thanks

TestBrep.gh (10.3 KB)
TestBrep.3dm (1.2 MB)

Hi @valentin.bonneau,

Equerre1 contains a nested instance reference of Equerre.

– Dale

Thanks @dale .

Think this is why I’ve been struggling so much :wink: