API to Open a specific file

API to Open a specific file

info83PHN
Advocate Advocate
3,161 Views
7 Replies
Message 1 of 8

API to Open a specific file

info83PHN
Advocate
Advocate

Trying to get my python script to open the Design that I need to work with ( rest of the script works perfectly if the Design is already open )

 

Found some code online, but even if I isolate this to a separate script, Fusion freezes up every time I try and run it.

 

Can anyone spot an error, or suggest better code that works to open a single Design file in the UI ?

 

#Author-
#Description-

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

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

        theData = app.data

        i = 0
        while i < theData.dataProjects.count:
          if (theData.dataProjects.item(i).name == 'My Client'):
            j = 0
            while j < theData.dataProjects.item(i).rootFolder.dataFolders.count:
              if (theData.dataProjects.item(i).rootFolder.dataFolders.item(j).name == 'Common Masters'):
              k = 0
              while k < theData.dataProjects.item(i).rootFolder.dataFolders.item(j).dataFiles.count:
                    app.documents.open(theData.dataProjects.item(i).rootFolder.dataFolders.item(j).dataFiles.item(k),False)
              k += 1
            return
          j += 1
        i += 1



    except:
        if ui:
            ui.messageBox('Failed:\n{}'.format(traceback.format_exc()))
0 Likes
Accepted solutions (2)
3,162 Views
7 Replies
Replies (7)
Message 2 of 8

kandennti
Mentor
Mentor
Accepted solution

Hi @info83PHN .

 

Instead of looping through DataProjects, DataFolders, and DataFiles, use the asArray method to get the list and then loop through it.
This will make the process faster.

 

# Fusion360API Python script

import traceback
import adsk.fusion
import adsk.core

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

        # ** target name ** 
        targetProjectName: str = 'My Client'
        targetFolderName: str = 'Common Masters'
        # ** **

        # get dataProject
        theData: adsk.core.Data = app.data
        targetProj: adsk.core.DataProject = None
        projLst: list = theData.dataProjects.asArray()
        proj: adsk.core.DataProject
        for proj in projLst:
            if proj.name == targetProjectName:
                targetProj = proj
                break

        if not targetProj:
            ui.messageBox(f'Project not found [{targetProjectName}]')
            return

        # get dataFolder
        targetFold: adsk.core.DataFolder = None
        foldLst: list = targetProj.rootFolder.dataFolders.asArray()
        fold: adsk.core.DataFolder
        for fold in foldLst:
            if fold.name == targetFolderName:
                targetFold = fold
                break

        if not targetFold:
            ui.messageBox(f'Folder not found [{targetFolderName}]')
            return

        # open datafiles
        fileLst: list = targetFold.dataFiles.asArray()
        file:adsk.core.DataFile
        for file in fileLst:
            app.documents.open(file)


        ui.messageBox('Done')

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

 

 

Also, it is better not to use False for the second argument of Documents.open. You can read about it in the documentation.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-879042c8-f064-4b31-927a-4b6a67134936 

Message 3 of 8

info83PHN
Advocate
Advocate

@kandenntiThank You

 

That is working great. Really appreciate it.

 

I am guessing, from what I have read, that the list of Projects and Folders is read from the online server ?

 

Is there a way to open the local copy of the file without needing to retrieve the lists from the online / cloud server ?

 

Tested by going Offline, and the code fails.
Is it possible to save the Design file offline and open that, instead of having to download the lists ?

0 Likes
Message 4 of 8

kandennti
Mentor
Mentor

@info83PHN .

 

Here is a sample of importing a local file.

https://help.autodesk.com/view/fusion360/ENU/?guid=GUID-3f24e9e8-422d-11e5-937b-f8b156d7cd97 

 

The local cached files are located in a folder that can be opened with the following text command

Cache.open

However, if you import this file with importManager, it will be treated as a separate file from the DataHub file.
Also, files that have links to other documents cannot be opened.

 

I don't know why you are doing this offline, but I think you should do it online.

0 Likes
Message 5 of 8

info83PHN
Advocate
Advocate

< I don't know why you are doing this offline, but I think you should do it online. >

 

I have a single master file that I use as a template - it never changes.

I open the Design, Save As a new name, then make changes and Save the STL file.

 

Because the Design ( template ) never changes, it would be better if I could open the local copy faster.

Message 6 of 8

BrianEkins
Mentor
Mentor
Accepted solution

You can get the folder in a more efficient way by using the line below. It would be nice if the DataProjects object also supported an itemByName method.

# get dataFolder
targetFold: adsk.core.DataFolder = targetProj.rootFolder.dataFolders.itemByName(targetFolderName)

 

For your use case, I think a local file is OK since you won't be changing and saving it.  You can create a new file, using the local file as the template, export your STL file, and then close the file without saving when you're finished.  Here's some code to create a new file using the local file as the template.

# Set the options.
importManager = app.importManager
f3dFilename = 'C:\\Temp\\YourTemplate.f3d'
importOptions = importManager.createFusionArchiveImportOptions(f3dFilename)

# Import archive file to root component
importManager.importToNewDocument(importOptions)

  

---------------------------------------------------------------
Brian Ekins
Inventor and Fusion 360 API Expert
Website/Blog: https://EkinsSolutions.com
Message 7 of 8

info83PHN
Advocate
Advocate

AWESOME !!  Thanks Brian

 

I had come across that method yesterday, but discounted the idea as it was adding the locally saved f3d file as an additional Component in to the unsaved untitled Design.

I see your code has 'importToNewDocument' instead of 'importToTarget' which makes all the difference. Thank You.

0 Likes
Message 8 of 8

info83PHN
Advocate
Advocate

After importing as a new Document, I have to re-do this code to get the focus back to the new document.

        app = adsk.core.Application.get()
        ui = app.userInterface
        product = app.activeProduct
        design = adsk.fusion.Design.cast(product)
        rootComp = design.rootComponent
        features = rootComp.features
        userParams = design.userParameters



Otherwise the rest of my script failed as it could not find :

skt1: adsk.fusion.Sketch = rootComp.sketches.itemByName('Sketch_Text')

 

It was looking for the Sketch in the first Untitled unsaved Design that was left open when importing the local file to a new Design

0 Likes