Last active
October 5, 2022 19:46
-
-
Save leoheck/0933ca63db54e2f7f22ada0e2a4f811b to your computer and use it in GitHub Desktop.
This code presents files linked inside a FreeCad (.FCStd)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Needs | |
# pip install anytree | |
# Leandro Sehnem Heck | |
# This gist presents files linked inside a FreeCad (.FCStd) | |
# Options | |
# - List of unique files / Tree with the hierarchy | |
# - Show relative and absolute paths | |
def list_files(tree=0, relative_path=1): | |
import os | |
from anytree import Node, RenderTree | |
def find_linked_files(relative_path=1): | |
def find_files(obj, root_dirpath, level=0, relative_path=1, parent_node=None, parent_filepath=None): | |
if obj == None: | |
return | |
filepath = obj.getLinkedObject().Document.FileName | |
if relative_path: | |
filepath = os.path.relpath(filepath, root_dirpath) | |
if obj.TypeId == "App::Link": | |
indexes_of_current_level = [idx for idx, s in enumerate(linked_files_level) if str(level) in str(s)] | |
linked_files_at_current_level = [linked_files[idx] for idx in indexes_of_current_level] | |
if not any(filepath in s for s in linked_files_at_current_level): | |
if filepath != parent_filepath: | |
linked_files.append(filepath) | |
linked_files_level.append(level) | |
node = Node(filepath, parent=parent_node) | |
find_files(obj.LinkedObject, root_dirpath, level+1, relative_path=relative_path, parent_node=node, parent_filepath=filepath) | |
# Navigate on objects inside a folders | |
if obj.TypeId == 'App::DocumentObjectGroup' or obj.TypeId == 'App::Part': | |
for objname in obj.getSubObjects(): | |
subobj = obj.Document.getObject(objname[0:-1]) | |
find_files(subobj, root_dirpath, level, relative_path=relative_path, parent_node=parent_node, parent_filepath=filepath) | |
linked_files = [] | |
linked_files_level = [] | |
level=0 | |
filename = FreeCAD.ActiveDocument.FileName | |
filepath = os.path.dirname(os.path.realpath(filename)) | |
root_dirpath = os.path.dirname(filepath) | |
if relative_path == 1: | |
filepath = os.path.relpath(filepath, root_dirpath) | |
linked_files.append(filepath) | |
linked_files_level.append(level) | |
file_tree = Node(filepath) | |
for obj in (FreeCAD.ActiveDocument.Objects): | |
find_files(obj, root_dirpath, level=level, relative_path=relative_path, parent_node=file_tree, parent_filepath=filepath) | |
return linked_files, linked_files_level, file_tree | |
linked_files, linked_files_level, file_tree = find_linked_files(relative_path=relative_path) | |
if not tree: # uniq | |
for filepath in set(linked_files): | |
print(filepath) | |
else: # show tree | |
for pre, fill, node in RenderTree(file_tree): | |
print("%s%s" % (pre, node.name)) | |
# Tree view | |
list_files(tree=1, relative_path=1) | |
# List view of unique files | |
list_files(tree=0, relative_path=0) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment