I also tried with commandTerminated.
When using messageBox, it became impossible to debug, and it was very difficult.
import adsk.core, adsk.fusion, traceback
_handlers = []
_occs = []
_app = adsk.core.Application.cast(None)
class MyFileImportHandler(adsk.core.ApplicationCommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
eventArgs = adsk.core.ApplicationCommandEventArgs.cast(args)
ui = _app.userInterface
cmdIdPair = '{}:{}'.format(ui.activeCommand,eventArgs.commandDefinition.id)
# print('^^^' + cmdIdPair) #debug
# self.dump_occs() #debug
if cmdIdPair == 'SelectCommand:ContinueDragFromDashboardCommand':
self.getOccs()
elif cmdIdPair == ':CommitCommand':
self.getInsertOccs()
def getOccs(self):
occs = _app.activeProduct.rootComponent.allOccurrences
global _occs
_occs = list([occ.name for occ in occs])
def getInsertOccs(self):
occs = _app.activeProduct.rootComponent.allOccurrences
tmp = list([occ.name for occ in occs])
global _occs
insertOccs = set(tmp) - set(_occs)
_occs = tmp.copy()
# Do not use messageBox. Debugging will not work.
# If you do, [Preferences]-[API]-[debugging Port]
# Please change the value of. ↓
# _app.ui.messageBox('\n'.join(insertOccs))
print('-- insert Occ Name --')
print('\n'.join(insertOccs))
# debug
def dump_occs(self):
occs = _app.activeProduct.rootComponent.allOccurrences
print([occ.name for occ in occs])
def run(context):
ui = None
try:
global _app, _occs
_app = adsk.core.Application.get()
ui = _app.userInterface
_occs = []
onImportStarting = MyFileImportHandler()
ui.commandTerminated.add(onImportStarting)
_handlers.append(onImportStarting)
ui.messageBox('Start addin')
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
ui.messageBox('Stop addin')
except:
if ui:
ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
I realized later that using Application.registerCustomEvent might be the right thing to do.
https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-85edd118-c2a4-11e6-b401-3417ebc87622