Sat, 04 Jun 2016 13:06:30 +0200
Remove empty lines output on bitmap plotter
15 | 1 | # This file is part of the Printrun suite. |
2 | # | |
3 | # Printrun is free software: you can redistribute it and/or modify | |
4 | # it under the terms of the GNU General Public License as published by | |
5 | # the Free Software Foundation, either version 3 of the License, or | |
6 | # (at your option) any later version. | |
7 | # | |
8 | # Printrun is distributed in the hope that it will be useful, | |
9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | # GNU General Public License for more details. | |
12 | # | |
13 | # You should have received a copy of the GNU General Public License | |
14 | # along with Printrun. If not, see <http://www.gnu.org/licenses/>. | |
15 | ||
16 | from Queue import Queue | |
17 | from collections import deque | |
18 | import numpy | |
19 | import wx | |
20 | import time | |
21 | from . import gcoder | |
22 | from .injectgcode import injector, injector_edit | |
23 | ||
24 | from .utils import imagefile, install_locale, get_home_pos | |
25 | install_locale('pronterface') | |
26 | ||
27 | class GvizBaseFrame(wx.Frame): | |
28 | ||
29 | def create_base_ui(self): | |
30 | self.CreateStatusBar(1) | |
31 | self.SetStatusText(_("Layer number and Z position show here when you scroll")) | |
32 | ||
33 | hpanel = wx.Panel(self, -1) | |
34 | hbox = wx.BoxSizer(wx.HORIZONTAL) | |
35 | ||
36 | panel = wx.Panel(hpanel, -1) | |
37 | vbox = wx.BoxSizer(wx.VERTICAL) | |
38 | ||
39 | vbox = wx.BoxSizer(wx.VERTICAL) | |
40 | self.toolbar = wx.ToolBar(panel, -1, style = wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_HORZ_TEXT) | |
41 | self.toolbar.AddSimpleTool(1, wx.Image(imagefile('zoom_in.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), _("Zoom In [+]"), '') | |
42 | self.toolbar.AddSimpleTool(2, wx.Image(imagefile('zoom_out.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), _("Zoom Out [-]"), '') | |
43 | self.toolbar.AddSeparator() | |
44 | self.toolbar.AddSimpleTool(3, wx.Image(imagefile('arrow_up.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), _("Move Up a Layer [U]"), '') | |
45 | self.toolbar.AddSimpleTool(4, wx.Image(imagefile('arrow_down.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), _("Move Down a Layer [D]"), '') | |
46 | self.toolbar.AddLabelTool(5, " " + _("Reset view"), wx.Image(imagefile('reset.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), shortHelp = _("Reset view"), longHelp = '') | |
47 | self.toolbar.AddSeparator() | |
48 | self.toolbar.AddSimpleTool(6, wx.Image(imagefile('inject.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), shortHelpString = _("Inject G-Code"), longHelpString = _("Insert code at the beginning of this layer")) | |
49 | self.toolbar.AddSimpleTool(7, wx.Image(imagefile('edit.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), shortHelpString = _("Edit layer"), longHelpString = _("Edit the G-Code of this layer")) | |
50 | ||
51 | vbox.Add(self.toolbar, 0, border = 5) | |
52 | ||
53 | panel.SetSizer(vbox) | |
54 | ||
55 | hbox.Add(panel, 1, flag = wx.EXPAND) | |
56 | self.layerslider = wx.Slider(hpanel, style = wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LEFT | wx.SL_INVERSE) | |
57 | self.layerslider.Bind(wx.EVT_SCROLL, self.process_slider) | |
58 | hbox.Add(self.layerslider, 0, border = 5, flag = wx.LEFT | wx.EXPAND) | |
59 | hpanel.SetSizer(hbox) | |
60 | ||
61 | return panel, vbox | |
62 | ||
63 | def setlayercb(self, layer): | |
64 | self.layerslider.SetValue(layer) | |
65 | ||
66 | def process_slider(self, event): | |
67 | raise NotImplementedError | |
68 | ||
69 | ID_ABOUT = 101 | |
70 | ID_EXIT = 110 | |
71 | class GvizWindow(GvizBaseFrame): | |
72 | def __init__(self, f = None, size = (600, 600), build_dimensions = [200, 200, 100, 0, 0, 0], grid = (10, 50), extrusion_width = 0.5, bgcolor = "#000000"): | |
73 | super(GvizWindow, self).__init__(None, title = _("Gcode view, shift to move view, mousewheel to set layer"), size = size) | |
74 | ||
75 | panel, vbox = self.create_base_ui() | |
76 | ||
77 | self.p = Gviz(panel, size = size, build_dimensions = build_dimensions, grid = grid, extrusion_width = extrusion_width, bgcolor = bgcolor, realparent = self) | |
78 | ||
79 | self.toolbar.Realize() | |
80 | vbox.Add(self.p, 1, wx.EXPAND) | |
81 | ||
82 | self.SetMinSize(self.ClientToWindowSize(vbox.GetMinSize())) | |
83 | self.Bind(wx.EVT_TOOL, lambda x: self.p.zoom(-1, -1, 1.2), id = 1) | |
84 | self.Bind(wx.EVT_TOOL, lambda x: self.p.zoom(-1, -1, 1 / 1.2), id = 2) | |
85 | self.Bind(wx.EVT_TOOL, lambda x: self.p.layerup(), id = 3) | |
86 | self.Bind(wx.EVT_TOOL, lambda x: self.p.layerdown(), id = 4) | |
87 | self.Bind(wx.EVT_TOOL, self.resetview, id = 5) | |
88 | self.Bind(wx.EVT_TOOL, lambda x: self.p.inject(), id = 6) | |
89 | self.Bind(wx.EVT_TOOL, lambda x: self.p.editlayer(), id = 7) | |
90 | ||
91 | self.initpos = None | |
92 | self.p.Bind(wx.EVT_KEY_DOWN, self.key) | |
93 | self.Bind(wx.EVT_KEY_DOWN, self.key) | |
94 | self.p.Bind(wx.EVT_MOUSEWHEEL, self.zoom) | |
95 | self.Bind(wx.EVT_MOUSEWHEEL, self.zoom) | |
96 | self.p.Bind(wx.EVT_MOUSE_EVENTS, self.mouse) | |
97 | self.Bind(wx.EVT_MOUSE_EVENTS, self.mouse) | |
98 | ||
99 | if f: | |
100 | gcode = gcoder.GCode(f, get_home_pos(self.p.build_dimensions)) | |
101 | self.p.addfile(gcode) | |
102 | ||
103 | def set_current_gline(self, gline): | |
104 | return | |
105 | ||
106 | def process_slider(self, event): | |
107 | self.p.layerindex = self.layerslider.GetValue() | |
108 | z = self.p.get_currentz() | |
109 | wx.CallAfter(self.SetStatusText, _("Layer %d - Z = %.03f mm") % (self.p.layerindex + 1, z), 0) | |
110 | self.p.dirty = True | |
111 | wx.CallAfter(self.p.Refresh) | |
112 | ||
113 | def resetview(self, event): | |
114 | self.p.translate = [0.0, 0.0] | |
115 | self.p.scale = self.p.basescale | |
116 | self.p.zoom(0, 0, 1.0) | |
117 | ||
118 | def mouse(self, event): | |
119 | if event.ButtonUp(wx.MOUSE_BTN_LEFT) or event.ButtonUp(wx.MOUSE_BTN_RIGHT): | |
120 | if self.initpos is not None: | |
121 | self.initpos = None | |
122 | elif event.Dragging(): | |
123 | e = event.GetPositionTuple() | |
124 | if self.initpos is None: | |
125 | self.initpos = e | |
126 | self.basetrans = self.p.translate | |
127 | self.p.translate = [self.basetrans[0] + (e[0] - self.initpos[0]), | |
128 | self.basetrans[1] + (e[1] - self.initpos[1])] | |
129 | self.p.dirty = True | |
130 | wx.CallAfter(self.p.Refresh) | |
131 | else: | |
132 | event.Skip() | |
133 | ||
134 | def key(self, event): | |
135 | # Keycode definitions | |
136 | kup = [85, 315] # Up keys | |
137 | kdo = [68, 317] # Down Keys | |
138 | kzi = [388, 316, 61] # Zoom In Keys | |
139 | kzo = [390, 314, 45] # Zoom Out Keys | |
140 | x = event.GetKeyCode() | |
141 | cx, cy = self.p.translate | |
142 | if x in kup: | |
143 | self.p.layerup() | |
144 | if x in kdo: | |
145 | self.p.layerdown() | |
146 | if x in kzi: | |
147 | self.p.zoom(cx, cy, 1.2) | |
148 | if x in kzo: | |
149 | self.p.zoom(cx, cy, 1 / 1.2) | |
150 | ||
151 | def zoom(self, event): | |
152 | z = event.GetWheelRotation() | |
153 | if event.ShiftDown(): | |
154 | if z > 0: self.p.layerdown() | |
155 | elif z < 0: self.p.layerup() | |
156 | else: | |
157 | if z > 0: self.p.zoom(event.GetX(), event.GetY(), 1.2) | |
158 | elif z < 0: self.p.zoom(event.GetX(), event.GetY(), 1 / 1.2) | |
159 | ||
160 | class Gviz(wx.Panel): | |
161 | ||
162 | # Mark canvas as dirty when setting showall | |
163 | _showall = 0 | |
164 | ||
165 | def _get_showall(self): | |
166 | return self._showall | |
167 | ||
168 | def _set_showall(self, showall): | |
169 | if showall != self._showall: | |
170 | self.dirty = True | |
171 | self._showall = showall | |
172 | showall = property(_get_showall, _set_showall) | |
173 | ||
174 | def __init__(self, parent, size = (200, 200), build_dimensions = [200, 200, 100, 0, 0, 0], grid = (10, 50), extrusion_width = 0.5, bgcolor = "#000000", realparent = None): | |
175 | wx.Panel.__init__(self, parent, -1) | |
176 | self.widget = self | |
177 | size = [max(1.0, x) for x in size] | |
178 | ratio = size[0] / size[1] | |
179 | self.SetMinSize((150, 150 / ratio)) | |
180 | self.parent = realparent if realparent else parent | |
181 | self.size = size | |
182 | self.build_dimensions = build_dimensions | |
183 | self.grid = grid | |
184 | self.Bind(wx.EVT_PAINT, self.paint) | |
185 | self.Bind(wx.EVT_SIZE, self.resize) | |
186 | self.hilight = deque() | |
187 | self.hilightarcs = deque() | |
188 | self.hilightqueue = Queue(0) | |
189 | self.hilightarcsqueue = Queue(0) | |
190 | self.clear() | |
191 | self.filament_width = extrusion_width # set it to 0 to disable scaling lines with zoom | |
192 | self.update_basescale() | |
193 | self.scale = self.basescale | |
194 | penwidth = max(1.0, self.filament_width * ((self.scale[0] + self.scale[1]) / 2.0)) | |
195 | self.translate = [0.0, 0.0] | |
196 | self.mainpen = wx.Pen(wx.Colour(0, 0, 0), penwidth) | |
197 | self.arcpen = wx.Pen(wx.Colour(255, 0, 0), penwidth) | |
198 | self.travelpen = wx.Pen(wx.Colour(10, 80, 80), penwidth) | |
199 | self.hlpen = wx.Pen(wx.Colour(200, 50, 50), penwidth) | |
200 | self.fades = [wx.Pen(wx.Colour(250 - 0.6 ** i * 100, 250 - 0.6 ** i * 100, 200 - 0.4 ** i * 50), penwidth) for i in xrange(6)] | |
201 | self.penslist = [self.mainpen, self.travelpen, self.hlpen] + self.fades | |
202 | self.bgcolor = wx.Colour() | |
203 | self.bgcolor.SetFromName(bgcolor) | |
204 | self.blitmap = wx.EmptyBitmap(self.GetClientSize()[0], self.GetClientSize()[1], -1) | |
205 | self.paint_overlay = None | |
206 | ||
207 | def inject(self): | |
208 | layer = self.layers.index(self.layerindex) | |
209 | injector(self.gcode, self.layerindex, layer) | |
210 | ||
211 | def editlayer(self): | |
212 | layer = self.layers.index(self.layerindex) | |
213 | injector_edit(self.gcode, self.layerindex, layer) | |
214 | ||
215 | def clearhilights(self): | |
216 | self.hilight.clear() | |
217 | self.hilightarcs.clear() | |
218 | while not self.hilightqueue.empty(): | |
219 | self.hilightqueue.get_nowait() | |
220 | while not self.hilightarcsqueue.empty(): | |
221 | self.hilightarcsqueue.get_nowait() | |
222 | ||
223 | def clear(self): | |
224 | self.gcode = None | |
225 | self.lastpos = [0, 0, 0, 0, 0, 0, 0] | |
226 | self.hilightpos = self.lastpos[:] | |
227 | self.lines = {} | |
228 | self.pens = {} | |
229 | self.arcs = {} | |
230 | self.arcpens = {} | |
231 | self.layers = {} | |
232 | self.layersz = [] | |
233 | self.clearhilights() | |
234 | self.layerindex = 0 | |
235 | self.showall = 0 | |
236 | self.dirty = True | |
237 | self.partial = False | |
238 | self.painted_layers = set() | |
239 | wx.CallAfter(self.Refresh) | |
240 | ||
241 | def get_currentz(self): | |
242 | z = self.layersz[self.layerindex] | |
243 | z = 0. if z is None else z | |
244 | return z | |
245 | ||
246 | def layerup(self): | |
247 | if self.layerindex + 1 < len(self.layers): | |
248 | self.layerindex += 1 | |
249 | z = self.get_currentz() | |
250 | wx.CallAfter(self.parent.SetStatusText, _("Layer %d - Going Up - Z = %.03f mm") % (self.layerindex + 1, z), 0) | |
251 | self.dirty = True | |
252 | self.parent.setlayercb(self.layerindex) | |
253 | wx.CallAfter(self.Refresh) | |
254 | ||
255 | def layerdown(self): | |
256 | if self.layerindex > 0: | |
257 | self.layerindex -= 1 | |
258 | z = self.get_currentz() | |
259 | wx.CallAfter(self.parent.SetStatusText, _("Layer %d - Going Down - Z = %.03f mm") % (self.layerindex + 1, z), 0) | |
260 | self.dirty = True | |
261 | self.parent.setlayercb(self.layerindex) | |
262 | wx.CallAfter(self.Refresh) | |
263 | ||
264 | def setlayer(self, layer): | |
265 | if layer in self.layers: | |
266 | self.clearhilights() | |
267 | self.layerindex = self.layers[layer] | |
268 | self.dirty = True | |
269 | self.showall = 0 | |
270 | wx.CallAfter(self.Refresh) | |
271 | ||
272 | def update_basescale(self): | |
273 | self.basescale = 2 * [min(float(self.size[0] - 1) / self.build_dimensions[0], | |
274 | float(self.size[1] - 1) / self.build_dimensions[1])] | |
275 | ||
276 | def resize(self, event): | |
277 | old_basescale = self.basescale | |
278 | width, height = self.GetClientSizeTuple() | |
279 | if width < 1 or height < 1: | |
280 | return | |
281 | self.size = (width, height) | |
282 | self.update_basescale() | |
283 | zoomratio = float(self.basescale[0]) / old_basescale[0] | |
284 | wx.CallLater(200, self.zoom, 0, 0, zoomratio) | |
285 | ||
286 | def zoom(self, x, y, factor): | |
287 | if x == -1 and y == -1: | |
288 | side = min(self.size) | |
289 | x = y = side / 2 | |
290 | self.scale = [s * factor for s in self.scale] | |
291 | ||
292 | self.translate = [x - (x - self.translate[0]) * factor, | |
293 | y - (y - self.translate[1]) * factor] | |
294 | penwidth = max(1.0, self.filament_width * ((self.scale[0] + self.scale[1]) / 2.0)) | |
295 | for pen in self.penslist: | |
296 | pen.SetWidth(penwidth) | |
297 | self.dirty = True | |
298 | wx.CallAfter(self.Refresh) | |
299 | ||
300 | def _line_scaler(self, x): | |
301 | return (self.scale[0] * x[0], | |
302 | self.scale[1] * x[1], | |
303 | self.scale[0] * x[2], | |
304 | self.scale[1] * x[3],) | |
305 | ||
306 | def _arc_scaler(self, x): | |
307 | return (self.scale[0] * x[0], | |
308 | self.scale[1] * x[1], | |
309 | self.scale[0] * x[2], | |
310 | self.scale[1] * x[3], | |
311 | self.scale[0] * x[4], | |
312 | self.scale[1] * x[5],) | |
313 | ||
314 | def _drawlines(self, dc, lines, pens): | |
315 | scaled_lines = map(self._line_scaler, lines) | |
316 | dc.DrawLineList(scaled_lines, pens) | |
317 | ||
318 | def _drawarcs(self, dc, arcs, pens): | |
319 | scaled_arcs = map(self._arc_scaler, arcs) | |
320 | dc.SetBrush(wx.TRANSPARENT_BRUSH) | |
321 | for i in range(len(scaled_arcs)): | |
322 | dc.SetPen(pens[i] if type(pens) == list else pens) | |
323 | dc.DrawArc(*scaled_arcs[i]) | |
324 | ||
325 | def repaint_everything(self): | |
326 | width = self.scale[0] * self.build_dimensions[0] | |
327 | height = self.scale[1] * self.build_dimensions[1] | |
328 | self.blitmap = wx.EmptyBitmap(width + 1, height + 1, -1) | |
329 | dc = wx.MemoryDC() | |
330 | dc.SelectObject(self.blitmap) | |
331 | dc.SetBackground(wx.Brush((250, 250, 200))) | |
332 | dc.Clear() | |
333 | dc.SetPen(wx.Pen(wx.Colour(180, 180, 150))) | |
334 | for grid_unit in self.grid: | |
335 | if grid_unit > 0: | |
336 | for x in xrange(int(self.build_dimensions[0] / grid_unit) + 1): | |
337 | draw_x = self.scale[0] * x * grid_unit | |
338 | dc.DrawLine(draw_x, 0, draw_x, height) | |
339 | for y in xrange(int(self.build_dimensions[1] / grid_unit) + 1): | |
340 | draw_y = self.scale[1] * (self.build_dimensions[1] - y * grid_unit) | |
341 | dc.DrawLine(0, draw_y, width, draw_y) | |
342 | dc.SetPen(wx.Pen(wx.Colour(0, 0, 0))) | |
343 | ||
344 | if not self.showall: | |
345 | # Draw layer gauge | |
346 | dc.SetBrush(wx.Brush((43, 144, 255))) | |
347 | dc.DrawRectangle(width - 15, 0, 15, height) | |
348 | dc.SetBrush(wx.Brush((0, 255, 0))) | |
349 | if self.layers: | |
350 | dc.DrawRectangle(width - 14, (1.0 - (1.0 * (self.layerindex + 1)) / len(self.layers)) * height, 13, height - 1) | |
351 | ||
352 | if self.showall: | |
353 | for i in range(len(self.layersz)): | |
354 | self.painted_layers.add(i) | |
355 | self._drawlines(dc, self.lines[i], self.pens[i]) | |
356 | self._drawarcs(dc, self.arcs[i], self.arcpens[i]) | |
357 | dc.SelectObject(wx.NullBitmap) | |
358 | return | |
359 | ||
360 | if self.layerindex < len(self.layers) and self.layerindex in self.lines: | |
361 | for layer_i in range(max(0, self.layerindex - 6), self.layerindex): | |
362 | self._drawlines(dc, self.lines[layer_i], self.fades[self.layerindex - layer_i - 1]) | |
363 | self._drawarcs(dc, self.arcs[layer_i], self.fades[self.layerindex - layer_i - 1]) | |
364 | self._drawlines(dc, self.lines[self.layerindex], self.pens[self.layerindex]) | |
365 | self._drawarcs(dc, self.arcs[self.layerindex], self.arcpens[self.layerindex]) | |
366 | ||
367 | self._drawlines(dc, self.hilight, self.hlpen) | |
368 | self._drawarcs(dc, self.hilightarcs, self.hlpen) | |
369 | ||
370 | self.paint_hilights(dc) | |
371 | ||
372 | dc.SelectObject(wx.NullBitmap) | |
373 | ||
374 | def repaint_partial(self): | |
375 | if self.showall: | |
376 | dc = wx.MemoryDC() | |
377 | dc.SelectObject(self.blitmap) | |
378 | for i in set(range(len(self.layersz))).difference(self.painted_layers): | |
379 | self.painted_layers.add(i) | |
380 | self._drawlines(dc, self.lines[i], self.pens[i]) | |
381 | self._drawarcs(dc, self.arcs[i], self.arcpens[i]) | |
382 | dc.SelectObject(wx.NullBitmap) | |
383 | ||
384 | def paint_hilights(self, dc = None): | |
385 | if self.hilightqueue.empty() and self.hilightarcsqueue.empty(): | |
386 | return | |
387 | hl = [] | |
388 | if not dc: | |
389 | dc = wx.MemoryDC() | |
390 | dc.SelectObject(self.blitmap) | |
391 | while not self.hilightqueue.empty(): | |
392 | hl.append(self.hilightqueue.get_nowait()) | |
393 | self._drawlines(dc, hl, self.hlpen) | |
394 | hlarcs = [] | |
395 | while not self.hilightarcsqueue.empty(): | |
396 | hlarcs.append(self.hilightarcsqueue.get_nowait()) | |
397 | self._drawarcs(dc, hlarcs, self.hlpen) | |
398 | dc.SelectObject(wx.NullBitmap) | |
399 | ||
400 | def paint(self, event): | |
401 | if self.dirty: | |
402 | self.dirty = False | |
403 | self.partial = False | |
404 | self.repaint_everything() | |
405 | elif self.partial: | |
406 | self.partial = False | |
407 | self.repaint_partial() | |
408 | self.paint_hilights() | |
409 | dc = wx.PaintDC(self) | |
410 | dc.SetBackground(wx.Brush(self.bgcolor)) | |
411 | dc.Clear() | |
412 | dc.DrawBitmap(self.blitmap, self.translate[0], self.translate[1]) | |
413 | if self.paint_overlay: | |
414 | self.paint_overlay(dc) | |
415 | ||
416 | def addfile_perlayer(self, gcode, showall = False): | |
417 | self.clear() | |
418 | self.gcode = gcode | |
419 | self.showall = showall | |
420 | generator = self.add_parsed_gcodes(gcode) | |
421 | generator_output = generator.next() | |
422 | while generator_output is not None: | |
423 | yield generator_output | |
424 | generator_output = generator.next() | |
425 | max_layers = len(self.layers) | |
426 | if hasattr(self.parent, "layerslider"): | |
427 | self.parent.layerslider.SetRange(0, max_layers - 1) | |
428 | self.parent.layerslider.SetValue(0) | |
429 | yield None | |
430 | ||
431 | def addfile(self, gcode = None, showall = False): | |
432 | generator = self.addfile_perlayer(gcode, showall) | |
433 | while generator.next() is not None: | |
434 | continue | |
435 | ||
436 | def _get_movement(self, start_pos, gline): | |
437 | """Takes a start position and a gcode, and returns a 3-uple containing | |
438 | (final position, line, arc), with line and arc being None if not | |
439 | used""" | |
440 | target = start_pos[:] | |
441 | target[5] = 0.0 | |
442 | target[6] = 0.0 | |
443 | if gline.current_x is not None: target[0] = gline.current_x | |
444 | if gline.current_y is not None: target[1] = gline.current_y | |
445 | if gline.current_z is not None: target[2] = gline.current_z | |
446 | if gline.e is not None: | |
447 | if gline.relative_e: | |
448 | target[3] += gline.e | |
449 | else: | |
450 | target[3] = gline.e | |
451 | if gline.f is not None: target[4] = gline.f | |
452 | if gline.i is not None: target[5] = gline.i | |
453 | if gline.j is not None: target[6] = gline.j | |
454 | ||
455 | if gline.command in ["G0", "G1"]: | |
456 | line = [self._x(start_pos[0]), | |
457 | self._y(start_pos[1]), | |
458 | self._x(target[0]), | |
459 | self._y(target[1])] | |
460 | return target, line, None | |
461 | elif gline.command in ["G2", "G3"]: | |
462 | # startpos, endpos, arc center | |
463 | arc = [self._x(start_pos[0]), self._y(start_pos[1]), | |
464 | self._x(target[0]), self._y(target[1]), | |
465 | self._x(start_pos[0] + target[5]), self._y(start_pos[1] + target[6])] | |
466 | if gline.command == "G2": # clockwise, reverse endpoints | |
467 | arc[0], arc[1], arc[2], arc[3] = arc[2], arc[3], arc[0], arc[1] | |
468 | return target, None, arc | |
469 | ||
470 | def _y(self, y): | |
471 | return self.build_dimensions[1] - (y - self.build_dimensions[4]) | |
472 | ||
473 | def _x(self, x): | |
474 | return x - self.build_dimensions[3] | |
475 | ||
476 | def add_parsed_gcodes(self, gcode): | |
477 | start_time = time.time() | |
478 | ||
479 | layer_idx = 0 | |
480 | while layer_idx < len(gcode.all_layers): | |
481 | layer = gcode.all_layers[layer_idx] | |
482 | has_move = False | |
483 | for gline in layer: | |
484 | if gline.is_move: | |
485 | has_move = True | |
486 | break | |
487 | if not has_move: | |
488 | yield layer_idx | |
489 | layer_idx += 1 | |
490 | continue | |
491 | viz_layer = len(self.layers) | |
492 | self.lines[viz_layer] = [] | |
493 | self.pens[viz_layer] = [] | |
494 | self.arcs[viz_layer] = [] | |
495 | self.arcpens[viz_layer] = [] | |
496 | for gline in layer: | |
497 | if not gline.is_move: | |
498 | continue | |
499 | ||
500 | target, line, arc = self._get_movement(self.lastpos[:], gline) | |
501 | ||
502 | if line is not None: | |
503 | self.lines[viz_layer].append(line) | |
504 | self.pens[viz_layer].append(self.mainpen if target[3] != self.lastpos[3] else self.travelpen) | |
505 | elif arc is not None: | |
506 | self.arcs[viz_layer].append(arc) | |
507 | self.arcpens[viz_layer].append(self.arcpen) | |
508 | ||
509 | self.lastpos = target | |
510 | # Transform into a numpy array for memory efficiency | |
511 | self.lines[viz_layer] = numpy.asarray(self.lines[viz_layer], dtype = numpy.float32) | |
512 | self.pens[viz_layer] = numpy.asarray(self.pens[viz_layer]) | |
513 | self.arcs[viz_layer] = numpy.asarray(self.arcs[viz_layer], dtype = numpy.float32) | |
514 | self.arcpens[viz_layer] = numpy.asarray(self.arcpens[viz_layer]) | |
515 | # Only add layer to self.layers now to prevent the display of an | |
516 | # unfinished layer | |
517 | self.layers[layer_idx] = viz_layer | |
518 | self.layersz.append(layer.z) | |
519 | ||
520 | # Refresh display if more than 0.2s have passed | |
521 | if time.time() - start_time > 0.2: | |
522 | start_time = time.time() | |
523 | self.partial = True | |
524 | wx.CallAfter(self.Refresh) | |
525 | ||
526 | yield layer_idx | |
527 | layer_idx += 1 | |
528 | ||
529 | self.dirty = True | |
530 | wx.CallAfter(self.Refresh) | |
531 | yield None | |
532 | ||
533 | def addgcodehighlight(self, gline): | |
534 | if gline.command not in ["G0", "G1", "G2", "G3"]: | |
535 | return | |
536 | ||
537 | target, line, arc = self._get_movement(self.hilightpos[:], gline) | |
538 | ||
539 | if line is not None: | |
540 | self.hilight.append(line) | |
541 | self.hilightqueue.put_nowait(line) | |
542 | elif arc is not None: | |
543 | self.hilightarcs.append(arc) | |
544 | self.hilightarcsqueue.put_nowait(arc) | |
545 | ||
546 | self.hilightpos = target | |
547 | wx.CallAfter(self.Refresh) | |
548 | ||
549 | if __name__ == '__main__': | |
550 | import sys | |
551 | app = wx.App(False) | |
552 | main = GvizWindow(open(sys.argv[1], "rU")) | |
553 | main.Show() | |
554 | app.MainLoop() |