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