OK Button Control

kandennti
Mentor
Mentor

OK Button Control

kandennti
Mentor
Mentor

Hello everyone. It will be helpful if you can tell me.

 

I am testing a script(addButtonDefinition) with two SelectionCommandInput.

The desired method is to enable the OK button only when two SelectionCommandInputs are selected, and to disable the button otherwise.

 

I understand that you can control the OK button with the areInputsValid property of the ValidateInputsEventArgs object.
However, the firing of ValidateInputsEvent is the timing when two SelectionCommandInputs are selected.

In the following code, if one is selected, the OK button will be enabled and the ValidateInputsEvent will not occur, so the OK button cannot be controlled. How should I do it?

Thanks in advance.

# python script
import adsk.core, adsk.fusion, traceback

_commandId = 'ValidateInputs'
_input1ID = 'Selectioninput1'
_input2ID = 'Selectioninput2'

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

            inputs = args.inputs
            i1 :adsk.core.SelectionCommandInput = inputs.itemById(_input1ID)
            i2 :adsk.core.SelectionCommandInput = inputs.itemById(_input2ID)

            if i1.selectionCount > 0 and i2.selectionCount > 0:
                eventArgs.areInputsValid = True
            else:
                eventArgs.areInputsValid = False
                
            _ui.messageBox('call ValidateInputs')
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

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

            onDestroy = CommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            onValidateInput = ValidateInputHandler()
            cmd.validateInputs.add(onValidateInput)
            _handlers.append(onValidateInput)

            inputs = cmd.commandInputs
            i1 = inputs.addSelectionInput(_input1ID, _input1ID,  _input1ID)
            i1.setSelectionLimits(1,1)
            i2 = inputs.addSelectionInput(_input2ID, _input2ID,  _input2ID)
            i2.setSelectionLimits(1,1)

        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

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

OKButton.png

 

0 Likes
Reply
Accepted solutions (1)
812 Views
3 Replies
Replies (3)

kandennti
Mentor
Mentor

That's the way I came up.

 

・During the command activate event, two SelectionCommandInputs are forcibly selected and a ValidateInput event is generated.
 ↓
・During the ValidateInput event, assign the generated ValidateInputsEventArgs to a variable with a large scope.
 ↓
・ Empty two SelectionCommandInput.
 ↓
・ Check the condition during the InputChange event and enable / disable the OK button.

 

I thought this would work, but I couldn't.

# python script
import adsk.core, adsk.fusion, traceback

_commandId = 'ValidateInputs'
_input1ID = 'Selectioninput1'
_input2ID = 'Selectioninput2'

_handlers = []
_app = None
_ui = None
_valiIptEvtArgs = adsk.core.ValidateInputsEventArgs.cast(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 ValidateInputHandler(adsk.core.ValidateInputsEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        global _valiIptEvtArgs
        if _valiIptEvtArgs is not None:
            return

        try:
            _valiIptEvtArgs = adsk.core.ValidateInputsEventArgs.cast(args)

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

class InputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        global _valiIptEvtArgs
        if _valiIptEvtArgs is None:
            return

        try:
            inputs = args.inputs
            i1 :adsk.core.SelectionCommandInput = inputs.itemById(_input1ID)
            i2 :adsk.core.SelectionCommandInput = inputs.itemById(_input2ID)

            if i1.selectionCount > 0 and i2.selectionCount > 0:
                _valiIptEvtArgs.areInputsValid = True
            else:
                _valiIptEvtArgs.areInputsValid = False
                
            _ui.messageBox('InputChangedHandler')
        except:
            if _ui:
                _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class ActivateHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        if _valiIptEvtArgs is not None:
            return

        try:
            inputs = args.command.commandInputs
            i1 = inputs.itemById(_input1ID)
            i2 = inputs.itemById(_input2ID)

            root = _app.activeProduct.rootComponent
            i1.addSelection(root.xYConstructionPlane)
            i2.addSelection(root.yZConstructionPlane)

            # call ValidateInput

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

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

            onDestroy = CommandDestroyHandler()
            cmd.destroy.add(onDestroy)
            _handlers.append(onDestroy)

            onValidateInput = ValidateInputHandler()
            cmd.validateInputs.add(onValidateInput)
            _handlers.append(onValidateInput)

            onActivate = ActivateHandler()
            cmd.activate.add(onActivate)
            _handlers.append(onActivate)

            onInputChanged = InputChangedHandler()
            cmd.inputChanged.add(onInputChanged)
            _handlers.append(onInputChanged)

            inputs = cmd.commandInputs
            i1 = inputs.addSelectionInput(_input1ID, _input1ID,  _input1ID)
            i1.setSelectionLimits(1,1)
            i2 = inputs.addSelectionInput(_input2ID, _input2ID,  _input2ID)
            i2.setSelectionLimits(1,1)

        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

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

 

What I want to achieve is, for example, the state of a sweep command.

1.png

If you select a profile and no path is selected, the OK button is disabled.
When the command cannot be executed, it is natural that the OK button is disabled.

 

0 Likes

MichaelT_123
Advisor
Advisor
Accepted solution

Hi, Kondennti-san,

I am not sure if I fully understand the issue (had only very short glimpse at the post),

but if you want OK button active for both scenarios:

1.)  Selectioninput1=selected and  Selectioninput2=selected

2.)  Selectioninput1=selected and  Selectioninput2=not_selected

consider dynamically switch between as required:

Selectioninput2.setSelectionLimits(1,1) <==> Selectioninput2.setSelectionLimits(0)

 

Regards

MichaelT

 

 

MichaelT
1 Like

kandennti
Mentor
Mentor

Thank you @MichaelT_123 .

 

By setting to setSelectionLimits (0), we found that ValidateInput event occurs frequently. It seems we can solve it.

 

I did not understand the timing of firing the ValidateInput event at all. . .
(Even if you click on nothing, it will fire)

1 Like