Open a Fusion 360 "Detail Drawing" using a Python script

Open a Fusion 360 "Detail Drawing" using a Python script

isocam
Collaborator Collaborator
1,324 Views
2 Replies
Message 1 of 3

Open a Fusion 360 "Detail Drawing" using a Python script

isocam
Collaborator
Collaborator

Can anybody help?

 

Has anybody got a Python script that will allow me (for testing purposes) to open a Fusion 360 "Detail Drawing"?

 

Please see the attached picture.

 

I want to specify the folder name, in this case "2000 - Sand Silo" & the drawing number "1000-001 Drawing" in the Python script so, when I run the script, the drawing will automatically open.

 

Many thanks in advance!!!

 

Darren

 

0 Likes
1,325 Views
2 Replies
Replies (2)
Message 2 of 3

tykapl.breuil
Advocate
Advocate

Hey there !

 

This is indeed possible through the API.

What you need to do is:

- Get the data project of the file you want to open

- Navigate through the folder to that file

- use app.document.open(dataFile)

 

Using your screenshot I tried to make a snippet but no guarantee it will work as I cannot test it.

 

app = adsk.core.Application.get()
# You should probably get the project this way by getting the id of your project
# projects = app.dataProjects
# project = projects.itemById('YourProjectID')
# If the project you're using is the active one this should work
project = app.data.activeProject
rootFolder = project.rootFolder
# You should probably use the id and not the name as I think that will allow you
# to rename the project without breaking the script.
sandSiloFolder = rootFolder.dataFolders.itemByName('2000 - Sand Silo')
# You should use the id here too, I saw that it was in third position in the list
# in your screenshot so i'm trying that out but no promises.
drawingFile = sandSiloFolder.dataFiles.item(2)
app.documents.open(drawingFile)

 

Message 3 of 3

kandennti
Mentor
Mentor

Hi @isocam .

 

We have created a sample that opens a drawing file(f2d) that is related to the active document (f3d).

# Fusion360API Python script
import traceback
import adsk.fusion
import adsk.core


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

        # activr document
        doc: adsk.fusion.FusionDocument = app.activeDocument
        
        # get Parent Reference Datafiles
        refDataLst = [d for d in doc.dataFile.parentReferences.asArray()]

        # get F2D Datafiles
        f2dFileList = []
        datafile: adsk.core.DataFile
        for datafile in refDataLst:
            if datafile.fileExtension == 'f2d':
                f2dFileList.append(datafile)

        # open document
        for datafile in f2dFileList:
            app.documents.open(datafile)

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