@Anonymous wrote:
That all happens inside the CommandExecuteEventHandler maybe thats the difference?
You're right. The code doesn't work when selectEntity is used inside the CommandExecuteEventHandler.
The documentation says that the method provides a simple way to prompt the user for a selection in a script.
Maybe @BrianEkins or @goyals can give us more information?
Warning: the code below make Fusion 360 to crash. Save your work before trying it.
import adsk.core, adsk.fusion, adsk.cam, traceback
_handlers = []
def run(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
cmdDefs = ui.commandDefinitions
profilesSelectionButton = cmdDefs.addButtonDefinition('profilesSelectionButton',
'Profiles Selection',
'',
'')
profilesSelectionCommandCreated = profilesSelectionCommandCreatedEventHandler()
profilesSelectionButton.commandCreated.add(profilesSelectionCommandCreated)
_handlers.append(profilesSelectionCommandCreated)
makePanel = ui.allToolbarPanels.itemById('MakePanel')
makePanel.controls.addCommand(profilesSelectionButton, '', False)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def stop(context):
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
cmdDef = ui.commandDefinitions.itemById('profilesSelectionButton')
if cmdDef:
cmdDef.deleteMe()
makePanel = ui.allToolbarPanels.itemById('MakePanel')
cntrl = makePanel.controls.itemById('profilesSelectionButton')
if cntrl:
cntrl.deleteMe()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
# Event handler for the commandCreated event.
class profilesSelectionCommandCreatedEventHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
cmd = eventArgs.command
inputs = cmd.commandInputs
inputs.addTextBoxCommandInput('myTextBox', '', 'Click OK', 1, True)
# Connect to the execute event.
onExecute = profilesSelectionCommandOKHandler()
cmd.execute.add(onExecute)
_handlers.append(onExecute)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()), '')
# Event handler for the execute event.
class profilesSelectionCommandOKHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
app = adsk.core.Application.get()
ui = app.userInterface
design = app.activeProduct
# Get the root component of the active design.
rootComp = design.rootComponent
# Create a new sketch on the xy plane.
sketches = rootComp.sketches
xyPlane = rootComp.xYConstructionPlane
sketch = sketches.add(xyPlane)
# Draw some circles.
circles = sketch.sketchCurves.sketchCircles
circle1 = circles.addByCenterRadius(adsk.core.Point3D.create(0, 0, 0), 2)
circle2 = circles.addByCenterRadius(adsk.core.Point3D.create(8, 3, 0), 3)
# Add a circle at the center of one of the existing circles.
circle3 = circles.addByCenterRadius(circle2.centerSketchPoint, 4)
profiles = adsk.core.ObjectCollection.create()
selection = ui.selectEntity('Select profiles', 'Profiles')
if selection:
profiles.add(selection.entity)
# while 1:
# try:
# selection = ui.selectEntity('Select profiles', 'Profiles')
# if selection:
# profiles.add(selection.entity)
# else:
# break
# except:
# break
if profiles.count > 0:
# Create an extrusion input
extrudes = rootComp.features.extrudeFeatures
extInput = extrudes.createInput(profiles, adsk.fusion.FeatureOperations.NewBodyFeatureOperation)
# Define that the extent is a distance extent of 5 cm
distance = adsk.core.ValueInput.createByReal(5.0)
# Set the distance extent
extInput.setDistanceExtent(False, distance)
# Set the extrude type to be solid
extInput.isSolid = True
# Create the extrusion
ext = extrudes.add(extInput)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()), '')