Hi,
I’m trying to import blocks from an external via a python script, currently in grasshopper, and I had no issue with simple blocks, but when it comes to nested blocks, the “parent” block would be created but not the nested blocks inside it. My approach is to select specific blocks from a file and get them imported in the file I’m working in without worrying if the block I selected has blocks inside, everything should be imported. Up to now I have achieved this:
import scriptcontext as sc
import Rhino as r
#Declare source file
file = sc.sticky['fpS']['filepathSource']
fileSource = r.FileIO.File3dm.Read(str(file))
#Get layer from the source file
layersSource = fileSource.AllLayers
Lays = []
LayNames = []
layRoot = []
for lay in layersSource:
Lays.append(lay)
LayNames.append(lay.FullPath)
#Find the blocks in the source file
importBlocks = []
for j in blocks:
importBlocks.append(fileSource.AllInstanceDefinitions.FindName(j))
for b in importBlocks:
print b
objectIds = []
layers = []
attr = [ ]
newLy = []
for c in b.GetObjectIds():
objectIds.append(fileSource.Objects.FindId(c).Geometry)
layers.append(fileSource.Objects.FindId(c).Attributes.LayerIndex)
attr.append(fileSource.Objects.FindId(c).Attributes)
#Layers needed in the current file
for ly in layers:
newLy.append(Lays[ly].FullPath)
strLay = Lays[ly].FullPath
for tx in newLy:
for ran in range(0,len(tx.split('::'))):
joinList=[]
for j in range(0,ran+1):
joinList.append(tx.split('::')[j])
layRoot.append("::".join(joinList))
for lay in layRoot:
layers.append(LayNames.index(lay))
for l in set(layers):
r.RhinoDoc.ActiveDoc.Layers.Add(Lays[l])
#Remap the Layer Indices to new created layers
for x in range(len(attr)):
attr[x].LayerIndex = r.RhinoDoc.ActiveDoc.Layers.FindByFullPath(newLy[x],0)
#Import the blocks
r.RhinoDoc.ActiveDoc.InstanceDefinitions.Add(b.Name,b.Description,r.Geometry.Point3d(0,0,0),objectIds,attr)
T = r.RhinoDoc.ActiveDoc.InstanceDefinitions.GetList(True)
blocklist = []
for i in T:
name = i.Name
blocklist.append(name)
BlockList = sorted(blocklist)
Thank you in advance