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: 

Forcing Chain Selection of Sketch Elements

2 REPLIES 2
Reply
Message 1 of 3
g.alaee
248 Views, 2 Replies

Forcing Chain Selection of Sketch Elements

Is it possible to force chain selection of sketch elements using SelectionCommandInput? I was not able to find any related filter in Selection Filters. I am looking for something like Chain Selection option in offset sketch UI.

 

Offset.png

2 REPLIES 2
Message 2 of 3
adminTCYL2
in reply to: g.alaee

Yes, that is what I am looking for as well. 

 

Message 3 of 3
kandennti
in reply to: adminTCYL2

@adminTCYL2 .

 

I gave it a shot.

If I write the result first, it is not well done.

It works correctly when just selecting, but incorrectly when clicking and deselecting the selected items.

# Fusion360API Python script
# -- It does not work correctly. --

import traceback
import adsk.fusion
import adsk.core

_app = None
_ui = None
_handlers = []
_lastSelectEntity = None


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

    def notify(self, args):
        try:
            global _handlers
            cmd = 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)

            onSelect = MySelectHandler()
            cmd.select.add(onSelect)
            _handlers.append(onSelect)

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

            selIpt: adsk.core.SelectionCommandInput = inputs.addSelectionInput(
                'selIpt',
                'Sketch curves',
                'Sketch curves'
            )
            selIpt.addSelectionFilter('SketchCurves')
            selIpt.setSelectionLimits(0)

            inputs.addBoolValueInput(
                'chainIpt',
                'Chain Selection',
                True,
                '',
                True
            )

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


class MyInputChangedHandler(adsk.core.InputChangedEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args):
        try:
            _app.log('Call InputChanged Event')
            eventArgs = adsk.core.InputChangedEventArgs.cast(args)

            if not eventArgs.input.id == 'selIpt':
                return

            ipts: adsk.core.CommandInputs = eventArgs.input.commandInputs
            chainIpt: adsk.core.BoolValueCommandInput = ipts.itemById(
                'chainIpt')
            if not chainIpt.value:
                return

            global _lastSelectEntity
            if not _lastSelectEntity:
                return

            path: adsk.fusion.Path = adsk.fusion.Path.create(
                _lastSelectEntity,
                adsk.fusion.ChainedCurveOptions.tangentChainedCurves)

            selIpt: adsk.core.SelectionCommandInput = eventArgs.input
            selectedEnts = [selIpt.selection(idx).entity.entityToken for idx in range(
                selIpt.selectionCount)]
            pathEnts = [p.entity for p in path]

            [selIpt.addSelection(ent)
             for ent in pathEnts if not ent.entityToken in selectedEnts]

            _lastSelectEntity = None
            _app.log('finish InputChanged Event')
        except:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))


class MySelectHandler(adsk.core.SelectionEventHandler):
    def __init__(self):
        super().__init__()

    def notify(self, args):
        _app.log('Call Select Event')
        eventArgs = adsk.core.SelectionEventArgs.cast(args)

        global _lastSelectEntity
        _lastSelectEntity = eventArgs.selection.entity


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

    def notify(self, args):
        _app.log('Call Execute Event')


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

    def notify(self, args):
        _app.log('Call Destroy Event')


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

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

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

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

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report