svg2gcode/svg2gcode.py

Sat, 07 Nov 2015 13:37:23 +0100

author
mbayer
date
Sat, 07 Nov 2015 13:37:23 +0100
changeset 4
234ad2069fdd
child 5
b41cdab37aab
permissions
-rwxr-xr-x

initial untested svg to gcode script

4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
2 import svg, sys
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
3 from gcode import Gcode
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
4 from optparse import OptionParser
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
5
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
6 parser = OptionParser()
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
7 parser.add_option("-f", "--file", dest="filename", default=None,
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
8 help="Load SVG file", metavar="FILE")
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
9 parser.add_option("-s", "--scale",
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
10 dest="scale", type="float", default=1.0,
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
11 help="set scale factor (default 1.0)")
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
12 parser.add_option("-e", "",
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
13 dest="engrave_speed", type="float", default=20,
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
14 help="engrave speed mm/sec (default 20)")
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
15 parser.add_option("-t", "",
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
16 dest="travel_speed", type="float", default=130,
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
17 help="travel speed mm/sec (default 130)")
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
18
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
19
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
20 (options, args) = parser.parse_args()
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
21
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
22
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
23 if not options.filename:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
24 print "no filename given!"
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
25 sys.exit(1)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
26
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
27 gcode = Gcode(scale=options.scale, travel_speed=options.travel_speed, engrave_speed=options.engrave_speed)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
28
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
29 im = svg.parse(options.filename)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
30 b1, b2 = im.bbox()
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
31 width, height = b2.coord()
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
32 print "Original dimension: %.2f x %.2f" % (width, height)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
33 width *= gcode.mm_pixel * options.scale
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
34 height *= gcode.mm_pixel * options.scale
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
35 print "Print dimension: %.2fmm x %.2fmm" % (width, height)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
36
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
37 def normalize(coord):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
38 x = coord[0]
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
39 y = coord[1]
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
40 # flip y
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
41 y = (b2.coord()[1] - y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
42 return (x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
43
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
44 data = im.flatten()
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
45 for d in data:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
46 if hasattr(d, "segments"):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
47 for l in d.segments(1):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
48 x, y = normalize(l[0].coord())
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
49 gcode.move(x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
50 for pt in l[1:]:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
51 x, y = normalize(pt.coord())
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
52 gcode.engrave(x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
53
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
54 # write gcode file
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
55 gcode.write(options.filename + ".g")

mercurial