Message 1 of 7
ExecutePreview will occur
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi there.
The following sample creates a sphere when a point in the sketch is selected.
In MyCommandCreatedHandler, SelectionCommandInput.setSelectionLimits is set to "2", but ExecutePreview occurs even when only one point is selected.
Also, Command.isExecutedWhenPreEmpted = True does not leave any execution results.
# Fusion360API Python script
import traceback
import adsk.fusion
import adsk.core
_app: adsk.core.Application = None
_ui: adsk.core.UserInterface = None
_handlers = []
def run(context):
try:
global _app, _ui
_app = adsk.core.Application.get()
_ui = _app.userInterface
cmdDef: adsk.core.CommandDefinition = _ui.commandDefinitions.itemById(
'test_cmd_id'
)
if not cmdDef:
cmdDef = _ui.commandDefinitions.addButtonDefinition(
'test_cmd_id',
'test',
'test'
)
global _handlers
onCommandCreated = MyCommandCreatedHandler()
cmdDef.commandCreated.add(onCommandCreated)
_handlers.append(onCommandCreated)
cmdDef.execute()
adsk.autoTerminate(False)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.CommandCreatedEventArgs):
adsk.core.Application.get().log(args.firingEvent.name)
try:
global _handlers
cmd: adsk.core.Command = adsk.core.Command.cast(args.command)
cmd.isExecutedWhenPreEmpted = True # It's not working.
# event
onDestroy = MyCommandDestroyHandler()
cmd.destroy.add(onDestroy)
_handlers.append(onDestroy)
onExecutePreview = MyExecutePreviewHandler()
cmd.executePreview.add(onExecutePreview)
_handlers.append(onExecutePreview)
# inputs
inputs: adsk.core.CommandInputs = cmd.commandInputs
selIpt: adsk.core.SelectionCommandInput = inputs.addSelectionInput(
'selIpt',
'Select Scetch Points',
'Select Scetch Points'
)
selIpt.addSelectionFilter('SketchPoints')
selIpt.setSelectionLimits(2) # Limit is 2 or more.
except:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MyExecutePreviewHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.CommandEventArgs):
adsk.core.Application.get().log(args.firingEvent.name)
selIpt: adsk.core.SelectionCommandInput = args.command.commandInputs.itemById(
'selIpt')
pnts = [selIpt.selection(idx).entity.worldGeometry for idx in range(
selIpt.selectionCount)]
createSphereBodies(pnts)
def createSphereBodies(pnts: list):
radius = 1
tmpMgr: adsk.fusion.TemporaryBRepManager = adsk.fusion.TemporaryBRepManager.get()
sphereBodies: list = []
pnt: adsk.core.Point3D
for pnt in pnts:
sphere: adsk.fusion.BRepBody = tmpMgr.createSphere(pnt, radius)
sphereBodies.append(sphere)
app: adsk.core.Application = adsk.core.Application.get()
des: adsk.fusion.Design = app.activeProduct
root: adsk.fusion.Component = des.rootComponent
isParametric: bool = True
if des.designType == adsk.fusion.DesignTypes.DirectDesignType:
isParametric = False
occ: adsk.fusion.Occurrence = root.occurrences.addNewComponent(
adsk.core.Matrix3D.create()
)
comp: adsk.fusion.Component = occ.component
baseFeat: adsk.fusion.BaseFeature = None
if isParametric:
baseFeat = comp.features.baseFeatures.add()
bodies: adsk.fusion.BRepBodies = comp.bRepBodies
if isParametric:
baseFeat.startEdit()
for body in sphereBodies:
bodies.add(body, baseFeat)
baseFeat.finishEdit()
else:
for body in sphereBodies:
bodies.add(body)
class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.CommandEventArgs):
adsk.core.Application.get().log(args.firingEvent.name)
adsk.terminate()