Iterate through folder and save Pro/E parts and assemblies as STEP file

Iterate through folder and save Pro/E parts and assemblies as STEP file

Anonymous
Not applicable
576 Views
0 Replies
Message 1 of 1

Iterate through folder and save Pro/E parts and assemblies as STEP file

Anonymous
Not applicable

Hello All, I have been working my way through Python trying to develop a script that will automate the following:

 

1. Iterate through all Pro/E prt & asm files in a defined folder and open them one at a time in Fusion 360.

2. Export the open file to STEP format, and/or all components of an assembly file.

3. Close the document and move to the next file.

 

The script to automate this process is turning out to be tougher than I thought. Most of my scripting experience has been in Solidworks VBA. I did find the script @BrianEkins made for Saving Files as an Image, but I believe that was for Inventor. I modified it within vscode for Python to try and match proper conventions, incorporating the ExportManagerAPI Sample.

 

Additionally, I am having issues trying to get vscode to debug properly; the launch.json file doesn't like localRoot and remoteRoot. Please see the below code. If you can give any insight at all in helping me figure out a solution, I would greatly appreciate it. Thanks!

 

import adsk.core, adsk.fusion, traceback

import os.path, sys
from pathlib import Path

def run(context)
    try:
        app = adsk.core.Application.get()
        ui = app.userInterface

        # Specify the path where the files exist.  The rest of the
        # code expects this path name to end with a backslash.
        dirName = "//network/CAD Data/"

        # Loop through the files in the specified directory.
        # Open files in the specified directory.
        filename = Dir(dirName + "INPUT/" + "*.*")
        while filename <> ""

            if "prt" in filename or "asm" in filename:
                # Open the document.
                doc = app.Documents.Open(dirName + "INPUT/" + filename)
                
                # Get active design
                product = app.activeProduct
                design = adsk.fusion.Design.cast(product)
                allComps = design.allComponents
                exportMgr = design.exportManager

                for comp in allComps:
                    compName = comp.name
                    stepFilename = dirName + "OUTPUT/" + compName

                    stpOptions = exportMgr.createSTEPExportOptions(stepFilename, comp)
                    exportMgr.execute(stpOptions)

                # Close the current document.
                doc.Close(True)

            # Get the next filename.
            filename = Dir

        ui.messageBox ('Finished processing.')
    
    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
577 Views
0 Replies
Replies (0)