Community
Fusion API and Scripts
Got a new add-in to share? Need something specialized to be scripted? Ask questions or share what you’ve discovered with the community.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Switching From 'Manufacture' to 'Design view' back to 'Manufacture' Crashes fusion

2 REPLIES 2
SOLVED
Reply
Message 1 of 3
enervino
141 Views, 2 Replies

Switching From 'Manufacture' to 'Design view' back to 'Manufacture' Crashes fusion

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()))

 

2 REPLIES 2
Message 2 of 3
kandennti
in reply to: enervino

Hi @enervino -San.

 

I tried it and it did indeed crash.

 

With the April update, it is now possible to access the root component of the design without leaving Manufacture.
I tried drawing a new sketch and drawing a line without going back to the design and it worked fine.

# Fusion360API Python script

import traceback
import adsk.core as core
import adsk.fusion as fusion
import adsk.cam as cam

def run(context):
    ui: core.UserInterface = None
    try:
        app: core.Application = core.Application.get()
        ui = app.userInterface
        app.documents.add(core.DocumentTypes.FusionDesignDocumentType)

        camObj: cam.CAM = get_cam_product()

        rootOcc: fusion.Occurrence = camObj.designRootOccurrence
        root: fusion.Component = rootOcc.component

        skt: fusion.Sketch = root.sketches.add(root.xYConstructionPlane)
        skt.sketchCurves.sketchLines.addByTwoPoints(
            core.Point3D.create(0,0,0),
            core.Point3D.create(1,1,1)
        )

        ui.messageBox("Done")

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


def get_cam_product() -> cam.CAM:
    app: core.Application = core.Application.get()
    activete_cam_env()

    return app.activeProduct


def activete_cam_env() -> None:
    app: core.Application = core.Application.get()
    ui: core.UserInterface = app.userInterface

    camWS: core.Workspace = ui.workspaces.itemById('CAMEnvironment') 
    camWS.activate()
Message 3 of 3
enervino
in reply to: enervino

That is the better way, I just could not get it to work the way you just did, keep on getting exceptions. Thanks!!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk DevCon in Munich May 28-29th


Autodesk Design & Make Report