Python Script - Save A New Part To A Specific Folder

Python Script - Save A New Part To A Specific Folder

isocam
Collaborator Collaborator
486 Views
1 Reply
Message 1 of 2

Python Script - Save A New Part To A Specific Folder

isocam
Collaborator
Collaborator

Can anybody help?

 

Please see the attached Python script. I have changed ",py" to ".txt"

 

I have a Python script that uses "SaveAs" to save an already existing opened part to a specific folder with a specific part number.

 

This script works perfectly!

 

However,,,,,

 

If I create a brand new part, draw a sketch then extrude the sketch, running the Python script does not work because it is trying to save a unsaved part rather than using an existing part that has previously been saved.

 

Does anybody know how to update the Python script so that it allows me to save a brand new part, with a specific part number, to a particular folder?

 

Many thanks in advance!

 

Darren

0 Likes
Accepted solutions (1)
487 Views
1 Reply
Reply (1)
Message 2 of 2

Rushikesh.kadam
Autodesk
Autodesk
Accepted solution

@isocam the script is not running for an unsaved file as you are trying to access datafile from None. Since the file is not saved documentToSaveAs.dataFile returns None.

Here is the modified script which will work for both saved and unsaved files. 

import adsk.core, adsk.fusion, adsk.cam, traceback

def run(context):
    ui = adsk.core.UserInterface.cast(None)
    try:
        app :adsk.fusion.Application = adsk.core.Application.get()

        ui = app.userInterface


        PartNumber = "12345"


        PartDescription = "FUSION 360 TEST"


        rootComponent = app.activeProduct.rootComponent

        rootComponent.description = PartDescription


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

def SaveDocument(PartNumber):
    ui = None
    try:
        app = adsk.core.Application.get()

        ui = app.userInterface

        destinationFolderName = "Fusion 360 Test Folder"

        documentToSaveAs = app.activeDocument
        
     # this code is added
     # ----------------------------------------   
        if documentToSaveAs.dataFile is None:
            dataProject = app.data.activeProject
        else:
            dataProject = documentToSaveAs.dataFile.parentProject
     # ----------------------------------------   
        destinationFolder = findFolderInProject(dataProject, destinationFolderName)

        if destinationFolder is not None:
            documentToSaveAs.saveAs(PartNumber, destinationFolder, "", "#tag")
        else:
            ui.messageBox("Destination {} folder not found!".format(destinationFolderName))

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

# ------------------------------------------------------------------------------------

def findFolderInProject(dataProject, dataFolderName: str):
    return findFolderSubfolders(dataProject.rootFolder, dataFolderName)

# ------------------------------------------------------------------------------------

def findFolderSubfolders(dataFolder, dataFolderName: str):
    for folder in dataFolder.dataFolders:
        if folder.name == dataFolderName:
            return folder
        else:
            return findFolderSubfolders(folder, dataFolderName)
    return None

# ------------------------------------------------------------------------------------

Hope it helps.

 

------------------------------------------------------------------------------------------------------------------------------

If my reply was helpful, please click the "Accept as Solution" button. It helps others get the answer quickly! A "Like" is always welcomed.

 




Rushikesh Kadam
Senior QA Engineer
Quality Assurance
Autodesk, Inc.


0 Likes