Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Is it possible to keep selected objects after closing UI for input handler?

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
chris.monachanZWSCF
183 Views, 2 Replies

Is it possible to keep selected objects after closing UI for input handler?

I'm trying to write an add-in with UI that helps me select objects. The issue I'm running into is that the selection is cleared when my add-in returns. 

 

Is there a way to ensure that my selection stays when the user hits OK on my add-in? The built in selection tools (by size) seem to be able to do this. 

 

Thanks

Labels (1)
2 REPLIES 2
Message 2 of 3

Hi @chris.monachanZWSCF .

 

I tried to use the UserInterface.commandTerminated event.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-E381414E-1CFF-4007-B1DF-C413D7E6841A 

 

I assigned the selection made at the time of the execute event to a global variable and reselected it with the CommandTerminated event, and was able to exit with the selection made.

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

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

_cmdId = 'test_cmd'
_selInpId = 'selIptId'
_targetBody: adsk.fusion.BRepBody = None


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

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

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

            onExecute = MyExecuteHandler()
            cmd.execute.add(onExecute)
            _handlers.append(onExecute)

            global _selInpId
            selIpt: adsk.core.SelectionCommandInput = inputs.addSelectionInput(
                _selInpId,
                'Body',
                'Select Body'
            )
            selIpt.addSelectionFilter('Bodies')

            global _ui
            onCommandTerminated = MyCommandTerminatedHandler()
            _ui.commandTerminated.add(onCommandTerminated)
            _handlers.append(onCommandTerminated)

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


class MyExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()

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

        global _selInpId, _targetBody
        inputs: adsk.core.CommandInputs = args.command.commandInputs
        selIpt: adsk.core.SelectionCommandInput = inputs.itemById(_selInpId)
        _targetBody = selIpt.selection(0).entity


class MyCommandTerminatedHandler(adsk.core.ApplicationCommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: adsk.core.ApplicationCommandEventArgs):
        adsk.core.Application.get().log(args.firingEvent.name)
        global _cmdId
        if args.commandId != _cmdId:
            return

        global _targetBody
        if not _targetBody:
            return

        global _ui
        sels: adsk.core.Selections = _ui.activeSelections
        sels.clear
        sels.add(_targetBody)


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

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


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

        global _cmdId
        cmdDef: adsk.core.CommandDefinition = _ui.commandDefinitions.itemById(
            _cmdId
        )

        if not cmdDef:
            cmdDef = _ui.commandDefinitions.addButtonDefinition(
                _cmdId,
                'Test',
                'Test'
            )

        global _handlers
        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        _handlers.append(onCommandCreated)

        cmdDef.execute()
        _app.log(u'TextCommandWindow.Clear')

        adsk.autoTerminate(False)

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

 

UserInterface.selectEntit has a hard time clearing what was selected.

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/can-t-clear-the-activeselections/m-p/96738... 

Message 3 of 3

Excellent. That works for me as well. I didn't realise there was a terminate handler. Very useful. Thanks.

It's a shame there isn't just a bool you can set for the CommandHandler that says "retain selection", it seems to me that would be a pretty common user requirement for making tools to select what they need. 

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Technology Administrators