svg2gcode/gcode.py

Sat, 07 Nov 2015 18:45:17 +0100

author
mbayer
date
Sat, 07 Nov 2015 18:45:17 +0100
changeset 7
421aae29d9d9
parent 4
234ad2069fdd
child 8
86f90bddac0f
permissions
-rw-r--r--

finished poly infill

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
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
4
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
5 class Gcode(object):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
6 def __init__(self, bbox=None, scale=1.0, travel_speed=20, engrave_speed=20):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
7 self.dpi = 300
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
8 self.e_factor = 0.1
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
9
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 self.steps_pixel = self.mm_pixel * 80 # mine is 80 steps/mm on XY
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
12 print "Resolution is %f mm per pixel" % self.mm_pixel
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
13 if self.steps_pixel <= 5:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
14 print "Warning: Steps per pixel (needs to be > 5, otherwise marlin joins lines): %f" % self.steps_pixel
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.lines = []
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
17 self.dist_start = (0, 0)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
18 self.dist = 0.0
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
19
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
20 self.scale = scale
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
21 self.travel_speed = travel_speed * 60
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
22 self.engrave_speed = engrave_speed * 60
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
23
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
24 def _dimensions(self, x, y):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
25 x = self.mm_pixel * x
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
26 y = self.mm_pixel * y
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
27 if self.scale != 1.0:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
28 x = x * self.scale
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
29 y = y * self.scale
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
30 return (x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
31
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
32 def move(self, x, y):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
33 x, y = self._dimensions(x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
34 if self.e_factor > 0:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
35 self.dist_start = (x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
36 self.dist = 0.0
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
37 self.lines.append("G92 E0") # reset extruder length
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
38 self.lines.append(
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
39 "G0 X%.4f Y%.4f F%s" % (x, y, self.travel_speed)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
40 )
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
41
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
42 def engrave(self, x, y):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
43 x, y = self._dimensions(x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
44 if self.e_factor > 0:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
45 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
46 e = "E%.4f" % (self.e_factor * self.dist)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
47 self.dist_start = (x, y)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
48 else:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
49 e = ""
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
50 self.lines.append(
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
51 "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
52 )
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
53
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
54
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
55 def write(self, filename):
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
56 # write gcode file
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
57 fout = open(filename, "w")
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
58
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
59 fout.write("""
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
60 ; Filename: %s
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
61 ; GCode generated by bitplotter one-night-quick-hack script (marlin code flavour)
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
62
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
63 G21 ; Metric
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
64 ; 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
65 G92 X0 Y0 E0 ; set zero position - new origin
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
66 G90 ; absolute positioning
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
67 M82 ; Set extruder (laser) to absolute positioning
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
68 M201 X1000 Y1000 E500 ; Set acceleration
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
69 M203 X1000 Y1000 Z4 E10 ; Set max feedrate
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
70 M209 S0 ; disable firmware retraction, we dont want to burn holes...
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
71 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
72 M571 S1 E1 ; Activate Laser output on extrusion, but block real motor movement!
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
73 G0 X0 Y0 F%.1f ; Set moving speed
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
74 G1 X0 Y0 F%.1f ; Set engrave speed
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
75
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
76 """ % (filename, self.travel_speed, self.engrave_speed))
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
77
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
78 for g in self.lines:
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
79 fout.write(g + "\n")
234ad2069fdd initial untested svg to gcode script
mbayer
parents:
diff changeset
80 fout.close()

mercurial