What's new

OpenFileDialog - Open existing part script

otrotabi

Member
I want to share with you a little script that uses the OpenFileDialog Function of the Windows class.

# open existing part script

import os
Win = Windows()

# show open dialog window, if user clicks on OK then a path and filename will be returned
FileName = Win.OpenFileDialog('Select a part file', 'Part Files|*.AD_PRT', '.AD_PRT')

# if user clicks on Cancel then an empty string will be returned
# otherwise FileName will have the complete file name including the path.
# if you use prt = Part(FileName), what happens is that a new part is created named "Path/FileName"

#in the next two lines you split FileName into two new variables

dir = os.path.dirname(FileName)
file = os.path.splitext(os.path.basename(FileName))[0]

# and then you open the part
prt = Part(dir,file)

Instead, you could in some cases hardcode the file name

Script 1)

dir = "C:\Users\Brian\Downloads"
file = "Triple gear.AD_PRT"
# if a filename was returned then here is where we would open the file and read the contents
prt = Part(dir,file)

Script 2)

import os
FileName = r"C:\Users\Brian\Downloads\Triple gear.AD_PRT"
dir = os.path.dirname(FileName)
file = os.path.splitext(os.path.basename(FileName))[0]
# if a filename was returned then here is where we would open the file and read the contents
prt = Part(dir,file)
 
Top