Adding a block from another file

Hi everyone!

I have a rhino file with hundreds of possible facade parts each of them modelled as a Block. Let’s call it a facade library. My goal is to import only certain blocks from the facade library to my current file according to the facade type defined by the user. Thus I can make the file size manageble and keep the library up-to-date without the need of manually importing blocks every time something is changed.

To this point I managed to collect all the InstanceDefinitions from the library file using Rhino.FileIO.File3dm.Read() and AllInstanceDefinitions property. But I can’t find a way to import an InstanceDefinition from a libary into my active RhinoDoc. Is there anything i’m missing?

I found an old thread with a similar question How to import selected blocks from a File3dm? (Or were blocks saved incorrectly in this file?) with an example from @dale but the file is not available anymore.

Max

i dont know if its doable via grasshopper but see elefront plugin for GH. there are some nodes for importing linked blocks etc

you can do this via elefront plugin if thats an option for you.

1 Like

Thanks for suggestion but it’s a little trade-off since all the blocks have to be kept in separate files. It’s interesting though how these components are done. I suppose it uses -insert command internally, I’ll give it a try anyway.

Ok. I made it finally
Here is a code sample in case anyone needs it.

private void RunScript(string filePath, string blockName)
{

    int index = -1; //instance definition index

    //check if block exists in current file;
    var localDef = RhinoDocument.InstanceDefinitions.Find(blockName);

    if (localDef != null)
    {
      index = localDef.Index;
    }
    else //get block from the outer file
    {
      var refDoc = Rhino.FileIO.File3dm.Read(filePath);

      var instDefs = refDoc.AllInstanceDefinitions;

      //find the blocks needed
      var instDef = instDefs.Where(d => d.Name == blockName).FirstOrDefault();

      if (instDef != null)
      {
        //add layers to current document otherwise imported blocks will loose layers/color
        var refLayers = refDoc.AllLayers;
        foreach (var layer in refLayers)
        {
          RhinoDocument.Layers.Add(layer);
        }

        //find block Objects
        var refObjects = refDoc.Objects;
        var ids = instDef.GetObjectIds();
        var objs = ids.Select(id => refObjects.FindId(id));

        //Add Instance Definition to the current file
        index = RhinoDocument.InstanceDefinitions.Add(blockName, string.Empty, Point3d.Origin, objs.Select(o => o.Geometry), objs.Select(o => o.Attributes));
      }
    }

    //Place new block to the document
    var xform = Transform.Identity;
    RhinoDocument.Objects.AddInstanceObject(index, xform);

}

2 Likes