Import nested Block from file

I want to import blocks from a file into the file. However, the nested blocks are not added to the InstanceDefinitionTable. I checked the data, and the nested blocks are imported as InstanceReferenceGeometry, but they are not added to the InstanceDefinitionTable.

        string filePath = string.Empty;
        if (!DA.GetData(0, ref filePath)) return;
        File3dm file3dm = File3dm.Read(filePath);
        RhinoDoc doc = RhinoDoc.ActiveDoc;
        List<string> stringGeomatry = new List<string>();
        foreach (var instanceDef in file3dm.InstanceDefinitions)
        {
            List<GeometryBase> blockList = new List<GeometryBase>();
            List<ObjectAttributes> blocattribute = new List<ObjectAttributes>();
            foreach (var obj in instanceDef.GetObjectIds())
            {
                var taype = file3dm.Objects.FindId(obj);
                blockList.Add(taype.Geometry);
                blocattribute.Add(taype.Attributes);
            }
            doc.InstanceDefinitions.Add(instanceDef.Name, instanceDef.Description, Point3d.Origin, blockList,blocattribute);
        }

private void RunScript(string path, ref object a)
{
    var doc3dm = RhinoDoc.OpenHeadless(path);
    var geometryList = new List<IGH_GeometricGoo>();
    foreach (var rhinoObject in doc3dm.Objects.GetObjectList(Rhino.DocObjects.ObjectType.InstanceReference))
    {
        var geometry = GH_Convert.ObjRefToGeometry(new ObjRef(rhinoObject));
        if (geometry != null)
            geometryList.Add(geometry);
    }
    doc3dm.Dispose();
    a = geometryList;
}

Blocks.zip (35.5 KB)

In rhino 8 blocks can created from imported geometry using the new data types in grasshopper.

1 Like

Thank you for your guidance, but I want to develop my program in Rhino version 7.I am working with files converted to binary arrays, and in my plugin, there is no access to the file path

Thank you, Mahyar! My problem is with importing data into Rhino 7