printrun-src/printrun/gcview.py

Thu, 07 Jul 2016 11:58:38 +0200

author
mbayer
date
Thu, 07 Jul 2016 11:58:38 +0200
changeset 27
c82c30811edd
parent 15
0bbb006204fc
child 46
cce0af6351f0
permissions
-rw-r--r--

svg crowbar fixes

15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
1 #!/usr/bin/env python
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
3 # This file is part of the Printrun suite.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
4 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
5 # Printrun is free software: you can redistribute it and/or modify
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
6 # it under the terms of the GNU General Public License as published by
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
7 # the Free Software Foundation, either version 3 of the License, or
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
8 # (at your option) any later version.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
9 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
10 # Printrun is distributed in the hope that it will be useful,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
13 # GNU General Public License for more details.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
14 #
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
15 # You should have received a copy of the GNU General Public License
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
16 # along with Printrun. If not, see <http://www.gnu.org/licenses/>.
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
17
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
18 import logging
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
19 import wx
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
20
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
21 from . import gcoder
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
22 from .gl.panel import wxGLPanel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
23 from .gl.trackball import build_rotmatrix
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
24 from .gl.libtatlin import actors
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
25 from .injectgcode import injector, injector_edit
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
26
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
27 from pyglet.gl import glPushMatrix, glPopMatrix, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
28 glTranslatef, glRotatef, glScalef, glMultMatrixd, \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
29 glGetDoublev, GL_MODELVIEW_MATRIX, GLdouble
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
30
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
31 from .gviz import GvizBaseFrame
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
32
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
33 from .utils import imagefile, install_locale, get_home_pos
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
34 install_locale('pronterface')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
35
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
36 def create_model(light):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
37 if light:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
38 return actors.GcodeModelLight()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
39 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
40 return actors.GcodeModel()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
41
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
42 def gcode_dims(g):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
43 return ((g.xmin, g.xmax, g.width),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
44 (g.ymin, g.ymax, g.depth),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
45 (g.zmin, g.zmax, g.height))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
46
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
47 def set_model_colors(model, root):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
48 for field in dir(model):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
49 if field.startswith("color_"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
50 root_fieldname = "gcview_" + field
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
51 if hasattr(root, root_fieldname):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
52 setattr(model, field, getattr(root, root_fieldname))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
53
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
54 def recreate_platform(self, build_dimensions, circular):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
55 self.platform = actors.Platform(build_dimensions, circular = circular)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
56 self.objects[0].model = self.platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
57 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
58
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
59 def set_gcview_params(self, path_width, path_height):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
60 self.path_halfwidth = path_width / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
61 self.path_halfheight = path_height / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
62 has_changed = False
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
63 for obj in self.objects[1:]:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
64 if isinstance(obj.model, actors.GcodeModel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
65 obj.model.set_path_size(self.path_halfwidth, self.path_halfheight)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
66 has_changed = True
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
67 return has_changed
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
68
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
69 class GcodeViewPanel(wxGLPanel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
70
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
71 def __init__(self, parent, id = wx.ID_ANY,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
72 build_dimensions = None, realparent = None,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
73 antialias_samples = 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
74 super(GcodeViewPanel, self).__init__(parent, id, wx.DefaultPosition,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
75 wx.DefaultSize, 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
76 antialias_samples = antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
77 self.canvas.Bind(wx.EVT_MOUSE_EVENTS, self.move)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 self.canvas.Bind(wx.EVT_LEFT_DCLICK, self.double)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79 self.canvas.Bind(wx.EVT_KEY_DOWN, self.keypress)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
80 self.initialized = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
81 self.canvas.Bind(wx.EVT_MOUSEWHEEL, self.wheel)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
82 self.parent = realparent if realparent else parent
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
83 self.initpos = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 if build_dimensions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 self.build_dimensions = build_dimensions
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 self.build_dimensions = [200, 200, 100, 0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
88 self.dist = max(self.build_dimensions[0], self.build_dimensions[1])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
89 self.basequat = [0, 0, 0, 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
90 self.mousepos = [0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 def inject(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
93 l = self.parent.model.num_layers_to_draw
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 filtered = [k for k, v in self.parent.model.layer_idxs_map.iteritems() if v == l]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
95 if filtered:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
96 injector(self.parent.model.gcode, l, filtered[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 logging.error(_("Invalid layer for injection"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 def editlayer(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 l = self.parent.model.num_layers_to_draw
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
102 filtered = [k for k, v in self.parent.model.layer_idxs_map.iteritems() if v == l]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 if filtered:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 injector_edit(self.parent.model.gcode, l, filtered[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
105 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
106 logging.error(_("Invalid layer for edition"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 def setlayercb(self, layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
110
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 def OnInitGL(self, *args, **kwargs):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 super(GcodeViewPanel, self).OnInitGL(*args, **kwargs)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 if hasattr(self.parent, "filenames") and self.parent.filenames:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 for filename in self.parent.filenames:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115 self.parent.load_file(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 self.parent.autoplate()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 if hasattr(self.parent, "loadcb"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118 self.parent.loadcb()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 self.parent.filenames = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
120
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
121 def create_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
122 '''create opengl objects when opengl is initialized'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
123 for obj in self.parent.objects:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 if obj.model and obj.model.loaded and not obj.model.initialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 obj.model.init()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
126
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 def update_object_resize(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128 '''called when the window recieves only if opengl is initialized'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 def draw_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 '''called in the middle of ondraw after the buffer has been cleared'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 self.create_objects()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
136 # Rotate according to trackball
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 glMultMatrixd(build_rotmatrix(self.basequat))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138 # Move origin to bottom left of platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 platformx0 = -self.build_dimensions[3] - self.parent.platform.width / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 platformy0 = -self.build_dimensions[4] - self.parent.platform.depth / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 glTranslatef(platformx0, platformy0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 for obj in self.parent.objects:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 if not obj.model \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 or not obj.model.loaded \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 or not obj.model.initialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 glTranslatef(*(obj.offsets))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150 glRotatef(obj.rot, 0.0, 0.0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 glTranslatef(*(obj.centeroffset))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 glScalef(*obj.scale)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
153
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 obj.model.display()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
155 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
156 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
157
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
158 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
159 # Utils
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
160 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 def get_modelview_mat(self, local_transform):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 mvmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 if local_transform:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 # Rotate according to trackball
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166 glMultMatrixd(build_rotmatrix(self.basequat))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 # Move origin to bottom left of platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 platformx0 = -self.build_dimensions[3] - self.parent.platform.width / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 platformy0 = -self.build_dimensions[4] - self.parent.platform.depth / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170 glTranslatef(platformx0, platformy0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 return mvmat
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 def double(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 if hasattr(self.parent, "clickcb") and self.parent.clickcb:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 self.parent.clickcb(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 def move(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 """react to mouse actions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 no mouse: show red mousedrop
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 LMB: rotate viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 RMB: move viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 if event.Entering():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 self.canvas.SetFocus()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
191 if event.Dragging() and event.LeftIsDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192 self.handle_rotation(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 elif event.Dragging() and event.RightIsDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 self.handle_translation(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 elif event.LeftUp():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 self.initpos = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 elif event.RightUp():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 self.initpos = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
203 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
204
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
205 def layerup(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
206 if not hasattr(self.parent, "model") or not self.parent.model:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
207 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
208 max_layers = self.parent.model.max_layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
209 current_layer = self.parent.model.num_layers_to_draw
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 # accept going up to max_layers + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 # max_layers means visualizing the last layer differently,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212 # max_layers + 1 means visualizing all layers with the same color
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 new_layer = min(max_layers + 1, current_layer + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
214 self.parent.model.num_layers_to_draw = new_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 self.parent.setlayercb(new_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 def layerdown(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 if not hasattr(self.parent, "model") or not self.parent.model:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 current_layer = self.parent.model.num_layers_to_draw
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 new_layer = max(1, current_layer - 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 self.parent.model.num_layers_to_draw = new_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 self.parent.setlayercb(new_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
227 def handle_wheel(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 delta = event.GetWheelRotation()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 factor = 1.05
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 if event.ControlDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 factor = 1.02
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 if hasattr(self.parent, "model") and event.ShiftDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 if not self.parent.model:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
235 count = 1 if not event.ControlDown() else 10
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 for i in range(count):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
237 if delta > 0: self.layerup()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
238 else: self.layerdown()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
239 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
240 x, y = event.GetPositionTuple()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
241 x, y, _ = self.mouse_to_3d(x, y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
242 if delta > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 self.zoom(factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 self.zoom(1 / factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 def wheel(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 """react to mouse wheel actions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 without shift: set max layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 with shift: zoom viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 self.handle_wheel(event)
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 fit(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 if not self.parent.model or not self.parent.model.loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 self.canvas.SetCurrent(self.context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 dims = gcode_dims(self.parent.model.gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260 self.reset_mview(1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261 center_x = (dims[0][0] + dims[0][1]) / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 center_y = (dims[1][0] + dims[1][1]) / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 center_x = self.build_dimensions[0] / 2 - center_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 center_y = self.build_dimensions[1] / 2 - center_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 if self.orthographic:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 ratio = float(self.dist) / max(dims[0][2], dims[1][2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 glScalef(ratio, ratio, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 glTranslatef(center_x, center_y, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271 def keypress(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272 """gets keypress events and moves/rotates acive shape"""
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 step = 1.1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 if event.ControlDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275 step = 1.05
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 kup = [85, 315] # Up keys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 kdo = [68, 317] # Down Keys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 kzi = [wx.WXK_PAGEDOWN, 388, 316, 61] # Zoom In Keys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 kzo = [wx.WXK_PAGEUP, 390, 314, 45] # Zoom Out Keys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 kfit = [70] # Fit to print keys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 kshowcurrent = [67] # Show only current layer keys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 kreset = [82] # Reset keys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283 key = event.GetKeyCode()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 if key in kup:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285 self.layerup()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 if key in kdo:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
287 self.layerdown()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
288 x, y, _ = self.mouse_to_3d(self.width / 2, self.height / 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
289 if key in kzi:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
290 self.zoom_to_center(step)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
291 if key in kzo:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
292 self.zoom_to_center(1 / step)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 if key in kfit:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
294 self.fit()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 if key in kshowcurrent:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
296 if not self.parent.model or not self.parent.model.loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
297 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 self.parent.model.only_current = not self.parent.model.only_current
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
299 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
300 if key in kreset:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
301 self.resetview()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
303
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
304 def resetview(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305 self.canvas.SetCurrent(self.context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
306 self.reset_mview(0.9)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
307 self.basequat = [0, 0, 0, 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
308 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
309
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310 class GCObject(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
311
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 def __init__(self, model):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313 self.offsets = [0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
314 self.centeroffset = [0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 self.rot = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316 self.curlayer = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317 self.scale = [1.0, 1.0, 1.0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318 self.model = model
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320 class GcodeViewLoader(object):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322 path_halfwidth = 0.2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323 path_halfheight = 0.15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
324
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325 def addfile_perlayer(self, gcode = None, showall = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326 self.model = create_model(self.root.settings.light3d
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 if self.root else False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 if isinstance(self.model, actors.GcodeModel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 self.model.set_path_size(self.path_halfwidth, self.path_halfheight)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 self.objects[-1].model = self.model
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331 if self.root:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332 set_model_colors(self.model, self.root)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333 if gcode is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
334 generator = self.model.load_data(gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335 generator_output = generator.next()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 while generator_output is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 yield generator_output
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338 generator_output = generator.next()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 yield None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342 def addfile(self, gcode = None, showall = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343 generator = self.addfile_perlayer(gcode, showall)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344 while generator.next() is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 def set_gcview_params(self, path_width, path_height):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 return set_gcview_params(self, path_width, path_height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
349
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350 class GcodeViewMainWrapper(GcodeViewLoader):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
352 def __init__(self, parent, build_dimensions, root, circular, antialias_samples):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 self.root = root
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 self.glpanel = GcodeViewPanel(parent, realparent = self,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355 build_dimensions = build_dimensions,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 antialias_samples = antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357 self.glpanel.SetMinSize((150, 150))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
358 if self.root and hasattr(self.root, "gcview_color_background"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 self.glpanel.color_background = self.root.gcview_color_background
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360 self.clickcb = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 self.widget = self.glpanel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362 self.refresh_timer = wx.CallLater(100, self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363 self.p = self # Hack for backwards compatibility with gviz API
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
364 self.platform = actors.Platform(build_dimensions, circular = circular)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
365 self.model = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366 self.objects = [GCObject(self.platform), GCObject(None)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
367
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 def __getattr__(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369 return getattr(self.glpanel, name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 def set_current_gline(self, gline):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372 if gline.is_move and gline.gcview_end_vertex is not None \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373 and self.model and self.model.loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374 self.model.printed_until = gline.gcview_end_vertex
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375 if not self.refresh_timer.IsRunning():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 self.refresh_timer.Start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 def recreate_platform(self, build_dimensions, circular):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
379 return recreate_platform(self, build_dimensions, circular)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
380
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 def addgcodehighlight(self, *a):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 def setlayer(self, layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385 if layer in self.model.layer_idxs_map:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386 viz_layer = self.model.layer_idxs_map[layer]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
387 self.parent.model.num_layers_to_draw = viz_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
388 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
389
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
390 def clear(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
391 self.model = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
392 self.objects[-1].model = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
393 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395 class GcodeViewFrame(GvizBaseFrame, GcodeViewLoader):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 '''A simple class for using OpenGL with wxPython.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 def __init__(self, parent, ID, title, build_dimensions, objects = None,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399 pos = wx.DefaultPosition, size = wx.DefaultSize,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400 style = wx.DEFAULT_FRAME_STYLE, root = None, circular = False,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
401 antialias_samples = 0):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
402 GvizBaseFrame.__init__(self, parent, ID, title,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403 pos, size, style)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404 self.root = root
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 panel, vbox = self.create_base_ui()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
407
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408 self.refresh_timer = wx.CallLater(100, self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409 self.p = self # Hack for backwards compatibility with gviz API
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410 self.clonefrom = objects
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411 self.platform = actors.Platform(build_dimensions, circular = circular)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
412 if objects:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
413 self.model = objects[1].model
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415 self.model = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 self.objects = [GCObject(self.platform), GCObject(None)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 fit_image = wx.Image(imagefile('fit.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419 self.toolbar.InsertLabelTool(6, 8, " " + _("Fit to plate"), fit_image,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
420 shortHelp = _("Fit to plate [F]"),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
421 longHelp = '')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
422 self.toolbar.Realize()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 self.glpanel = GcodeViewPanel(panel,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424 build_dimensions = build_dimensions,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 realparent = self,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426 antialias_samples = antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427 vbox.Add(self.glpanel, 1, flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.zoom_to_center(1.2), id = 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.zoom_to_center(1 / 1.2), id = 2)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.layerup(), id = 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
432 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.layerdown(), id = 4)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
433 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.resetview(), id = 5)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.fit(), id = 8)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.inject(), id = 6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.editlayer(), id = 7)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
437
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438 def setlayercb(self, layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439 self.layerslider.SetValue(layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
440 self.update_status("")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 def update_status(self, extra):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
443 layer = self.model.num_layers_to_draw
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
444 filtered = [k for k, v in self.model.layer_idxs_map.iteritems() if v == layer]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
445 if filtered:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
446 true_layer = filtered[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447 z = self.model.gcode.all_layers[true_layer].z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
448 message = _("Layer %d -%s Z = %.03f mm") % (layer, extra, z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
449 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450 message = _("Entire object")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
451 wx.CallAfter(self.SetStatusText, message, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453 def process_slider(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
454 new_layer = self.layerslider.GetValue()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455 new_layer = min(self.model.max_layers + 1, new_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 new_layer = max(1, new_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457 self.model.num_layers_to_draw = new_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
458 self.update_status("")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
459 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
460
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
461 def set_current_gline(self, gline):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
462 if gline.is_move and gline.gcview_end_vertex is not None \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
463 and self.model and self.model.loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
464 self.model.printed_until = gline.gcview_end_vertex
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
465 if not self.refresh_timer.IsRunning():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
466 self.refresh_timer.Start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
467
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
468 def recreate_platform(self, build_dimensions, circular):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
469 return recreate_platform(self, build_dimensions, circular)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
470
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
471 def addfile(self, gcode = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
472 if self.clonefrom:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
473 self.model = self.clonefrom[-1].model.copy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
474 self.objects[-1].model = self.model
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
475 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
476 GcodeViewLoader.addfile(self, gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
477 self.layerslider.SetRange(1, self.model.max_layers + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
478 self.layerslider.SetValue(self.model.max_layers + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
479 wx.CallAfter(self.SetStatusText, _("Entire object"), 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
480 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
481
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
482 def clear(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
483 self.model = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
484 self.objects[-1].model = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
485 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
486
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
487 if __name__ == "__main__":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
488 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
489 app = wx.App(redirect = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
490 build_dimensions = [200, 200, 100, 0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
491 title = 'Gcode view, shift to move view, mousewheel to set layer'
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
492 frame = GcodeViewFrame(None, wx.ID_ANY, title, size = (400, 400),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
493 build_dimensions = build_dimensions)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
494 gcode = gcoder.GCode(open(sys.argv[1]), get_home_pos(build_dimensions))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
495 frame.addfile(gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
496
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
497 first_move = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
498 for i in range(len(gcode.lines)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
499 if gcode.lines[i].is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
500 first_move = gcode.lines[i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
501 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
502 last_move = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
503 for i in range(len(gcode.lines) - 1, -1, -1):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
504 if gcode.lines[i].is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
505 last_move = gcode.lines[i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
506 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
507 nsteps = 20
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
508 steptime = 500
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
509 lines = [first_move] + [gcode.lines[int(float(i) * (len(gcode.lines) - 1) / nsteps)] for i in range(1, nsteps)] + [last_move]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
510 current_line = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
511
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
512 def setLine():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
513 global current_line
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
514 frame.set_current_gline(lines[current_line])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
515 current_line = (current_line + 1) % len(lines)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
516 timer.Start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
517 timer = wx.CallLater(steptime, setLine)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
518 timer.Start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
519
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
520 frame.Show(True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
521 app.MainLoop()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
522 app.Destroy()

mercurial