
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm writing an add-in that is supposed to open a bunch of designs specified by a 'file path' that is relative to the current project. Here is the best way I've figured out how to do that so far. It takes a long time for complex paths as it has to check and compare every single dataFile name. There must be a better way to do this... this solution also doesn't handle when two documents have the same name very elegantly. (It just chooses whichever matches first). Is there any way to use some sort of unique document id to open the doc quickly? Here's the code I've been working with, and if a find function doesn't exist, perhaps someone has recommendations for how I can improve the speed of the code below:
def open_fdoc(file_path: str, root_folder = None): """ Takes a file path string and opens the Fusion API dataFile given that file path using the defined root_folder. If no root_folder is specified AND it is a relative path, defaults to the currently active product's parent folder as the root folder. If the root_folder is specified AND it is an absolute path, the root folder is set to the active project's root folder. If the root folder IS set, the file path is treated as a relative path to the set root folder. The root_folder param is a dataFolder Note that this will work much faster the closer the desired file is to the root_folder. Therefore you should try as much as possible to give a short folder path and a specific root folder. The document object is then returned """ fp_list = file_path.split("/") # User specified root_folder if root_folder: root_folder = adsk.fusion.dataFolder.cast(root_folder) # root_folder assumed based on absolute path rules elif fp_list[0] == '': root_folder = adsk.core.Application.get().data.activeProject.rootFolder fp_list = fp_list[1:] # root_folder assumed based on relative path rules else: root_folder = adsk.core.Application.get().activeDocument.dataFile.parentFolder # Extract the final doc name and version d_desired = fp_list[len(fp_list)-1].split(":") d_name = d_desired[0] d_version = None if len(d_desired) == 2: d_version = d_desired[1] elif len(d_desired) > 2: raise ValueError("Version numbering is ambiguous, {} is not a proper " "version name".format(''.join(d_desired[1:]))) # dig into the final folder before hitting the document if len(fp_list) > 1: for f_name in fp_list[0:len(fp_list)-1]: root_folder = find_dataFolder(root_folder, f_name) # search through the datafiles to find the right one. d = None d = find_dataFile(root_folder, d_name) # if no colens, open the datafile directly to the latest version. # Otherwise, open to the version specified if d_version: d = find_version(d,d_version) adsk.core.Application.get().documents.open(d) def find_version(dataFile, version_number): """ Opens the specified version_number of the passed in dataFile """ for i in range(dataFile.versions.count): d_potential = dataFile.versions.item(i) if d_potential: if d_potential.versionNumber == version_number: return d_potential def find_dataFile(dataFolder, dataFile_name): """ Finds the dataFile in the dataFolder. Will not search within subfolders. """ for i in range(dataFolder.dataFiles.count): df = dataFolder.dataFiles.item(i) if df.name == dataFile_name: return df else: raise ValueError("The dataFile {} was not found in the folder {}".format(dataFile_name, dataFolder.name)) def find_dataFolder(parent_dataFolder, dataFolder_name): """ Finds a dataFolder within a dataFolder. Will not search within subfolders. """ df = parent_dataFolder.dataFolders.itemByName(dataFolder_name) if df: return df else: raise ValueError("The dataFolder {} was not found in the folder {}".format(dataFolder_name, parent_dataFolder.name))
Solved! Go to Solution.