From ba5db6c8798b0536d627105bf8a019a9f85a35be Mon Sep 17 00:00:00 2001 From: Adam Mayer Date: Fri, 24 Nov 2017 15:52:48 -0500 Subject: [PATCH] just handle one path type --- apple410.py | 5 ++-- svg_to_plot.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) create mode 100755 svg_to_plot.py diff --git a/apple410.py b/apple410.py index 7482588..3079baa 100755 --- a/apple410.py +++ b/apple410.py @@ -13,10 +13,11 @@ class Apple410: def sendchar(self, c): self.serial.flush() while not self.serial.cts: - time.sleep(0.1) + time.sleep(0.2) while not self.serial.dsr: - time.sleep(0.1) + time.sleep(0.2) self.serial.write(c.encode('ascii')) + self.serial.flush() def send(self, command): for c in command: diff --git a/svg_to_plot.py b/svg_to_plot.py new file mode 100755 index 0000000..55db830 --- /dev/null +++ b/svg_to_plot.py @@ -0,0 +1,73 @@ +#!/usr/bin/python3 + +import sys +import re +import string +from xml.dom import minidom + +coord_re = re.compile(r"([+-]?[0-9]+\.?[0-9]*),([+-]?[0-9]+\.?[0-9]*)") + +def parse_coord(d): + m = coord_re.match(d) + coords = (float(m.group(1)),float(m.group(2))) + remain = d[m.end():].strip() + #print("d:{} -- {} -- {}".format(d,coords,remain)) + return (coords, remain) + +def d_to_commands(d): + segs = [] + (cmd, remain) = (d[0],d[1:].strip()) + if cmd == 'm': + (coord, remain) = parse_coord(remain) + while remain and (remain[0] == '-' or remain[0] in string.digits): + (delta, remain) = parse_coord(remain) + nc = (delta[0]+coord[0],delta[1]+coord[1]) + segs.append( (coord, nc) ) + coord = nc + return segs + return segs + + + + +# 2394 x 1700 +w=2394 +h=1700 + + +if __name__ == '__main__': + scr = '-' + if len(sys.argv) > 1: + scr = sys.argv[1] + if scr == "-": + f = sys.stdin + else: + f = open(scr) + doc = minidom.parse(f) + segs = [] + for path in doc.getElementsByTagName('path'): + d = path.getAttribute('d') + segs.extend(d_to_commands(d)) + #print(segs) + maxx = max(map(lambda x: max(x[0][0], x[1][0]),segs)) + maxy = max(map(lambda x: max(x[0][1], x[1][1]),segs)) + minx = min(map(lambda x: min(x[0][0], x[1][0]),segs)) + miny = min(map(lambda x: min(x[0][1], x[1][1]),segs)) + sys.stderr.write("X {} {}\n".format(minx,maxx)) + sys.stderr.write("Y {} {}\n".format(miny,maxy)) + (spanx,spany) = (maxx-minx,maxy-miny) + xoff = -minx + yoff = -miny + (s1, s2) = (1.0,1.0) + if spanx > w: + s1 = spanx/w + if spany > h: + s2 = spany/h + s = max(s1,s2) + xoff = xoff/s + yoff = yoff/s + segs = map(lambda x: ((x[0][0]/s + xoff,x[0][1]/s + yoff), + (x[1][0]/s + xoff,x[1][1]/s + yoff)), segs) + for (sfrom, sto) in segs: + print("MA{:.2f},{:.2f}\nDA{:.2f},{:.2f}".format(sfrom[0],sfrom[1],sto[0],sto[1])) + doc.unlink()