svg2gcode/svg2gcode.py

changeset 4
234ad2069fdd
child 5
b41cdab37aab
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/svg2gcode/svg2gcode.py	Sat Nov 07 13:37:23 2015 +0100
@@ -0,0 +1,55 @@
+#!/usr/bin/env python
+import svg, sys
+from gcode import Gcode
+from optparse import OptionParser
+
+parser = OptionParser()
+parser.add_option("-f", "--file", dest="filename", default=None,
+                  help="Load SVG file", metavar="FILE")
+parser.add_option("-s", "--scale",
+                  dest="scale", type="float", default=1.0,
+                  help="set scale factor (default 1.0)")
+parser.add_option("-e", "",
+                  dest="engrave_speed", type="float", default=20,
+                  help="engrave speed mm/sec (default 20)")
+parser.add_option("-t", "",
+                  dest="travel_speed", type="float", default=130,
+                  help="travel speed mm/sec (default 130)")
+
+
+(options, args) = parser.parse_args()
+
+
+if not options.filename:
+    print "no filename given!"
+    sys.exit(1)
+
+gcode = Gcode(scale=options.scale, travel_speed=options.travel_speed, engrave_speed=options.engrave_speed)
+
+im = svg.parse(options.filename)
+b1, b2 = im.bbox()
+width, height = b2.coord()
+print "Original dimension: %.2f x %.2f" % (width, height)
+width *= gcode.mm_pixel * options.scale
+height *= gcode.mm_pixel * options.scale
+print "Print dimension: %.2fmm x %.2fmm" % (width, height)
+
+def normalize(coord):
+    x = coord[0]
+    y = coord[1]
+    # flip y
+    y = (b2.coord()[1] - y)
+    return (x, y)
+
+data = im.flatten()
+for d in data:
+    if hasattr(d, "segments"):
+        for l in d.segments(1):
+            x, y = normalize(l[0].coord())
+            gcode.move(x, y)
+            for pt in l[1:]:
+                x, y = normalize(pt.coord())
+                gcode.engrave(x, y)
+
+# write gcode file
+gcode.write(options.filename + ".g")

mercurial