-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
121 lines (83 loc) · 2.52 KB
/
main.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
import os
import xml.etree.ElementTree as ET
import numpy as np
DATA_PATH = "simple.svg"
# Need to calculate that
MM_PER_STEP = 1
MM_PEN_WIDTH = 0.7
class Path():
def __init__(self, target, usePen, style):
self.target = target
self.usePen = usePen
self.style = style
def set_curve(self, start, curve, usePen, style):
pass
def __repr__(self):
drawn = " Drawn" if self.usePen else " Not Drawn"
return "\n" + str(self.target) + drawn
class ParseError(NameError):
pass
def formDict(strList):
tplist = [x.split(":") for x in strList]
for tp in tplist:
try:
tp[1] = float(tp[1])
except ValueError:
pass
ret = dict(tplist)
return ret
def filterDict(dic, char):
ret = {}
for key, value in dic.items():
newKey = key[key.find(char) + 1:]
try:
ret[newKey] = float(value)
except ValueError:
ret[newKey] = value
assert (len(dic) == len(ret))
return ret
def driveToStart(posX, posY, listofPaths):
p = Path((posX, posY), False, None)
listofPaths.append(p)
def drawRect(obj, style, listofPaths):
posX = float(obj.attrib["x"])
posY = float(obj.attrib["y"])
width = float(obj.attrib["width"])
height = float(obj.attrib["height"])
driveToStart(posX, posY, listofPaths)
p1 = Path((posX + width, posY), True, None)
p2 = Path((posX + width, posY + height), True, None)
p3 = Path((posX, posY + height), True, None)
p4 = Path((posX, posY), True, None)
listofPaths.extend([p1, p2, p3, p4])
if style["fill"] is not "none":
pass
def drawEllipse(obj, style, listofPaths):
pass
def drawPath(obj, style, listofPaths):
pass
def parseObj(obj):
obj.attrib = filterDict(obj.attrib, "}")
style = obj.attrib["style"].split(";")
style = formDict(style)
return obj, style
def main():
listOfPaths = []
svgNamespace = ".//{http://www.w3.org/2000/svg}"
tree = ET.parse(DATA_PATH)
root = tree.getroot()
graphics = root.find(svgNamespace + "g")
for obj in graphics:
obj, style = parseObj(obj)
entity = obj.tag[obj.tag.find("}") + 1:]
if entity == "path":
drawPath(obj, style, listOfPaths)
elif entity == "ellipse":
drawEllipse(obj, style, listOfPaths)
elif entity == "rect":
drawRect(obj, style, listOfPaths)
else:
raise ParseError
print(listOfPaths)
if __name__ == "__main__":
main()