svg2gcode/gcode.py

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

author
mbayer
date
Sat, 07 Nov 2015 13:37:23 +0100
changeset 4
234ad2069fdd
child 8
86f90bddac0f
permissions
-rw-r--r--

initial untested svg to gcode script

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()

mercurial