printrun-src/printrun/gcoder.py

Wed, 20 Jan 2021 10:15:13 +0100

author
mdd
date
Wed, 20 Jan 2021 10:15:13 +0100
changeset 46
cce0af6351f0
parent 15
0bbb006204fc
permissions
-rwxr-xr-x

updated and added new files for printrun

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

mercurial