Sat, 07 Nov 2015 13:37:23 +0100
initial untested svg to gcode script
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/svg2gcode/autobot.svg Sat Nov 07 13:37:23 2015 +0100 @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="355.032pt" height="349.346pt" viewBox="0 0 355.032 349.346" version="1.1"> +<g id="surface1"> +<path style="fill:none;stroke-width:1;stroke-linecap:butt;stroke-linejoin:miter;stroke:rgb(0%,0%,0%);stroke-opacity:1;stroke-miterlimit:10;" d="M 261.144531 11.808594 L 234.71875 4.609375 L 205.703125 0 L 143.351562 0.574219 L 92.234375 12.382812 L 69.914062 21.527344 L 75.890625 50.472656 L 177.625 117.71875 L 279.21875 50.328125 L 285.195312 21.382812 L 261.144531 11.808594 M 0 17.425781 L 22.894531 122.398438 L 77.761719 161.929688 L 77.902344 162 L 143.710938 172.511719 L 132.550781 102.167969 L 63.648438 56.519531 L 56.59375 21.601562 L 0 17.207031 L 0 17.425781 M 273.167969 162.578125 L 277.34375 161.929688 L 332.207031 122.402344 L 355.03125 17.855469 L 354.742188 17.210938 L 298.511719 21.601562 L 291.457031 56.449219 L 222.550781 102.167969 L 211.320312 172.511719 L 273.167969 162.578125 M 145.585938 110.160156 L 145.511719 110.304688 L 155.230469 175.25 L 155.230469 259.992188 L 199.800781 259.992188 L 199.871094 175.320312 L 209.59375 110.160156 L 177.480469 132.335938 L 145.585938 110.160156 M 31.464844 147.527344 L 41.113281 272.089844 L 93.023438 316.078125 L 93.3125 316.152344 L 93.457031 204.695312 L 66.816406 185.398438 L 66.816406 168.984375 L 31.246094 143.351562 L 31.464844 147.527344 M 262.585938 316.511719 L 313.992188 272.015625 L 323.929688 143.351562 L 323.710938 143.425781 L 288.289062 169.054688 L 288.289062 185.542969 L 261.648438 204.839844 L 261.792969 316.945312 L 262.585938 316.511719 M 134.566406 190.367188 L 106.414062 205.058594 L 106.414062 326.59375 L 123.40625 341.714844 L 123.910156 342 L 144.070312 293.328125 L 211.03125 293.328125 L 231.121094 342 L 232.199219 341.28125 L 248.6875 325.367188 L 248.6875 204.984375 L 210.886719 185.546875 L 211.246094 272.523438 L 143.785156 272.449219 L 144.214844 185.472656 L 134.566406 190.367188 M 151.632812 305.785156 L 134.929688 348.050781 L 134.566406 349.347656 L 220.753906 349.347656 L 203.542969 305.570312 L 151.777344 305.570312 L 151.632812 305.785156 M 285.191406 21.382812 L 279.214844 50.328125 L 177.625 117.71875 L 75.886719 50.472656 L 69.910156 21.527344 L 92.230469 12.382812 L 143.351562 0.574219 L 205.703125 0 L 234.722656 4.609375 L 261.144531 11.808594 L 285.191406 21.382812 M 177.480469 76.535156 L 234.648438 38.449219 L 235.296875 37.871094 L 119.589844 37.945312 L 177.480469 76.535156 M 0 17.207031 L 56.59375 21.601562 L 63.648438 56.519531 L 132.550781 102.167969 L 143.710938 172.511719 L 77.902344 162 L 77.761719 161.929688 L 22.894531 122.398438 L 0 17.425781 L 0 17.207031 M 32.328125 65.160156 L 110.953125 118.222656 L 109.367188 107.566406 L 30.023438 53.929688 L 32.328125 65.160156 M 37.871094 93.527344 L 38.519531 96.265625 L 116.785156 148.894531 L 114.695312 137.953125 L 35.785156 85.25 L 37.871094 93.527344 M 211.320312 172.511719 L 222.550781 102.167969 L 291.457031 56.449219 L 298.511719 21.601562 L 354.742188 17.210938 L 355.03125 17.855469 L 332.207031 122.398438 L 277.34375 161.929688 L 211.320312 172.511719 M 320.328125 66.816406 L 322.777344 65.089844 L 324.9375 53.855469 L 324.792969 54 L 245.734375 107.640625 L 244.152344 118.296875 L 320.328125 66.816406 M 277.417969 122.6875 L 316.511719 96.265625 L 319.175781 85.246094 L 240.410156 138.097656 L 238.320312 148.96875 L 277.417969 122.6875 M 119.59375 37.945312 L 177.480469 76.535156 L 234.648438 38.449219 L 235.296875 37.871094 L 119.59375 37.945312 M 30.023438 53.929688 L 32.328125 65.160156 L 110.953125 118.226562 L 109.367188 107.566406 L 30.023438 53.929688 M 244.152344 118.296875 L 322.777344 65.089844 L 324.9375 53.855469 L 324.792969 54 L 245.738281 107.640625 L 244.152344 118.296875 M 238.320312 148.96875 L 316.511719 96.265625 L 319.175781 85.25 L 240.410156 138.097656 L 238.320312 148.96875 M 177.480469 132.335938 L 145.585938 110.160156 L 145.511719 110.304688 L 155.230469 175.25 L 155.230469 259.992188 L 199.800781 259.992188 L 199.871094 175.320312 L 209.59375 110.160156 L 177.480469 132.335938 M 31.246094 143.351562 L 41.113281 272.089844 L 93.023438 316.082031 L 93.3125 316.152344 L 93.457031 204.695312 L 66.816406 185.402344 L 66.816406 168.984375 L 31.246094 143.351562 M 261.792969 316.945312 L 262.585938 316.511719 L 313.992188 272.015625 L 323.929688 143.351562 L 323.710938 143.425781 L 288.289062 169.058594 L 288.289062 185.542969 L 261.648438 204.839844 L 261.792969 316.945312 M 144.214844 185.472656 L 106.417969 205.054688 L 106.417969 326.59375 L 123.40625 341.710938 L 123.910156 342 L 144.070312 293.328125 L 211.03125 293.328125 L 231.121094 342 L 232.199219 341.28125 L 248.6875 325.367188 L 248.6875 204.984375 L 210.886719 185.542969 L 211.25 272.519531 L 143.785156 272.449219 L 144.214844 185.472656 M 151.777344 305.570312 L 151.632812 305.785156 L 134.929688 348.050781 L 134.566406 349.34375 L 220.753906 349.34375 L 203.542969 305.570312 L 151.777344 305.570312 M 35.785156 85.25 L 38.519531 96.265625 L 116.785156 148.894531 L 114.695312 137.953125 L 35.785156 85.25 "/> +</g> +</svg>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/svg2gcode/gcode.py Sat Nov 07 13:37:23 2015 +0100 @@ -0,0 +1,80 @@ +import math + +INCH = 25.4 # mm + +class Gcode(object): + def __init__(self, bbox=None, scale=1.0, travel_speed=20, engrave_speed=20): + self.dpi = 300 + self.e_factor = 0.1 + + self.mm_pixel = round(INCH / self.dpi, 4) + self.steps_pixel = self.mm_pixel * 80 # mine is 80 steps/mm on XY + print "Resolution is %f mm per pixel" % self.mm_pixel + if self.steps_pixel <= 5: + print "Warning: Steps per pixel (needs to be > 5, otherwise marlin joins lines): %f" % self.steps_pixel + + self.lines = [] + self.dist_start = (0, 0) + self.dist = 0.0 + + self.scale = scale + self.travel_speed = travel_speed * 60 + self.engrave_speed = engrave_speed * 60 + + def _dimensions(self, x, y): + x = self.mm_pixel * x + y = self.mm_pixel * y + if self.scale != 1.0: + x = x * self.scale + y = y * self.scale + return (x, y) + + def move(self, x, y): + x, y = self._dimensions(x, y) + if self.e_factor > 0: + self.dist_start = (x, y) + self.dist = 0.0 + self.lines.append("G92 E0") # reset extruder length + self.lines.append( + "G0 X%.4f Y%.4f F%s" % (x, y, self.travel_speed) + ) + + def engrave(self, x, y): + x, y = self._dimensions(x, y) + if self.e_factor > 0: + self.dist += abs(math.hypot(x - self.dist_start[0], y - self.dist_start[1])) + e = "E%.4f" % (self.e_factor * self.dist) + self.dist_start = (x, y) + else: + e = "" + self.lines.append( + "G1 X%.4f Y%.4f %s F%s" % (x, y, e, self.engrave_speed) + ) + + + def write(self, filename): + # write gcode file + fout = open(filename, "w") + + fout.write(""" +; Filename: %s +; GCode generated by bitplotter one-night-quick-hack script (marlin code flavour) + +G21 ; Metric +; We assume Z is in focus height and laser head is focus at bottom left of image! +G92 X0 Y0 E0 ; set zero position - new origin +G90 ; absolute positioning +M82 ; Set extruder (laser) to absolute positioning +M201 X1000 Y1000 E500 ; Set acceleration +M203 X1000 Y1000 Z4 E10 ; Set max feedrate +M209 S0 ; disable firmware retraction, we dont want to burn holes... +M302 ; Allow cold extrudes - doesnt matter because we hack the extruder physically off with the M571 E mod +M571 S1 E1 ; Activate Laser output on extrusion, but block real motor movement! +G0 X0 Y0 F%.1f ; Set moving speed +G1 X0 Y0 F%.1f ; Set engrave speed + +""" % (filename, self.travel_speed, self.engrave_speed)) + + for g in self.lines: + fout.write(g + "\n") + fout.close()
--- /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")