Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Command Cancel Button

3 REPLIES 3
SOLVED
Reply
Message 1 of 4
lucabortolotti1984
601 Views, 3 Replies

Command Cancel Button

I have an Addins which creates a command with different inputs.

This command has an Ok and a Cancel button, I have 2 questions:

  1. How can I hide the cancel button? I found a reference in the manual (here) to isCancelButtonVisible property, but it seems that it doesn't really exist. CancelButton.PNG
  2. How can I register a callback for Cancel button? For the 'Ok' button there's the 'execute' event. For cancel I tried the 'destroy' event, but it's not called on 'Cancel', just on destroy (as the name says!). I don't wanna destroy the button, which is always there in the toolbar, but I need to cleanup some things if the user press cancel...

 

Thanks for the reply. 

3 REPLIES 3
Message 2 of 4

Hi @lucabortolotti1984 .

 

Probably the isCancelButtonVisible property does not exist.

 

When I tried it, the destroy event was called by both the OK and Cancel buttons.
In order to determine which button was pressed, I prepared a variable with a large scope and tried to determine whether the Execute event was called or not.

# Fusion360API Python Addin
import adsk.core, adsk.fusion, traceback

_app = None
_ui  = None
_handlers = []
_isSelectedCancel = True #Record that the Cancel button was pressed.

class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            global _handlers
            cmd = adsk.core.Command.cast(args.command)
            inputs = cmd.commandInputs
            
            onDestroy = MyCommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            onExecute = MyExecuteHandler()
            cmd.execute.add(onExecute)
            _handlers.append(onExecute)
            
            inputs.addTextBoxCommandInput('test', 'test', '-', 1, True)
            
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class MyExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        dumpMsg('Call Execute Event')

        global _isSelectedCancel
        _isSelectedCancel = False

class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        dumpMsg('Call Destroy Event')

        global _ui, _isSelectedCancel
        msg = 'Cancel' if _isSelectedCancel else 'OK'

        _ui.messageBox(f'Selected {msg} Button')

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface
        
        cmdDef = _ui.commandDefinitions.itemById('Test')
        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition('Test', '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()))

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

def dumpMsg(msg :str):
    adsk.core.Application.get().userInterface.palettes.itemById('TextCommands').writeText(str(msg))

 

I don't know the content, but if you use the executePreview event, you may not need to clean up.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-dbecfaf8-9bb6-4e40-9df3-0a3e48136a4b 

Message 3 of 4

Hi @kandennti, thanks for the reply.
Actually in your case is called because you are using a script, I guess.
In my case, the button still always open (never calls destroy, also in case of ok), because it's a command in the toolbar, and it creates command inputs when its createdHandler is executed. It's an addin.
The destroy is called only when I stop the addin.

Anyway, I solved by resetting the variables at the beginning of the "createHandler".

Thanks again for the reply.
Message 4 of 4

No way, it works also in my case when I hit cancel.

I should've missed something when I tried, but it was late last time 😉

 

Thanks again

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report