-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpypenwriter.py
179 lines (149 loc) · 4.27 KB
/
pypenwriter.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
177
178
179
#!/usr/bin/python3
import serial
import math
import time
import sys
import re
from xml.dom import minidom
class PlotterDrawing:
BLACK = 0
RED = 1
GREEN = 2
BLUE = 3
def __init__(self):
self.output = b'\x1bc\rI\r'
self.lastcommand = ""
self.thiscommand = ""
self.lastpos = (None,None)
self.lastcolor = -1
def append(self, cmd):
if self.lastcommand == "D" and self.thiscommand == "D":
self.output = self.output[:-1]
self.output += bytes(cmd.encode('ascii')) + b'\r'
self.lastcommand = self.thiscommand
def color(self, color):
if self.lastcolor != color:
self.thiscommand = "C"
self.append(f'C{color}')
self.lastcolor = color
def move(self, x, y):
self.thiscommand = "M"
self.append(f'M{round(x)},{round(y)}')
self.lastpos = (round(x),round(y))
def line(self, x, y):
self.thiscommand = "D"
if self.lastcommand == "D":
self.append(f',{round(x)},{round(y)}')
else:
self.append(f'D{round(x)},{round(y)}')
self.lastpos = (round(x),round(y))
def circle(self, radius):
self.thiscommand = "Y"
self.append(f'Y{round(radius)}')
def sethome(self):
self.thiscommand = "I"
self.append(f'I')
def home(self):
self.thiscommand = "H"
self.append(f'H')
def islastpos(self, x, y):
return (round(x),round(y)) == self.lastpos
def usage():
print(f"usage: {sys.argv[0]} <input.svg> <com-port> [<width-in-steps>] [<debug>]")
exit(0)
def SVG_to_plotter(draw, filename, scale):
doc = minidom.parse(filename)
viewbox = doc.getElementsByTagName('svg')[0].getAttribute('viewBox').split(' ')
plot_scale = scale
scale_side = float(viewbox[2])
point_scale = 1 / scale_side * plot_scale
halfway_down = int(float(viewbox[3]) * point_scale / 2)
draw.move(-60,-halfway_down)
draw.sethome()
groups = doc.getElementsByTagName('g')
for group in groups:
if group.hasAttribute('stroke'):
color = group.getAttribute('stroke')
if color == "#ff0000":
group.color = draw.RED
if color == "#00ff00":
group.color = draw.GREEN
if color == "#0000ff":
group.color = draw.BLUE
if color == "#000000":
group.color = draw.BLACK
else:
group.color = draw.BLACK
elements = group.childNodes
for element in elements:
if element.nodeName == 'polygon' or \
element.nodeName == 'polyline' or \
element.nodeName == 'line':
if element.hasAttribute('style'):
style = element.getAttribute('style')
color = re.findall(r'(?:stroke:#)......', style)[0]
if color == "stroke:#ff0000":
draw.color(draw.RED)
if color == "stroke:#00ff00":
draw.color(draw.GREEN)
if color == "stroke:#0000ff":
draw.color(draw.BLUE)
if color == "stroke:#000000":
draw.color(draw.BLACK)
else:
draw.color(group.color)
if element.tagName == 'polygon' or \
element.tagName == 'polyline':
points = element.getAttribute('points').split(' ')
lastx = -1
lasty = -1
point_idx = 0
for point in points:
if ',' in point:
x,y = point.split(',')
x = int(float(x) * point_scale)
y = halfway_down-int(float(y) * point_scale)
if point_idx == 0:
firstx = x
firsty = y
draw.move(x, y)
else:
draw.line(x,y)
point_idx += 1
if element.tagName == 'polygon':
draw.line(firstx,firsty)
elif element.tagName == 'line':
x1 = int(float(element.getAttribute('x1')) * point_scale)
y1 = halfway_down-int(float(element.getAttribute('y1')) * point_scale)
x2 = int(float(element.getAttribute('x2')) * point_scale)
y2 = halfway_down-int(float(element.getAttribute('y2')) * point_scale)
if not draw.islastpos(x1,y1):
draw.move(x1,y1)
draw.line(x2,y2)
draw.home()
doc.unlink()
# for debug
#print(draw.output)
if __name__ == "__main__":
if len(sys.argv) < 3:
usage()
if len(sys.argv) < 4:
scale = 960
else:
scale = int(sys.argv[3])
filename = sys.argv[1]
com_port = sys.argv[2]
draw = PlotterDrawing()
SVG_to_plotter(draw, filename, scale)
if len(sys.argv) < 5:
with serial.Serial(com_port,2400,rtscts=True) as ser:
commands = draw.output.split(b'\r')
idx = 0
for command in commands:
ser.write(command + b':\r')
ser.flush()
idx += 1;
print(f"\rprogress: {int(idx / len(commands) * 100)}%", end="")
ser.close()
else:
print(draw.output)