How to explode block from referenced file (InstanceReferenceGeometry)?

Hi,

I am importing geometry from .dwg file via a c# script since I don’t want to open file in Rhino as this script will be running headless later.

Problem that I am facing is that I cannot seem to unwrap the blocks within the referenced geometry. I have tried both Human and eleFront but not working as you can see eblow.

Should be possible to unwrap/explode within the first c# component or in a secondary component, either would work. Any assistance in this would be greatly appreciated. sample files attached.

Code inside first c# component:

explode sample.gh (7.5 KB)
test.dwg (43.5 KB)

Hi!

Check this: (not deeply tested)
explode sample 2.gh (7.6 KB)

I’m expecting the reference class to only contains the original block guid id and a transformation (+ some simple attribute of the istance…)
You probably won’t be able at all to “explode the block” in another component, as you are missing the original document where the original block is.
You must do it where you also have access to the original document. The first script.


  private void RunScript(string path, ref object geometry, ref object layerNames)
  {
    using( var doc = Rhino.RhinoDoc.CreateHeadless(null)){
      doc.Import(path);
      var geometryList = new System.Collections.Generic.List<GeometryBase>();
      var names = new System.Collections.Generic.List<string>();
      foreach(Rhino.DocObjects.RhinoObject obj in doc.Objects){
        if(obj.Geometry.ObjectType.ToString() == "InstanceReference"){
          InstanceReferenceGeometry irg = (InstanceReferenceGeometry) obj.Geometry.Duplicate();
          Transform tra = irg.Xform;
          foreach(Rhino.DocObjects.RhinoObject obj2 in doc.InstanceDefinitions.FindId(irg.ParentIdefId).GetObjects()){
            GeometryBase geo = obj2.Geometry.Duplicate();
            geo.Transform(tra);
            geometryList.Add(geo);
            var layer = doc.Layers[obj2.Attributes.LayerIndex];
            names.Add(layer.FullPath);
          }
        }else{
          geometryList.Add(obj.Geometry.Duplicate());
          var layer = doc.Layers[obj.Attributes.LayerIndex];
          names.Add(layer.FullPath);
        }
      }
      geometry = geometryList;
      layerNames = names;
    }
  }
1 Like

Thanks @maje90 that worked a treat.

Appreciate your assistance a lot!

One more thing, would it be possible to also extract the block insertion point or plane? With that info i could finalize my task.

Much appreciated anyway, legend!

Block InstanceDefinition (not InstanceReference) are simply defined around WorldXYZ 0,0,0 origin.
If you want the insertion point, just create a 0,0,0 point and transform it with that “tra” transformation.

You can do the same thing with a simple XY plane and transform that too (but remember that a plane will not store all the other shearing and scaling information that you can have in a transform/block).

1 Like

Ok, thank you I will try that. Much appreciated! :pray:

This is how I solved it in the end, thanks again for your help!

using( var doc = Rhino.RhinoDoc.CreateHeadless(null)){
  doc.Import(path);
  var geometryList = new System.Collections.Generic.List<GeometryBase>();
  var names = new System.Collections.Generic.List<string>();

  var namesBlock = new System.Collections.Generic.List<string>();
  var blockgeometryList = new System.Collections.Generic.List<GeometryBase>();

  var blockTransformList = new System.Collections.Generic.List<Transform>();

  foreach(Rhino.DocObjects.RhinoObject obj in doc.Objects){
    if(obj.Geometry.ObjectType.ToString() == "InstanceReference"){
      InstanceReferenceGeometry irg = (InstanceReferenceGeometry) obj.Geometry.Duplicate();
      Transform tra = irg.Xform;
      blockTransformList.Add(tra);

      var layer = doc.Layers[obj.Attributes.LayerIndex];
      namesBlock.Add(layer.FullPath);

      foreach(Rhino.DocObjects.RhinoObject obj2 in doc.InstanceDefinitions.FindId(irg.ParentIdefId).GetObjects())
      {
        GeometryBase geo = obj2.Geometry.Duplicate();
        geo.Transform(tra);
        blockgeometryList.Add(geo);
      }
    }else{
      geometryList.Add(obj.Geometry.Duplicate());
      var layer = doc.Layers[obj.Attributes.LayerIndex];
      names.Add(layer.FullPath);
    }
  }
  geometry = geometryList;
  layerNames = names;
  blockLayer = namesBlock;
  blockGeometry = blockgeometryList;
  blockTransform = blockTransformList;
}
1 Like