-
Notifications
You must be signed in to change notification settings - Fork 69
/
a2p_fcdocumentreader.py
296 lines (255 loc) · 10.6 KB
/
a2p_fcdocumentreader.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#***************************************************************************
#* *
#* Copyright (c) 2018 kbwbe *
#* *
#* This program is free software; you can redistribute it and/or modify *
#* it under the terms of the GNU Lesser General Public License (LGPL) *
#* as published by the Free Software Foundation; either version 2 of *
#* the License, or (at your option) any later version. *
#* for detail see the LICENCE text file. *
#* *
#* This program is distributed in the hope that it will be useful, *
#* but WITHOUT ANY WARRANTY; without even the implied warranty of *
#* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
#* GNU Library General Public License for more details. *
#* *
#* You should have received a copy of the GNU Library General Public *
#* License along with this program; if not, write to the Free Software *
#* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 *
#* USA *
#* *
#***************************************************************************
import FreeCAD, FreeCADGui, os
from a2p_translateUtils import *
import zipfile
try:
import xml.etree.cElementTree as ET
except ImportError:
import xml.etree.ElementTree as ET
#------------------------------------------------------------------------------
class A2p_xmldoc_Property(object):
"""
BaseClass for xml-Properties
"""
def __init__(self,treeElement, name,_type):
self.treeElement = treeElement
self.name = name
self.type = _type
def __str__(self):
return "PropertyName: {}, Type: {}".format(self.name,self.type)
#------------------------------------------------------------------------------
class A2p_xmldoc_PropertyString(A2p_xmldoc_Property):
def getStringValue(self):
s = self.treeElement.find('String')
return s.attrib['value']
#------------------------------------------------------------------------------
class A2p_xmldoc_PropertyBool(A2p_xmldoc_Property):
def getBool(self):
s = self.treeElement.find('Bool')
if s.attrib['value'] == 'true':
return True
else:
return False
#------------------------------------------------------------------------------
class A2p_xmldoc_PropertyFloat(A2p_xmldoc_Property):
def getFloat(self):
s = self.treeElement.find('Float')
return float( s.attrib['value'] )
#------------------------------------------------------------------------------
class A2p_xmldoc_PropertyFile(A2p_xmldoc_Property):
def getStringValue(self):
s = self.treeElement.find('String')
return s.attrib['value']
#------------------------------------------------------------------------------
class A2p_xmldoc_PropertySheet(A2p_xmldoc_Property):
def getCellValues(self):
"""returns a dict: cellAddress:value"""
cellEntries = self.treeElement.findall('Cells/Cell')
cellDict = {}
for ce in cellEntries:
try:
cellDict[ce.attrib['address']] = ce.attrib['content']
except:
pass # no content attribute, perhaps backgroundcolor or somethin else...
return cellDict
#------------------------------------------------------------------------------
class A2p_xmldoc_Object(object):
"""
class prototype to store FC objects found in document.xml
"""
def __init__(self,name,_type, tree):
self.tree = tree
self.dataElement = None
self.name = name
self.type = _type
self.propertyDict = {}
self.loadPropertyDict(self.tree)
self.label = self.propertyDict['Label'].getStringValue()
def __str__(self):
return u"ObjName: {}, Label: {}, Type: {}".format(
self.name,
self.label,
self.type
)
def loadPropertyDict(self,tree):
for elem in tree.iterfind('ObjectData/Object'):
if elem.attrib['name'] == self.name:
self.dataElement = elem
for e in elem.findall('Properties/Property'):
if e.attrib['type'] == 'App::PropertyString':
p = A2p_xmldoc_PropertyString(
e,
e.attrib['name'],
e.attrib['type']
)
self.propertyDict[e.attrib['name']] = p
elif e.attrib['type'] == 'App::PropertyBool':
p = A2p_xmldoc_PropertyBool(
e,
e.attrib['name'],
e.attrib['type']
)
self.propertyDict[e.attrib['name']] = p
elif e.attrib['type'] == 'App::PropertyFloat':
p = A2p_xmldoc_PropertyFloat(
e,
e.attrib['name'],
e.attrib['type']
)
self.propertyDict[e.attrib['name']] = p
elif e.attrib['type'] == 'App::PropertyFile':
p = A2p_xmldoc_PropertyFile(
e,
e.attrib['name'],
e.attrib['type']
)
self.propertyDict[e.attrib['name']] = p
elif e.attrib['type'] == 'Spreadsheet::PropertySheet':
p = A2p_xmldoc_PropertySheet(
e,
e.attrib['name'],
e.attrib['type']
)
self.propertyDict[e.attrib['name']] = p
else:
pass # unsupported property type
#------------------------------------------------------------------------------
class A2p_xmldoc_SpreadSheet(A2p_xmldoc_Object):
def getCells(self):
return self.propertyDict['cells'].getCellValues()
#------------------------------------------------------------------------------
class A2p_xmldoc_FeaturePython(A2p_xmldoc_Object):
def isA2pObject(self):
if self.propertyDict.get('a2p_Version',None) is not None: return True
return False
def getA2pSource(self):
if self.isA2pObject:
return self.propertyDict['sourceFile'].getStringValue()
return None
def isSubassembly(self):
if self.isA2pObject:
propFound = self.propertyDict.get('subassemblyImport',None)
if propFound:
return propFound.getBool()
else:
return False
return False
def getTimeLastImport(self):
if self.isA2pObject:
propFound = self.propertyDict.get('timeLastImport',None)
if propFound:
return propFound.getFloat()
else:
return 0
return 0
#------------------------------------------------------------------------------
class FCdocumentReader(object):
"""
class for extracting the XML-Documentdata from a fcstd-file given by
filepath. Some data can be extracted without opening the whole document
within FreeCAD
"""
def __init__(self):
self.tree = None
self.root = None
self.objects = []
def clear(self):
self.realPath = ''
self.tree = None
self.root = None
self.objects = []
def openDocument(self,fileName):
self.clear()
# check whether file exists or not...
if not os.path.exists( fileName ):
print (u"fcDocumentReader: file {} does not exist!".format(fileName))
return
#
# decompress the file
f = zipfile.ZipFile(fileName,'r')
xml = f.read('Document.xml')
f.close()
#
# load the ElementTree
self.tree = ET.ElementTree(ET.fromstring(xml))
#
self.loadObjects()
def loadObjects(self):
self.objects = []
for elem in self.tree.iterfind('Objects/Object'):
if elem.attrib['type'].startswith('Spreadsheet'):
ob = A2p_xmldoc_SpreadSheet(
elem.attrib['name'],
elem.attrib['type'],
self.tree
)
self.objects.append(ob)
if elem.attrib['type'].startswith('Part::FeaturePython'):
ob = A2p_xmldoc_FeaturePython(
elem.attrib['name'],
elem.attrib['type'],
self.tree
)
self.objects.append(ob)
else:
pass # unhandled object types
def getA2pObjects(self):
out = []
for ob in self.objects:
if ob.propertyDict.get('a2p_Version',None) is not None:
out.append(ob)
continue
elif ob.propertyDict.get('assembly2Version',None) is not None: # for very old a2p projects...
out.append(ob)
continue
return out
def getSpreadsheetObjects(self):
out = []
for ob in self.objects:
if ob.type.startswith('Spreadsheet'):
out.append(ob)
return out
def getObjectByName(self,name):
for ob in self.objects:
if ob.name == name:
return ob
return None
#------------------------------------------------------------------------------
if __name__ == "__main__":
doc = FreeCAD.activeDocument()
dr = FCdocumentReader()
dr.openDocument(doc.FileName)
for ob in dr.getSpreadsheetObjects():
if ob.name == '_PARTINFO_':
cellDict = ob.getCells()
for k in cellDict.keys():
print(u"Address: {}, content {}".format(
k,
cellDict[k]
)
)
for ob in dr.getA2pObjects():
print(u"sourcefile: {}".format(
ob.getA2pSource()
)
)