I want to create new rhino file and copy all block definition from origin file.
But This code is work incompletely. The TextEntities in original block definition are not copied as expected.
I found that if the origin definition have 4 geometries(1 curve, 4 texts) but copied definition has only one curve geometry.
I think this happens in this method.
temp_doc.InstanceDefinitions.Add()
If I change this temp_doc to current_doc(document currently opened), it works well.
One thing more! similar things happen, when I import InstanceDefinitions, InstanceObject in InstanceDefinition is skipped.
Why this happen, and How can I fix this?
def export_block_definition_to_3dm(current_doc, filepath):
"""Block definition을 3dm 파일로 저장"""
# CREATE TEMP DOC
temp_doc = Rhino.RhinoDoc.CreateHeadless(None)
try:
block_defs = sinutils.get_all_block_definitions(current_doc)
for block_def in block_defs:
# Block definition을 임시 문서에 추가
origin_geoms = sinutils.get_geom_list_from_block_defintion(
current_doc, block_def
)
res = temp_doc.InstanceDefinitions.Add(
f"{block_def.Name} COPY",
block_def.Description,
geo.Point3d(0, 0, 0),
[obj.Geometry for obj in block_def.GetObjects()],
[obj.Attributes for obj in block_def.GetObjects()],
)
created_def = temp_doc.InstanceDefinitions[res]
created_geoms = sinutils.get_geom_list_from_block_defintion(
temp_doc, created_def
)
if len(origin_geoms) != len(created_geoms):
raise ValueError(
f"{block_def.Name} ERROR input {len(origin_geoms)} != output {len(created_geoms)}"
)
# SAVE 3dm
temp_doc.WriteFile(filepath, FILE_WRITE_OPTION) # 8 = Rhino 8 버전
print(f"Saved: {filepath}")
finally:
temp_doc.Dispose()