Fri, 28 Jul 2017 15:56:52 +0200
Bugfix SVG offset correction
Changed settings min/max values
16 | 1 | """ |
2 | Lasercutter library | |
28 | 3 | 2015-2016 by NeoSoft, Malte Di Donato |
16 | 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 |
23 | 49 | # 30mm/sec works for wood (regulate the output power to something between 10-30%) |
50 | # 30mm/sec for black anodized aluminum to get a light engraving @ 100% power | |
51 | # 10mm/sec for black anodized aluminum to get maximum possible engraving! @ 100% power | |
22
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 |
25 | 66 | self.lc_svg_smoothness = 0.2 |
67 | self.lc_svg_width = 50 | |
68 | self.lc_svg_height = 50 | |
69 | self.lc_svg_scalemode = "scale" | |
70 | ||
16 | 71 | |
72 | class Lasercutter: | |
73 | """ | |
74 | Lasercutter methods | |
20 | 75 | parameters: log = logger function (fuction has to accept a string) |
16 | 76 | """ |
77 | def __init__(self, pronterwindow = None): | |
78 | if pronterwindow: | |
79 | self.pronterwindow = pronterwindow | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
80 | self.settings = pronterwindow.settings |
21 | 81 | #self.log = pronterwindow.log |
82 | self.log = self.log_print | |
20 | 83 | self.pronterwindow.clear_log(None) |
16 | 84 | else: |
85 | self.pronterwindow = None | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
86 | self.settings = LasercutterSettings() |
16 | 87 | self.log = lambda : None |
22
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 | # STATIC DEFINITIONS, DO NOT CHANGE WORLD's RULES! |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
90 | self.INCH = 25.4 # mm |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
91 | 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
|
92 | 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
|
93 | |
23 | 94 | self.log("Lasercutter library initialized\n%d DPI (%f mm/pixel)" % ( |
95 | self.settings.lc_dpi, self.MM_PIXEL)) | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
96 | if self.STEPS_PIXEL <= 5: |
23 | 97 | self.log("WARNING: STEPS PER PIXEL NEEDS TO BE > 5 (otherwise marlin joins lines): %f" % ( |
98 | self.STEPS_PIXEL)) | |
20 | 99 | 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
|
100 | self.settings.lc_travel_speed, self.settings.lc_engrave_speed) ) |
20 | 101 | self.log("") |
16 | 102 | |
21 | 103 | def log_print(self, msg): |
104 | print(msg) | |
105 | ||
16 | 106 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
107 | def pixel2bit(self, pixel): |
16 | 108 | """Convert the pixel value to a bit.""" |
109 | # some really weird stuff here ;-P | |
110 | ||
111 | # RGB to greyscale | |
112 | #print pixel | |
113 | #print type(pixel) | |
114 | if isinstance(pixel, tuple): | |
115 | #rgb | |
116 | 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
|
117 | if pixel > self.settings.lc_grey_threshold: |
16 | 118 | return 1 |
119 | else: | |
120 | return 0 | |
121 | ||
122 | # color palette | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
123 | # 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
|
124 | if pixel <= self.settings.lc_grey_threshold: |
16 | 125 | return 1 |
126 | else: | |
127 | return 0 | |
128 | ||
129 | def image2gcode(self, filename): | |
130 | """ | |
131 | Open a image file and get the basic information about it. | |
132 | Then convert it to gcode (replacing the existing gcode buffer contents) | |
133 | """ | |
134 | try: | |
135 | im = Image.open(filename) | |
136 | except: | |
137 | self.log("Unable to open %s" % filename) | |
138 | return False | |
139 | ||
140 | self.log("Converting Image for lasercut:") | |
141 | self.log("File: %s" % filename) | |
142 | self.log("format: %s, mode: %s" % (im.format, im.mode)) | |
143 | width,height = im.size | |
144 | self.log("size: %d x %d pixels" % im.size) | |
145 | ||
146 | pix = im.load() | |
147 | ||
148 | fo = open(filename + ".g", "w") | |
20 | 149 | fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD)) |
16 | 150 | |
151 | fo.write(";Start engraving the raster image: %dx%d points @ %d DPI = %.0fx%.0f mm\n\n" % ( | |
23 | 152 | im.size[0], im.size[1], self.settings.lc_dpi, |
153 | im.size[0] * self.MM_PIXEL, im.size[1] * self.MM_PIXEL) ) | |
16 | 154 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
155 | INVERT_Y = self.MM_PIXEL * (im.size[1] -1) * (-1) |
16 | 156 | DIR = 1 |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
157 | travel_speed = self.settings.lc_travel_speed * 60 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
158 | engrave_speed = self.settings.lc_engrave_speed * 60 * self.settings.lc_bitmap_speed_factor |
16 | 159 | for X in range(im.size[0]): |
23 | 160 | gcode_col = "" |
16 | 161 | E = 0 |
162 | last_bit = 1 # we engrave on black pixel = 0 | |
163 | START_Y = 0 | |
164 | if DIR > 0: | |
165 | range_start = 0 | |
166 | range_stop = im.size[1] | |
167 | else: | |
168 | range_start = im.size[1] -1 | |
169 | range_stop = -1 | |
170 | ||
171 | 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
|
172 | YMM = abs((Y * self.MM_PIXEL) + INVERT_Y) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
173 | XMM = X * self.MM_PIXEL |
16 | 174 | #print "X %d Y %d" % (X, Y) |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
175 | bit = self.pixel2bit(pix[X, Y]) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
176 | if self.settings.lc_invert_cut: |
16 | 177 | if bit == 0: |
178 | bit = 1 | |
179 | else: | |
180 | bit = 0 | |
181 | if last_bit == bit: | |
182 | if bit == 1: | |
183 | # nothing to do, | |
184 | continue | |
185 | else: | |
186 | # are we at the end of Y range? | |
187 | #print Y | |
188 | if (Y == (im.size[1] - 1)) or (Y == 0): | |
189 | # draw line | |
190 | if DIR > 0: | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
191 | E = E + self.MM_PIXEL * (Y - START_Y) |
16 | 192 | else: |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
193 | E = E + self.MM_PIXEL * (START_Y - Y) |
23 | 194 | gcode_col += "G1 X%.4f Y%.4f E%.4f F%.4f\n" % ( |
195 | XMM, YMM, E * E_FACTOR, engrave_speed) | |
16 | 196 | else: |
197 | # bit value has changed! | |
198 | if bit == 0: | |
199 | # jump to start of line to write | |
200 | START_Y = Y | |
23 | 201 | gcode_col += "G0 X%.4f Y%.4f F%.4f\n" % ( |
202 | XMM, YMM, travel_speed) | |
16 | 203 | else: |
204 | # end of line to write | |
205 | if DIR > 0: | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
206 | E = E + (self.MM_PIXEL * (Y - START_Y)) |
16 | 207 | else: |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
208 | E = E + (self.MM_PIXEL * (START_Y - Y)) |
23 | 209 | gcode_col += "G1 X%.4f Y%.4f E%.4f F%.4f\n" % ( |
210 | XMM, YMM, E * E_FACTOR, engrave_speed) | |
16 | 211 | last_bit = bit |
23 | 212 | if gcode_col <> "": |
213 | # we skip empty columns | |
214 | fo.write("M400 ; X=%d printing row: direction %i\nG92 E0\n%s" % ( | |
215 | X, DIR, gcode_col)) | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
216 | if self.settings.lc_change_dir: |
16 | 217 | DIR = DIR * (-1) # change y direction on every X |
218 | ||
20 | 219 | fo.write(GCODE_FOOT) |
16 | 220 | fo.close() |
221 | ||
222 | if self.pronterwindow: | |
223 | self.log("") | |
224 | self.pronterwindow.load_gcode_async(filename + '.g') | |
225 | ||
226 | def hpgl2gcode(self, filename): | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
227 | # FOR HPGL: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
228 | SCALE_FACTOR = 1.0 / 40.0 # 40 plotter units |
16 | 229 | OFFSET_X = 0.0 |
230 | OFFSET_Y = 0.0 | |
231 | ||
232 | self.log("Converting HPGL plot for lasercut:") | |
233 | self.log("File: %s" % filename) | |
234 | ||
235 | fi = open(filename, "r") | |
236 | fo = open(filename + ".g", "w") | |
20 | 237 | fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD)) |
16 | 238 | |
239 | G = "0" | |
240 | LASER_STATE = 0 | |
241 | last_coord = [0.0,0.0] | |
242 | last_cmd = "" | |
243 | ||
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
244 | travel_speed = self.settings.lc_travel_speed * 60 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
245 | 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
|
246 | |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
247 | |
16 | 248 | for line in fi.readlines(): |
249 | for action in line.split(";"): | |
250 | action = action.strip() | |
251 | if action != "": | |
252 | cmd = action[:2] | |
253 | if cmd == "PD": | |
254 | LASER_STATE = 1 | |
255 | elif cmd == "PU": | |
256 | LASER_STATE = 0 | |
257 | if last_cmd == "PD": | |
258 | OFFSET_X = coord[0] * -1 | |
259 | OFFSET_Y = coord[1] * -1 | |
23 | 260 | fo.write("; PD PU detected, set coord offset %.4f x %.4f mm\n" % ( |
261 | OFFSET_X, OFFSET_Y)) | |
16 | 262 | elif cmd == "PA" or cmd == "PR": |
263 | # TODO: convert relative coordinates to absolute here! | |
264 | coord = action[2:].split(",") | |
265 | coord[0] = (float(coord[0]) + OFFSET_X) * SCALE_FACTOR | |
266 | coord[1] = (float(coord[1]) + OFFSET_Y) * SCALE_FACTOR | |
267 | if LASER_STATE: | |
268 | EN = " E%.4f F%.4f" % ( | |
269 | 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
|
270 | engrave_speed) |
16 | 271 | else: |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
272 | EN = " F%.4f" % travel_speed |
16 | 273 | |
274 | fo.write("G%d X%.4f Y%.4f%s\n" % ( | |
275 | LASER_STATE, coord[0], coord[1], EN) ) | |
276 | last_coord = coord | |
277 | elif cmd == "IN": | |
278 | pass | |
279 | elif cmd == "PT": | |
280 | print "Ignoring pen thickness" | |
281 | else: | |
282 | print "UNKNOWN: %s" % action | |
283 | last_cmd = cmd | |
284 | ||
20 | 285 | fo.write(GCODE_FOOT) |
16 | 286 | fi.close() |
287 | fo.close() | |
288 | ||
289 | if self.pronterwindow: | |
290 | self.log("") | |
291 | self.pronterwindow.load_gcode_async(filename + '.g') | |
292 | ||
25 | 293 | def svg2gcode(self, filename): |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
294 | # Imports for SVG |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
295 | import xml.etree.ElementTree as ET |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
296 | from svg2gcode import shapes as shapes_pkg |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
297 | from svg2gcode.shapes import point_generator |
18 | 298 | |
25 | 299 | self.log("Generating paths from SVG (outlines only)...") |
16 | 300 | svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path']) |
301 | tree = ET.parse(filename) | |
302 | root = tree.getroot() | |
303 | ||
28 | 304 | # Todo: force viewbox values configurable? |
305 | #width = root.get('width') | |
306 | #height = root.get('height') | |
307 | # if width == None or height == None: | |
308 | viewbox = root.get('viewBox') | |
309 | if viewbox: | |
310 | _, _, width, height = viewbox.split() | |
16 | 311 | |
312 | if width == None or height == None: | |
313 | self.log("Unable to get width and height for the svg!") | |
314 | return False | |
28 | 315 | else: |
316 | self.log("SVG Dimensions are %s x %s" % (width, height)) | |
16 | 317 | |
27 | 318 | # TODO: use cm or mm as absolute dimensions! |
319 | width = float(width.replace("px", "").replace("pt", "").replace("mm", "")) | |
320 | height = float(height.replace("px", "").replace("pt", "").replace("mm", "")) | |
28 | 321 | |
25 | 322 | smoothness = self.settings.lc_svg_smoothness |
323 | if smoothness < 0.1: smoothness = 0.1 | |
16 | 324 | |
28 | 325 | # get the minimum x and y values to get an offset to 0,0 |
326 | ofs_x = 99999999999.0 | |
327 | ofs_y = 99999999999.0 | |
328 | max_x = -99999999999.0 | |
329 | max_y = -99999999999.0 | |
330 | for elem in root.iter(): | |
331 | try: | |
332 | _, tag_suffix = elem.tag.split('}') | |
333 | except ValueError: | |
334 | continue | |
335 | if tag_suffix in svg_shapes: | |
336 | shape_class = getattr(shapes_pkg, tag_suffix) | |
337 | shape_obj = shape_class(elem) | |
338 | d = shape_obj.d_path() | |
339 | m = shape_obj.transformation_matrix() | |
340 | ||
341 | if d: | |
342 | p = point_generator(d, m, smoothness) | |
343 | start = True | |
344 | for x,y,pen in p: | |
345 | if x < ofs_x: | |
346 | ofs_x = x | |
347 | if y < ofs_y: | |
348 | ofs_y = y | |
349 | if x > max_x: | |
350 | max_x = x | |
351 | if y > max_y: | |
352 | max_y = y | |
353 | ofs_x *= -1 | |
354 | ofs_y *= -1 | |
355 | max_x += ofs_x | |
356 | max_y += ofs_y | |
357 | self.log("Calculated Offset to 0,0 is %f,%f" % (ofs_x, ofs_y)) | |
358 | ||
359 | """ | |
360 | self.log("Calculated Dimension is %f,%f" % (max_x, max_y)) | |
361 | width = max_x | |
362 | height = max_y | |
363 | """ | |
364 | ||
25 | 365 | bed_max_x = self.settings.lc_svg_width |
366 | bed_max_y = self.settings.lc_svg_height | |
367 | ||
368 | if self.settings.lc_svg_scalemode == "scale": | |
369 | scale_x = scale_y = min(bed_max_x, bed_max_y) / max(width, height) | |
370 | else: | |
28 | 371 | scale_x = bed_max_x / width |
372 | scale_y = bed_max_y / height | |
16 | 373 | |
374 | self.log("Scaling factor: %.2f, %.2f" % (scale_x,scale_y)) | |
375 | ||
376 | fo = open(filename + ".g", "w") | |
20 | 377 | fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD)) |
16 | 378 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
379 | travel_speed = self.settings.lc_travel_speed * 60 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
380 | 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
|
381 | |
28 | 382 | |
16 | 383 | for elem in root.iter(): |
384 | try: | |
385 | _, tag_suffix = elem.tag.split('}') | |
386 | except ValueError: | |
387 | continue | |
388 | ||
389 | if tag_suffix in svg_shapes: | |
28 | 390 | self.log("Parsing shape: %s" % tag_suffix) |
16 | 391 | shape_class = getattr(shapes_pkg, tag_suffix) |
392 | shape_obj = shape_class(elem) | |
393 | d = shape_obj.d_path() | |
394 | m = shape_obj.transformation_matrix() | |
395 | ||
396 | if d: | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
397 | fo.write("M400 ; start %s\n" % (tag_suffix)) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
398 | fo.write("G92 E0\n") |
16 | 399 | E = 0 |
400 | xo = 0 | |
401 | yo = 0 | |
402 | p = point_generator(d, m, smoothness) | |
403 | start = True | |
404 | for x,y,pen in p: | |
28 | 405 | x += ofs_x |
406 | y += ofs_y | |
407 | y = height - y # invert the bed | |
16 | 408 | xs = scale_x * x |
409 | ys = scale_y * y | |
19 | 410 | if xo == xs and yo == ys: continue |
411 | ||
16 | 412 | if not pen: start = True |
28 | 413 | if xs >= 0 and xs <= bed_max_x+0.1 and ys >= 0 and ys <= bed_max_y+0.1: |
16 | 414 | if start: |
23 | 415 | fo.write("G0 X%0.2f Y%0.2f F%.4f ; Move to start of shape\n" % ( |
416 | xs, ys, travel_speed)) | |
16 | 417 | start = False |
418 | xo = xs | |
21 | 419 | yo = ys |
420 | object_xs = xs | |
421 | object_ys = ys | |
16 | 422 | else: |
423 | e_distance = math.hypot(xs - xo, ys - yo) | |
424 | xo = xs | |
425 | yo = ys | |
19 | 426 | E = E + (e_distance) |
23 | 427 | fo.write("G1 X%0.2f Y%0.2f E%.4f F%.4f\n" % ( |
428 | xs, ys, E * E_FACTOR, engrave_speed)) | |
28 | 429 | else: |
430 | self.log("Position outside print dimension: %d, %d" % (xs, ys)) | |
27 | 431 | |
21 | 432 | if shape_obj.xml_node.get('fill'): |
433 | # Close the polygon | |
434 | e_distance = math.hypot(object_xs - xo, object_ys - yo) | |
435 | E = E + (e_distance) | |
23 | 436 | fo.write("G1 X%0.2f Y%0.2f E%.4f F%.4f ; Close the object polygon\n" % ( |
437 | object_xs, object_ys, E * E_FACTOR, engrave_speed)) | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
438 | print "connecting filled path end to start" |
16 | 439 | |
20 | 440 | fo.write(GCODE_FOOT) |
441 | fo.close() | |
16 | 442 | |
443 | if self.pronterwindow: | |
444 | self.log("") | |
445 | self.pronterwindow.load_gcode_async(filename + '.g') | |
446 |