What is the Selection filter for operation?

What is the Selection filter for operation?

enervino
Enthusiast Enthusiast
470 Views
4 Replies
Message 1 of 5

What is the Selection filter for operation?

enervino
Enthusiast
Enthusiast

I would like to know how to select an operation using the GUI and then perform some modifications to it.

 

I have looked here and there is no option for this listed

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-03033DE6-AD8E-46B3-B4E6-DADA8D389E4E

 

I can select something like sketches, but not something like Setup or Operations

0 Likes
Accepted solutions (1)
471 Views
4 Replies
Replies (4)
Message 2 of 5

kandennti
Mentor
Mentor

Hi @enervino -San.

 

Unfortunately, there is currently no selection filter for CAM entities.

An alternative is to use a SelectionCommandInput object and use the Command.preSelect event to create the same effect as a selection filter.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-568db63a-0f28-4307-9e02-e29f54820db1 

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-E3D24F04-8576-420E-B9AD-44746A6B12EE 


However, the process would be more complex than the UserInterface.selectEntity method.

0 Likes
Message 3 of 5

enervino
Enthusiast
Enthusiast

I tried to make a sample script for this but I cannot seem to get it to work, what have I done wrong here?
ui.messageBox('Test0') is not even triggering here is what i have so far:

 

 

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

handlers = []  # This is where we will keep our event handlers
app = adsk.core.Application.get()
ui  = app.userInterface

def run(context):
    try:
        # Create a command definition.
        cmdDef = ui.commandDefinitions.itemById('SelectionFilter')
        if not cmdDef:
            cmdDef = ui.commandDefinitions.addButtonDefinition('SelectionFilter', 'Selection Filter', 'Creates a selection filter for CAM entities')

        # Connect to the command created event.
        cmdCreated = CommandCreatedHandler()
        cmdDef.commandCreated.add(cmdCreated)
        handlers.append(cmdCreated)

        # Execute the command.
        cmdDef.execute()

        # Prevent this module from being terminated when the script returns, because we are waiting for event handlers to fire.
        adsk.autoTerminate(False)
    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:
            # Get the command.
            cmd:adsk.core.Command = args.command

            # Get the CommandInputs collection to create new command inputs.
            inputs = cmd.commandInputs

            # Create a text box input.
            textBoxInput = inputs.addTextBoxCommandInput('myTextBox', 'Enter Text', '', 1, False)
            textBoxInput.text = 'Default Text'
            
            # Connect to the validateInputs event.
            onValidateInputs = ValidateInputsHandler()
            cmd.validateInputs.add(onValidateInputs)
            handlers.append(onValidateInputs)

            # Connect to the execute event.
            onExecute = ExecuteHandler()
            cmd.execute.add(onExecute)
            handlers.append(onExecute)

            # Connect to the preSelect event.
            onPreSelect = PreSelectHandler()
            cmd.preSelect.add(onPreSelect)
            handlers.append(onPreSelect)
            


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

selectedOperationName = ''
class PreSelectHandler(adsk.core.SelectionEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        global selectedOperationName
        
        ui.messageBox('Test0')
        
        try:
            # Get the selection.
            selection = args.selection
            
            # Check if the selected entity is a Operation
            if isinstance(selection.entity, adsk.cam.Operation):
                args.isSelectable = True

                # Store the name of the selected operation.
                selectedOperationName = selection.entity.name
            else:
                args.isSelectable = False
        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

class ValidateInputsHandler(adsk.core.ValidateInputsEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        global selectedOperationName
        try:
            #ui.messageBox("Test2")
            # Get the command.
            cmd = args.firingEvent.sender

            # Get the CommandInputs collection.
            inputs = cmd.commandInputs

            # Get the text box input.
            textBoxInput = inputs.itemById('myTextBox')

            # Set the text of the text box to the name of the selected operation.
            textBoxInput.text = selectedOperationName
        except:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

# Event handler for the execute event.
class ExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        pass
        ui.messageBox("Test3")

 

0 Likes
Message 4 of 5

enervino
Enthusiast
Enthusiast

OK, the issue seemed to be it needed a selection input to start the events. so I basically added:

selectInput = inputs.addSelectionInput('test', 'test2', 'test3')
Message 5 of 5

kandennti
Mentor
Mentor
Accepted solution

@enervino -San.

 

Without the SelectionCommandInput object, the PreSelect event will not be fired.

 

This is a minimal sample where only the Cam.Operation object can be selected.

# Fusion360API Python script

import traceback
import adsk
import adsk.core as core
import adsk.cam as cam

_app: core.Application = None
_ui: core.UserInterface = None
_handlers = []

CMD_INFO = {
    "id": "Selection_Filter_Test",
    "name": "test",
    "tooltip": "test"
}


class MyCommandCreatedHandler(core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args: core.CommandCreatedEventArgs):
        core.Application.get().log(args.firingEvent.name)
        try:
            global _handlers
            cmd: core.Command = core.Command.cast(args.command)
            inputs: core.CommandInputs = cmd.commandInputs

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

            onPreSelect = MyPreSelectHandler()
            cmd.preSelect.add(onPreSelect)
            _handlers.append(onPreSelect)

            operationIpt: core.SelectionCommandInput = inputs.addSelectionInput(
                "operationIptId",
                "Operation",
                "Select Operation",
            )

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


class MyPreSelectHandler(adsk.core.SelectionEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: core.SelectionEventArgs):
        if args.activeInput.id != "operationIptId": return

        entity = args.selection.entity
        if entity.classType() != cam.Operation.classType():
            args.isSelectable = False


class MyCommandDestroyHandler(core.CommandEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args: core.CommandEventArgs):
        core.Application.get().log(args.firingEvent.name)
        adsk.terminate()


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

        cmdDef: core.CommandDefinition = _ui.commandDefinitions.itemById(
            CMD_INFO["id"]
        )

        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition(
                CMD_INFO["id"],
                CMD_INFO["name"],
                CMD_INFO["tooltip"]
            )

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