Feature Request: Undo entire script execution rather than script procedures

Feature Request: Undo entire script execution rather than script procedures

gscotti
Enthusiast Enthusiast
660 Views
2 Replies
Message 1 of 3

Feature Request: Undo entire script execution rather than script procedures

gscotti
Enthusiast
Enthusiast

I have a script that creates thousands of operations and having to "Ctrl-Z" a thousand times to get where it was before is kind of annoying.

 

0 Likes
661 Views
2 Replies
Replies (2)
Message 2 of 3

liujac
Alumni
Alumni

Hi,

 

You can wrap the operations in a Command, so that there is only one transaction (undo/redo) for the operations. Take the following script for example.

 

 

import adsk.core, adsk.fusion, traceback

app = None
ui  = None
commandId = 'NyCommandId'
commandName = 'My Command'
commandDescription = 'Demo My Command'

# Global set of event handlers to keep them referenced for the duration of the command
handlers = []
                

class MyCommandExecuteHandler(adsk.core.CommandEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            design = adsk.fusion.Design.cast(app.activeProduct)
            root = design.rootComponent
            
            # Do operations here
            
            basefeat = root.features.baseFeatures.add()
            
            basefeat.startEdit()
            root.sketches.addToBaseOrFormFeature(root.xYConstructionPlane, basefeat, False)
            basefeat.finishEdit()
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
                
        adsk.terminate()

class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args):
        try:
            cmd = args.command
            
            onExecute = MyCommandExecuteHandler()
            cmd.execute.add(onExecute)
            handlers.append(onExecute)    
        except:
            if ui:
                ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))

def run(context):
    ui = None
    try:
        global app
        app = adsk.core.Application.get()
        global ui
        ui = app.userInterface

        global commandId
        global commandName
        global commandDescription

        # Create command defintion
        cmdDef = ui.commandDefinitions.itemById(commandId)
        if not cmdDef:
            cmdDef = ui.commandDefinitions.addButtonDefinition(commandId, commandName, commandDescription)

        # Add command created event
        onCommandCreated = MyCommandCreatedHandler()
        cmdDef.commandCreated.add(onCommandCreated)
        # Keep the handler referenced beyond this function
        handlers.append(onCommandCreated)

        # Execute command
        cmdDef.execute()

        # Prevent this module from being terminate when the script returns, because we are waiting for event handlers to fire
        adsk.autoTerminate(False)

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

Jack

Message 3 of 3

gscotti
Enthusiast
Enthusiast

Thanks, Jack.

Your answer was very helpful.

 

As a reference to others (including myself):

"Fusion Commands" http://help.autodesk.com/view/NINVFUS/ENU/?guid=GUID-3922697A-7BF1-4799-9A5B-C8539DF57051 has everything about the User Interface and script structure - must read

 

"Command Inputs" http://help.autodesk.com/view/NINVFUS/ENU/?guid=GUID-8B9041D5-75CC-4515-B4BB-4CF2CD5BC359 has a nice description of visual assist command inputs that is really, really, useful.

 

0 Likes