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