printrun-src/printrun/gcoder.py

Sat, 23 Sep 2017 10:27:49 +0200

author
mdd
date
Sat, 23 Sep 2017 10:27:49 +0200
changeset 34
654a41b13258
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Added more default options
Code cleanup

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2 # This file is copied from GCoder.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 # GCoder is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 # GCoder is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import re
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 import math
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20 import datetime
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 from array import array
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 gcode_parsed_args = ["x", "y", "e", "f", "z", "i", "j"]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 gcode_parsed_nonargs = ["g", "t", "m", "n"]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26 to_parse = "".join(gcode_parsed_args + gcode_parsed_nonargs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 gcode_exp = re.compile("\([^\(\)]*\)|;.*|[/\*].*\n|([%s])([-+]?[0-9]*\.?[0-9]*)" % to_parse)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 gcode_strip_comment_exp = re.compile("\([^\(\)]*\)|;.*|[/\*].*\n")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 m114_exp = re.compile("\([^\(\)]*\)|[/\*].*\n|([XYZ]):?([-+]?[0-9]*\.?[0-9]*)")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30 specific_exp = "(?:\([^\(\)]*\))|(?:;.*)|(?:[/\*].*\n)|(%s[-+]?[0-9]*\.?[0-9]*)"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 move_gcodes = ["G0", "G1", "G2", "G3"]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 class PyLine(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35 __slots__ = ('x', 'y', 'z', 'e', 'f', 'i', 'j',
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 'raw', 'command', 'is_move',
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 'relative', 'relative_e',
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 'current_x', 'current_y', 'current_z', 'extruding',
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 'current_tool',
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 'gcview_end_vertex')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 def __init__(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 self.raw = l
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 def __getattr__(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 class PyLightLine(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 __slots__ = ('raw', 'command')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 def __init__(self, l):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53 self.raw = l
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 def __getattr__(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58 try:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 import gcoder_line
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 Line = gcoder_line.GLine
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 LightLine = gcoder_line.GLightLine
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 except Exception, e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 logging.warning("Memory-efficient GCoder implementation unavailable: %s" % e)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 Line = PyLine
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 LightLine = PyLightLine
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 def find_specific_code(line, code):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68 exp = specific_exp % code
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 bits = [bit for bit in re.findall(exp, line.raw) if bit]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70 if not bits: return None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 else: return float(bits[0][1:])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 def S(line):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 return find_specific_code(line, "S")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 def P(line):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 return find_specific_code(line, "P")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 def split(line):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 split_raw = gcode_exp.findall(line.raw.lower())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 if split_raw and split_raw[0][0] == "n":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 del split_raw[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 if not split_raw:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 line.command = line.raw
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 line.is_move = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 logging.warning("raw G-Code line \"%s\" could not be parsed" % line.raw)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 return [line.raw]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 command = split_raw[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 line.command = command[0].upper() + command[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 line.is_move = line.command in move_gcodes
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 return split_raw
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 def parse_coordinates(line, split_raw, imperial = False, force = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 # Not a G-line, we don't want to parse its arguments
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 if not force and line.command[0] != "G":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 unit_factor = 25.4 if imperial else 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 for bit in split_raw:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99 code = bit[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 if code not in gcode_parsed_nonargs and bit[1]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 setattr(line, code, unit_factor * float(bit[1]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 class Layer(list):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 __slots__ = ("duration", "z")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107 def __init__(self, lines, z = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 super(Layer, self).__init__(lines)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 self.z = z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 class GCode(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 line_class = Line
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 lines = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 layers = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 all_layers = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 layer_idxs = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 line_idxs = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120 append_layer = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 append_layer_id = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 imperial = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 relative = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 relative_e = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126 current_tool = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 # Home position: current absolute position counted from machine origin
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 home_x = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 home_y = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 home_z = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 # Current position: current absolute position counted from machine origin
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 current_x = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 current_y = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134 current_z = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 # For E this is the absolute position from machine start
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 current_e = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 current_e_multi=[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 total_e = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 total_e_multi=[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 max_e = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 max_e_multi=[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142 # Current feedrate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 current_f = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 # Offset: current offset between the machine origin and the machine current
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 # absolute coordinate system (as shifted by G92s)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 offset_x = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 offset_y = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 offset_z = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 offset_e = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 offset_e_multi = [0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 # Expected behavior:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153 # - G28 X => X axis is homed, offset_x <- 0, current_x <- home_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 # - G92 Xk => X axis does not move, so current_x does not change
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 # and offset_x <- current_x - k,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 # - absolute G1 Xk => X axis moves, current_x <- offset_x + k
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157 # How to get...
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 # current abs X from machine origin: current_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 # current abs X in machine current coordinate system: current_x - offset_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 filament_length = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 filament_length_multi=[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 duration = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 xmin = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 xmax = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 ymin = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 ymax = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 zmin = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 zmax = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 width = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 depth = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 height = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 est_layer_height = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 # abs_x is the current absolute X in machine current coordinate system
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 # (after the various G92 transformations) and can be used to store the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 # absolute position of the head at a given time
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 def _get_abs_x(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 return self.current_x - self.offset_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 abs_x = property(_get_abs_x)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 def _get_abs_y(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 return self.current_y - self.offset_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 abs_y = property(_get_abs_y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 def _get_abs_z(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 return self.current_z - self.offset_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 abs_z = property(_get_abs_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 def _get_abs_e(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 return self.current_e - self.offset_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 abs_e = property(_get_abs_e)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 def _get_abs_e_multi(self,i):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 return self.current_e_multi[i] - self.offset_e_multi[i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 abs_e = property(_get_abs_e)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 def _get_abs_pos(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 return (self.abs_x, self.abs_y, self.abs_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 abs_pos = property(_get_abs_pos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 def _get_current_pos(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 return (self.current_x, self.current_y, self.current_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 current_pos = property(_get_current_pos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 def _get_home_pos(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 return (self.home_x, self.home_y, self.home_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 def _set_home_pos(self, home_pos):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 if home_pos:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 self.home_x, self.home_y, self.home_z = home_pos
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 home_pos = property(_get_home_pos, _set_home_pos)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 def _get_layers_count(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 return len(self.all_zs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 layers_count = property(_get_layers_count)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 def __init__(self, data = None, home_pos = None,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 layer_callback = None, deferred = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 if not deferred:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 self.prepare(data, home_pos, layer_callback)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 def prepare(self, data = None, home_pos = None, layer_callback = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 self.home_pos = home_pos
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 if data:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 line_class = self.line_class
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 self.lines = [line_class(l2) for l2 in
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 (l.strip() for l in data)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 if l2]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 self._preprocess(build_layers = True,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 layer_callback = layer_callback)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 self.lines = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 self.append_layer_id = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 self.append_layer = Layer([])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 self.all_layers = [self.append_layer]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 self.all_zs = set()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 self.layers = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240 self.layer_idxs = array('I', [])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 self.line_idxs = array('I', [])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 def __len__(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 return len(self.line_idxs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 def __iter__(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 return self.lines.__iter__()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 def prepend_to_layer(self, commands, layer_idx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 # Prepend commands in reverse order
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 commands = [c.strip() for c in commands[::-1] if c.strip()]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 layer = self.all_layers[layer_idx]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 # Find start index to append lines
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254 # and end index to append new indices
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255 start_index = self.layer_idxs.index(layer_idx)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 for i in range(start_index, len(self.layer_idxs)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 if self.layer_idxs[i] != layer_idx:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 end_index = i
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261 end_index = i + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 end_line = self.line_idxs[end_index - 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 for i, command in enumerate(commands):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 gline = Line(command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 # Split to get command
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 split(gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 # Force is_move to False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 gline.is_move = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 # Insert gline at beginning of layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 layer.insert(0, gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271 # Insert gline at beginning of list
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272 self.lines.insert(start_index, gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 # Update indices arrays & global gcodes list
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 self.layer_idxs.insert(end_index + i, layer_idx)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275 self.line_idxs.insert(end_index + i, end_line + i + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 return commands[::-1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 def rewrite_layer(self, commands, layer_idx):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 # Prepend commands in reverse order
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 commands = [c.strip() for c in commands[::-1] if c.strip()]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 layer = self.all_layers[layer_idx]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 # Find start index to append lines
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283 # and end index to append new indices
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 start_index = self.layer_idxs.index(layer_idx)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285 for i in range(start_index, len(self.layer_idxs)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 if self.layer_idxs[i] != layer_idx:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 end_index = i
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 end_index = i + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291 self.layer_idxs = self.layer_idxs[:start_index] + array('I', len(commands) * [layer_idx]) + self.layer_idxs[end_index:]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 self.line_idxs = self.line_idxs[:start_index] + array('I', range(len(commands))) + self.line_idxs[end_index:]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 del self.lines[start_index:end_index]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 del layer[:]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 for i, command in enumerate(commands):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296 gline = Line(command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297 # Split to get command
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 split(gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299 # Force is_move to False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300 gline.is_move = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
301 # Insert gline at beginning of layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 layer.insert(0, gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
303 # Insert gline at beginning of list
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
304 self.lines.insert(start_index, gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305 return commands[::-1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
306
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
307 def append(self, command, store = True):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
308 command = command.strip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
309 if not command:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
311 gline = Line(command)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 self._preprocess([gline])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313 if store:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
314 self.lines.append(gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 self.append_layer.append(gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316 self.layer_idxs.append(self.append_layer_id)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317 self.line_idxs.append(len(self.append_layer))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318 return gline
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320 def _preprocess(self, lines = None, build_layers = False,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321 layer_callback = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322 """Checks for imperial/relativeness settings and tool changes"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323 if not lines:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324 lines = self.lines
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325 imperial = self.imperial
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326 relative = self.relative
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 relative_e = self.relative_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 current_tool = self.current_tool
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 current_x = self.current_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 current_y = self.current_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331 current_z = self.current_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332 offset_x = self.offset_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333 offset_y = self.offset_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
334 offset_z = self.offset_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 # Extrusion computation
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 current_e = self.current_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338 offset_e = self.offset_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339 total_e = self.total_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 max_e = self.max_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342 current_e_multi = self.current_e_multi[current_tool]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343 offset_e_multi = self.offset_e_multi[current_tool]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344 total_e_multi = self.total_e_multi[current_tool]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345 max_e_multi = self.max_e_multi[current_tool]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 # Store this one out of the build_layers scope for efficiency
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 cur_layer_has_extrusion = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
349
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350 # Initialize layers and other global computations
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351 if build_layers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 # Bounding box computation
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 xmin = float("inf")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 ymin = float("inf")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355 zmin = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 xmax = float("-inf")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357 ymax = float("-inf")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
358 zmax = float("-inf")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 # Also compute extrusion-only values
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 xmin_e = float("inf")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 ymin_e = float("inf")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362 xmax_e = float("-inf")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363 ymax_e = float("-inf")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365 # Duration estimation
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366 # TODO:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367 # get device caps from firmware: max speed, acceleration/axis
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 # (including extruder)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369 # calculate the maximum move duration accounting for above ;)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370 lastx = lasty = lastz = laste = lastf = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 lastdx = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372 lastdy = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373 x = y = e = f = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374 currenttravel = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375 moveduration = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 totalduration = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377 acceleration = 2000.0 # mm/s^2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 layerbeginduration = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380 # Initialize layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 all_layers = self.all_layers = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382 all_zs = self.all_zs = set()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383 layer_idxs = self.layer_idxs = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 line_idxs = self.line_idxs = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 layer_id = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 layer_line = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389 last_layer_z = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390 prev_z = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391 prev_base_z = (None, None)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392 cur_z = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 cur_lines = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395 if self.line_class != Line:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 get_line = lambda l: Line(l.raw)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 get_line = lambda l: l
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399 for true_line in lines:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400 # # Parse line
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 # Use a heavy copy of the light line to preprocess
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402 line = get_line(true_line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403 split_raw = split(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404 if line.command:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 # Update properties
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 if line.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
407 line.relative = relative
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408 line.relative_e = relative_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409 line.current_tool = current_tool
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410 elif line.command == "G20":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411 imperial = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
412 elif line.command == "G21":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
413 imperial = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414 elif line.command == "G90":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415 relative = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 relative_e = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417 elif line.command == "G91":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 relative = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419 relative_e = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
420 elif line.command == "M82":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
421 relative_e = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
422 elif line.command == "M83":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 relative_e = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424 elif line.command[0] == "T":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 current_tool = int(line.command[1:])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426 while(current_tool+1>len(self.current_e_multi)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427 self.current_e_multi+=[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428 self.offset_e_multi+=[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429 self.total_e_multi+=[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 self.max_e_multi+=[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 current_e_multi = self.current_e_multi[current_tool]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
432 offset_e_multi = self.offset_e_multi[current_tool]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
433 total_e_multi = self.total_e_multi[current_tool]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 max_e_multi = self.max_e_multi[current_tool]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
437 if line.command[0] == "G":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438 parse_coordinates(line, split_raw, imperial)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
440 # Compute current position
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441 if line.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 x = line.x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
443 y = line.y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
444 z = line.z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
445
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
446 if line.f is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447 self.current_f = line.f
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
448
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
449 if line.relative:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450 x = current_x + (x or 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
451 y = current_y + (y or 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452 z = current_z + (z or 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
454 if x is not None: x = x + offset_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455 if y is not None: y = y + offset_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 if z is not None: z = z + offset_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
458 if x is not None: current_x = x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
459 if y is not None: current_y = y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
460 if z is not None: current_z = z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
461
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
462 elif line.command == "G28":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
463 home_all = not any([line.x, line.y, line.z])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
464 if home_all or line.x is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
465 offset_x = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
466 current_x = self.home_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
467 if home_all or line.y is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
468 offset_y = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
469 current_y = self.home_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
470 if home_all or line.z is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
471 offset_z = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
472 current_z = self.home_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
473
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
474 elif line.command == "G92":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
475 if line.x is not None: offset_x = current_x - line.x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
476 if line.y is not None: offset_y = current_y - line.y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
477 if line.z is not None: offset_z = current_z - line.z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
478
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
479 line.current_x = current_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
480 line.current_y = current_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
481 line.current_z = current_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
482
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
483 # # Process extrusion
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
484 if line.e is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
485 if line.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
486 if line.relative_e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
487 line.extruding = line.e > 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
488 total_e += line.e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
489 current_e += line.e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
490 total_e_multi += line.e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
491 current_e_multi += line.e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
492 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
493 new_e = line.e + offset_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
494 line.extruding = new_e > current_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
495 total_e += new_e - current_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
496 current_e = new_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
497 new_e_multi = line.e + offset_e_multi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
498 total_e_multi += new_e_multi - current_e_multi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
499 current_e_multi = new_e_multi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
500
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
501 max_e = max(max_e, total_e)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
502 max_e_multi=max(max_e_multi, total_e_multi)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
503 cur_layer_has_extrusion |= line.extruding
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
504 elif line.command == "G92":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
505 offset_e = current_e - line.e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
506 offset_e_multi = current_e_multi - line.e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
507
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
508 self.current_e_multi[current_tool]=current_e_multi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
509 self.offset_e_multi[current_tool]=offset_e_multi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
510 self.max_e_multi[current_tool]=max_e_multi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
511 self.total_e_multi[current_tool]=total_e_multi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
512
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
513 # # Create layers and perform global computations
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
514 if build_layers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
515 # Update bounding box
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
516 if line.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
517 if line.extruding:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
518 if line.current_x is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
519 xmin_e = min(xmin_e, line.current_x)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
520 xmax_e = max(xmax_e, line.current_x)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
521 if line.current_y is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
522 ymin_e = min(ymin_e, line.current_y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
523 ymax_e = max(ymax_e, line.current_y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
524 if max_e <= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
525 if line.current_x is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
526 xmin = min(xmin, line.current_x)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
527 xmax = max(xmax, line.current_x)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
528 if line.current_y is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
529 ymin = min(ymin, line.current_y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
530 ymax = max(ymax, line.current_y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
531
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
532 # Compute duration
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
533 if line.command == "G0" or line.command == "G1":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
534 x = line.x if line.x is not None else lastx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
535 y = line.y if line.y is not None else lasty
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
536 z = line.z if line.z is not None else lastz
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
537 e = line.e if line.e is not None else laste
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
538 # mm/s vs mm/m => divide by 60
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
539 f = line.f / 60.0 if line.f is not None else lastf
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
540
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
541 # given last feedrate and current feedrate calculate the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
542 # distance needed to achieve current feedrate.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
543 # if travel is longer than req'd distance, then subtract
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
544 # distance to achieve full speed, and add the time it took
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
545 # to get there.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
546 # then calculate the time taken to complete the remaining
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
547 # distance
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
548
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
549 # FIXME: this code has been proven to be super wrong when 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
550 # subsquent moves are in opposite directions, as requested
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
551 # speed is constant but printer has to fully decellerate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
552 # and reaccelerate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
553 # The following code tries to fix it by forcing a full
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
554 # reacceleration if this move is in the opposite direction
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
555 # of the previous one
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
556 dx = x - lastx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
557 dy = y - lasty
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
558 if dx * lastdx + dy * lastdy <= 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
559 lastf = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
560
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
561 currenttravel = math.hypot(dx, dy)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
562 if currenttravel == 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
563 if line.z is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
564 currenttravel = abs(line.z) if line.relative else abs(line.z - lastz)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
565 elif line.e is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
566 currenttravel = abs(line.e) if line.relative_e else abs(line.e - laste)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
567 # Feedrate hasn't changed, no acceleration/decceleration planned
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
568 if f == lastf:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
569 moveduration = currenttravel / f if f != 0 else 0.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
570 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
571 # FIXME: review this better
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
572 # this looks wrong : there's little chance that the feedrate we'll decelerate to is the previous feedrate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
573 # shouldn't we instead look at three consecutive moves ?
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
574 distance = 2 * abs(((lastf + f) * (f - lastf) * 0.5) / acceleration) # multiply by 2 because we have to accelerate and decelerate
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
575 if distance <= currenttravel and lastf + f != 0 and f != 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
576 moveduration = 2 * distance / (lastf + f) # This is distance / mean(lastf, f)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
577 moveduration += (currenttravel - distance) / f
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
578 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
579 moveduration = 2 * currenttravel / (lastf + f) # This is currenttravel / mean(lastf, f)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
580 # FIXME: probably a little bit optimistic, but probably a much better estimate than the previous one:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
581 # moveduration = math.sqrt(2 * distance / acceleration) # probably buggy : not taking actual travel into account
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
582
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
583 lastdx = dx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
584 lastdy = dy
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
585
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
586 totalduration += moveduration
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
587
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
588 lastx = x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
589 lasty = y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
590 lastz = z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
591 laste = e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
592 lastf = f
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
593 elif line.command == "G4":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
594 moveduration = P(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
595 if moveduration:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
596 moveduration /= 1000.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
597 totalduration += moveduration
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
598
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
599 # FIXME : looks like this needs to be tested with "lift Z on move"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
600 if line.z is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
601 if line.command == "G92":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
602 cur_z = line.z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
603 elif line.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
604 if line.relative and cur_z is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
605 cur_z += line.z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
606 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
607 cur_z = line.z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
608
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
609 # FIXME: the logic behind this code seems to work, but it might be
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
610 # broken
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
611 if cur_z != prev_z:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
612 if prev_z is not None and last_layer_z is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
613 offset = self.est_layer_height if self.est_layer_height else 0.01
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
614 if abs(prev_z - last_layer_z) < offset:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
615 if self.est_layer_height is None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
616 zs = sorted([l.z for l in all_layers if l.z is not None])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
617 heights = [round(zs[i + 1] - zs[i], 3) for i in range(len(zs) - 1)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
618 heights = [height for height in heights if height]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
619 if len(heights) >= 2: self.est_layer_height = heights[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
620 elif heights: self.est_layer_height = heights[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
621 else: self.est_layer_height = 0.1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
622 base_z = round(prev_z - (prev_z % self.est_layer_height), 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
623 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
624 base_z = round(prev_z, 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
625 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
626 base_z = prev_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
627
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
628 if base_z != prev_base_z:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
629 new_layer = Layer(cur_lines, base_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
630 new_layer.duration = totalduration - layerbeginduration
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
631 layerbeginduration = totalduration
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
632 all_layers.append(new_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
633 if cur_layer_has_extrusion and prev_z not in all_zs:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
634 all_zs.add(prev_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
635 cur_lines = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
636 cur_layer_has_extrusion = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
637 layer_id += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
638 layer_line = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
639 last_layer_z = base_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
640 if layer_callback is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
641 layer_callback(self, len(all_layers) - 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
642
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
643 prev_base_z = base_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
644
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
645 if build_layers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
646 cur_lines.append(true_line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
647 layer_idxs.append(layer_id)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
648 line_idxs.append(layer_line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
649 layer_line += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
650 prev_z = cur_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
651 # ## Loop done
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
652
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
653 # Store current status
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
654 self.imperial = imperial
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
655 self.relative = relative
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
656 self.relative_e = relative_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
657 self.current_tool = current_tool
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
658 self.current_x = current_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
659 self.current_y = current_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
660 self.current_z = current_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
661 self.offset_x = offset_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
662 self.offset_y = offset_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
663 self.offset_z = offset_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
664 self.current_e = current_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
665 self.offset_e = offset_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
666 self.max_e = max_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
667 self.total_e = total_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
668 self.current_e_multi[current_tool]=current_e_multi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
669 self.offset_e_multi[current_tool]=offset_e_multi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
670 self.max_e_multi[current_tool]=max_e_multi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
671 self.total_e_multi[current_tool]=total_e_multi
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
672
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
673
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
674 # Finalize layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
675 if build_layers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
676 if cur_lines:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
677 new_layer = Layer(cur_lines, prev_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
678 new_layer.duration = totalduration - layerbeginduration
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
679 layerbeginduration = totalduration
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
680 all_layers.append(new_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
681 if cur_layer_has_extrusion and prev_z not in all_zs:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
682 all_zs.add(prev_z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
683
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
684 self.append_layer_id = len(all_layers)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
685 self.append_layer = Layer([])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
686 self.append_layer.duration = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
687 all_layers.append(self.append_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
688 self.layer_idxs = array('I', layer_idxs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
689 self.line_idxs = array('I', line_idxs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
690
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
691 # Compute bounding box
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
692 all_zs = self.all_zs.union(set([zmin])).difference(set([None]))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
693 zmin = min(all_zs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
694 zmax = max(all_zs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
695
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
696 self.filament_length = self.max_e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
697 while len(self.filament_length_multi)<len(self.max_e_multi):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
698 self.filament_length_multi+=[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
699 for i in enumerate(self.max_e_multi):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
700 self.filament_length_multi[i[0]]=i[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
701
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
702
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
703 if self.filament_length > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
704 self.xmin = xmin_e if not math.isinf(xmin_e) else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
705 self.xmax = xmax_e if not math.isinf(xmax_e) else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
706 self.ymin = ymin_e if not math.isinf(ymin_e) else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
707 self.ymax = ymax_e if not math.isinf(ymax_e) else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
708 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
709 self.xmin = xmin if not math.isinf(xmin) else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
710 self.xmax = xmax if not math.isinf(xmax) else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
711 self.ymin = ymin if not math.isinf(ymin) else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
712 self.ymax = ymax if not math.isinf(ymax) else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
713 self.zmin = zmin if not math.isinf(zmin) else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
714 self.zmax = zmax if not math.isinf(zmax) else 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
715 self.width = self.xmax - self.xmin
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
716 self.depth = self.ymax - self.ymin
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
717 self.height = self.zmax - self.zmin
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
718
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
719 # Finalize duration
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
720 totaltime = datetime.timedelta(seconds = int(totalduration))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
721 self.duration = totaltime
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
722
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
723 def idxs(self, i):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
724 return self.layer_idxs[i], self.line_idxs[i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
725
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
726 def estimate_duration(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
727 return self.layers_count, self.duration
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
728
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
729 class LightGCode(GCode):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
730 line_class = LightLine
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
731
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
732 def main():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
733 if len(sys.argv) < 2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
734 print "usage: %s filename.gcode" % sys.argv[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
735 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
736
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
737 print "Line object size:", sys.getsizeof(Line("G0 X0"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
738 print "Light line object size:", sys.getsizeof(LightLine("G0 X0"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
739 gcode = GCode(open(sys.argv[1], "rU"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
740
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
741 print "Dimensions:"
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
742 xdims = (gcode.xmin, gcode.xmax, gcode.width)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
743 print "\tX: %0.02f - %0.02f (%0.02f)" % xdims
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
744 ydims = (gcode.ymin, gcode.ymax, gcode.depth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
745 print "\tY: %0.02f - %0.02f (%0.02f)" % ydims
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
746 zdims = (gcode.zmin, gcode.zmax, gcode.height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
747 print "\tZ: %0.02f - %0.02f (%0.02f)" % zdims
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
748 print "Filament used: %0.02fmm" % gcode.filament_length
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
749 for i in enumerate(gcode.filament_length_multi):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
750 print "E%d %0.02fmm" % (i[0],i[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
751 print "Number of layers: %d" % gcode.layers_count
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
752 print "Estimated duration: %s" % gcode.estimate_duration()[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
753
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
754 if __name__ == '__main__':
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
755 main()

mercurial