Fri, 28 Jul 2017 16:05:08 +0200
Added module watcher for easier development (automatic module reload)
16 | 1 | #!/usr/bin/env python |
2 | ||
3 | import sys | |
4 | import xml.etree.ElementTree as ET | |
5 | import shapes as shapes_pkg | |
6 | from shapes import point_generator | |
7 | from config import * | |
8 | ||
9 | def generate_gcode(): | |
10 | svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path']) | |
11 | ||
12 | tree = ET.parse(sys.stdin) | |
13 | root = tree.getroot() | |
14 | ||
15 | width = root.get('width') | |
16 | height = root.get('height') | |
17 | if width == None or height == None: | |
18 | viewbox = root.get('viewBox') | |
19 | if viewbox: | |
20 | _, _, width, height = viewbox.split() | |
21 | ||
22 | if width == None or height == None: | |
23 | print "Unable to get width and height for the svg" | |
24 | sys.exit(1) | |
25 | ||
26 | width = float(width) | |
27 | height = float(height) | |
28 | ||
29 | scale_x = bed_max_x / max(width, height) | |
30 | scale_y = bed_max_y / max(width, height) | |
31 | ||
32 | print preamble | |
33 | ||
34 | for elem in root.iter(): | |
35 | ||
36 | try: | |
37 | _, tag_suffix = elem.tag.split('}') | |
38 | except ValueError: | |
39 | continue | |
40 | ||
41 | if tag_suffix in svg_shapes: | |
42 | shape_class = getattr(shapes_pkg, tag_suffix) | |
43 | shape_obj = shape_class(elem) | |
44 | d = shape_obj.d_path() | |
45 | m = shape_obj.transformation_matrix() | |
46 | ||
47 | if d: | |
48 | print shape_preamble | |
49 | p = point_generator(d, m, smoothness) | |
50 | for x,y in p: | |
51 | if x > 0 and x < bed_max_x and y > 0 and y < bed_max_y: | |
52 | print "G1 X%0.1f Y%0.1f" % (scale_x*x, scale_y*y) | |
53 | print shape_postamble | |
54 | ||
55 | print postamble | |
56 | ||
57 | if __name__ == "__main__": | |
58 | generate_gcode() | |
59 | ||
60 | ||
61 |