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