-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImportGlobalVars.py
147 lines (98 loc) · 4.25 KB
/
ImportGlobalVars.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from os import path
import FreeCAD
class FCProject():
"""Contains the values defined in the FCProject file"""
def __init__(self, filePath: str, values: dict):
"""
filepath : the path of the FCProject file
values : the parsed values (key-val dict)
"""
self.filePath = filePath
self.globalVarsFilePath = values.get('GLOBAL_VARS_FILE_PATH')
self.globalVarsSpreadsheetName = values.get('GLOBAL_VARS_SPREADSHEET_NAME')
def getFilePath(self) -> str:
return self.filePath
def getGlobalVarsFilePath(self) -> str:
return self.globalVarsFilePath
def getGlobalVarsSpreadsheetName(self) -> str:
return self.globalVarsSpreadsheetName
def readFcProjectFile(filepath: str) -> FCProject:
defaultValues: dict = {
'GLOBAL_VARS_FILE_PATH': path.join(path.dirname(filepath), 'GlobalVars.FCStd'),
'GLOBAL_VARS_SPREADSHEET_NAME': 'GlobalVars',
}
parsedValues: dict = {}
file = open(filepath, 'r')
for line in file:
split = line.strip().split('=')
if len(split) < 2:
continue
key = split[0]
if key not in defaultValues:
continue
val = split[1]
parsedValues[key] = val
file.close()
return FCProject(filepath, defaultValues | parsedValues)
def fetchFcProject(childPath: str) -> FCProject:
"""Recursively looks up for a FCProject file in the current path and ancestors.
When found, returns a FCProject object containing the file data.
"""
(parentPath, childElement) = path.split(childPath)
if ('' == childElement):
raise FileNotFoundError('FCProject file not found')
fcprojectFilePath = path.join(parentPath, 'FCProject')
if not path.isfile(fcprojectFilePath):
return fetchFcProject(parentPath)
return readFcProjectFile(fcprojectFilePath)
def documentHasALinkToGlobalVars(document: FreeCAD.Document, globalVarsFileAbsolutePath: str, globalVarsSpreadsheetName: str) -> bool:
for obj in document.Objects:
if 'App::Link' != obj.TypeId:
continue
if 'Spreadsheet::Sheet' != obj.LinkedObject.TypeId:
continue
if globalVarsFileAbsolutePath != obj.LinkedObject.Document.FileName:
continue
if globalVarsSpreadsheetName != obj.LinkedObject.Label:
continue
return True
return False
def makeGlobalVarsLink(document: FreeCAD.Document, globalVarsFileAbsolutePath: str, globalVarsSpreadsheetName: str) -> None:
globalVarsDocument = FreeCAD.openDocument(globalVarsFileAbsolutePath)
FreeCAD.setActiveDocument(document.Name)
spreadsheetObject: FreeCAD.DocumentObject = None
for obj in globalVarsDocument.Objects:
if 'Spreadsheet::Sheet' != obj.TypeId:
continue
if globalVarsSpreadsheetName != obj.Label:
continue
spreadsheetObject = obj
break
if not spreadsheetObject:
raise NameError('GlobalVars document "' + globalVarsFileAbsolutePath + '" doesn\'t have a "' + globalVarsSpreadsheetName + '" spreadsheet.')
link = document.addObject('App::Link', globalVarsSpreadsheetName)
link.LinkedObject = spreadsheetObject
def linkGlobalVarsToDocument(document: FreeCAD.Document) -> None:
documentPath: str = document.FileName
fcproject: FCProject = None
try:
fcproject = fetchFcProject(documentPath)
except FileNotFoundError as e:
FreeCAD.Console.PrintLog(e)
return
globalVarsFileAbsolutePath = path.normpath(path.join(path.dirname(fcproject.getFilePath()), fcproject.getGlobalVarsFilePath()))
if not path.isfile(globalVarsFileAbsolutePath):
FreeCAD.Console.PrintWarning('GlobalVars file "' + globalVarsFileAbsolutePath + '" does not exist.')
return
if documentPath == globalVarsFileAbsolutePath:
# do not link GlobalVars to itself
return
if documentHasALinkToGlobalVars(document, globalVarsFileAbsolutePath, fcproject.getGlobalVarsSpreadsheetName()):
# already linked
return
try:
makeGlobalVarsLink(document, globalVarsFileAbsolutePath, fcproject.getGlobalVarsSpreadsheetName())
except NameError as e:
FreeCAD.Console.PrintError(e)
return
document.recompute()