chiark / gitweb /
Add uppercase STL and HEX to file dialog filters for linux/MacOS
[cura.git] / Cura / fabmetheus_utilities / geometry / geometry_tools / dictionary.py
1 """
2 Boolean geometry dictionary object.
3
4 """
5
6
7 from __future__ import absolute_import
8 #Init has to be imported first because it has code to workaround the python bug where relative imports don't work if the module is imported as a main module.
9 import __init__
10
11 from fabmetheus_utilities.geometry.geometry_utilities import evaluate
12 from fabmetheus_utilities.geometry.geometry_utilities import matrix
13 from fabmetheus_utilities import euclidean
14 from fabmetheus_utilities import xml_simple_writer
15 import cStringIO
16
17
18 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
19 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
20 __date__ = '$Date: 2008/21/04 $'
21 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
22
23
24 def getAllPaths(paths, xmlObject):
25         'Get all paths.'
26         for archivableObject in xmlObject.archivableObjects:
27                 paths += archivableObject.getPaths()
28         return paths
29
30 def getAllTransformedPaths(transformedPaths, xmlObject):
31         'Get all transformed paths.'
32         for archivableObject in xmlObject.archivableObjects:
33                 transformedPaths += archivableObject.getTransformedPaths()
34         return transformedPaths
35
36 def getAllTransformedVertexes(transformedVertexes, xmlObject):
37         'Get all transformed vertexes.'
38         for archivableObject in xmlObject.archivableObjects:
39                 transformedVertexes += archivableObject.getTransformedVertexes()
40         return transformedVertexes
41
42 def getAllVertexes(vertexes, xmlObject):
43         'Get all vertexes.'
44         for archivableObject in xmlObject.archivableObjects:
45                 vertexes += archivableObject.getVertexes()
46         return vertexes
47
48 def processElementNode(elementNode):
49         'Process the xml element.'
50         evaluate.processArchivable( Dictionary, elementNode)
51
52
53 class Dictionary:
54         'A dictionary object.'
55         def __init__(self):
56                 'Add empty lists.'
57                 self.archivableObjects = []
58                 self.elementNode = None
59
60         def __repr__(self):
61                 'Get the string representation of this object info.'
62                 output = xml_simple_writer.getBeginGeometryXMLOutput(self.elementNode)
63                 self.addXML( 1, output )
64                 return xml_simple_writer.getEndGeometryXMLString(output)
65
66         def addXML(self, depth, output):
67                 'Add xml for this object.'
68                 attributeCopy = {}
69                 if self.elementNode != None:
70                         attributeCopy = evaluate.getEvaluatedDictionaryByCopyKeys(['paths', 'target', 'vertexes'], self.elementNode)
71                 euclidean.removeElementsFromDictionary(attributeCopy, matrix.getKeysM())
72                 euclidean.removeTrueFromDictionary(attributeCopy, 'visible')
73                 innerOutput = cStringIO.StringIO()
74                 self.addXMLInnerSection(depth + 1, innerOutput)
75                 self.addXMLArchivableObjects(depth + 1, innerOutput)
76                 xml_simple_writer.addBeginEndInnerXMLTag(attributeCopy, depth, innerOutput.getvalue(), self.getXMLLocalName(), output)
77
78         def addXMLArchivableObjects(self, depth, output):
79                 'Add xml for this object.'
80                 xml_simple_writer.addXMLFromObjects( depth, self.archivableObjects, output )
81
82         def addXMLInnerSection(self, depth, output):
83                 'Add xml section for this object.'
84                 pass
85
86         def createShape(self):
87                 'Create the shape.'
88                 pass
89
90         def getAttributes(self):
91                 'Get attribute table.'
92                 if self.elementNode == None:
93                         return {}
94                 return self.elementNode.attributes
95
96         def getComplexTransformedPathLists(self):
97                 'Get complex transformed path lists.'
98                 complexTransformedPathLists = []
99                 for archivableObject in self.archivableObjects:
100                         complexTransformedPathLists.append(euclidean.getComplexPaths(archivableObject.getTransformedPaths()))
101                 return complexTransformedPathLists
102
103         def getFabricationExtension(self):
104                 'Get fabrication extension.'
105                 return 'xml'
106
107         def getFabricationText(self, addLayerTemplate):
108                 'Get fabrication text.'
109                 return self.__repr__()
110
111         def getGeometryOutput(self):
112                 'Get geometry output dictionary.'
113                 shapeOutput = []
114                 for visibleObject in evaluate.getVisibleObjects(self.archivableObjects):
115                         geometryOutput = visibleObject.getGeometryOutput()
116                         if geometryOutput != None:
117                                 visibleObject.transformGeometryOutput(geometryOutput)
118                                 shapeOutput.append(geometryOutput)
119                 if len(shapeOutput) < 1:
120                         return None
121                 return {self.getXMLLocalName() : {'shapes' : shapeOutput}}
122
123         def getMatrix4X4(self):
124                 "Get the matrix4X4."
125                 return None
126
127         def getMatrixChainTetragrid(self):
128                 'Get the matrix chain tetragrid.'
129                 return self.elementNode.parentNode.xmlObject.getMatrixChainTetragrid()
130
131         def getMinimumZ(self):
132                 'Get the minimum z.'
133                 return None
134
135         def getPaths(self):
136                 'Get all paths.'
137                 return getAllPaths([], self)
138
139         def getTransformedPaths(self):
140                 'Get all transformed paths.'
141                 return getAllTransformedPaths([], self)
142
143         def getTransformedVertexes(self):
144                 'Get all transformed vertexes.'
145                 return getAllTransformedVertexes([], self)
146
147         def getTriangleMeshes(self):
148                 'Get all triangleMeshes.'
149                 triangleMeshes = []
150                 for archivableObject in self.archivableObjects:
151                         triangleMeshes += archivableObject.getTriangleMeshes()
152                 return triangleMeshes
153
154         def getType(self):
155                 'Get type.'
156                 return self.__class__.__name__
157
158         def getVertexes(self):
159                 'Get all vertexes.'
160                 return getAllVertexes([], self)
161
162         def getVisible(self):
163                 'Get visible.'
164                 return False
165
166         def getXMLLocalName(self):
167                 'Get xml local name.'
168                 return self.__class__.__name__.lower()
169
170         def setToElementNode(self, elementNode):
171                 'Set the shape of this carvable object info.'
172                 self.elementNode = elementNode
173                 elementNode.parentNode.xmlObject.archivableObjects.append(self)
174
175         def transformGeometryOutput(self, geometryOutput):
176                 'Transform the geometry output by the local matrix4x4.'
177                 if self.getMatrix4X4() != None:
178                         matrix.transformVector3sByMatrix(self.getMatrix4X4().tetragrid, matrix.getVertexes(geometryOutput))