printrun-src/printrun/laser.py

Thu, 30 May 2019 19:02:52 +0200

author
mdd
date
Thu, 30 May 2019 19:02:52 +0200
changeset 42
ea4c43494a19
parent 37
926424f97c8e
child 43
f7e9bd735ce1
permissions
-rw-r--r--

bugfixes on rastered image column endings, force laser off with G0 + little offset

16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
1 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
2 Lasercutter library
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
3 2015-2017 by NeoSoft, Malte Di Donato
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
4 Intended to use standalone or implemented in Pronterface/Printrun
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
5 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
6
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
7 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
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
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
10 """
19
234037fbca4b Bugfixing, Added M400 magic
mbayer
parents: 18
diff changeset
11 E_FACTOR = 0.5
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
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
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
17
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
18 # GENERAL HEADER AND FOOTER GCODE
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
19 GCODE_HEAD = """
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
20 ; GCode generated by laser.py pronterface library (marlin code flavour)
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
21 ; 2015-2017 by NeoSoft - Malte Di Donato
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
22
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
23 G21 ; Metric
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
24 ; We assume Z is in focus height and laser head is focus at bottom left of image!
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
25 G92 X0 Y0 E0; set zero position - new origin
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
26 G90 ; absolute positioning
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
27 M82 ; Set extruder (laser) to absolute positioning
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
28 M201 X1000 Y1000 E1000 ; Set acceleration
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
29 M203 X1000 Y1000 Z4 E1000 ; Set max feedrate
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
30 M209 S0 ; disable firmware retraction, we dont want to burn holes...
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
31 M302 ; Allow cold extrudes - doesnt matter because we hack the extruder physically off with the M571 E mod
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
32 M571 S1 E1 ; Activate Laser output on extrusion, but block real motor movement!
35
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
33 ;M85 S0 ; Disable idle hold timeout (BUG!)
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
34 M84 ; enable motors
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
35
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
36 """
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
37
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
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
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
40 M42 P28 S0 ; Force laser off!
35
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
41 ;M85 S30 ; re-enable idle hold timeout (BUG!)
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
42 G0 X0 Y0 F%.4f ; Move back to origin
35
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
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
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
46
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
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
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
54 # 30mm/sec works for wood (regulate the output power to something between 10-30%)
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
55 # 30mm/sec for black anodized aluminum to get a light engraving @ 100% power
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
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
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
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
0e3e7fbf0bc6 Added more svg options to settings
mbayer
parents: 23
diff changeset
71 self.lc_svg_smoothness = 0.2
0e3e7fbf0bc6 Added more svg options to settings
mbayer
parents: 23
diff changeset
72 self.lc_svg_width = 50
0e3e7fbf0bc6 Added more svg options to settings
mbayer
parents: 23
diff changeset
73 self.lc_svg_height = 50
0e3e7fbf0bc6 Added more svg options to settings
mbayer
parents: 23
diff changeset
74 self.lc_svg_scalemode = "scale"
0e3e7fbf0bc6 Added more svg options to settings
mbayer
parents: 23
diff changeset
75
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
76
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
77 class Lasercutter:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
78 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
79 Lasercutter methods
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
80 parameters: log = logger function (fuction has to accept a string)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
81 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
82 def __init__(self, pronterwindow = None):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
83 if pronterwindow:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
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
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
86 #self.log = pronterwindow.log
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
87 self.log = self.log_print
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
88 self.pronterwindow.clear_log(None)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
89 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
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
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
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
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
99 self.log("Lasercutter library initialized\n%d DPI (%f mm/pixel)" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
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
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
102 self.log("WARNING: STEPS PER PIXEL NEEDS TO BE > 5 (otherwise marlin joins lines): %f" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
103 self.STEPS_PIXEL))
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
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
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
106 self.log("")
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
107
21
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
108 def log_print(self, msg):
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
109 print(msg)
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
110
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
111
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
112 def pixel2bit(self, pixel):
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
113 """Convert the pixel value to a bit."""
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
114 # some really weird stuff here ;-P
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
115
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
116 # RGB to greyscale
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
117 #print pixel
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
118 #print type(pixel)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
119 if isinstance(pixel, tuple):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
120 #rgb
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
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
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
123 return 1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
124 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
125 return 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
126
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
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
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
130 return 1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
131 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
132 return 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
133
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
134 def image2gcode(self, filename):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
135 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
136 Open a image file and get the basic information about it.
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
137 Then convert it to gcode (replacing the existing gcode buffer contents)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
138 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
139 try:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
140 im = Image.open(filename)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
141 except:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
142 self.log("Unable to open %s" % filename)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
143 return False
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
144
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
145 self.log("Converting Image for lasercut:")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
146 self.log("File: %s" % filename)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
147 self.log("format: %s, mode: %s" % (im.format, im.mode))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
148 width,height = im.size
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
149 self.log("size: %d x %d pixels" % im.size)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
150
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
151 pix = im.load()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
152
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
153 fo = open(filename + ".g", "w")
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
154 fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
155
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
156 fo.write(";Start engraving the raster image: %dx%d points @ %d DPI = %.0fx%.0f mm\n\n" % (
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
157 im.size[0], im.size[1], self.settings.lc_dpi,
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
158 im.size[0] * self.MM_PIXEL, im.size[1] * self.MM_PIXEL) )
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
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
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
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
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
164 for X in range(im.size[0]):
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
165 gcode_col = ""
42
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
166 first_ymm = None
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
167 first_xmm = None
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
168 E = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
169 last_bit = 1 # we engrave on black pixel = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
170 START_Y = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
171 if DIR > 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
172 range_start = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
173 range_stop = im.size[1]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
174 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
175 range_start = im.size[1] -1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
176 range_stop = -1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
177
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
178 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
179 YMM = abs((Y * self.MM_PIXEL) + INVERT_Y)
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
180 XMM = X * self.MM_PIXEL
42
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
181
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
182 #print "X %d Y %d" % (X, Y)
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
183 bit = self.pixel2bit(pix[X, Y])
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
184 if self.settings.lc_invert_cut:
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
185 if bit == 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
186 bit = 1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
187 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
188 bit = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
189 if last_bit == bit:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
190 if bit == 1:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
191 # nothing to do,
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
192 continue
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
193 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
194 # are we at the end of Y range?
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
195 #print Y
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
196 if (Y == (im.size[1] - 1)) or (Y == 0):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
197 # draw line
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
198 if DIR > 0:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
199 E = E + self.MM_PIXEL * (Y - START_Y)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
200 else:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
201 E = E + self.MM_PIXEL * (START_Y - Y)
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
202 gcode_col += "G1 X%.4f Y%.4f E%.4f F%.4f\n" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
203 XMM, YMM, E * E_FACTOR, engrave_speed)
42
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
204 if not first_xmm:
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
205 first_xmm = XMM + 0.1 # little offset needed!
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
206 first_ymm = YMM * 1
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
207 else:
42
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
208 if not first_xmm:
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
209 first_xmm = XMM + 0.1 # little offset needed!
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
210 first_ymm = YMM * 1
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
211 # bit value has changed!
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
212 if bit == 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
213 # jump to start of line to write
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
214 START_Y = Y
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
215 gcode_col += "G0 X%.4f Y%.4f F%.4f\n" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
216 XMM, YMM, travel_speed)
42
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
217 last_xmm = None
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
218 last_ymm = None
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
219 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
220 # end of line to write
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
221 if DIR > 0:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
222 E = E + (self.MM_PIXEL * (Y - START_Y))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
223 else:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
224 E = E + (self.MM_PIXEL * (START_Y - Y))
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
225 gcode_col += "G1 X%.4f Y%.4f E%.4f F%.4f\n" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
226 XMM, YMM, E * E_FACTOR, engrave_speed)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
227 last_bit = bit
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
228 if gcode_col <> "":
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
229 # we skip empty columns
42
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
230 # place last position as G0 to be sure to switch off laser immediately at finish of the line!
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
231 if first_xmm:
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
232 fo.write("G0 X%.4f Y%.4f F%.4f ; force laser off\n" % (
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
233 first_xmm, first_ymm, travel_speed))
ea4c43494a19 bugfixes on rastered image column endings, force laser off with G0 + little offset
mdd
parents: 37
diff changeset
234
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
235 fo.write("M400 ; X=%d printing row: direction %i\nG92 E0\n%s" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
236 X, DIR, gcode_col))
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
237 if self.settings.lc_change_dir:
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
238 DIR = DIR * (-1) # change y direction on every X
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
239
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
240 fo.write(GCODE_FOOT)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
241 fo.close()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
242
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
243 if self.pronterwindow:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
244 self.log("")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
245 self.pronterwindow.load_gcode_async(filename + '.g')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
246
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
247 def hpgl2gcode(self, filename):
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
248 # FOR HPGL:
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
249 SCALE_FACTOR = 1.0 / 40.0 # 40 plotter units
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
250 OFFSET_X = 0.0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
251 OFFSET_Y = 0.0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
252
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
253 self.log("Converting HPGL plot for lasercut:")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
254 self.log("File: %s" % filename)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
255
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
256 fi = open(filename, "r")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
257 fo = open(filename + ".g", "w")
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
258 fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
259
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
260 G = "0"
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
261 LASER_STATE = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
262 last_coord = [0.0,0.0]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
263 last_cmd = ""
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
264
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
265 travel_speed = self.settings.lc_travel_speed * 60
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
266 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
267
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
268
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
269 for line in fi.readlines():
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
270 for action in line.split(";"):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
271 action = action.strip()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
272 if action != "":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
273 cmd = action[:2]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
274 if cmd == "PD":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
275 LASER_STATE = 1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
276 elif cmd == "PU":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
277 LASER_STATE = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
278 if last_cmd == "PD":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
279 OFFSET_X = coord[0] * -1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
280 OFFSET_Y = coord[1] * -1
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
281 fo.write("; PD PU detected, set coord offset %.4f x %.4f mm\n" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
282 OFFSET_X, OFFSET_Y))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
283 elif cmd == "PA" or cmd == "PR":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
284 # TODO: convert relative coordinates to absolute here!
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
285 coord = action[2:].split(",")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
286 coord[0] = (float(coord[0]) + OFFSET_X) * SCALE_FACTOR
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
287 coord[1] = (float(coord[1]) + OFFSET_Y) * SCALE_FACTOR
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
288 if LASER_STATE:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
289 EN = " E%.4f F%.4f" % (
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
290 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
291 engrave_speed)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
292 else:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
293 EN = " F%.4f" % travel_speed
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
294
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
295 fo.write("G%d X%.4f Y%.4f%s\n" % (
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
296 LASER_STATE, coord[0], coord[1], EN) )
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
297 last_coord = coord
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
298 elif cmd == "IN":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
299 pass
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
300 elif cmd == "PT":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
301 print "Ignoring pen thickness"
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
302 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
303 print "UNKNOWN: %s" % action
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
304 last_cmd = cmd
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
305
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
306 fo.write(GCODE_FOOT)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
307 fi.close()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
308 fo.close()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
309
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
310 if self.pronterwindow:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
311 self.log("")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
312 self.pronterwindow.load_gcode_async(filename + '.g')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
313
25
0e3e7fbf0bc6 Added more svg options to settings
mbayer
parents: 23
diff changeset
314 def svg2gcode(self, filename):
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
315 # Imports for SVG
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
316 import xml.etree.ElementTree as ET
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
317 from svg2gcode import shapes as shapes_pkg
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
318 from svg2gcode.shapes import point_generator
35
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
319 from svg2gcode import simplepath, cspsubdiv, cubicsuperpath, simpletransform
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
320
36
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
321 bed_max_x = float(self.settings.lc_svg_width)
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
322 bed_max_y = float(self.settings.lc_svg_height)
18
11f6d97f83b0 forgot svg preamble & postamble
mbayer
parents: 16
diff changeset
323
25
0e3e7fbf0bc6 Added more svg options to settings
mbayer
parents: 23
diff changeset
324 self.log("Generating paths from SVG (outlines only)...")
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
325 svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path'])
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
326 tree = ET.parse(filename)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
327 root = tree.getroot()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
328
28
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
329 # Todo: force viewbox values configurable?
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
330 #width = root.get('width')
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
331 #height = root.get('height')
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
332 # if width == None or height == None:
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
333 viewbox = root.get('viewBox')
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
334 if viewbox:
35
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
335 _, _, width, height = viewbox.split()
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
336 else:
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
337 # no viewbox element, try to get from svg root element
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
338 try:
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
339 width = root.attrib["width"]
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
340 height = root.attrib["height"]
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
341 self.log("No ViewBox, got dimensions from root element)")
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
342 except:
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
343 width = None
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
344 height = None
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
345
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
346 if width == None or height == None:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
347 self.log("Unable to get width and height for the svg!")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
348 return False
28
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
349 else:
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
350 self.log("SVG Dimensions are %s x %s" % (width, height))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
351
27
c82c30811edd svg crowbar fixes
mbayer
parents: 25
diff changeset
352 # TODO: use cm or mm as absolute dimensions!
c82c30811edd svg crowbar fixes
mbayer
parents: 25
diff changeset
353 width = float(width.replace("px", "").replace("pt", "").replace("mm", ""))
c82c30811edd svg crowbar fixes
mbayer
parents: 25
diff changeset
354 height = float(height.replace("px", "").replace("pt", "").replace("mm", ""))
28
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
355
25
0e3e7fbf0bc6 Added more svg options to settings
mbayer
parents: 23
diff changeset
356 smoothness = self.settings.lc_svg_smoothness
0e3e7fbf0bc6 Added more svg options to settings
mbayer
parents: 23
diff changeset
357 if smoothness < 0.1: smoothness = 0.1
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
358
28
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
359 # get the minimum x and y values to get an offset to 0,0
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
360 ofs_x = 99999999999.0
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
361 ofs_y = 99999999999.0
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
362 max_x = -99999999999.0
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
363 max_y = -99999999999.0
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
364 for elem in root.iter():
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
365 try:
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
366 _, tag_suffix = elem.tag.split('}')
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
367 except ValueError:
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
368 continue
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
369 if tag_suffix in svg_shapes:
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
370 shape_class = getattr(shapes_pkg, tag_suffix)
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
371 shape_obj = shape_class(elem)
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
372 d = shape_obj.d_path()
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
373 m = shape_obj.transformation_matrix()
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
374
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
375 if d:
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
376 p = point_generator(d, m, smoothness)
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
377 start = True
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
378 for x,y,pen in p:
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
379 if x < ofs_x:
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
380 ofs_x = x
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
381 if y < ofs_y:
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
382 ofs_y = y
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
383 if x > max_x:
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
384 max_x = x
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
385 if y > max_y:
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
386 max_y = y
36
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
387
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
388 if self.settings.lc_svg_offset:
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
389 ofs_x *= -1
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
390 ofs_y *= -1
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
391 max_x += ofs_x
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
392 max_y += ofs_y
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
393 self.log("Calculated Offset to 0,0 is %f,%f" % (ofs_x, ofs_y))
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
394 else:
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
395 ofs_x = 0
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
396 ofs_y = 0
28
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
397
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
398 """
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
399 self.log("Calculated Dimension is %f,%f" % (max_x, max_y))
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
400 width = max_x
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
401 height = max_y
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
402 """
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
403
36
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
404 if self.settings.lc_svg_scalemode == "original":
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
405 scale_x = 1.0
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
406 scale_y = 1.0
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
407 elif self.settings.lc_svg_scalemode == "scale":
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
408 scale_x = bed_max_x / width
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
409 scale_y = bed_max_y / height
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
410 if (scale_x * height) > bed_max_y:
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
411 # use y scale
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
412 scale_x = scale_y
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
413 elif (scale_y * width) > bed_max_x:
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
414 # use x scale
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
415 scale_y = scale_x
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
416 # double-check
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
417 if (scale_x * width > bed_max_x) or (scale_y * height > bed_max_y):
f4730ef55ca8 SVG options: offset, original scale
mdd
parents: 35
diff changeset
418 scale_x = scale_y = min(bed_max_x, bed_max_y) / max(width, height)
25
0e3e7fbf0bc6 Added more svg options to settings
mbayer
parents: 23
diff changeset
419 else:
28
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
420 scale_x = bed_max_x / width
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
421 scale_y = bed_max_y / height
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
422
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
423 self.log("Scaling factor: %.2f, %.2f" % (scale_x,scale_y))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
424
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
425 fo = open(filename + ".g", "w")
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
426 fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
427 fo.write("M571 S0 E1 ; On SVG we control the laser by ourself\n")
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
428
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
429 travel_speed = self.settings.lc_travel_speed * 60
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
430 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
431
35
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
432 errors = 0
37
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
433 elemidx = 0
28
23efe2c53872 Bugfix SVG offset correction
mdd
parents: 27
diff changeset
434
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
435 for elem in root.iter():
37
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
436 elemidx += 1
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
437 try:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
438 _, tag_suffix = elem.tag.split('}')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
439 except ValueError:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
440 continue
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
441
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
442 if tag_suffix in svg_shapes:
37
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
443 try:
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
444 styles = elem.attrib['style'].split(';')
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
445 except KeyError:
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
446 styles = []
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
447 skip = False
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
448 for style in styles:
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
449 style = style.split(':')
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
450 if style[0] == 'stroke':
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
451 # ignore all stroke colors which are not #000000
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
452 if style[1] != "#000000":
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
453 self.log("Ignoring shape %s (%i) by stroke color" % (tag_suffix, elemidx))
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
454 skip = True
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
455 break
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
456 if skip:
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
457 continue
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
458
926424f97c8e SVG gcode generator: interpret color style if present
mdd
parents: 36
diff changeset
459 self.log("Parsing shape: %s (%i)" % (tag_suffix, elemidx))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
460 shape_class = getattr(shapes_pkg, tag_suffix)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
461 shape_obj = shape_class(elem)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
462 d = shape_obj.d_path()
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
463 mat = shape_obj.transformation_matrix()
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
464
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
465 if d:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
466 fo.write("M400 ; start %s\n" % (tag_suffix))
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
467 fo.write("G92 E0\n")
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
468 E = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
469 xo = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
470 yo = 0
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
471 idxo = None
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
472 #p = point_generator(d, mat, smoothness)
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
473
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
474 simple_path = simplepath.parsePath(d)
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
475 if len(simple_path) == 0:
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
476 self.log("Path length zero!")
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
477 continue
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
478
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
479 p = cubicsuperpath.parsePath(d)
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
480
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
481 if mat:
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
482 simpletransform.applyTransformToPath(mat, p)
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
483
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
484 for sp in p:
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
485 cspsubdiv.subdiv( sp, smoothness)
33
eee51ca7cbe7 Added support for multiple cutting passes with automatic Z refocusing
mdd
parents: 32
diff changeset
486 #self.log("Laser ON at: " + repr(sp[0][0]))
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
487 x = sp[0][0][0] + ofs_x
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
488 y = sp[0][0][1] + ofs_y
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
489 y = height - y # invert the bed
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
490 xs = scale_x * x
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
491 ys = scale_y * y
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
492 fo.write("M400 ; Wait for all moves to finish\n")
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
493 fo.write("M42 P28 S0 ; Turn off laser\n")
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
494 fo.write("G0 X%0.4f Y%0.4f F%.4f ; Move to start of shape\n" % (
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
495 xs, ys, travel_speed))
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
496 fo.write("M400 ; Wait for all moves to finish\n")
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
497 fo.write("M42 P28 S255 ; Turn on laser\n")
19
234037fbca4b Bugfixing, Added M400 magic
mbayer
parents: 18
diff changeset
498
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
499 xo = xs
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
500 yo = ys
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
501 object_xs = xs
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
502 object_ys = ys
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
503
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
504 for csp in sp:
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
505 ctrl_pt1 = csp[0]
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
506 ctrl_pt2 = csp[1]
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
507 end_pt = csp[2]
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
508
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
509 x = end_pt[0] + ofs_x
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
510 y = end_pt[1] + ofs_y
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
511
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
512 y = height - y # invert the bed
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
513 xs = round(scale_x * x, 4)
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
514 ys = round(scale_y * y, 4)
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
515 if xo == xs and yo == ys: continue
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
516
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
517 #self.log(" Point " + repr(end_pt))
35
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
518 e_distance = math.hypot(xs - xo, ys - yo)
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
519 xo = xs
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
520 yo = ys
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
521 E = E + (e_distance)
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
522
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
523 if xs >= 0 and xs <= bed_max_x+0.1 and ys >= 0 and ys <= bed_max_y+0.1:
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
524 fo.write("G1 X%0.4f Y%0.4f E%.4f F%.4f\n" % (
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
525 xs, ys, E * E_FACTOR, engrave_speed))
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
526 else:
35
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
527 fo.write("G0 X%0.4f Y%0.4f F%.4f\n" % (
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
528 xs, ys, travel_speed))
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
529 errors += 1
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
530 if errors < 10:
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
531 self.log("Position outside print dimension: %d, %d" % (xs, ys))
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
532
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
533 #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
534 #self.log("Laser OFF at: " + repr(sp[-1][-1]))
27
c82c30811edd svg crowbar fixes
mbayer
parents: 25
diff changeset
535
21
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
536 if shape_obj.xml_node.get('fill'):
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
537 # Close the polygon
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
538 e_distance = math.hypot(object_xs - xo, object_ys - yo)
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
539 E = E + (e_distance)
32
0abfa4642776 SVG bugfix: first shape line not drawn
mdd
parents: 28
diff changeset
540 fo.write("G1 X%0.4f Y%0.4f E%.4f F%.4f ; Close the object polygon\n" % (
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
541 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
542 print "connecting filled path end to start"
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
543
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
544 fo.write(GCODE_FOOT)
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
545 fo.close()
35
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
546
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
547 if errors > 0:
72c4cbf64211 Final bugfixes, SVG working like a charm now
mdd
parents: 33
diff changeset
548 self.log("%i errors while generating gcode" % errors)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
549
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
550 if self.pronterwindow:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
551 self.log("")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
552 self.pronterwindow.load_gcode_async(filename + '.g')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
553

mercurial