svg2gcode/gcode.py

Tue, 19 Jan 2021 20:45:09 +0100

author
mdd
date
Tue, 19 Jan 2021 20:45:09 +0100
changeset 45
c82943fb205f
parent 8
86f90bddac0f
permissions
-rw-r--r--

updated main files to new github master version

4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
1 import math
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
2
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
3 INCH = 25.4 # mm
8
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
4 STEPS_PER_MM = 80 # hardware: steps/mm on XY
4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
5
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
6 class Gcode(object):
8
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
7 def __init__(self, scale=1.0, travel_speed=20, engrave_speed=20):
4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
8 self.dpi = 300
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
9 self.e_factor = 0.1
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
10 self.mm_pixel = round(INCH / self.dpi, 4)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
11
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
12 self.lines = []
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
13 self.dist_start = (0, 0)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
14 self.dist = 0.0
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
15
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
16 self.scale = scale
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
17 self.travel_speed = travel_speed * 60
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
18 self.engrave_speed = engrave_speed * 60
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
19
8
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
20 self._check_resolution()
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
21
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
22 def _check_resolution(self):
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
23 steps_pixel = self.mm_pixel * STEPS_PER_MM
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
24 print "Resolution is %f mm per pixel" % self.mm_pixel
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
25 if steps_pixel <= 5:
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
26 print "Warning: Steps per pixel (needs to be > 5, otherwise marlin joins lines): %f" % steps_pixel
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
27
4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
28 def _dimensions(self, x, y):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
29 x = self.mm_pixel * x
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
30 y = self.mm_pixel * y
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
31 if self.scale != 1.0:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
32 x = x * self.scale
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
33 y = y * self.scale
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
34 return (x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
35
8
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
36 def comment(self, msg):
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
37 self.lines.append("; %s" % msg)
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
38
4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
39 def move(self, x, y):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
40 x, y = self._dimensions(x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
41 if self.e_factor > 0:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
42 self.dist_start = (x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
43 self.dist = 0.0
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
44 self.lines.append("G92 E0") # reset extruder length
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
45 self.lines.append(
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
46 "G0 X%.4f Y%.4f F%s" % (x, y, self.travel_speed)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
47 )
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
48
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
49 def engrave(self, x, y):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
50 x, y = self._dimensions(x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
51 if self.e_factor > 0:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
52 self.dist += abs(math.hypot(x - self.dist_start[0], y - self.dist_start[1]))
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
53 e = "E%.4f" % (self.e_factor * self.dist)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
54 self.dist_start = (x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
55 else:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
56 e = ""
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
57 self.lines.append(
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
58 "G1 X%.4f Y%.4f %s F%s" % (x, y, e, self.engrave_speed)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
59 )
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
60
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
61
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
62 def write(self, filename):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
63 # write gcode file
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
64 fout = open(filename, "w")
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
65
8
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
66 fout.write("""; Filename: %s
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
67 ; GCode generated by one-night-and-several-days-not-so-quick-hack-script (marlin code flavour)
86f90bddac0f code refactoring
mbayer
parents: 4
diff changeset
68 ; https://neo-soft.org/hg-public/lasercut-scripts
4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
69 G21 ; Metric
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
70 ; We assume Z is in focus height and laser head is focus at bottom left of image!
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
71 G92 X0 Y0 E0 ; set zero position - new origin
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
72 G90 ; absolute positioning
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
73 M82 ; Set extruder (laser) to absolute positioning
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
74 M201 X1000 Y1000 E500 ; Set acceleration
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
75 M203 X1000 Y1000 Z4 E10 ; Set max feedrate
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
76 M209 S0 ; disable firmware retraction, we dont want to burn holes...
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
77 M302 ; Allow cold extrudes - doesnt matter because we hack the extruder physically off with the M571 E mod
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
78 M571 S1 E1 ; Activate Laser output on extrusion, but block real motor movement!
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
79 G0 X0 Y0 F%.1f ; Set moving speed
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
80 G1 X0 Y0 F%.1f ; Set engrave speed
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
81
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
82 """ % (filename, self.travel_speed, self.engrave_speed))
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
83
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
84 for g in self.lines:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
85 fout.write(g + "\n")
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
86 fout.close()

mercurial