printrun-src/printrun/svg2gcode/svg2gcode.py

Fri, 28 Jul 2017 15:57:22 +0200

author
mdd
date
Fri, 28 Jul 2017 15:57:22 +0200
changeset 29
c96f20e95029
parent 16
36d478bde840
permissions
-rw-r--r--

Changed min/max setting values

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

mercurial