I have a set of closed curves that were made into blocks. Each block contains one closed curve. Somehow we broke all the closed curves open and they are now all open curve segments. I can edit the block and select the curves and type ‘join’ and it fixes the issue. but I have thousands of blocks. What is the script that might accomplish this task.
I am trying the following but it appears to have no affect. I have one instance of each block in the file and I can print out ids for each curve and it seems to be looping correctly.
block_names = rs.BlockNames();
for block_name in block_names:
block_objects = rs.BlockObjects(block_name)
curves = []
for id in block_objects:
if rs.IsCurve(id):
curves.append(id)
rs.JoinCurves(curves)
import rhinoscriptsyntax as rs
import scriptcontext as sc
import Rhino
rs.EnableRedraw(False)
blockNames = rs.BlockNames()
for blockName in blockNames:
if rs.IsBlockReference(blockName) :
print("Cannot edit linked block.")
break
blockObjects = rs.BlockObjects(blockName)
# Separate the curves from others type of objects
blockObjectsCurvesOnly = []
blockObjectsWithoutCurves = []
for object in blockObjects:
if rs.IsCurve(object):
blockObjectsCurvesOnly.append(object)
else:
blockObjectsWithoutCurves.append(object)
# rs.JoinCurves need atleast 2 curves
if (len(blockObjectsCurvesOnly) < 2):
break
joinedCurve = rs.JoinCurves(blockObjectsCurvesOnly)
# Make a union of the list with the newly created list of curves from rs.JoinCurves()
modifiedBlockObjects = blockObjectsWithoutCurves + joinedCurve
# Create a list of derived object from GeometryBase with rs.coercegeometry()
newGeometry = []
# Keep the same attributes assigned to the objects
newAttributes = []
for modifiedObject in modifiedBlockObjects:
newGeometry.append(rs.coercegeometry(modifiedObject))
ref = Rhino.DocObjects.ObjRef(modifiedObject)
attr = ref.Object().Attributes
newAttributes.append(attr)
# Get the instance definition by block name
idef = sc.doc.InstanceDefinitions.Find(blockName)
idefIndex = idef.Index
# Assign a new list of derived object from geometryBase to the instance definition
sc.doc.InstanceDefinitions.ModifyGeometry(idefIndex,newGeometry,newAttributes)
# Delete objects created by rs.JoinCurves()
rs.DeleteObjects(modifiedBlockObjects)
rs.EnableRedraw(True)
Thank you so much! I’m very glad I didn’t try to finish this myself. I was correct up to a point, but the block part was not going to go well for me. This works for my situation. Thanks again.