CommandCreatedEvent not firing for Native Commands

CommandCreatedEvent not firing for Native Commands

DanStory
Explorer Explorer
720 Views
3 Replies
Message 1 of 4

CommandCreatedEvent not firing for Native Commands

DanStory
Explorer
Explorer

I am trying to detect when some preexisting (native) commands are created, and is not working. It works just fine if it is my own custom command.

 

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

_app = None
_ui = None
_handlers = []

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG)

class CamCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        # Never get here if the command is native
        global _app, _ui
        try:
            eventArgs = adsk.core.CommandCreatedEventArgs.cast(args)
            cmd = eventArgs.command
            cmdDef = cmd.parentCommandDefinition
            
            if _ui:        
                _ui.messageBox('CamCommandCreatedHandler; Id: {0}; Command: {1}'.format(cmdDef.id, cmd.objectType))
                
        except:
             if _ui:
                 _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
        
def run(context):
    global _app, _ui
    _app = adsk.core.Application.get()
    _ui = _app.userInterface
    try:
        logging.debug('Starting')

        onCommandCreated = CamCommandCreatedHandler()
        _handlers.append(onCommandCreated)
        
        cmdDefs = _ui.commandDefinitions
        cmdDef = cmdDefs.itemById('cmdMyCustomCommand')
        if cmdDef:
            cmdDef.deleteMe()
            
        for i in range(0, cmdDefs.count):
            cmdDef = cmdDefs.item(i)
            logging.debug('Name: {0}; Native: {1}; Id: {2}'.format(cmdDef.name, cmdDef.isNative, cmdDef.id))
        
        cmdDef = cmdDefs.itemById('IronStrategy_face') # vs: cmdDefs.addButtonDefinition('cmdMyCustomCommand','My Custom Command', '', '')
        handlerAdded = cmdDef.commandCreated.add(onCommandCreated)
        logging.debug('Name: {0}; Native: {1}; Id: {2}; CommandCreatedEvent: {3}'.format(cmdDef.name, cmdDef.isNative, cmdDef.id, handlerAdded))
        
        cmdDef.execute()
        
        adsk.autoTerminate(False)
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def stop(context):
    global _app, _ui
    try:
        logging.debug('Stopping')
        
    except:
        if _ui:
            _ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

My end goal is to add some custom UI elements to some native (CAM) commands' input, if there is a better method than injecting into each command's create event.

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

BrianEkins
Mentor
Mentor

What you're trying to do is not supported. It's not possible to customize any of the built-in commands.

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
0 Likes
Message 3 of 4

DanStory
Explorer
Explorer

Thanks @BrianEkins, would have excepted `commandCreated.add` to return false or an exception to be thrown.

Is it at all possible to programmatically set command input values on the "current command"? I am trying to write an add-in that can import "feeds & speeds" into CAM operations/commands from an external source (i.e. Clipboard or File)

0 Likes
Message 4 of 4

BrianEkins
Mentor
Mentor
Accepted solution

I agree. I would have expected a failure in that case. There isn't any support for interacting with a running command.

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