Sat, 04 Jun 2016 12:41:32 +0200
Added the Lasercut settings to the pronterface options dialog
(default settings when used without pronterwindow)
16 | 1 | """ |
2 | Lasercutter library | |
3 | 2015/2016 by NeoSoft, Malte Bayer | |
4 | Intended to use standalone or implemented in Pronterface/Printrun | |
5 | """ | |
6 | ||
7 | """ | |
8 | LASERCUT SETTINGS | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
9 | Will be overridden from pronterface settings |
16 | 10 | """ |
19 | 11 | E_FACTOR = 0.5 |
16 | 12 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
13 | from PIL import Image |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
14 | import sys |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
15 | import math |
16 | 16 | |
17 | ||
20 | 18 | # GENERAL HEADER AND FOOTER GCODE |
19 | GCODE_HEAD = """ | |
20 | ; GCode generated by laser.py pronterface library (marlin code flavour) | |
21 | ; 2015/2016 by NeoSoft - Malte Bayer | |
22 | ||
23 | G21 ; Metric | |
24 | ; We assume Z is in focus height and laser head is focus at bottom left of image! | |
25 | G92 X0 Y0 E0; set zero position - new origin | |
26 | G90 ; absolute positioning | |
27 | M82 ; Set extruder (laser) to absolute positioning | |
28 | M201 X1000 Y1000 E1000 ; Set acceleration | |
29 | M203 X1000 Y1000 Z4 E1000 ; Set max feedrate | |
30 | M209 S0 ; disable firmware retraction, we dont want to burn holes... | |
31 | M302 ; Allow cold extrudes - doesnt matter because we hack the extruder physically off with the M571 E mod | |
32 | M571 S1 E1 ; Activate Laser output on extrusion, but block real motor movement! | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
33 | """ |
20 | 34 | |
21 | 35 | GCODE_FOOT = """G0 X0 Y0 F%.4f |
36 | M400 ; Wait for all moves to finish | |
20 | 37 | M571 S0 E0 |
38 | M42 P28 S0 ; Force laser off! | |
39 | M501 ; undo all settings made | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
40 | """ % (100*60) |
16 | 41 | |
42 | ||
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
43 | class LasercutterSettings: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
44 | """ |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
45 | Default settings object |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
46 | """ |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
47 | def __init__(self): |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
48 | self.lc_engrave_speed = 10 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
49 | # 30mm/min works for wood (regulate the output power to something between 10-30%) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
50 | # 30mm/min for black anodized aluminum to get a light engraving @ 100% power |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
51 | # 10mm/min for black anodized aluminum to get more "silver" @ 100% power |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
52 | self.lc_travel_speed = 120 |
16 | 53 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
54 | # BITMAP: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
55 | self.lc_bitmap_speed_factor = 1.0 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
56 | self.lc_dpi = 300 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
57 | self.lc_grey_threshold = 0 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
58 | self.lc_change_dir = True |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
59 | self.lc_invert_cut = True |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
60 | |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
61 | # HPGL: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
62 | self.lc_hpgl_speed_factor = 1.0 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
63 | |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
64 | # SVG: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
65 | self.lc_svg_speed_factor = 1.0 |
16 | 66 | |
67 | class Lasercutter: | |
68 | """ | |
69 | Lasercutter methods | |
20 | 70 | parameters: log = logger function (fuction has to accept a string) |
16 | 71 | """ |
72 | def __init__(self, pronterwindow = None): | |
73 | if pronterwindow: | |
74 | self.pronterwindow = pronterwindow | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
75 | self.settings = pronterwindow.settings |
21 | 76 | #self.log = pronterwindow.log |
77 | self.log = self.log_print | |
20 | 78 | self.pronterwindow.clear_log(None) |
16 | 79 | else: |
80 | self.pronterwindow = None | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
81 | self.settings = LasercutterSettings() |
16 | 82 | self.log = lambda : None |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
83 | |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
84 | # STATIC DEFINITIONS, DO NOT CHANGE WORLD's RULES! |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
85 | self.INCH = 25.4 # mm |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
86 | self.MM_PIXEL = round(self.INCH / self.settings.lc_dpi, 4) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
87 | self.STEPS_PIXEL = self.MM_PIXEL * 80 # mine is 80 steps/mm on XY |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
88 | |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
89 | self.log("Lasercutter library initialized\n%d DPI (%f mm/pixel)" % (self.settings.lc_dpi, self.MM_PIXEL)) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
90 | if self.STEPS_PIXEL <= 5: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
91 | self.log("WARNING: STEPS PER PIXEL NEEDS TO BE > 5 (otherwise marlin joins lines): %f" % self.STEPS_PIXEL) |
20 | 92 | self.log("Travel/Engrave speed: %d mm/sec, %d mm/sec" % ( |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
93 | self.settings.lc_travel_speed, self.settings.lc_engrave_speed) ) |
20 | 94 | self.log("") |
16 | 95 | |
21 | 96 | def log_print(self, msg): |
97 | print(msg) | |
98 | ||
16 | 99 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
100 | def pixel2bit(self, pixel): |
16 | 101 | """Convert the pixel value to a bit.""" |
102 | # some really weird stuff here ;-P | |
103 | ||
104 | # RGB to greyscale | |
105 | #print pixel | |
106 | #print type(pixel) | |
107 | if isinstance(pixel, tuple): | |
108 | #rgb | |
109 | pixel = pixel[0]*0.2989 + pixel[1]*0.5870 + pixel[2]*0.1140 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
110 | if pixel > self.settings.lc_grey_threshold: |
16 | 111 | return 1 |
112 | else: | |
113 | return 0 | |
114 | ||
115 | # color palette | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
116 | # TODO: get the grey value of the palette index instead of using pixel which is the palette index? |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
117 | if pixel <= self.settings.lc_grey_threshold: |
16 | 118 | return 1 |
119 | else: | |
120 | return 0 | |
121 | ||
122 | def image2gcode(self, filename): | |
123 | """ | |
124 | Open a image file and get the basic information about it. | |
125 | Then convert it to gcode (replacing the existing gcode buffer contents) | |
126 | """ | |
127 | try: | |
128 | im = Image.open(filename) | |
129 | except: | |
130 | self.log("Unable to open %s" % filename) | |
131 | return False | |
132 | ||
133 | self.log("Converting Image for lasercut:") | |
134 | self.log("File: %s" % filename) | |
135 | self.log("format: %s, mode: %s" % (im.format, im.mode)) | |
136 | width,height = im.size | |
137 | self.log("size: %d x %d pixels" % im.size) | |
138 | ||
139 | pix = im.load() | |
140 | ||
141 | fo = open(filename + ".g", "w") | |
20 | 142 | fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD)) |
16 | 143 | |
144 | fo.write(";Start engraving the raster image: %dx%d points @ %d DPI = %.0fx%.0f mm\n\n" % ( | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
145 | im.size[0], im.size[1], self.settings.lc_dpi, im.size[0] * self.MM_PIXEL, im.size[1] * self.MM_PIXEL) ) |
16 | 146 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
147 | INVERT_Y = self.MM_PIXEL * (im.size[1] -1) * (-1) |
16 | 148 | DIR = 1 |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
149 | travel_speed = self.settings.lc_travel_speed * 60 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
150 | engrave_speed = self.settings.lc_engrave_speed * 60 * self.settings.lc_bitmap_speed_factor |
16 | 151 | for X in range(im.size[0]): |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
152 | # TODO: Skip empty rows!!! |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
153 | fo.write("M400 ; X=%d printing row: direction %i\n" % (X, DIR)) |
16 | 154 | fo.write("G92 E0\n") |
155 | E = 0 | |
156 | last_bit = 1 # we engrave on black pixel = 0 | |
157 | START_Y = 0 | |
158 | if DIR > 0: | |
159 | range_start = 0 | |
160 | range_stop = im.size[1] | |
161 | else: | |
162 | range_start = im.size[1] -1 | |
163 | range_stop = -1 | |
164 | ||
165 | for Y in range(range_start, range_stop, DIR): | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
166 | YMM = abs((Y * self.MM_PIXEL) + INVERT_Y) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
167 | XMM = X * self.MM_PIXEL |
16 | 168 | #print "X %d Y %d" % (X, Y) |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
169 | bit = self.pixel2bit(pix[X, Y]) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
170 | if self.settings.lc_invert_cut: |
16 | 171 | if bit == 0: |
172 | bit = 1 | |
173 | else: | |
174 | bit = 0 | |
175 | if last_bit == bit: | |
176 | if bit == 1: | |
177 | # nothing to do, | |
178 | continue | |
179 | else: | |
180 | # are we at the end of Y range? | |
181 | #print Y | |
182 | if (Y == (im.size[1] - 1)) or (Y == 0): | |
183 | # draw line | |
184 | if DIR > 0: | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
185 | E = E + self.MM_PIXEL * (Y - START_Y) |
16 | 186 | else: |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
187 | E = E + self.MM_PIXEL * (START_Y - Y) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
188 | fo.write("G1 X%.4f Y%.4f E%.4f F%.4f\n" % (XMM, YMM, E * E_FACTOR, engrave_speed)) |
16 | 189 | else: |
190 | # bit value has changed! | |
191 | if bit == 0: | |
192 | # jump to start of line to write | |
193 | START_Y = Y | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
194 | fo.write("G0 X%.4f Y%.4f F%.4f\n" % (XMM, YMM, travel_speed)) |
16 | 195 | else: |
196 | # end of line to write | |
197 | if DIR > 0: | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
198 | E = E + (self.MM_PIXEL * (Y - START_Y)) |
16 | 199 | else: |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
200 | E = E + (self.MM_PIXEL * (START_Y - Y)) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
201 | fo.write("G1 X%.4f Y%.4f E%.4f F%.4f\n" % (XMM, YMM, E * E_FACTOR, engrave_speed)) |
16 | 202 | last_bit = bit |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
203 | if self.settings.lc_change_dir: |
16 | 204 | DIR = DIR * (-1) # change y direction on every X |
205 | ||
20 | 206 | fo.write(GCODE_FOOT) |
16 | 207 | fo.close() |
208 | ||
209 | if self.pronterwindow: | |
210 | self.log("") | |
211 | self.pronterwindow.load_gcode_async(filename + '.g') | |
212 | ||
213 | def hpgl2gcode(self, filename): | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
214 | # FOR HPGL: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
215 | SCALE_FACTOR = 1.0 / 40.0 # 40 plotter units |
16 | 216 | OFFSET_X = 0.0 |
217 | OFFSET_Y = 0.0 | |
218 | ||
219 | self.log("Converting HPGL plot for lasercut:") | |
220 | self.log("File: %s" % filename) | |
221 | ||
222 | fi = open(filename, "r") | |
223 | fo = open(filename + ".g", "w") | |
20 | 224 | fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD)) |
16 | 225 | |
226 | G = "0" | |
227 | LASER_STATE = 0 | |
228 | last_coord = [0.0,0.0] | |
229 | last_cmd = "" | |
230 | ||
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
231 | travel_speed = self.settings.lc_travel_speed * 60 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
232 | engrave_speed = self.settings.lc_engrave_speed * 60 * self.settings.lc_hpgl_speed_factor |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
233 | |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
234 | |
16 | 235 | for line in fi.readlines(): |
236 | for action in line.split(";"): | |
237 | action = action.strip() | |
238 | if action != "": | |
239 | cmd = action[:2] | |
240 | if cmd == "PD": | |
241 | LASER_STATE = 1 | |
242 | elif cmd == "PU": | |
243 | LASER_STATE = 0 | |
244 | if last_cmd == "PD": | |
245 | OFFSET_X = coord[0] * -1 | |
246 | OFFSET_Y = coord[1] * -1 | |
247 | fo.write("; PD PU detected, set coord offset %.4f x %.4f mm\n" % (OFFSET_X, OFFSET_Y)) | |
248 | elif cmd == "PA" or cmd == "PR": | |
249 | # TODO: convert relative coordinates to absolute here! | |
250 | coord = action[2:].split(",") | |
251 | coord[0] = (float(coord[0]) + OFFSET_X) * SCALE_FACTOR | |
252 | coord[1] = (float(coord[1]) + OFFSET_Y) * SCALE_FACTOR | |
253 | if LASER_STATE: | |
254 | EN = " E%.4f F%.4f" % ( | |
255 | E_FACTOR * math.hypot(coord[0] - last_coord[0], coord[1] - last_coord[1]), | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
256 | engrave_speed) |
16 | 257 | else: |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
258 | EN = " F%.4f" % travel_speed |
16 | 259 | |
260 | fo.write("G%d X%.4f Y%.4f%s\n" % ( | |
261 | LASER_STATE, coord[0], coord[1], EN) ) | |
262 | last_coord = coord | |
263 | elif cmd == "IN": | |
264 | pass | |
265 | elif cmd == "PT": | |
266 | print "Ignoring pen thickness" | |
267 | else: | |
268 | print "UNKNOWN: %s" % action | |
269 | last_cmd = cmd | |
270 | ||
20 | 271 | fo.write(GCODE_FOOT) |
16 | 272 | fi.close() |
273 | fo.close() | |
274 | ||
275 | if self.pronterwindow: | |
276 | self.log("") | |
277 | self.pronterwindow.load_gcode_async(filename + '.g') | |
278 | ||
19 | 279 | def svg2gcode(self, filename, bed_max_x = 50, bed_max_y = 50, smoothness = 0.2): |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
280 | # Imports for SVG |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
281 | import xml.etree.ElementTree as ET |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
282 | from svg2gcode import shapes as shapes_pkg |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
283 | from svg2gcode.shapes import point_generator |
18 | 284 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
285 | self.log("Generating paths from SVG, alternative lib (outlines only)...") |
16 | 286 | if smoothness < 0.1: smoothness = 0.1 |
287 | svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path']) | |
288 | tree = ET.parse(filename) | |
289 | root = tree.getroot() | |
290 | ||
291 | width = root.get('width') | |
292 | height = root.get('height') | |
293 | if width == None or height == None: | |
294 | viewbox = root.get('viewBox') | |
295 | if viewbox: | |
296 | _, _, width, height = viewbox.split() | |
297 | ||
298 | if width == None or height == None: | |
299 | self.log("Unable to get width and height for the svg!") | |
300 | return False | |
301 | ||
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
302 | width = float(width.replace("px", "").replace("pt", "")) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
303 | height = float(height.replace("px", "").replace("pt", "")) |
16 | 304 | |
305 | scale_x = bed_max_x / max(width, height) | |
306 | scale_y = bed_max_y / max(width, height) | |
307 | ||
308 | self.log("Scaling factor: %.2f, %.2f" % (scale_x,scale_y)) | |
309 | ||
310 | fo = open(filename + ".g", "w") | |
20 | 311 | fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD)) |
16 | 312 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
313 | travel_speed = self.settings.lc_travel_speed * 60 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
314 | engrave_speed = self.settings.lc_engrave_speed * 60 * self.settings.lc_svg_speed_factor |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
315 | |
16 | 316 | for elem in root.iter(): |
317 | try: | |
318 | _, tag_suffix = elem.tag.split('}') | |
319 | except ValueError: | |
320 | continue | |
321 | ||
322 | if tag_suffix in svg_shapes: | |
323 | shape_class = getattr(shapes_pkg, tag_suffix) | |
324 | shape_obj = shape_class(elem) | |
325 | d = shape_obj.d_path() | |
326 | m = shape_obj.transformation_matrix() | |
327 | ||
328 | if d: | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
329 | fo.write("M400 ; start %s\n" % (tag_suffix)) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
330 | fo.write("G92 E0\n") |
16 | 331 | E = 0 |
332 | xo = 0 | |
333 | yo = 0 | |
334 | p = point_generator(d, m, smoothness) | |
335 | start = True | |
336 | for x,y,pen in p: | |
337 | y = height - y | |
338 | xs = scale_x * x | |
339 | ys = scale_y * y | |
19 | 340 | if xo == xs and yo == ys: continue |
341 | ||
16 | 342 | if not pen: start = True |
343 | if xs >= 0 and xs <= bed_max_x and ys >= 0 and ys <= bed_max_y: | |
344 | if start: | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
345 | fo.write("G0 X%0.2f Y%0.2f F%.4f ; Move to start of shape\n" % (xs, ys, travel_speed)) |
16 | 346 | start = False |
347 | xo = xs | |
21 | 348 | yo = ys |
349 | object_xs = xs | |
350 | object_ys = ys | |
16 | 351 | else: |
352 | e_distance = math.hypot(xs - xo, ys - yo) | |
353 | xo = xs | |
354 | yo = ys | |
19 | 355 | E = E + (e_distance) |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
356 | fo.write("G1 X%0.2f Y%0.2f E%.4f F%.4f\n" % (xs, ys, E * E_FACTOR, engrave_speed)) |
16 | 357 | else: |
358 | self.log("Position outside print dimension: %d, %d" % (xs, ys)) | |
21 | 359 | if shape_obj.xml_node.get('fill'): |
360 | # Close the polygon | |
361 | e_distance = math.hypot(object_xs - xo, object_ys - yo) | |
362 | E = E + (e_distance) | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
363 | fo.write("G1 X%0.2f Y%0.2f E%.4f F%.4f ; Close the object polygon\n" % (object_xs, object_ys, E * E_FACTOR, engrave_speed)) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
364 | print "connecting filled path end to start" |
16 | 365 | |
20 | 366 | fo.write(GCODE_FOOT) |
367 | fo.close() | |
16 | 368 | |
369 | if self.pronterwindow: | |
370 | self.log("") | |
371 | self.pronterwindow.load_gcode_async(filename + '.g') | |
372 |