How to programmatically check if a selection input was cleared

How to programmatically check if a selection input was cleared

Anonymous
Not applicable
694 Views
1 Reply
Message 1 of 2

How to programmatically check if a selection input was cleared

Anonymous
Not applicable

I want to know if it is possible and if so, how to programmatically check if a selection input was cleared, like when a user has used the selection input from a GUI and now the selection input is highlighted blue (like in the image below), and then that user clicks on the "X" next to the selection input, the selection is cleared, but how can you check if they do so? 

Fusion selection example.png

0 Likes
Accepted solutions (1)
695 Views
1 Reply
Reply (1)
Message 2 of 2

ekinsb
Alumni
Alumni
Accepted solution

I'm not sure when you want to check to see if the selection was cleared but there are a couple of approaches to this.  If you only care when the command is executed you can just check the value of the selectionCount property of the selection input and if it's 0 then nothing is currently selected; either because nothing was ever selected or it was selected and then cleared.

 

The other approach is to watch for this while the command is running.  You can use the inputChanged event for this.  This event fires when anything related to any of the command inputs changes, including changes to the selections associated with selection command inputs.  I wrote a simple script to test this.  There is a StringValueCommandInput in the dialog that I'm using to report the current value of the selectionCount property when the selection changes. My test script is below.

 

import adsk.core, adsk.fusion, adsk.cam, traceback

_app = adsk.core.Application.cast(None)
_ui = adsk.core.UserInterface.cast(None)
_handlers = []


# Event handler for the inputChanged event.
class MyInputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.InputChangedEventArgs.cast(args)

        info = adsk.core.StringValueCommandInput.cast(eventArgs.inputs.itemById('info'))
        if eventArgs.input.id == 'select':
            sel = adsk.core.SelectionCommandInput.cast(eventArgs.inputs.itemById('select'))
            info.value = 'Selection count: ' + str(sel.selectionCount)
        else:
            info.value = ''            
        

# Event handler for the execute event.
class MyExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.CommandEventArgs.cast(args)

        # Code to react to the event.
        sel = adsk.core.SelectionCommandInput.cast(eventArgs.command.commandInputs.itemById('select'))
        _ui.messageBox('There are ' + str(sel.selectionCount) + ' items selected.')


# Event handler for the commandCreated event.
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
        cmd = eventArgs.command
        inputs = cmd.commandInputs

        onInputChanged = MyInputChangedHandler()
        cmd.inputChanged.add(onInputChanged)
        _handlers.append(onInputChanged)
        
        onExecute = MyExecuteHandler()
        cmd.execute.add(onExecute)
        _handlers.append(onExecute)
        
        onDestroy = MyDestroyHandler()
        cmd.destroy.add(onDestroy)
        _handlers.append(onDestroy)        
        
        # Add some inputs to the command dialog.
        sel = inputs.addSelectionInput('select', 'Entity', 'Select an entity')
        sel.addSelectionFilter('Faces')
        sel.setSelectionLimits(0,0)

        chkBox = inputs.addBoolValueInput('bool', 'Extra', True, '', False)
        
        txt = inputs.addStringValueInput('info', '', '')
        txt.isReadOnly = True        
    

# Event handler for the destroy event.
class MyDestroyHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        # Terminate the script.
        adsk.terminate()
        
        
def run(context):
    try:
        global _app, _ui
        _app = adsk.core.Application.get()
        _ui  = _app.userInterface

        # Create the command definition.
        cmdDef = _ui.commandDefinitions.itemById('testSelect')
        if cmdDef:
            cmdDef.deleteMe()
            
        cmdDef = _ui.commandDefinitions.addButtonDefinition('testSelect', 'Test Selection', 'Test Selection', '')
        
        # Connect to the command created event.
        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)  
        
        # Execute the command.
        cmdDef.execute()
        
        # Set this so the script doesn't automatically terminate after running the run function.
        adsk.autoTerminate(False)
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

Brian Ekins
Inventor and Fusion 360 API Expert
Mod the Machine blog