insertFromDataPanel Event

insertFromDataPanel Event

hawx75
Participant Participant
1,047 Views
5 Replies
Message 1 of 6

insertFromDataPanel Event

hawx75
Participant
Participant

Is there a way to get an event for when the user inserts a design from the data panel similar to the Application.insertedFromUrl event?

0 Likes
Accepted solutions (2)
1,048 Views
5 Replies
Replies (5)
Message 2 of 6

kandennti
Mentor
Mentor
Accepted solution

Hi @hawx75 .

 

When I tried it, an event was triggered when I inserted the design from the data panel.
Using UserInterface.commandStarting Event, it was made to react only to FusionImportCommand.

#Fusion360API python addin

import adsk.core, adsk.fusion, traceback

handlers = []

class MyImportStartingHandler(adsk.core.ApplicationCommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.ApplicationCommandEventArgs.cast(args)

        ui  = eventArgs.firingEvent.sender
        if ui.activeCommand != 'FusionImportCommand':
            return
        ui.messageBox('Import Starting Event')

def run(context):
    ui = None
    try:
        app :adsk.core.Application = adsk.core.Application.get()
        ui  = app.userInterface

        onImportStarting = MyImportStartingHandler()
        ui.commandStarting.add(onImportStarting)
        handlers.append(onImportStarting)

    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()))

Since we have only performed simple tests, there may be some problems.

0 Likes
Message 3 of 6

hawx75
Participant
Participant

Thank you very much. You have also help me discover the huge amount of events that I can use. Never thought to print out UserInterface.commandDefinitions, it seems so obvious now.

0 Likes
Message 4 of 6

hawx75
Participant
Participant

@kandennti 

 

Is it possible to get the occurrence? It looks as if this event is triggered at the beginning of the import and so there doesn't seem to be a reference to an occurrence. I also tried using UserInterface.commandTerminated again no reference to an occurrence.

0 Likes
Message 5 of 6

kandennti
Mentor
Mentor
Accepted solution

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

 

0 Likes
Message 6 of 6

hawx75
Participant
Participant

Thank @kandennti, your a legend!

0 Likes