printrun-src/printrun/gviz.py

Fri, 03 Jun 2016 09:16:07 +0200

author
mbayer
date
Fri, 03 Jun 2016 09:16:07 +0200
changeset 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

Added printrun sourcecode from
https://github.com/kliment/Printrun
03.06.2016 09:10

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
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 from Queue import Queue
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)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41 self.toolbar.AddSimpleTool(1, wx.Image(imagefile('zoom_in.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), _("Zoom In [+]"), '')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 self.toolbar.AddSimpleTool(2, wx.Image(imagefile('zoom_out.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), _("Zoom Out [-]"), '')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 self.toolbar.AddSeparator()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 self.toolbar.AddSimpleTool(3, wx.Image(imagefile('arrow_up.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), _("Move Up a Layer [U]"), '')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 self.toolbar.AddSimpleTool(4, wx.Image(imagefile('arrow_down.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), _("Move Down a Layer [D]"), '')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46 self.toolbar.AddLabelTool(5, " " + _("Reset view"), wx.Image(imagefile('reset.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap(), shortHelp = _("Reset view"), longHelp = '')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 self.toolbar.AddSeparator()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
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"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
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"))
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():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 e = event.GetPositionTuple()
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
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 class Gviz(wx.Panel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 # Mark canvas as dirty when setting showall
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 _showall = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 def _get_showall(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 return self._showall
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 def _set_showall(self, showall):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 if showall != self._showall:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 self.dirty = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 self._showall = showall
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 showall = property(_get_showall, _set_showall)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
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):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 wx.Panel.__init__(self, parent, -1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 self.widget = self
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 size = [max(1.0, x) for x in size]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 ratio = size[0] / size[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 self.SetMinSize((150, 150 / ratio))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 self.parent = realparent if realparent else parent
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 self.size = size
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 self.build_dimensions = build_dimensions
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 self.grid = grid
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 self.Bind(wx.EVT_PAINT, self.paint)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 self.Bind(wx.EVT_SIZE, self.resize)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 self.hilight = deque()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 self.hilightarcs = deque()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 self.hilightqueue = Queue(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 self.hilightarcsqueue = Queue(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 self.clear()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 self.filament_width = extrusion_width # set it to 0 to disable scaling lines with zoom
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 self.update_basescale()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 self.scale = self.basescale
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 penwidth = max(1.0, self.filament_width * ((self.scale[0] + self.scale[1]) / 2.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 self.translate = [0.0, 0.0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 self.mainpen = wx.Pen(wx.Colour(0, 0, 0), penwidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 self.arcpen = wx.Pen(wx.Colour(255, 0, 0), penwidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 self.travelpen = wx.Pen(wx.Colour(10, 80, 80), penwidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 self.hlpen = wx.Pen(wx.Colour(200, 50, 50), penwidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
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)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 self.penslist = [self.mainpen, self.travelpen, self.hlpen] + self.fades
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 self.bgcolor = wx.Colour()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 self.bgcolor.SetFromName(bgcolor)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204 self.blitmap = wx.EmptyBitmap(self.GetClientSize()[0], self.GetClientSize()[1], -1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 self.paint_overlay = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 def inject(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 layer = self.layers.index(self.layerindex)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 injector(self.gcode, self.layerindex, layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 def editlayer(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 layer = self.layers.index(self.layerindex)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 injector_edit(self.gcode, self.layerindex, layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 def clearhilights(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 self.hilight.clear()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 self.hilightarcs.clear()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 while not self.hilightqueue.empty():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 self.hilightqueue.get_nowait()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 while not self.hilightarcsqueue.empty():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 self.hilightarcsqueue.get_nowait()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 def clear(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 self.gcode = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 self.lastpos = [0, 0, 0, 0, 0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 self.hilightpos = self.lastpos[:]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 self.lines = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 self.pens = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 self.arcs = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 self.arcpens = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 self.layers = {}
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 self.layersz = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 self.clearhilights()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 self.layerindex = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 self.showall = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 self.dirty = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 self.partial = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 self.painted_layers = set()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 def get_currentz(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 z = self.layersz[self.layerindex]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 z = 0. if z is None else z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 return z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 def layerup(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 if self.layerindex + 1 < len(self.layers):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 self.layerindex += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 z = self.get_currentz()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 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
251 self.dirty = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 self.parent.setlayercb(self.layerindex)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
255 def layerdown(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 if self.layerindex > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 self.layerindex -= 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 z = self.get_currentz()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 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
260 self.dirty = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261 self.parent.setlayercb(self.layerindex)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 def setlayer(self, layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 if layer in self.layers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 self.clearhilights()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 self.layerindex = self.layers[layer]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 self.dirty = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 self.showall = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272 def update_basescale(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 self.basescale = 2 * [min(float(self.size[0] - 1) / self.build_dimensions[0],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 float(self.size[1] - 1) / self.build_dimensions[1])]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 def resize(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 old_basescale = self.basescale
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 width, height = self.GetClientSizeTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 if width < 1 or height < 1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 self.size = (width, height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 self.update_basescale()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283 zoomratio = float(self.basescale[0]) / old_basescale[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 wx.CallLater(200, self.zoom, 0, 0, zoomratio)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 def zoom(self, x, y, factor):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 if x == -1 and y == -1:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 side = min(self.size)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289 x = y = side / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 self.scale = [s * factor for s in self.scale]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 self.translate = [x - (x - self.translate[0]) * factor,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 y - (y - self.translate[1]) * factor]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 penwidth = max(1.0, self.filament_width * ((self.scale[0] + self.scale[1]) / 2.0))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 for pen in self.penslist:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296 pen.SetWidth(penwidth)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297 self.dirty = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300 def _line_scaler(self, x):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
301 return (self.scale[0] * x[0],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 self.scale[1] * x[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
303 self.scale[0] * x[2],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
304 self.scale[1] * x[3],)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
306 def _arc_scaler(self, x):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
307 return (self.scale[0] * x[0],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
308 self.scale[1] * x[1],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
309 self.scale[0] * x[2],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310 self.scale[1] * x[3],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
311 self.scale[0] * x[4],
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 self.scale[1] * x[5],)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
314 def _drawlines(self, dc, lines, pens):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 scaled_lines = map(self._line_scaler, lines)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316 dc.DrawLineList(scaled_lines, pens)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318 def _drawarcs(self, dc, arcs, pens):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319 scaled_arcs = map(self._arc_scaler, arcs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320 dc.SetBrush(wx.TRANSPARENT_BRUSH)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321 for i in range(len(scaled_arcs)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322 dc.SetPen(pens[i] if type(pens) == list else pens)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323 dc.DrawArc(*scaled_arcs[i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325 def repaint_everything(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326 width = self.scale[0] * self.build_dimensions[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 height = self.scale[1] * self.build_dimensions[1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 self.blitmap = wx.EmptyBitmap(width + 1, height + 1, -1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 dc = wx.MemoryDC()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 dc.SelectObject(self.blitmap)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331 dc.SetBackground(wx.Brush((250, 250, 200)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332 dc.Clear()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333 dc.SetPen(wx.Pen(wx.Colour(180, 180, 150)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
334 for grid_unit in self.grid:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335 if grid_unit > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 for x in xrange(int(self.build_dimensions[0] / grid_unit) + 1):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 draw_x = self.scale[0] * x * grid_unit
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338 dc.DrawLine(draw_x, 0, draw_x, height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339 for y in xrange(int(self.build_dimensions[1] / grid_unit) + 1):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 draw_y = self.scale[1] * (self.build_dimensions[1] - y * grid_unit)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341 dc.DrawLine(0, draw_y, width, draw_y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342 dc.SetPen(wx.Pen(wx.Colour(0, 0, 0)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344 if not self.showall:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345 # Draw layer gauge
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346 dc.SetBrush(wx.Brush((43, 144, 255)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 dc.DrawRectangle(width - 15, 0, 15, height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 dc.SetBrush(wx.Brush((0, 255, 0)))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
349 if self.layers:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350 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
351
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 if self.showall:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 for i in range(len(self.layersz)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 self.painted_layers.add(i)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355 self._drawlines(dc, self.lines[i], self.pens[i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 self._drawarcs(dc, self.arcs[i], self.arcpens[i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357 dc.SelectObject(wx.NullBitmap)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
358 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 if self.layerindex < len(self.layers) and self.layerindex in self.lines:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 for layer_i in range(max(0, self.layerindex - 6), self.layerindex):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362 self._drawlines(dc, self.lines[layer_i], self.fades[self.layerindex - layer_i - 1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363 self._drawarcs(dc, self.arcs[layer_i], self.fades[self.layerindex - layer_i - 1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364 self._drawlines(dc, self.lines[self.layerindex], self.pens[self.layerindex])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365 self._drawarcs(dc, self.arcs[self.layerindex], self.arcpens[self.layerindex])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367 self._drawlines(dc, self.hilight, self.hlpen)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 self._drawarcs(dc, self.hilightarcs, self.hlpen)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370 self.paint_hilights(dc)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372 dc.SelectObject(wx.NullBitmap)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374 def repaint_partial(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375 if self.showall:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 dc = wx.MemoryDC()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377 dc.SelectObject(self.blitmap)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 for i in set(range(len(self.layersz))).difference(self.painted_layers):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379 self.painted_layers.add(i)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380 self._drawlines(dc, self.lines[i], self.pens[i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 self._drawarcs(dc, self.arcs[i], self.arcpens[i])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382 dc.SelectObject(wx.NullBitmap)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 def paint_hilights(self, dc = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385 if self.hilightqueue.empty() and self.hilightarcsqueue.empty():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 hl = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388 if not dc:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389 dc = wx.MemoryDC()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390 dc.SelectObject(self.blitmap)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391 while not self.hilightqueue.empty():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392 hl.append(self.hilightqueue.get_nowait())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 self._drawlines(dc, hl, self.hlpen)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394 hlarcs = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395 while not self.hilightarcsqueue.empty():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 hlarcs.append(self.hilightarcsqueue.get_nowait())
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 self._drawarcs(dc, hlarcs, self.hlpen)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 dc.SelectObject(wx.NullBitmap)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400 def paint(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 if self.dirty:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402 self.dirty = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403 self.partial = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404 self.repaint_everything()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 elif self.partial:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 self.partial = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
407 self.repaint_partial()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408 self.paint_hilights()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409 dc = wx.PaintDC(self)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410 dc.SetBackground(wx.Brush(self.bgcolor))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411 dc.Clear()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
412 dc.DrawBitmap(self.blitmap, self.translate[0], self.translate[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
413 if self.paint_overlay:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414 self.paint_overlay(dc)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 def addfile_perlayer(self, gcode, showall = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417 self.clear()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 self.gcode = gcode
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419 self.showall = showall
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
420 generator = self.add_parsed_gcodes(gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
421 generator_output = generator.next()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
422 while generator_output is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 yield generator_output
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424 generator_output = generator.next()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 max_layers = len(self.layers)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426 if hasattr(self.parent, "layerslider"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427 self.parent.layerslider.SetRange(0, max_layers - 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428 self.parent.layerslider.SetValue(0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429 yield None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 def addfile(self, gcode = None, showall = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
432 generator = self.addfile_perlayer(gcode, showall)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
433 while generator.next() is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 def _get_movement(self, start_pos, gline):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
437 """Takes a start position and a gcode, and returns a 3-uple containing
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438 (final position, line, arc), with line and arc being None if not
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439 used"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
440 target = start_pos[:]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441 target[5] = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 target[6] = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
443 if gline.current_x is not None: target[0] = gline.current_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
444 if gline.current_y is not None: target[1] = gline.current_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
445 if gline.current_z is not None: target[2] = gline.current_z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
446 if gline.e is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447 if gline.relative_e:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
448 target[3] += gline.e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
449 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450 target[3] = gline.e
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
451 if gline.f is not None: target[4] = gline.f
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452 if gline.i is not None: target[5] = gline.i
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453 if gline.j is not None: target[6] = gline.j
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
454
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455 if gline.command in ["G0", "G1"]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 line = [self._x(start_pos[0]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457 self._y(start_pos[1]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
458 self._x(target[0]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
459 self._y(target[1])]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
460 return target, line, None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
461 elif gline.command in ["G2", "G3"]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
462 # startpos, endpos, arc center
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
463 arc = [self._x(start_pos[0]), self._y(start_pos[1]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
464 self._x(target[0]), self._y(target[1]),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
465 self._x(start_pos[0] + target[5]), self._y(start_pos[1] + target[6])]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
466 if gline.command == "G2": # clockwise, reverse endpoints
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
467 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
468 return target, None, arc
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
469
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
470 def _y(self, y):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
471 return self.build_dimensions[1] - (y - self.build_dimensions[4])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
472
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
473 def _x(self, x):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
474 return x - self.build_dimensions[3]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
475
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
476 def add_parsed_gcodes(self, gcode):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
477 start_time = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
478
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
479 layer_idx = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
480 while layer_idx < len(gcode.all_layers):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
481 layer = gcode.all_layers[layer_idx]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
482 has_move = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
483 for gline in layer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
484 if gline.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
485 has_move = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
486 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
487 if not has_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
488 yield layer_idx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
489 layer_idx += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
490 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
491 viz_layer = len(self.layers)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
492 self.lines[viz_layer] = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
493 self.pens[viz_layer] = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
494 self.arcs[viz_layer] = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
495 self.arcpens[viz_layer] = []
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
496 for gline in layer:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
497 if not gline.is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
498 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
499
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
500 target, line, arc = self._get_movement(self.lastpos[:], gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
501
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
502 if line is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
503 self.lines[viz_layer].append(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
504 self.pens[viz_layer].append(self.mainpen if target[3] != self.lastpos[3] else self.travelpen)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
505 elif arc is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
506 self.arcs[viz_layer].append(arc)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
507 self.arcpens[viz_layer].append(self.arcpen)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
508
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
509 self.lastpos = target
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
510 # Transform into a numpy array for memory efficiency
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
511 self.lines[viz_layer] = numpy.asarray(self.lines[viz_layer], dtype = numpy.float32)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
512 self.pens[viz_layer] = numpy.asarray(self.pens[viz_layer])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
513 self.arcs[viz_layer] = numpy.asarray(self.arcs[viz_layer], dtype = numpy.float32)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
514 self.arcpens[viz_layer] = numpy.asarray(self.arcpens[viz_layer])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
515 # Only add layer to self.layers now to prevent the display of an
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
516 # unfinished layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
517 self.layers[layer_idx] = viz_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
518 self.layersz.append(layer.z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
519
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
520 # Refresh display if more than 0.2s have passed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
521 if time.time() - start_time > 0.2:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
522 start_time = time.time()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
523 self.partial = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
524 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
525
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
526 yield layer_idx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
527 layer_idx += 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
528
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
529 self.dirty = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
530 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
531 yield None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
532
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
533 def addgcodehighlight(self, gline):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
534 if gline.command not in ["G0", "G1", "G2", "G3"]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
535 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
536
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
537 target, line, arc = self._get_movement(self.hilightpos[:], gline)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
538
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
539 if line is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
540 self.hilight.append(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
541 self.hilightqueue.put_nowait(line)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
542 elif arc is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
543 self.hilightarcs.append(arc)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
544 self.hilightarcsqueue.put_nowait(arc)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
545
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
546 self.hilightpos = target
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
547 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
548
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
549 if __name__ == '__main__':
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
550 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
551 app = wx.App(False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
552 main = GvizWindow(open(sys.argv[1], "rU"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
553 main.Show()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
554 app.MainLoop()

mercurial