How to control ok button enable disable?

obatake5TC3R
Advocate Advocate
819 Views
3 Replies
Message 1 of 4

How to control ok button enable disable?

obatake5TC3R
Advocate
Advocate

I do not search to control ok button in 'command' object. There are isOkButtonVisible or okButtonText propaty. But there are no control ok button.

Can I control ok button disable or enable?

0 Likes
820 Views
3 Replies
Replies (3)
Message 2 of 4

ekinsb
Alumni
Alumni

The validateInputs event allows you to control this.  In this event you can do whatever validation you need to determine if the needed input has been specified and if the OK button should be enabled.  The ValidateInputsEventArgs object that's passed in through the validateInputs event has the areInputsValid property.  If you set this to False, then the OK button will be disabled.

 


Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog
0 Likes
Message 3 of 4

brad.bylls
Collaborator
Collaborator

Is there simple code for this?

I just need to disable and enable the OK button.

Most of my input is through preloaded drop down boxes.

I know I can make the OK button invisible but can't make it visible again. Who'd a thought?

Thank you.

Brad Bylls
0 Likes
Message 4 of 4

kandennti
Mentor
Mentor

Hi @brad.bylls .

 

It is probably not possible to display the [OK] button in the middle of the process.

As described by @ekinsb , I think it is better to use the validateInputs event to control the [OK] button.

 

We have created a simple sample here.

When the number of selections is even, the OK button is enabled, and when the number is odd, it is disabled.

1.png

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

_commandId = 'ValidateInputsSample'
_SelIptId = 'selIpt'

_handlers = []
_app = None
_ui = None

class CommandDestroyHandler(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 ValidateInputsHandler(adsk.core.ValidateInputsEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.ValidateInputsEventArgs.cast(args)

        global _SelIptId
        selIpt = eventArgs.inputs.itemById(_SelIptId)
        if not selIpt:
            return

        if selIpt.selectionCount % 2 == 0:
            # OK button enabled
            eventArgs.areInputsValid = True
        else:
            # OK button disabled
            eventArgs.areInputsValid = False


class CommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            cmd = args.command

            # event
            global _handlers
            onDestroy = CommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            onValidateInputs = ValidateInputsHandler()
            cmd.validateInputs.add(onValidateInputs)
            _handlers.append(onValidateInputs)

            # inputs
            inputs = cmd.commandInputs

            global _SelIptId
            msg = 'Select an entity. \nThe OK button will be enabled only \nwhen the number of selections is even.'
            selIpt = inputs.addSelectionInput(_SelIptId, msg, msg)
            selIpt.setSelectionLimits(0)

        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui = _app.userInterface

        global _commandId
        cmdDefs = _ui.commandDefinitions
        cmdDef = cmdDefs.itemById(_commandId)
        if cmdDef:
            cmdDef.deleteMe()

        cmdDef = cmdDefs.addButtonDefinition(_commandId, _commandId, _commandId)

        onCmdCreated = CommandCreatedHandler()
        cmdDef.commandCreated.add(onCmdCreated)
        _handlers.append(onCmdCreated)
        cmdDef.execute()

        adsk.autoTerminate(False)
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

No action will be taken when the OK button is pressed.

0 Likes