@kandennti
The preselect handler works great unless I set the selection filter to occurrences. When I do this I get an error:

There is not an isVisible property for an adsk.fusion.component and when I hover over anything in the tree or screen that is not an occurrence or body I get this error. Seems to be anything at the root component level, like a plane, etc. I have included code below that will throw the error. I've tried putting in if statements in the handler to try and correct this with no success. As soon as it enters the handler it throws the error. Any help here would be greatly appreciated. This will work for me if I can get it to work.
Eric
# Fusion360API Python script
import adsk.core, adsk.fusion, traceback
ui = adsk.core.UserInterface.cast(None)
handlers = []
_prodBodies = []
_cushBodies = []
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
cmd = args.command
cmd.isExecutedWhenPreEmpted = False
global _inputs
inputs = cmd.commandInputs
selectInput = inputs.addSelectionInput(
'productBodies',
'productBodies',
'Please select Bodies'
)
selectInput.addSelectionFilter('Occurrences')
selectInput.setSelectionLimits(1)
selectInput = inputs.addSelectionInput(
'cushionBodies',
'cushionBodies',
'Please select Bodies'
)
selectInput.addSelectionFilter('Bodies')
selectInput.setSelectionLimits(1)
# Connect to the command related events.
onDestroy = MyCommandDestroyHandler()
cmd.destroy.add(onDestroy)
handlers.append(onDestroy)
onSelect = MySelectHandler()
cmd.select.add(onSelect)
handlers.append(onSelect)
onUnSelect = MyUnSelectHandler()
cmd.unselect.add(onUnSelect)
handlers.append(onUnSelect)
onPreSelect = MyPreSelectHandler()
cmd.preSelect.add(onPreSelect)
handlers.append(onPreSelect)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def stop(context):
"""This function will unload the add-in and icon. No customization necessary within this function."""
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
#ui.messageBox('Stop addin')
#deleting the com button definition (use the id created in the run function
# (adsk.core.CommandDefinitions.addButtonDefinition(butName))
cmdDef = ui.commandDefinitions.itemById('SelectionEventsTest')
if cmdDef:
cmdDef.deleteMe()
#deleting the button
addinsToolbarPanel = ui.allToolbarPanels.itemById('SolidScriptsAddinsPanel' )
cntrl = addinsToolbarPanel.controls.itemById('SelectionEventsTest')
if cntrl:
cntrl.deleteMe()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
adsk.terminate()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MyPreSelectHandler(adsk.core.SelectionEventHandler):
def __init__(self):
super().__init__()
def notify(self, args: adsk.core.SelectionEventArgs):
try:
app = adsk.core.Application.get()
ui = app.userInterface
if not args.selection.entity.isVisible:
args.isSelectable = False
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MySelectHandler(adsk.core.SelectionEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
app = adsk.core.Application.get()
ui = app.userInterface
try:
eventArgs = adsk.core.SelectionEventArgs.cast(args)
activeSelectionInput = eventArgs.firingEvent.activeInput
selectedBody = adsk.fusion.BRepBody.cast(args.selection.entity)
if selectedBody:
if activeSelectionInput.id == 'productBodies':
_prodBodies.append(selectedBody)
if activeSelectionInput.id == 'cushionBodies':
_cushBodies.append(selectedBody)
# dumpSelectionInputItem()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MyUnSelectHandler(adsk.core.SelectionEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
app = adsk.core.Application.get()
ui = app.userInterface
try:
eventArgs = adsk.core.SelectionEventArgs.cast(args)
activeSelectionInput = eventArgs.firingEvent.activeInput
selectedBody = adsk.fusion.BRepBody.cast(args.selection.entity)
if selectedBody:
if activeSelectionInput.id == 'productBodies':
_prodBodies.remove(selectedBody)
if activeSelectionInput.id == 'cushionBodies':
_cushBodies.remove(selectedBody)
# dumpSelectionInputItem()
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
def run(context):
global ui
ui = None
try:
app = adsk.core.Application.get()
ui = app.userInterface
myCmdDef = ui.commandDefinitions.itemById('SelectionEventsTest')
if myCmdDef is None:
myCmdDef = ui.commandDefinitions.addButtonDefinition(
'SelectionEventsTest',
'Selection Events Test',
'',
''
)
onCommandCreated = MyCommandCreatedHandler()
myCmdDef.commandCreated.add(onCommandCreated)
handlers.append(onCommandCreated)
myCmdDef.execute()
adsk.autoTerminate(False)
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))