-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #110 from kbwbe/devel
Devel
- Loading branch information
Showing
2 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
#*************************************************************************** | ||
#* * | ||
#* 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 | ||
import a2plib | ||
|
||
import zipfile | ||
try: | ||
import xml.etree.cElementTree as ET | ||
except ImportError: | ||
import xml.etree.ElementTree as ET | ||
|
||
import os | ||
|
||
#------------------------------------------------------------------------------ | ||
class A2p_xmldoc_Property(object): | ||
''' | ||
BaseClass for xml-Properties | ||
''' | ||
def __init__(self,treeElement, name,_type): | ||
self.treeElement = treeElement | ||
self.name = name | ||
self.type = _type | ||
print(self) | ||
|
||
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_PropertySheet(A2p_xmldoc_Property): | ||
|
||
def getCellValues(self): | ||
'''returns a dict: cellAddress:value ''' | ||
cellEntries = self.treeElement.findall('Cells/Cell') | ||
cellDict = {} | ||
for ce in cellEntries: | ||
cellDict[ce.attrib['address']] = ce.attrib['content'] | ||
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() | ||
print(self) | ||
|
||
def __str__(self): | ||
return "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'] == '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 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() | ||
# | ||
# 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) | ||
else: | ||
pass # unhandled object types | ||
|
||
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) | ||
cellDict = dr.getObjectByName('Spreadsheet').getCells() | ||
for k in cellDict.keys(): | ||
print(u"Address: {}, content {}".format( | ||
k, | ||
cellDict[k] | ||
) | ||
) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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