printrun-src/printrun/gviz.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
-rw-r--r--

updated and added new files for printrun

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

mercurial