Tue, 26 Sep 2017 19:08:02 +0200
SVG options: offset, original scale
SVG Bugfix: "scale" does now really fit to dimensions
16 | 1 | """ |
2 | Lasercutter library | |
32 | 3 | 2015-2017 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) | |
32 | 21 | ; 2015-2017 by NeoSoft - Malte Di Donato |
20 | 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! | |
35 | 33 | ;M85 S0 ; Disable idle hold timeout (BUG!) |
32 | 34 | M84 ; enable motors |
35 | ||
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
36 | """ |
20 | 37 | |
32 | 38 | GCODE_FOOT = """ |
33
eee51ca7cbe7
Added support for multiple cutting passes with automatic Z refocusing
mdd
parents:
32
diff
changeset
|
39 | M400 ; Wait for all moves to finish |
32 | 40 | M42 P28 S0 ; Force laser off! |
35 | 41 | ;M85 S30 ; re-enable idle hold timeout (BUG!) |
32 | 42 | G0 X0 Y0 F%.4f ; Move back to origin |
35 | 43 | M571 S0 E0 ; disable extruder firmware hack |
33
eee51ca7cbe7
Added support for multiple cutting passes with automatic Z refocusing
mdd
parents:
32
diff
changeset
|
44 | ; M501 ; undo all settings made |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
45 | """ % (100*60) |
16 | 46 | |
47 | ||
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
48 | class LasercutterSettings: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
49 | """ |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
50 | Default settings object |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
51 | """ |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
52 | def __init__(self): |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
53 | self.lc_engrave_speed = 10 |
23 | 54 | # 30mm/sec works for wood (regulate the output power to something between 10-30%) |
55 | # 30mm/sec for black anodized aluminum to get a light engraving @ 100% power | |
56 | # 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
|
57 | self.lc_travel_speed = 120 |
16 | 58 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
59 | # BITMAP: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
60 | self.lc_bitmap_speed_factor = 1.0 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
61 | self.lc_dpi = 300 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
62 | self.lc_grey_threshold = 0 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
63 | self.lc_change_dir = True |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
64 | self.lc_invert_cut = True |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
65 | |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
66 | # HPGL: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
67 | self.lc_hpgl_speed_factor = 1.0 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
68 | |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
69 | # SVG: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
70 | self.lc_svg_speed_factor = 1.0 |
25 | 71 | self.lc_svg_smoothness = 0.2 |
72 | self.lc_svg_width = 50 | |
73 | self.lc_svg_height = 50 | |
74 | self.lc_svg_scalemode = "scale" | |
75 | ||
16 | 76 | |
77 | class Lasercutter: | |
78 | """ | |
79 | Lasercutter methods | |
20 | 80 | parameters: log = logger function (fuction has to accept a string) |
16 | 81 | """ |
82 | def __init__(self, pronterwindow = None): | |
83 | if pronterwindow: | |
84 | self.pronterwindow = pronterwindow | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
85 | self.settings = pronterwindow.settings |
21 | 86 | #self.log = pronterwindow.log |
87 | self.log = self.log_print | |
20 | 88 | self.pronterwindow.clear_log(None) |
16 | 89 | else: |
90 | self.pronterwindow = None | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
91 | self.settings = LasercutterSettings() |
16 | 92 | self.log = lambda : None |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
93 | |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
94 | # STATIC DEFINITIONS, DO NOT CHANGE WORLD's RULES! |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
95 | self.INCH = 25.4 # mm |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
96 | 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
|
97 | 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
|
98 | |
23 | 99 | self.log("Lasercutter library initialized\n%d DPI (%f mm/pixel)" % ( |
100 | self.settings.lc_dpi, self.MM_PIXEL)) | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
101 | if self.STEPS_PIXEL <= 5: |
23 | 102 | self.log("WARNING: STEPS PER PIXEL NEEDS TO BE > 5 (otherwise marlin joins lines): %f" % ( |
103 | self.STEPS_PIXEL)) | |
20 | 104 | 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
|
105 | self.settings.lc_travel_speed, self.settings.lc_engrave_speed) ) |
20 | 106 | self.log("") |
16 | 107 | |
21 | 108 | def log_print(self, msg): |
109 | print(msg) | |
110 | ||
16 | 111 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
112 | def pixel2bit(self, pixel): |
16 | 113 | """Convert the pixel value to a bit.""" |
114 | # some really weird stuff here ;-P | |
115 | ||
116 | # RGB to greyscale | |
117 | #print pixel | |
118 | #print type(pixel) | |
119 | if isinstance(pixel, tuple): | |
120 | #rgb | |
121 | 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
|
122 | if pixel > self.settings.lc_grey_threshold: |
16 | 123 | return 1 |
124 | else: | |
125 | return 0 | |
126 | ||
127 | # color palette | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
128 | # 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
|
129 | if pixel <= self.settings.lc_grey_threshold: |
16 | 130 | return 1 |
131 | else: | |
132 | return 0 | |
133 | ||
134 | def image2gcode(self, filename): | |
135 | """ | |
136 | Open a image file and get the basic information about it. | |
137 | Then convert it to gcode (replacing the existing gcode buffer contents) | |
138 | """ | |
139 | try: | |
140 | im = Image.open(filename) | |
141 | except: | |
142 | self.log("Unable to open %s" % filename) | |
143 | return False | |
144 | ||
145 | self.log("Converting Image for lasercut:") | |
146 | self.log("File: %s" % filename) | |
147 | self.log("format: %s, mode: %s" % (im.format, im.mode)) | |
148 | width,height = im.size | |
149 | self.log("size: %d x %d pixels" % im.size) | |
150 | ||
151 | pix = im.load() | |
152 | ||
153 | fo = open(filename + ".g", "w") | |
20 | 154 | fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD)) |
16 | 155 | |
156 | fo.write(";Start engraving the raster image: %dx%d points @ %d DPI = %.0fx%.0f mm\n\n" % ( | |
23 | 157 | im.size[0], im.size[1], self.settings.lc_dpi, |
158 | im.size[0] * self.MM_PIXEL, im.size[1] * self.MM_PIXEL) ) | |
16 | 159 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
160 | INVERT_Y = self.MM_PIXEL * (im.size[1] -1) * (-1) |
16 | 161 | DIR = 1 |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
162 | travel_speed = self.settings.lc_travel_speed * 60 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
163 | engrave_speed = self.settings.lc_engrave_speed * 60 * self.settings.lc_bitmap_speed_factor |
16 | 164 | for X in range(im.size[0]): |
23 | 165 | gcode_col = "" |
16 | 166 | E = 0 |
167 | last_bit = 1 # we engrave on black pixel = 0 | |
168 | START_Y = 0 | |
169 | if DIR > 0: | |
170 | range_start = 0 | |
171 | range_stop = im.size[1] | |
172 | else: | |
173 | range_start = im.size[1] -1 | |
174 | range_stop = -1 | |
175 | ||
176 | 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
|
177 | YMM = abs((Y * self.MM_PIXEL) + INVERT_Y) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
178 | XMM = X * self.MM_PIXEL |
16 | 179 | #print "X %d Y %d" % (X, Y) |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
180 | bit = self.pixel2bit(pix[X, Y]) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
181 | if self.settings.lc_invert_cut: |
16 | 182 | if bit == 0: |
183 | bit = 1 | |
184 | else: | |
185 | bit = 0 | |
186 | if last_bit == bit: | |
187 | if bit == 1: | |
188 | # nothing to do, | |
189 | continue | |
190 | else: | |
191 | # are we at the end of Y range? | |
192 | #print Y | |
193 | if (Y == (im.size[1] - 1)) or (Y == 0): | |
194 | # draw line | |
195 | if DIR > 0: | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
196 | E = E + self.MM_PIXEL * (Y - START_Y) |
16 | 197 | else: |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
198 | E = E + self.MM_PIXEL * (START_Y - Y) |
23 | 199 | gcode_col += "G1 X%.4f Y%.4f E%.4f F%.4f\n" % ( |
200 | XMM, YMM, E * E_FACTOR, engrave_speed) | |
16 | 201 | else: |
202 | # bit value has changed! | |
203 | if bit == 0: | |
204 | # jump to start of line to write | |
205 | START_Y = Y | |
23 | 206 | gcode_col += "G0 X%.4f Y%.4f F%.4f\n" % ( |
207 | XMM, YMM, travel_speed) | |
16 | 208 | else: |
209 | # end of line to write | |
210 | if DIR > 0: | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
211 | E = E + (self.MM_PIXEL * (Y - START_Y)) |
16 | 212 | else: |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
213 | E = E + (self.MM_PIXEL * (START_Y - Y)) |
23 | 214 | gcode_col += "G1 X%.4f Y%.4f E%.4f F%.4f\n" % ( |
215 | XMM, YMM, E * E_FACTOR, engrave_speed) | |
16 | 216 | last_bit = bit |
23 | 217 | if gcode_col <> "": |
218 | # we skip empty columns | |
219 | fo.write("M400 ; X=%d printing row: direction %i\nG92 E0\n%s" % ( | |
220 | X, DIR, gcode_col)) | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
221 | if self.settings.lc_change_dir: |
16 | 222 | DIR = DIR * (-1) # change y direction on every X |
223 | ||
20 | 224 | fo.write(GCODE_FOOT) |
16 | 225 | fo.close() |
226 | ||
227 | if self.pronterwindow: | |
228 | self.log("") | |
229 | self.pronterwindow.load_gcode_async(filename + '.g') | |
230 | ||
231 | def hpgl2gcode(self, filename): | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
232 | # FOR HPGL: |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
233 | SCALE_FACTOR = 1.0 / 40.0 # 40 plotter units |
16 | 234 | OFFSET_X = 0.0 |
235 | OFFSET_Y = 0.0 | |
236 | ||
237 | self.log("Converting HPGL plot for lasercut:") | |
238 | self.log("File: %s" % filename) | |
239 | ||
240 | fi = open(filename, "r") | |
241 | fo = open(filename + ".g", "w") | |
20 | 242 | fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD)) |
16 | 243 | |
244 | G = "0" | |
245 | LASER_STATE = 0 | |
246 | last_coord = [0.0,0.0] | |
247 | last_cmd = "" | |
248 | ||
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
249 | travel_speed = self.settings.lc_travel_speed * 60 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
250 | 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
|
251 | |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
252 | |
16 | 253 | for line in fi.readlines(): |
254 | for action in line.split(";"): | |
255 | action = action.strip() | |
256 | if action != "": | |
257 | cmd = action[:2] | |
258 | if cmd == "PD": | |
259 | LASER_STATE = 1 | |
260 | elif cmd == "PU": | |
261 | LASER_STATE = 0 | |
262 | if last_cmd == "PD": | |
263 | OFFSET_X = coord[0] * -1 | |
264 | OFFSET_Y = coord[1] * -1 | |
23 | 265 | fo.write("; PD PU detected, set coord offset %.4f x %.4f mm\n" % ( |
266 | OFFSET_X, OFFSET_Y)) | |
16 | 267 | elif cmd == "PA" or cmd == "PR": |
268 | # TODO: convert relative coordinates to absolute here! | |
269 | coord = action[2:].split(",") | |
270 | coord[0] = (float(coord[0]) + OFFSET_X) * SCALE_FACTOR | |
271 | coord[1] = (float(coord[1]) + OFFSET_Y) * SCALE_FACTOR | |
272 | if LASER_STATE: | |
273 | EN = " E%.4f F%.4f" % ( | |
274 | 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
|
275 | engrave_speed) |
16 | 276 | else: |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
277 | EN = " F%.4f" % travel_speed |
16 | 278 | |
279 | fo.write("G%d X%.4f Y%.4f%s\n" % ( | |
280 | LASER_STATE, coord[0], coord[1], EN) ) | |
281 | last_coord = coord | |
282 | elif cmd == "IN": | |
283 | pass | |
284 | elif cmd == "PT": | |
285 | print "Ignoring pen thickness" | |
286 | else: | |
287 | print "UNKNOWN: %s" % action | |
288 | last_cmd = cmd | |
289 | ||
20 | 290 | fo.write(GCODE_FOOT) |
16 | 291 | fi.close() |
292 | fo.close() | |
293 | ||
294 | if self.pronterwindow: | |
295 | self.log("") | |
296 | self.pronterwindow.load_gcode_async(filename + '.g') | |
297 | ||
25 | 298 | def svg2gcode(self, filename): |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
299 | # Imports for SVG |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
300 | import xml.etree.ElementTree as ET |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
301 | from svg2gcode import shapes as shapes_pkg |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
302 | from svg2gcode.shapes import point_generator |
35 | 303 | from svg2gcode import simplepath, cspsubdiv, cubicsuperpath, simpletransform |
304 | ||
36 | 305 | bed_max_x = float(self.settings.lc_svg_width) |
306 | bed_max_y = float(self.settings.lc_svg_height) | |
18 | 307 | |
25 | 308 | self.log("Generating paths from SVG (outlines only)...") |
16 | 309 | svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path']) |
310 | tree = ET.parse(filename) | |
311 | root = tree.getroot() | |
312 | ||
28 | 313 | # Todo: force viewbox values configurable? |
314 | #width = root.get('width') | |
315 | #height = root.get('height') | |
316 | # if width == None or height == None: | |
317 | viewbox = root.get('viewBox') | |
318 | if viewbox: | |
35 | 319 | _, _, width, height = viewbox.split() |
320 | else: | |
321 | # no viewbox element, try to get from svg root element | |
322 | try: | |
323 | width = root.attrib["width"] | |
324 | height = root.attrib["height"] | |
325 | self.log("No ViewBox, got dimensions from root element)") | |
326 | except: | |
327 | width = None | |
328 | height = None | |
16 | 329 | |
330 | if width == None or height == None: | |
331 | self.log("Unable to get width and height for the svg!") | |
332 | return False | |
28 | 333 | else: |
334 | self.log("SVG Dimensions are %s x %s" % (width, height)) | |
16 | 335 | |
27 | 336 | # TODO: use cm or mm as absolute dimensions! |
337 | width = float(width.replace("px", "").replace("pt", "").replace("mm", "")) | |
338 | height = float(height.replace("px", "").replace("pt", "").replace("mm", "")) | |
28 | 339 | |
25 | 340 | smoothness = self.settings.lc_svg_smoothness |
341 | if smoothness < 0.1: smoothness = 0.1 | |
16 | 342 | |
28 | 343 | # get the minimum x and y values to get an offset to 0,0 |
344 | ofs_x = 99999999999.0 | |
345 | ofs_y = 99999999999.0 | |
346 | max_x = -99999999999.0 | |
347 | max_y = -99999999999.0 | |
348 | for elem in root.iter(): | |
349 | try: | |
350 | _, tag_suffix = elem.tag.split('}') | |
351 | except ValueError: | |
352 | continue | |
353 | if tag_suffix in svg_shapes: | |
354 | shape_class = getattr(shapes_pkg, tag_suffix) | |
355 | shape_obj = shape_class(elem) | |
356 | d = shape_obj.d_path() | |
357 | m = shape_obj.transformation_matrix() | |
358 | ||
359 | if d: | |
360 | p = point_generator(d, m, smoothness) | |
361 | start = True | |
362 | for x,y,pen in p: | |
363 | if x < ofs_x: | |
364 | ofs_x = x | |
365 | if y < ofs_y: | |
366 | ofs_y = y | |
367 | if x > max_x: | |
368 | max_x = x | |
369 | if y > max_y: | |
370 | max_y = y | |
36 | 371 | |
372 | if self.settings.lc_svg_offset: | |
373 | ofs_x *= -1 | |
374 | ofs_y *= -1 | |
375 | max_x += ofs_x | |
376 | max_y += ofs_y | |
377 | self.log("Calculated Offset to 0,0 is %f,%f" % (ofs_x, ofs_y)) | |
378 | else: | |
379 | ofs_x = 0 | |
380 | ofs_y = 0 | |
28 | 381 | |
382 | """ | |
383 | self.log("Calculated Dimension is %f,%f" % (max_x, max_y)) | |
384 | width = max_x | |
385 | height = max_y | |
386 | """ | |
387 | ||
36 | 388 | if self.settings.lc_svg_scalemode == "original": |
389 | scale_x = 1.0 | |
390 | scale_y = 1.0 | |
391 | elif self.settings.lc_svg_scalemode == "scale": | |
392 | scale_x = bed_max_x / width | |
393 | scale_y = bed_max_y / height | |
394 | if (scale_x * height) > bed_max_y: | |
395 | # use y scale | |
396 | scale_x = scale_y | |
397 | elif (scale_y * width) > bed_max_x: | |
398 | # use x scale | |
399 | scale_y = scale_x | |
400 | # double-check | |
401 | if (scale_x * width > bed_max_x) or (scale_y * height > bed_max_y): | |
402 | scale_x = scale_y = min(bed_max_x, bed_max_y) / max(width, height) | |
25 | 403 | else: |
28 | 404 | scale_x = bed_max_x / width |
405 | scale_y = bed_max_y / height | |
16 | 406 | |
407 | self.log("Scaling factor: %.2f, %.2f" % (scale_x,scale_y)) | |
408 | ||
409 | fo = open(filename + ".g", "w") | |
20 | 410 | fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD)) |
32 | 411 | fo.write("M571 S0 E1 ; On SVG we control the laser by ourself\n") |
16 | 412 | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
413 | travel_speed = self.settings.lc_travel_speed * 60 |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
414 | 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
|
415 | |
35 | 416 | errors = 0 |
28 | 417 | |
16 | 418 | for elem in root.iter(): |
419 | try: | |
420 | _, tag_suffix = elem.tag.split('}') | |
421 | except ValueError: | |
422 | continue | |
423 | ||
424 | if tag_suffix in svg_shapes: | |
28 | 425 | self.log("Parsing shape: %s" % tag_suffix) |
16 | 426 | shape_class = getattr(shapes_pkg, tag_suffix) |
427 | shape_obj = shape_class(elem) | |
428 | d = shape_obj.d_path() | |
32 | 429 | mat = shape_obj.transformation_matrix() |
16 | 430 | |
431 | if d: | |
22
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
432 | fo.write("M400 ; start %s\n" % (tag_suffix)) |
4c9bb8f93ae8
Added the Lasercut settings to the pronterface options dialog
mbayer
parents:
21
diff
changeset
|
433 | fo.write("G92 E0\n") |
16 | 434 | E = 0 |
435 | xo = 0 | |
436 | yo = 0 | |
32 | 437 | idxo = None |
438 | #p = point_generator(d, mat, smoothness) | |
439 | ||
440 | simple_path = simplepath.parsePath(d) | |
441 | if len(simple_path) == 0: | |
442 | self.log("Path length zero!") | |
443 | continue | |
444 | ||
445 | p = cubicsuperpath.parsePath(d) | |
446 | ||
447 | if mat: | |
448 | simpletransform.applyTransformToPath(mat, p) | |
449 | ||
450 | for sp in p: | |
451 | cspsubdiv.subdiv( sp, smoothness) | |
33
eee51ca7cbe7
Added support for multiple cutting passes with automatic Z refocusing
mdd
parents:
32
diff
changeset
|
452 | #self.log("Laser ON at: " + repr(sp[0][0])) |
32 | 453 | x = sp[0][0][0] + ofs_x |
454 | y = sp[0][0][1] + ofs_y | |
455 | y = height - y # invert the bed | |
456 | xs = scale_x * x | |
457 | ys = scale_y * y | |
458 | fo.write("M400 ; Wait for all moves to finish\n") | |
459 | fo.write("M42 P28 S0 ; Turn off laser\n") | |
460 | fo.write("G0 X%0.4f Y%0.4f F%.4f ; Move to start of shape\n" % ( | |
461 | xs, ys, travel_speed)) | |
462 | fo.write("M400 ; Wait for all moves to finish\n") | |
463 | fo.write("M42 P28 S255 ; Turn on laser\n") | |
19 | 464 | |
32 | 465 | xo = xs |
466 | yo = ys | |
467 | object_xs = xs | |
468 | object_ys = ys | |
469 | ||
470 | for csp in sp: | |
471 | ctrl_pt1 = csp[0] | |
472 | ctrl_pt2 = csp[1] | |
473 | end_pt = csp[2] | |
474 | ||
475 | x = end_pt[0] + ofs_x | |
476 | y = end_pt[1] + ofs_y | |
477 | ||
478 | y = height - y # invert the bed | |
479 | xs = round(scale_x * x, 4) | |
480 | ys = round(scale_y * y, 4) | |
481 | if xo == xs and yo == ys: continue | |
482 | ||
483 | #self.log(" Point " + repr(end_pt)) | |
35 | 484 | e_distance = math.hypot(xs - xo, ys - yo) |
485 | xo = xs | |
486 | yo = ys | |
487 | E = E + (e_distance) | |
32 | 488 | |
489 | if xs >= 0 and xs <= bed_max_x+0.1 and ys >= 0 and ys <= bed_max_y+0.1: | |
490 | fo.write("G1 X%0.4f Y%0.4f E%.4f F%.4f\n" % ( | |
491 | xs, ys, E * E_FACTOR, engrave_speed)) | |
492 | else: | |
35 | 493 | fo.write("G0 X%0.4f Y%0.4f F%.4f\n" % ( |
494 | xs, ys, travel_speed)) | |
495 | errors += 1 | |
496 | if errors < 10: | |
497 | self.log("Position outside print dimension: %d, %d" % (xs, ys)) | |
32 | 498 | |
499 | #print " Point: ", end_pt[0], end_pt[1], pen | |
33
eee51ca7cbe7
Added support for multiple cutting passes with automatic Z refocusing
mdd
parents:
32
diff
changeset
|
500 | #self.log("Laser OFF at: " + repr(sp[-1][-1])) |
27 | 501 | |
21 | 502 | if shape_obj.xml_node.get('fill'): |
503 | # Close the polygon | |
504 | e_distance = math.hypot(object_xs - xo, object_ys - yo) | |
505 | E = E + (e_distance) | |
32 | 506 | fo.write("G1 X%0.4f Y%0.4f E%.4f F%.4f ; Close the object polygon\n" % ( |
23 | 507 | 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
|
508 | print "connecting filled path end to start" |
16 | 509 | |
20 | 510 | fo.write(GCODE_FOOT) |
511 | fo.close() | |
35 | 512 | |
513 | if errors > 0: | |
514 | self.log("%i errors while generating gcode" % errors) | |
16 | 515 | |
516 | if self.pronterwindow: | |
517 | self.log("") | |
518 | self.pronterwindow.load_gcode_async(filename + '.g') | |
519 |