Open A Part Using A Python Script

Open A Part Using A Python Script

isocam
Collaborator Collaborator
329 Views
1 Reply
Message 1 of 2

Open A Part Using A Python Script

isocam
Collaborator
Collaborator

Can anybody help?

 

I have a folder called "TEST FILES" that I have created inside the "Project Files" folder.

 

Inside this "TEST FILES" folder I have a part called "TEST PART".

 

I can call the routine using the following two variables.

 

FolderName = "TEST FILES"

 

PartNumber = "TEST PART"

 

I can open the "TEST PART" successfully using the following Python code....

 

def OpenPart(FolderName, PartNumber):
app = adsk.core.Application.get()

proj: core.DataProject = None

for x in app.data.dataProjects.asArray():
if x.name == "Project Files":
proj = x
break

folder: core.DataFolder = None

for x in proj.rootFolder.dataFolders.asArray():
if x.name == FolderName:
folder = x
break

file: core.DataFile = None

for x in folder.dataFiles.asArray():
if x.name == PartNumber:
file = x
break

app.documents.open(file, True)

 

How can I open a part if the "TEST PART" is located in a folder called:

 

Project Files  >  Purchased Parts  >   Gearboxes   > "TEST PART"?

 

(In the path I have an extra Sub-Folder called "Gearboxes")

 

Many thanks in advance!

 

Darren

0 Likes
330 Views
1 Reply
Reply (1)
Message 2 of 2

RohitMane_
Autodesk
Autodesk

Hello @isocam,

You can loop through the Datafolder.dataFolders object to find the required folder, or you can call the function recursively by providing the full path.

As a suggestion, please note that sometimes two data files can have the same name. Therefore, using the Datafile ID will be more helpful; otherwise, you will need to check other parameters to ensure you get the correct data file.

for y in folder.dataFolders.asArray():
    if y.name == "Gearboxes":
       folder = y
       break

 

0 Likes