Events/Handlers for sketch selections and UserParameters

william-c-anderson
Advocate
Advocate

Events/Handlers for sketch selections and UserParameters

william-c-anderson
Advocate
Advocate

Is there a UI event (and associated handler) for when a new selection is made in the active sketch?

 

Similarly, is there an event/handler for when a change is made to UserParameters?

 

I'm interested in designing a new class in Python derived from SketchEntity, and this class will need to see when a member of its type is selected, as well as when a change is made to UserParameters.

0 Likes
Reply
Accepted solutions (1)
558 Views
2 Replies
Replies (2)

kandennti
Mentor
Mentor

Hi @william-c-anderson .

 

It is interpreted that UserParameters changes are made in the GUI.
The following code displays a dialog when the OK button is pressed.

1.png

#Fusion360 python adinn

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

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

class CommandHandler(adsk.core.ApplicationCommandEventHandler):
    def __init__(self, targetCommand):
        super().__init__()
        self.tgtCmd = targetCommand
    def notify(self, args):
        try:
            if _ui.activeCommand == self.tgtCmd:
                # Here, the change of UserParameters is checked.
                _ui.messageBox('call ' + self.tgtCmd)

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

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

        onCommand = CommandHandler('ChangeParameterCommand')
        _ui.commandTerminated.add(onCommand)
        _handlers.append(onCommand)
        print('--- Start addin ---')

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

def stop(context):
    try:
        for hdl in _handlers:
            _ui.commandTerminated.remove(hdl)
        _handlers.clear()
        print('--- Stop addin ---')

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


At this point, you should check that UserParameters has changed since the previous call.

0 Likes

william-c-anderson
Advocate
Advocate
Accepted solution

どうもありがとう, @kandennti ! Just what I needed this morning.

I've decided to give up on the idea of a derived class (no way to "pickle" the Python class instance to make it persistent, I'm guessing), but I think I have another way to accomplish my task.

0 Likes