chiark / gitweb /
Add back the ultimaker platform, and made the platform mesh simpler.
[cura.git] / Cura / slice / cura_sf / fabmetheus_utilities / xml_simple_writer.py
1 """
2 XML tag writer utilities.
3
4 """
5
6
7 from __future__ import absolute_import
8
9 import sys
10
11 if sys.version_info[0] < 3:
12         import cStringIO
13 else:
14         import io as cStringIO
15
16
17 __author__ = 'Enrique Perez (perez_enrique@yahoo.com)'
18 __credits__ = 'Nophead <http://hydraraptor.blogspot.com/>\nArt of Illusion <http://www.artofillusion.org/>'
19 __date__ = '$Date: 2008/21/04 $'
20 __license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html'
21
22
23 def addBeginEndInnerXMLTag(attributes, depth, innerText, localName, output, text=''):
24         'Add the begin and end xml tag and the inner text if any.'
25         if len( innerText ) > 0:
26                 addBeginXMLTag(attributes, depth, localName, output, text)
27                 output.write( innerText )
28                 addEndXMLTag(depth, localName, output)
29         else:
30                 addClosedXMLTag(attributes, depth, localName, output, text)
31
32 def addBeginXMLTag(attributes, depth, localName, output, text=''):
33         'Add the begin xml tag.'
34         depthStart = '\t' * depth
35         output.write('%s<%s%s>%s\n' % (depthStart, localName, getAttributesString(attributes), text))
36
37 def addClosedXMLTag(attributes, depth, localName, output, text=''):
38         'Add the closed xml tag.'
39         depthStart = '\t' * depth
40         attributesString = getAttributesString(attributes)
41         if len(text) > 0:
42                 output.write('%s<%s%s >%s</%s>\n' % (depthStart, localName, attributesString, text, localName))
43         else:
44                 output.write('%s<%s%s />\n' % (depthStart, localName, attributesString))
45
46 def addEndXMLTag(depth, localName, output):
47         'Add the end xml tag.'
48         depthStart = '\t' * depth
49         output.write('%s</%s>\n' % (depthStart, localName))
50
51 def addXMLFromLoopComplexZ(attributes, depth, loop, output, z):
52         'Add xml from loop.'
53         addBeginXMLTag(attributes, depth, 'path', output)
54         for pointComplexIndex in xrange(len(loop)):
55                 pointComplex = loop[pointComplexIndex]
56                 addXMLFromXYZ(depth + 1, pointComplexIndex, output, pointComplex.real, pointComplex.imag, z)
57         addEndXMLTag(depth, 'path', output)
58
59 def addXMLFromObjects(depth, objects, output):
60         'Add xml from objects.'
61         for object in objects:
62                 object.addXML(depth, output)
63
64 def addXMLFromVertexes(depth, output, vertexes):
65         'Add xml from loop.'
66         for vertexIndex in xrange(len(vertexes)):
67                 vertex = vertexes[vertexIndex]
68                 addXMLFromXYZ(depth + 1, vertexIndex, output, vertex.x, vertex.y, vertex.z)
69
70 def addXMLFromXYZ(depth, index, output, x, y, z):
71         'Add xml from x, y & z.'
72         attributes = {'index' : str(index)}
73         if x != 0.0:
74                 attributes['x'] = str(x)
75         if y != 0.0:
76                 attributes['y'] = str(y)
77         if z != 0.0:
78                 attributes['z'] = str(z)
79         addClosedXMLTag(attributes, depth, 'vertex', output)
80
81 def compareAttributeKeyAscending(key, otherKey):
82         'Get comparison in order to sort attribute keys in ascending order, with the id key first and name second.'
83         if key == 'id':
84                 return - 1
85         if otherKey == 'id':
86                 return 1
87         if key == 'name':
88                 return - 1
89         if otherKey == 'name':
90                 return 1
91         if key < otherKey:
92                 return - 1
93         return int(key > otherKey)
94
95 def getAttributesString(attributes):
96         'Add the closed xml tag.'
97         attributesString = ''
98         attributesKeys = attributes.keys()
99         attributesKeys.sort(compareAttributeKeyAscending)
100         for attributesKey in attributesKeys:
101                 valueString = str(attributes[attributesKey])
102                 if "'" in valueString:
103                         attributesString += ' %s="%s"' % (attributesKey, valueString)
104                 else:
105                         attributesString += " %s='%s'" % (attributesKey, valueString)
106         return attributesString
107
108 def getBeginGeometryXMLOutput(elementNode=None):
109         'Get the beginning of the string representation of this boolean geometry object info.'
110         output = getBeginXMLOutput()
111         attributes = {}
112         if elementNode != None:
113                 documentElement = elementNode.getDocumentElement()
114                 attributes = documentElement.attributes
115         addBeginXMLTag(attributes, 0, 'fabmetheus', output)
116         return output
117
118 def getBeginXMLOutput():
119         'Get the beginning of the string representation of this object info.'
120         output = cStringIO.StringIO()
121         output.write("<?xml version='1.0' ?>\n")
122         return output
123
124 def getDictionaryWithoutList(dictionary, withoutList):
125         'Get the dictionary without the keys in the list.'
126         dictionaryWithoutList = {}
127         for key in dictionary:
128                 if key not in withoutList:
129                         dictionaryWithoutList[key] = dictionary[key]
130         return dictionaryWithoutList
131
132 def getEndGeometryXMLString(output):
133         'Get the string representation of this object info.'
134         addEndXMLTag(0, 'fabmetheus', output)
135         return output.getvalue()