Crashing After Saving Project

Crashing After Saving Project

Joshua.mursic
Advocate Advocate
266 Views
0 Replies
Message 1 of 1

Crashing After Saving Project

Joshua.mursic
Advocate
Advocate

I am trying to create a tool that allows me to copy a CAD model of a material stock from a template project and put it in another project that is full of active stock blocks. My goal is to be able to then target the new stock data file and insert it as a linked project into design projects so that I can easily place my models into a block of material and see what space is left in the block. 

 

Its almost working, except that when I save the new stock block into the active stock library project, Fusion is crashing.

 

I am trying to use this solution provided by @BrianEkins, to wait for the saved project to finish being saved, but I do not think I am using it correctly. 

https://forums.autodesk.com/t5/fusion-360-api-and-scripts/open-file-that-was-just-created-in-folder/... 

 

import adsk.core, adsk.fusion
from ...lib import fusion360utils as futil
app = adsk.core.Application.get()
ui = app.userInterface
local_handlers = []
stockSaveName = ''

def command_created(args: adsk.core.CommandCreatedEventArgs):
    # https://help.autodesk.com/view/fusion360/ENU/?contextId=CommandInputs
    inputs = args.command.commandInputs
    futil.add_handler(args.command.execute, command_execute, local_handlers=local_handlers)

    drop_down_style = adsk.core.DropDownStyles.LabeledIconDropDownStyle
    materialSelectionInput = inputs.addDropDownCommandInput('materialSelection', 'Material', drop_down_style)
    materialSelection = materialSelectionInput.listItems
    materialSelection.add('Titanium', True)
    materialSelection.add('Aluminium', False)
    materialSelection.add('Chrome Cobalt', False)

    thicknessSelectionInput = inputs.addDropDownCommandInput('thicknessSelection', 'Thickness', drop_down_style)
    thicknessSelection = thicknessSelectionInput.listItems
    thicknessSelection.add('14mm', True)
    thicknessSelection.add('16mm', False)
    thicknessSelection.add('17mm', False)
    thicknessSelection.add('18mm', False)
    thicknessSelection.add('20mm', False)
    thicknessSelection.add('25mm', False)
    thicknessSelection.add('30mm', False)

def command_execute(args: adsk.core.CommandEventArgs):
    # Connect to the dataFileComplete event, to watch for when the file has been fully saved on Fusion Team.
    onDataFileComplete = MyDataFileCompleteHandler()
    app.dataFileComplete.add(onDataFileComplete)
    local_handlers.append(onDataFileComplete)
    adsk.autoTerminate(False)
    
    data = app.data
    inputs = args.command.commandInputs
    materialSelection: adsk.core.DropDownCommandInput = inputs.itemById('materialSelection')
    thicknessSelection: adsk.core.DropDownCommandInput = inputs.itemById('thicknessSelection')

    stockSaveName = f'{materialSelection.selectedItem.name}-{thicknessSelection.selectedItem.name}'
    stockLibraryFolder = data.dataProjects.itemById(config.ACTIVE_STOCK_LIBRARY).rootFolder

    # Get the thickness of materal and get the cad model for the stock material
    machine = data.dataProjects.itemById(config.MACHINE_SETUP_FOLDER_ID)
    stockTypesFolder = machine.rootFolder.dataFolders.itemByName("Stock Types")
    stockTypes = stockTypesFolder.dataFiles.asArray()

    # Loop through stock types looking for type that user selected
    for stock in stockTypes:
        stockName = stock.name
        if stockName == stockSaveName:
            stockDataFile = stock
            break

    # Create a new document
    newDoc: adsk.fusion.FusionDocument = app.documents.add(adsk.core.DocumentTypes.FusionDesignDocumentType, True)
    # Save document so we can import Datafile
    newDoc.saveAs(stockSaveName, stockLibraryFolder, '', '')

    # Get the Design and root component from the document.
    design: adsk.fusion.Design = newDoc.products.itemByProductType('DesignProductType')
    newStock = design.rootComponent.occurrences.addByInsert(stockDataFile,adsk.core.Matrix3D.create(),False)
    newDoc.close(True)

class MyDataFileCompleteHandler(adsk.core.DataEventHandler):
    def __init__(self):
        super().__init__()
    def notify(self, args: adsk.core.DataEventArgs):
        # Check to see if the document we care about is the one that saved.
        if args.file.name == stockSaveName:
            topDoc = app.activeDocument

            # Insert the saved document into the activate document.
            des: adsk.fusion.Design = topDoc.products.itemByProductType('DesignProductType')
            root = des.rootComponent

            root.occurrences.addByInsert(args.file, adsk.core.Matrix3D.create(), True)

            adsk.terminate()

 

Am I going about this the wrong way? Is there a better way to achieve what I am trying to do?

0 Likes
267 Views
0 Replies
Replies (0)