Actually, let me be more specific... I tried to pare down both a script and an add-in to the bare minimum to reproduce the problems I'm having (they are both supposed to do the same thing).
The first listing is a python "script" that mostly functions as intended. If you select a sketch curve, it selects ALL curves in that same sketch. There is one issue with this one that hopefully someone can answer: Why doesn't the ui.selectEntity function display the prompt as it's supposed to. (On my computer it shows nothing, although it does accept my selection).
The second listing is a python "add-in" with a command definition that allows the user to select that initial curve. The problem with this one is as I originally described: it doesn't keep the selections that are made. In addition, it seems to only select ONE of the curves, even though all of the "add" methods ARE being called within the loop (ie: it seems to only keep the LAST "add" since the message box shows 1 instead of the actual number of curves in the sketch).
Again, my 3 questions are:
- Why doesn't ui.selectEntity display the prompt as it's supposed to according to the api docs
- Why doesn't the add-in version of the script keep the selections after closing the messagebox?
- Why is it only selecting ONE of the curves instead of all of them (the loop is the same in both versions below)
Thanks for any help...
----- SCRIPT VERSION ------
#Author-
#Description-
import adsk.core, adsk.fusion, adsk.cam, traceback
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
sel = ui.selectEntity("Select a curve", "SketchCurves")
selcurve = sel.entity
sketch = selcurve.parentSketch
curves = sketch.sketchCurves
selections = ui.activeSelections
selections.clear()
for curve in curves:
selections.add(curve)
ui.messageBox(str(selections.count))
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
--- ADD-IN VERSION ----
import adsk.core, adsk.fusion, traceback
import os
handlers = []
app = adsk.core.Application.get()
if app:
ui = app.userInterface
class SelectionCommandExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
command = args.firingEvent.sender
inputs = command.commandInputs
input0 = inputs[0];
sel = input0.selection(0);
selcurve = sel.entity
sketch = selcurve.parentSketch
curves = sketch.sketchCurves
selections = ui.activeSelections
selections.clear()
for curve in curves:
selections.add(curve)
ui.messageBox(str(selections.count))
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class SelectionCommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
# when the command is done, terminate the script
# this will release all globals which will remove all event handlers
adsk.terminate()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class SelectionValidateInputHandler(adsk.core.ValidateInputsEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
sels = ui.activeSelections;
if len(sels) == 1:
args.areInputsValid = True
else:
args.areInputsValid = False
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class SelectionCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
cmd = args.command
onExecute = SelectionCommandExecuteHandler()
cmd.execute.add(onExecute)
onDestroy = SelectionCommandDestroyHandler()
cmd.destroy.add(onDestroy)
onValidateInput = SelectionValidateInputHandler()
cmd.validateInputs.add(onValidateInput)
# keep the handler referenced beyond this function
handlers.append(onExecute)
handlers.append(onDestroy)
handlers.append(onValidateInput)
#define the inputs
inputs = cmd.commandInputs
i1 = inputs.addSelectionInput('entity', 'Sketch Curve', 'Please select a sketch curve')
i1.addSelectionFilter(adsk.core.SelectionCommandInput.SketchCurves);
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context):
try:
product = app.activeProduct
design = adsk.fusion.Design.cast(product)
if not design:
ui.messageBox('It is not supported in current workspace, please change to MODEL workspace and try again.')
return
commandDefinitions = ui.commandDefinitions
# check the command exists or not
cmdDef = commandDefinitions.itemById('SelectionCMDDef')
if not cmdDef:
resourceDir = os.path.dirname(os.path.realpath(__file__)) # os.path.join(os.path.dirname(os.path.realpath(__file__)), 'resources') # absolute resource file path is specified
cmdDef = commandDefinitions.addButtonDefinition('SelectionCMDDef',
'Select Curves',
'Select sketch curves',
resourceDir)
onCommandCreated = SelectionCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
# keep the handler referenced beyond this function
handlers.append(onCommandCreated)
inputs = adsk.core.NamedValues.create()
cmdDef.execute(inputs)
# prevent this module from being terminate when the script returns, because we are waiting for event handlers to fire
adsk.autoTerminate(False)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))