printrun-src/printrun/laser.py

Sat, 04 Jun 2016 13:07:59 +0200

author
mbayer
date
Sat, 04 Jun 2016 13:07:59 +0200
changeset 24
620f9cd7ee9d
parent 23
e18b2a4ef561
child 25
0e3e7fbf0bc6
permissions
-rw-r--r--

Etikett BITMAP_FINISHED für den Änderungssatz e18b2a4ef561 ergänzt

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
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
3 2015/2016 by NeoSoft, Malte Bayer
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)
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
21 ; 2015/2016 by NeoSoft - Malte Bayer
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!
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
33 """
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
34
21
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
35 GCODE_FOOT = """G0 X0 Y0 F%.4f
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
36 M400 ; Wait for all moves to finish
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
37 M571 S0 E0
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
38 M42 P28 S0 ; Force laser off!
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
39 M501 ; undo all settings made
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
40 """ % (100*60)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
41
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
42
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
43 class LasercutterSettings:
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
44 """
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
45 Default settings object
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
46 """
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
47 def __init__(self):
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
48 self.lc_engrave_speed = 10
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
49 # 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
50 # 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
51 # 10mm/sec for black anodized aluminum to get maximum possible engraving! @ 100% power
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
52 self.lc_travel_speed = 120
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
53
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
54 # BITMAP:
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
55 self.lc_bitmap_speed_factor = 1.0
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
56 self.lc_dpi = 300
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
57 self.lc_grey_threshold = 0
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
58 self.lc_change_dir = True
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
59 self.lc_invert_cut = True
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
60
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
61 # HPGL:
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
62 self.lc_hpgl_speed_factor = 1.0
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
63
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
64 # SVG:
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
65 self.lc_svg_speed_factor = 1.0
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
66
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
67 class Lasercutter:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
68 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
69 Lasercutter methods
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
70 parameters: log = logger function (fuction has to accept a string)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
71 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
72 def __init__(self, pronterwindow = None):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
73 if pronterwindow:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
74 self.pronterwindow = pronterwindow
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
75 self.settings = pronterwindow.settings
21
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
76 #self.log = pronterwindow.log
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
77 self.log = self.log_print
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
78 self.pronterwindow.clear_log(None)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
79 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
80 self.pronterwindow = None
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
81 self.settings = LasercutterSettings()
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
82 self.log = lambda : None
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
83
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
84 # STATIC DEFINITIONS, DO NOT CHANGE WORLD's RULES!
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
85 self.INCH = 25.4 # mm
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
86 self.MM_PIXEL = round(self.INCH / self.settings.lc_dpi, 4)
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
87 self.STEPS_PIXEL = self.MM_PIXEL * 80 # mine is 80 steps/mm on XY
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
88
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
89 self.log("Lasercutter library initialized\n%d DPI (%f mm/pixel)" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
90 self.settings.lc_dpi, self.MM_PIXEL))
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
91 if self.STEPS_PIXEL <= 5:
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
92 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
93 self.STEPS_PIXEL))
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
94 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
95 self.settings.lc_travel_speed, self.settings.lc_engrave_speed) )
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
96 self.log("")
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
97
21
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
98 def log_print(self, msg):
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
99 print(msg)
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
100
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
101
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
102 def pixel2bit(self, pixel):
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
103 """Convert the pixel value to a bit."""
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
104 # some really weird stuff here ;-P
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
105
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
106 # RGB to greyscale
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
107 #print pixel
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
108 #print type(pixel)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
109 if isinstance(pixel, tuple):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
110 #rgb
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
111 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
112 if pixel > self.settings.lc_grey_threshold:
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
113 return 1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
114 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
115 return 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
116
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
117 # color palette
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
118 # 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
119 if pixel <= self.settings.lc_grey_threshold:
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
120 return 1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
121 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
122 return 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
123
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
124 def image2gcode(self, filename):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
125 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
126 Open a image file and get the basic information about it.
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
127 Then convert it to gcode (replacing the existing gcode buffer contents)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
128 """
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
129 try:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
130 im = Image.open(filename)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
131 except:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
132 self.log("Unable to open %s" % filename)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
133 return False
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
134
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
135 self.log("Converting Image for lasercut:")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
136 self.log("File: %s" % filename)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
137 self.log("format: %s, mode: %s" % (im.format, im.mode))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
138 width,height = im.size
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
139 self.log("size: %d x %d pixels" % im.size)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
140
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
141 pix = im.load()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
142
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
143 fo = open(filename + ".g", "w")
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
144 fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
145
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
146 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
147 im.size[0], im.size[1], self.settings.lc_dpi,
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
148 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
149
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
150 INVERT_Y = self.MM_PIXEL * (im.size[1] -1) * (-1)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
151 DIR = 1
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
152 travel_speed = self.settings.lc_travel_speed * 60
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
153 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
154 for X in range(im.size[0]):
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
155 gcode_col = ""
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
156 E = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
157 last_bit = 1 # we engrave on black pixel = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
158 START_Y = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
159 if DIR > 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
160 range_start = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
161 range_stop = im.size[1]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
162 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
163 range_start = im.size[1] -1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
164 range_stop = -1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
165
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
166 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
167 YMM = abs((Y * self.MM_PIXEL) + INVERT_Y)
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
168 XMM = X * self.MM_PIXEL
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
169 #print "X %d Y %d" % (X, Y)
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
170 bit = self.pixel2bit(pix[X, Y])
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
171 if self.settings.lc_invert_cut:
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
172 if bit == 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
173 bit = 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 bit = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
176 if last_bit == bit:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
177 if bit == 1:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
178 # nothing to do,
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
179 continue
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
180 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
181 # are we at the end of Y range?
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
182 #print Y
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
183 if (Y == (im.size[1] - 1)) or (Y == 0):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
184 # draw line
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
185 if DIR > 0:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
186 E = E + self.MM_PIXEL * (Y - START_Y)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
187 else:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
188 E = E + self.MM_PIXEL * (START_Y - Y)
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
189 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
190 XMM, YMM, E * E_FACTOR, engrave_speed)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
191 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
192 # bit value has changed!
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
193 if bit == 0:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
194 # jump to start of line to write
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
195 START_Y = Y
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
196 gcode_col += "G0 X%.4f Y%.4f F%.4f\n" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
197 XMM, YMM, travel_speed)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
198 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
199 # end of line to write
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
200 if DIR > 0:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
201 E = E + (self.MM_PIXEL * (Y - START_Y))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
202 else:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
203 E = E + (self.MM_PIXEL * (START_Y - Y))
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
204 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
205 XMM, YMM, E * E_FACTOR, engrave_speed)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
206 last_bit = bit
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
207 if gcode_col <> "":
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
208 # we skip empty columns
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
209 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
210 X, DIR, gcode_col))
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
211 if self.settings.lc_change_dir:
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
212 DIR = DIR * (-1) # change y direction on every X
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
213
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
214 fo.write(GCODE_FOOT)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
215 fo.close()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
216
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
217 if self.pronterwindow:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
218 self.log("")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
219 self.pronterwindow.load_gcode_async(filename + '.g')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
220
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
221 def hpgl2gcode(self, filename):
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
222 # FOR HPGL:
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
223 SCALE_FACTOR = 1.0 / 40.0 # 40 plotter units
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
224 OFFSET_X = 0.0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
225 OFFSET_Y = 0.0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
226
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
227 self.log("Converting HPGL plot for lasercut:")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
228 self.log("File: %s" % filename)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
229
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
230 fi = open(filename, "r")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
231 fo = open(filename + ".g", "w")
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
232 fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
233
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
234 G = "0"
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
235 LASER_STATE = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
236 last_coord = [0.0,0.0]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
237 last_cmd = ""
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
238
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
239 travel_speed = self.settings.lc_travel_speed * 60
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
240 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
241
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
242
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
243 for line in fi.readlines():
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
244 for action in line.split(";"):
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
245 action = action.strip()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
246 if action != "":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
247 cmd = action[:2]
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
248 if cmd == "PD":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
249 LASER_STATE = 1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
250 elif cmd == "PU":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
251 LASER_STATE = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
252 if last_cmd == "PD":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
253 OFFSET_X = coord[0] * -1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
254 OFFSET_Y = coord[1] * -1
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
255 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
256 OFFSET_X, OFFSET_Y))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
257 elif cmd == "PA" or cmd == "PR":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
258 # TODO: convert relative coordinates to absolute here!
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
259 coord = action[2:].split(",")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
260 coord[0] = (float(coord[0]) + OFFSET_X) * SCALE_FACTOR
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
261 coord[1] = (float(coord[1]) + OFFSET_Y) * SCALE_FACTOR
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
262 if LASER_STATE:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
263 EN = " E%.4f F%.4f" % (
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
264 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
265 engrave_speed)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
266 else:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
267 EN = " F%.4f" % travel_speed
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
268
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
269 fo.write("G%d X%.4f Y%.4f%s\n" % (
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
270 LASER_STATE, coord[0], coord[1], EN) )
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
271 last_coord = coord
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
272 elif cmd == "IN":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
273 pass
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
274 elif cmd == "PT":
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
275 print "Ignoring pen thickness"
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
276 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
277 print "UNKNOWN: %s" % action
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
278 last_cmd = cmd
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
279
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
280 fo.write(GCODE_FOOT)
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
281 fi.close()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
282 fo.close()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
283
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
284 if self.pronterwindow:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
285 self.log("")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
286 self.pronterwindow.load_gcode_async(filename + '.g')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
287
19
234037fbca4b Bugfixing, Added M400 magic
mbayer
parents: 18
diff changeset
288 def svg2gcode(self, filename, bed_max_x = 50, bed_max_y = 50, smoothness = 0.2):
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
289 # Imports for SVG
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
290 import xml.etree.ElementTree as ET
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
291 from svg2gcode import shapes as shapes_pkg
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
292 from svg2gcode.shapes import point_generator
18
11f6d97f83b0 forgot svg preamble & postamble
mbayer
parents: 16
diff changeset
293
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
294 self.log("Generating paths from SVG, alternative lib (outlines only)...")
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
295 if smoothness < 0.1: smoothness = 0.1
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
296 svg_shapes = set(['rect', 'circle', 'ellipse', 'line', 'polyline', 'polygon', 'path'])
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
297 tree = ET.parse(filename)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
298 root = tree.getroot()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
299
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
300 width = root.get('width')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
301 height = root.get('height')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
302 if width == None or height == None:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
303 viewbox = root.get('viewBox')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
304 if viewbox:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
305 _, _, width, height = viewbox.split()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
306
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
307 if width == None or height == None:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
308 self.log("Unable to get width and height for the svg!")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
309 return False
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
310
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
311 width = float(width.replace("px", "").replace("pt", ""))
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
312 height = float(height.replace("px", "").replace("pt", ""))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
313
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
314 scale_x = bed_max_x / max(width, height)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
315 scale_y = bed_max_y / max(width, height)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
316
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
317 self.log("Scaling factor: %.2f, %.2f" % (scale_x,scale_y))
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
318
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
319 fo = open(filename + ".g", "w")
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
320 fo.write("; Filename: %s\n%s" % (filename, GCODE_HEAD))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
321
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
322 travel_speed = self.settings.lc_travel_speed * 60
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
323 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
324
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
325 for elem in root.iter():
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
326 try:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
327 _, tag_suffix = elem.tag.split('}')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
328 except ValueError:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
329 continue
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
330
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
331 if tag_suffix in svg_shapes:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
332 shape_class = getattr(shapes_pkg, tag_suffix)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
333 shape_obj = shape_class(elem)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
334 d = shape_obj.d_path()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
335 m = shape_obj.transformation_matrix()
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
336
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
337 if d:
22
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
338 fo.write("M400 ; start %s\n" % (tag_suffix))
4c9bb8f93ae8 Added the Lasercut settings to the pronterface options dialog
mbayer
parents: 21
diff changeset
339 fo.write("G92 E0\n")
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
340 E = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
341 xo = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
342 yo = 0
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
343 p = point_generator(d, m, smoothness)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
344 start = True
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
345 for x,y,pen in p:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
346 y = height - y
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
347 xs = scale_x * x
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
348 ys = scale_y * y
19
234037fbca4b Bugfixing, Added M400 magic
mbayer
parents: 18
diff changeset
349 if xo == xs and yo == ys: continue
234037fbca4b Bugfixing, Added M400 magic
mbayer
parents: 18
diff changeset
350
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
351 if not pen: start = True
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
352 if xs >= 0 and xs <= bed_max_x and ys >= 0 and ys <= bed_max_y:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
353 if start:
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
354 fo.write("G0 X%0.2f Y%0.2f F%.4f ; Move to start of shape\n" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
355 xs, ys, travel_speed))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
356 start = False
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
357 xo = xs
21
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
358 yo = ys
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
359 object_xs = xs
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
360 object_ys = ys
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
361 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
362 e_distance = math.hypot(xs - xo, ys - yo)
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
363 xo = xs
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
364 yo = ys
19
234037fbca4b Bugfixing, Added M400 magic
mbayer
parents: 18
diff changeset
365 E = E + (e_distance)
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
366 fo.write("G1 X%0.2f Y%0.2f E%.4f F%.4f\n" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
367 xs, ys, E * E_FACTOR, engrave_speed))
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
368 else:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
369 self.log("Position outside print dimension: %d, %d" % (xs, ys))
21
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
370 if shape_obj.xml_node.get('fill'):
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
371 # Close the polygon
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
372 e_distance = math.hypot(object_xs - xo, object_ys - yo)
8551b89bd05e more fixes before trying to finish svg compiler
mbayer
parents: 20
diff changeset
373 E = E + (e_distance)
23
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
374 fo.write("G1 X%0.2f Y%0.2f E%.4f F%.4f ; Close the object polygon\n" % (
e18b2a4ef561 Remove empty lines output on bitmap plotter
mbayer
parents: 22
diff changeset
375 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
376 print "connecting filled path end to start"
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
377
20
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
378 fo.write(GCODE_FOOT)
03b34402d405 Code cleanup
mbayer
parents: 19
diff changeset
379 fo.close()
16
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
380
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
381 if self.pronterwindow:
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
382 self.log("")
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
383 self.pronterwindow.load_gcode_async(filename + '.g')
36d478bde840 Implemented svg, png and hpgl compilers to pronterface
mbayer
parents:
diff changeset
384

mercurial