Selection Event only returning the first item selected

Selection Event only returning the first item selected

Joshua.mursic
Advocate Advocate
617 Views
2 Replies
Message 1 of 3

Selection Event only returning the first item selected

Joshua.mursic
Advocate
Advocate

I am confused on how to use the selection Event, it is only returning the first item that is selected when I select multiple items. When I select component1:1 it correctly logs that, but when I select the next component, it logs the first item again.

 

Joshuamursic_0-1690322383713.png

 

 

 

import adsk.core,adsk.fusion,adsk.cam
from .lib import fusion360utils as futil
app = adsk.core.Application.get()
ui = app.userInterface
selections = []

def command_created(args: adsk.core.CommandCreatedEventArgs):
    '''Function that is called when a user clicks the corresponding button in the UI. This defines the contents of the command dialog and connects to the command related events.'''
    cmd = args.command
    inputs = cmd.commandInputs
    # Add All Handlers
    futil.add_handler(cmd.execute, command_execute)
    futil.add_handler(cmd.destroy, command_destroy)
    futil.add_handler(cmd.select, command_select)
    futil.add_handler(cmd.unselect, command_unSelect)
    
    cmdSelections = inputs.addSelectionInput("itemSelector","Selections","")
    cmdSelections.addSelectionFilter(adsk.core.SelectionCommandInput.Occurrences)
    cmdSelections.setSelectionLimits(1,0)


def command_select(args:adsk.core.SelectionEventArgs):
    selection:adsk.fusion.Occurrence = args.selection.entity
    app.log(f"selecting: {selection.name}")
    if selection:
        if selection.name not in selections:
            selections.append(selection.name)


def command_unSelect(args:adsk.core.SelectionEventArgs):
    selection:adsk.fusion.Occurrence = args.selection.entity
    app.log(f"unselecting: {selection.name}")
    if selection:
        if selection.name in selections:
            selections.remove(selection.name)


def command_execute(args:adsk.core.CommandEventArgs):
    app.log(f'{selections}')


def command_destroy(args: adsk.core.CommandEventArgs):
    app.log("DESTORY")
    pass

 

 

0 Likes
Accepted solutions (1)
618 Views
2 Replies
Replies (2)
Message 2 of 3

kandennti
Mentor
Mentor

Hi @Joshua.mursic .

 

I tried it. I can reproduce it.
I don't know if this is a bug or a specification, but how about changing the command_select function as follows?

def command_select(args: adsk.core.SelectionEventArgs):
    selIpt: adsk.core.SelectionCommandInput = args.activeInput
    if selIpt.selectionCount > len(selections):
        selection: adsk.fusion.Occurrence = selIpt.selection(
            selIpt.selectionCount - 1).entity
        app.log(f"selecting: {selection.name}")

        if selection:
            if selection.name not in selections:
                selections.append(selection.name)
0 Likes
Message 3 of 3

BrianEkins
Mentor
Mentor
Accepted solution

I've been told this is a bug and will be fixed in a hotfix. There is a workaround in the near term. Instead of using the selection property of the SelectionEventArgs to get the last thing selected you can instead use the selection property of the SelectionInput and get the last item in the collection. The workaround will continue to work even after the problem is fixed, but I would expect the fix sometime within the next couple of weeks.

selArgs: adsk.core.SelectionEventArgs = args
activeInput: adsk.core.SelectionCommandInput = selArgs.activeInput
selection = activeInput.selection(activeInput.selectionCount-1)

 

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com