forked from hamish2014/FreeCAD_drawing_dimensioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLlib.py
176 lines (156 loc) · 6.83 KB
/
XMLlib.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
# -*- coding: utf-8 -*-
'''
Library to parse XML codes such as those used for the FreeCAD view result, as to get graphics elements out.
'''
# Future Work,
# - replace custom XML parser lib with an established svg library ...
#
import numpy
from numpy import sin, cos, dot
def findOffset( text, subtext, p_start ):
p = text.find(subtext, p_start)
return p + len(subtext) if p > -1 else -1
def splitMultiSep( text, seperators ):
parts = []
i = 0
j = 0
while i < len(text):
for s in seperators:
if text[i:i+len(s)] == s:
parts.append( text[j:i] )
i = i + len(s)
j = i
continue
i = i + 1
if j < len(text):
parts.append( text[j:] )
return parts
def extractParms( text, p_start, startText, sep, endText ):
p1 = findOffset( text, startText, p_start)
assert p1 > -1
p2 = text.find( endText , p1 )
assert p2 > p1
return [ part for part in splitMultiSep(text[p1:p2], sep) if len(part) > 0 ]
def findClose_sub(text, subText, pStart):
p = text.find(subText, pStart)
return p if p <> -1 else len(text)-1 #if p == -1 then broken XML
def findClose(text, closingOptions, pStart):
return min( [ findClose_sub(text, subText, pStart) for subText in closingOptions ] )
class SvgXMLTreeNode:
def __init__(self, XML, pStart, parent=None):
self.XML = XML
self.pStart = pStart
self.parent = parent
self.children = []
assert XML[pStart] == '<'
pNextElement = XML.find('<', pStart + 1)
pClose = findClose( XML, ['/','-->'], pStart)
while pNextElement > -1 and pNextElement +1 < pClose: #+1 to handle '</svg>' styled close tags
child = SvgXMLTreeNode( XML, pNextElement, self )
self.children.append(child)
pNextElement = XML.find('<', child.pEnd)
pClose = findClose(XML, ['/','-->'], child.pEnd)
self.pClose = pClose
self.pEnd = findClose(XML, '>', pClose)+1
if len(self.children) == 0:
if XML[ pClose-1 ] == '<':
self.header = XML[ pStart:pClose-1 ]
self.footer = XML[ pClose-1:self.pEnd ]
else:
self.header = XML[ pStart:pClose ]
self.footer = XML[ pClose:self.pEnd ]
else:
self.header = XML[ pStart: self.children[0].pStart ]
self.footer = XML[ self.children[-1].pEnd : self.pEnd ]
self.header = self.header.replace('\n','')
self.footer = self.footer.replace('\n','')
self.tag = self.header.split()[0][1:]
if self.tag.startswith('!--'): #comment special case
self.tag = '!--'
self.parms = {}
h = self.header
p = h.find('=')
while p > -1:
i = p-1
key = ''
while key == '' or h[i] <> ' ':
if h[i] <> ' ':
key = h[i] + key
i = i - 1
p1 = h.find('"', p)
p2 = h.find('"', p1+1)
self.parms[key] = h[p1+1:p2]
p = self.header.find('=', p+1)
#print(self.parms)
def rootNode(self):
if self.parent==None:
return self
else:
return self.parent.rootNode()
def prettify(self, indent='', indentIncrease=' '):
if len(self.children) == 0:
return indent + self.header + self.footer
else:
return indent + self.header + '\n' + '\n'.join([c.prettify( indent+indentIncrease ) for c in self.children]) + '\n' + indent + self.footer
def __repr__(self):
return self.prettify()
def applyTransforms(self, x, y):
R = numpy.eye(2)
r_o = numpy.zeros(2)
tx, ty = 0, 0
sx, sy = 1.0, 1.0
if 'transform=' in self.header:
if 'rotate(' in self.header:
rotateDegrees, rx, ry = map(float, extractParms(self.header, 0, 'rotate(', ', ', ')'))
rads = numpy.pi * rotateDegrees / 180
R = numpy.array([ [ cos(rads), -sin(rads)], [ sin(rads), cos(rads)] ])
r_o = numpy.array([ rx, ry])
if 'translate(' in self.header:
tx, ty = map(float, extractParms(self.header, 0, 'translate(', ', ', ')'))
if 'scale(' in self.header:
sx, sy = map(float, extractParms(self.header, 0, 'scale(', ', ', ')'))
p = numpy.array( [sx*x + tx, sy*y + ty] )
point = dot(R, p-r_o) +r_o
if self.parent <> None:
return self.parent.applyTransforms(*point)
else:
return point[0], point[1]
def scaling(self):
sx = 1.0
if 'scale(' in self.header:
sx = map(float, extractParms(self.header, 0, 'scale(', ',', ')'))[0]
if len(self.children) == 1:
sx_child = self.children[0].scaling()
else:
sx_child = 1.0
return sx * sx_child
def getAllElements(self):
return [self] + sum([c.getAllElements() for c in self.children],[])
if __name__ == "__main__":
print('Testing XMLlib')
XML = '''<svg id="Ortho_0_1" width="640" height="480"
transform="rotate(90,122.43,123.757) translate(122.43,123.757) scale(1.5,1.5)"
>
<g stroke="rgb(0, 0, 0)"
stroke-width="0.233333"
stroke-linecap="butt"
stroke-linejoin="miter"
fill="none"
transform="scale(1,-1)"
>
<path id= "1" d=" M 65.9612 -59.6792 L -56.2041 -59.6792 " />
<path d="M-56.2041 -59.6792 A4.2 4.2 0 0 0 -60.4041 -55.4792" /><path id= "3" d=" M 65.9612 49.7729 L 65.9612 -59.6792 " />
<path id= "4" d=" M -60.4041 -55.4792 L -60.4041 49.7729 " />
<path id= "5" d=" M -60.4041 49.7729 L 65.9612 49.7729 " />
<circle cx ="22.2287" cy ="-15.2218" r ="13.8651" /><!--Comment-->
<path id= "7" d="M18,0 L17.9499,-4.32955e-16 L17.8019,-4.00111e-16 L17.5637,-3.47203e-16 L17.247,-2.76885e-16 L16.8678,-1.92683e-16 L16.445,-9.88191e-17 L16,-5.43852e-32 L15.555,9.88191e-17 L15.1322,1.92683e-16 L14.753,2.76885e-16 L14.4363,3.47203e-16 L14.1981,4.00111e-16 L14.0501,4.32955e-16 L14,4.44089e-16 " />
<path d="M12.7,-53.35 C13.0276,-53.3497 13.3353,-53.4484 13.5837,-53.6066 C13.8332,-53.7643 14.0231,-53.9807 14.1457,-54.2047 C14.4256,-54.721 14.41,-55.3038 14.1502,-55.787 C14.0319,-56.0053 13.8546,-56.213 13.6163,-56.3722 C13.3789,-56.5307 13.0795,-56.6413 12.7378,-56.6496 C12.3961,-56.6571 12.0892,-56.5598 11.8429,-56.4099 C11.5956,-56.2597 11.4083,-56.0565 11.282,-55.8436 C11.0014,-55.3672 10.9667,-54.7868 11.2231,-54.2642 C11.3401,-54.0279 11.5293,-53.7969 11.7844,-53.6273 C12.0382,-53.4574 12.3575,-53.3497 12.7,-53.35 " />
</g>
<text x="50" y="-60" fill="blue" style="font-size:8" transform="rotate(0.000000 50,-60)">256.426</text>
</svg>'''
print('parsing')
print(XML)
print('')
print('result:')
svg = SvgXMLTreeNode(XML,0)
print(svg)