Message 1 of 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Please have a look at the following simplified Python script for fusion, the part with the issue happens in
"MyExecuteHandler"
Basically, I'm trying to switch to Design view, draw something, then switch back to Manufacture to make a program including that new geometry, then generate a response.
Note: this will Crash Fusion!!
Run this script from the 'Manufacture' workspace.
import adsk.core
import adsk.fusion
import adsk.cam
import traceback
# Globals
_app: adsk.core.Application = adsk.core.Application.cast(None)
_ui: adsk.core.UserInterface = adsk.core.UserInterface.cast(None)
_design: adsk.core.Product = adsk.core.Product.cast(None)
handlers = []
# main Function
def run(context):
try:
global _app, _ui, _design
_app = adsk.core.Application.get()
_ui = _app.userInterface
_design = _app.activeProduct
myCmdDef = _ui.commandDefinitions.itemById(
'SelectionEventsSample_Python')
if myCmdDef is None:
myCmdDef = _ui.commandDefinitions.addButtonDefinition(
'SelectionEventsSample_Python', 'Selection Events Sample', '', '')
# Connect to the command created event.
onCommandCreated = MyCommandCreatedHandler()
myCmdDef.commandCreated.add(onCommandCreated)
handlers.append(onCommandCreated)
# Execute the command.
myCmdDef.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()))
# Event Handelers
class MyCommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
cmd = args.command
cmd.isExecutedWhenPreEmpted = False
inputs = adsk.core.CommandInputs.cast(cmd.commandInputs)
####################################################################################################
######Create Events to handle inputs################################################################
selectSectionLines = inputs.addSelectionInput(
'SectionLines', 'Lines', 'Select Sketch Lines')
selectSectionLines.addSelectionFilter(adsk.core.SelectionCommandInput.SketchLines)
selectSectionLines.addSelectionFilter(adsk.core.SelectionCommandInput.Sketches)
selectSectionLines.setSelectionLimits(0)
# Connect to the command related events.
onExecutePreview = MyCommandExecutePreviewHandler()
cmd.executePreview.add(onExecutePreview)
handlers.append(onExecutePreview)
# on Cancel Button Press
onDestroy = MyCommandDestroyHandler()
cmd.destroy.add(onDestroy)
handlers.append(onDestroy)
# on OK button press
onExecute = MyExecuteHandler()
cmd.execute.add(onExecute)
handlers.append(onExecute)
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MyCommandExecutePreviewHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
pass
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
class MyCommandDestroyHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
adsk.terminate()
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
#gets run on ok click
class MyExecuteHandler(adsk.core.CommandEventHandler):
def __init__(self):
super().__init__()
def notify(self, args):
try:
#be sure to start this in "Manufacture" to encounter the crash
#if not crashing Run in "Design view" then "Manufacture"
#With my testing this sort of switshing is possible when you run a script with no GUI
#switch to Design view
designWorkspace = _ui.workspaces.itemById("FusionSolidEnvironment")
designWorkspace.activate()
#draw some lines on a sketch
#note you need to be in "Design view" to actually draw something it seems
#Switch to Cam enviorment
camWorkspace = _ui.workspaces.itemById("CAMEnvironment")
camWorkspace.activate()
#Put newly created lines into a program
#generate program
#note you need to be in "Manufacture" to generate progams
#cam.generateAllToolpaths(True)
adsk.terminate()
except:
if _ui:
_ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
Solved! Go to Solution.