svg2gcode/gcode.py

Sun, 08 Nov 2015 05:22:24 +0100

author
mbayer
date
Sun, 08 Nov 2015 05:22:24 +0100
changeset 14
51bf56ba3c10
parent 8
86f90bddac0f
permissions
-rw-r--r--

Added tag OUTLINES_STABLE for changeset e2fd4d7b3cb6

import math

INCH = 25.4 # mm
STEPS_PER_MM = 80 # hardware: steps/mm on XY

class Gcode(object):
    def __init__(self, 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.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

        self._check_resolution()

    def _check_resolution(self):
        steps_pixel = self.mm_pixel * STEPS_PER_MM
        print "Resolution is %f mm per pixel" % self.mm_pixel
        if steps_pixel <= 5:
            print "Warning: Steps per pixel (needs to be > 5, otherwise marlin joins lines): %f" % steps_pixel

    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 comment(self, msg):
        self.lines.append("; %s" % msg)

    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 one-night-and-several-days-not-so-quick-hack-script (marlin code flavour)
; https://neo-soft.org/hg-public/lasercut-scripts
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