Well, @BrianEkins , here's something I cooked up this morning to move constraints from one sketchEntity to another. It is not thoroughly tested (use at your own risk), and it doesn't handle the most complex constraints (CircularPattern, Offset, Polygon and RectangularPattern), but it does the job that I needed in 40-ish lines of code. I will say that any API interface which requires this kind of coding is rather complex. I hope someone finds this useful / instructive.
Here the code for the moveconstraints.py module:
import adsk.core, adsk.fusion, traceback
def remove_suffix(text, suffix):
return text[:-len(suffix)] if text.endswith(suffix) else text
# NOTA BENE: SOME CONSTRAINTS ARE NOT HANDLED:
# CircularPattern, Offset, Polygon, RectangularPattern
# list of [Constraint name, list of property names to retrieve in "add call" order]
constraintNameList = [
['Coincident', ['point','entity']],
['Collinear', ['lineOne','lineTwo']],
['Concentric', ['entityOne','entityTwo']],
['Equal', ['curveOne','curveTwo']],
['Horizontal', ['line']],
['HorizontalPoints', ['pointOne','pointTwo']],
['MidPoint', ['point','midPointCurve']],
['Parallel', ['lineOne','lineTwo']],
['Perpendicular', ['lineOne','lineTwo']],
['Smooth', ['curveOne','curveTwo']],
['Symmetry', ['entityOne','entityTwo','symmetryLine']],
['Tangent', ['curveOne','curveTwo']],
['Vertical', ['line']],
['VerticalPoints', ['pointOne','pointTwo']]
]
# build dictionary lookup for args list at import time
constraintArgs = {}
for c in constraintNameList: constraintArgs[c[0]] = c[1]
# move constraints from one SketchEntity to another
def moveConstraints(toEntity, fromEntity):
toEntity = adsk.fusion.SketchEntity.cast(toEntity)
fromEntity = adsk.fusion.SketchEntity.cast(fromEntity)
count = fromEntity.geometricConstraints.count
# for each constraint in fromEntity
for i in list(range(count)):
# get constraint name and arguments
fromConstraint = fromEntity.geometricConstraints.item(i)
name = remove_suffix(fromConstraint.objectType.split('::')[-1],'Constraint')
argNames = constraintArgs[name]
args = []
# get the values of the arguments in the argument list
for a in argNames: args.append(eval('fromConstraint.'+a))
# substitute toEntity for fromEntity
for i in list(range(len(args))):
if args[i] == fromEntity:
args[i] = toEntity
# add new constraint back to sketch and delete old constraint
eval('toEntity.parentSketch.geometricConstraints.add'+name+'(*args)')
fromConstraint.deleteMe()
and here is a simple-minded test driver script, to be run during an open sketch:
#Author-WCA
#Description-tests moveConstraint
import adsk.core, adsk.fusion, traceback
from .moveconstraints import moveConstraints
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
filt = 'SketchPoints,SketchCurves'
fromEnt = ui.selectEntity('Select fromEntity', filt)
if fromEnt != None:
toEnt = ui.selectEntity('Select toEntity', filt)
if toEnt != None:
moveConstraints(toEnt.entity, fromEnt.entity)
ui.messageBox('moveConstraint() complete')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))