printrun-src/printrun/gcview.py

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

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

updated and added new files for printrun

46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
1 #!/usr/bin/env python3
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
2
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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
54 def recreate_platform(self, build_dimensions, circular, grid):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
55 self.platform = actors.Platform(build_dimensions, circular = circular, grid = grid)
15
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
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
69 # E selected for Up because is above D
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
70 LAYER_UP_KEYS = ord('U'), ord('E'), wx.WXK_UP
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
71 LAYER_DOWN_KEYS = ord('D'), wx.WXK_DOWN
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
72 ZOOM_IN_KEYS = wx.WXK_PAGEDOWN, 388, wx.WXK_RIGHT, ord('=')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
73 ZOOM_OUT_KEYS = wx.WXK_PAGEUP, 390, wx.WXK_LEFT, ord('-')
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
74 FIT_KEYS = [ord('F')]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
75 CURRENT_LAYER_KEYS = [ord('C')]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
76 RESET_KEYS = [ord('R')]
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
77
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
78 class GcodeViewPanel(wxGLPanel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
79
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
80 def __init__(self, parent,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
81 build_dimensions = (200, 200, 100, 0, 0, 0),
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
82 realparent = None, antialias_samples = 0):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
83 super().__init__(parent, wx.DefaultPosition,
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
84 wx.DefaultSize, 0,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
85 antialias_samples = antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
86 self.canvas.Bind(wx.EVT_MOUSE_EVENTS, self.move)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
87 self.canvas.Bind(wx.EVT_LEFT_DCLICK, self.double)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
88 # self.canvas.Bind(wx.EVT_KEY_DOWN, self.keypress)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
89 # in Windows event inspector shows only EVT_CHAR_HOOK events
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
90 self.canvas.Bind(wx.EVT_CHAR_HOOK, self.keypress)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
91 self.initialized = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
92 self.canvas.Bind(wx.EVT_MOUSEWHEEL, self.wheel)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
93 self.parent = realparent or parent
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
94 self.initpos = None
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
95 self.build_dimensions = build_dimensions
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
96 self.dist = max(self.build_dimensions[:2])
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
97 self.basequat = [0, 0, 0, 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
98 self.mousepos = [0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
99
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
100 def inject(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
101 l = self.parent.model.num_layers_to_draw
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
102 filtered = [k for k, v in self.parent.model.layer_idxs_map.items() if v == l]
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
103 if filtered:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
104 injector(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 injection"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
107
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
108 def editlayer(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
109 l = self.parent.model.num_layers_to_draw
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
110 filtered = [k for k, v in self.parent.model.layer_idxs_map.items() if v == l]
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
111 if filtered:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
112 injector_edit(self.parent.model.gcode, l, filtered[0])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
113 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
114 logging.error(_("Invalid layer for edition"))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
115
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
116 def setlayercb(self, layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
117 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
118
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
119 def OnInitGL(self, *args, **kwargs):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
120 super().OnInitGL(*args, **kwargs)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
121 filenames = getattr(self.parent, 'filenames', None)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
122 if filenames:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
123 for filename in filenames:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
124 self.parent.load_file(filename)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
125 self.parent.autoplate()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
126 getattr(self.parent, 'loadcb', bool)()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
127 self.parent.filenames = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
128
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
129 def create_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
130 '''create opengl objects when opengl is initialized'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
131 for obj in self.parent.objects:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
132 if obj.model and obj.model.loaded and not obj.model.initialized:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
133 obj.model.init()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
134
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
135 def update_object_resize(self):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
136 '''called when the window receives only if opengl is initialized'''
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
137 pass
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
138
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
139 def draw_objects(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
140 '''called in the middle of ondraw after the buffer has been cleared'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
141 self.create_objects()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
142
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
143 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
144 # Rotate according to trackball
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
145 glMultMatrixd(build_rotmatrix(self.basequat))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
146 # Move origin to bottom left of platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
147 platformx0 = -self.build_dimensions[3] - self.parent.platform.width / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
148 platformy0 = -self.build_dimensions[4] - self.parent.platform.depth / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
149 glTranslatef(platformx0, platformy0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
150
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
151 for obj in self.parent.objects:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
152 if not obj.model \
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
153 or not obj.model.loaded:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
154 continue
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
155 # Skip (comment out) initialized check, which safely causes empty
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
156 # model during progressive load. This can cause exceptions/garbage
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
157 # render, but seems fine for now
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
158 # May need to lock init() and draw_objects() together
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
159 # if not obj.model.initialized:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
160 # continue
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
161 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
162 glTranslatef(*(obj.offsets))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
163 glRotatef(obj.rot, 0.0, 0.0, 1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
164 glTranslatef(*(obj.centeroffset))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
165 glScalef(*obj.scale)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
166
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
167 obj.model.display()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
168 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
169 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
170
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
171 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
172 # Utils
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
173 # ==========================================================================
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
174 def get_modelview_mat(self, local_transform):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
175 mvmat = (GLdouble * 16)()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
176 if local_transform:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
177 glPushMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
178 # Rotate according to trackball
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
179 glMultMatrixd(build_rotmatrix(self.basequat))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
180 # Move origin to bottom left of platform
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
181 platformx0 = -self.build_dimensions[3] - self.parent.platform.width / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
182 platformy0 = -self.build_dimensions[4] - self.parent.platform.depth / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
183 glTranslatef(platformx0, platformy0, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
184 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
185 glPopMatrix()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
186 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
187 glGetDoublev(GL_MODELVIEW_MATRIX, mvmat)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
188 return mvmat
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
189
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
190 def double(self, event):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
191 getattr(self.parent, 'clickcb', bool)(event)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
192
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
193 def move(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
194 """react to mouse actions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
195 no mouse: show red mousedrop
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
196 LMB: rotate viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
197 RMB: move viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
198 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
199 if event.Entering():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
200 self.canvas.SetFocus()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
201 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
202 return
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
203 if event.Dragging():
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
204 if event.LeftIsDown():
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
205 self.handle_rotation(event)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
206 elif event.RightIsDown():
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
207 self.handle_translation(event)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
208 self.Refresh(False)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
209 elif event.LeftUp() or event.RightUp():
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
210 self.initpos = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
211 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
212
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
213 def layerup(self):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
214 if not getattr(self.parent, 'model', False):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
215 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
216 max_layers = self.parent.model.max_layers
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
217 current_layer = self.parent.model.num_layers_to_draw
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
218 # accept going up to max_layers + 1
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
219 # max_layers means visualizing the last layer differently,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
220 # max_layers + 1 means visualizing all layers with the same color
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
221 new_layer = min(max_layers + 1, current_layer + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
222 self.parent.model.num_layers_to_draw = new_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
223 self.parent.setlayercb(new_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
224 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
225
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
226 def layerdown(self):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
227 if not getattr(self.parent, 'model', False):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
228 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
229 current_layer = self.parent.model.num_layers_to_draw
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
230 new_layer = max(1, current_layer - 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
231 self.parent.model.num_layers_to_draw = new_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
232 self.parent.setlayercb(new_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
233 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
234
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
235 wheelTimestamp = None
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
236 def handle_wheel(self, event):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
237 if self.wheelTimestamp == event.Timestamp:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
238 # filter duplicate event delivery in Ubuntu, Debian issue #1110
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
239 return
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
240
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
241 self.wheelTimestamp = event.Timestamp
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
242
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
243 delta = event.GetWheelRotation()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
244 factor = 1.05
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
245 if event.ControlDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
246 factor = 1.02
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
247 if hasattr(self.parent, "model") and event.ShiftDown():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
248 if not self.parent.model:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
249 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
250 count = 1 if not event.ControlDown() else 10
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
251 for i in range(count):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
252 if delta > 0: self.layerup()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
253 else: self.layerdown()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
254 return
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
255 x, y = event.GetPosition()
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
256 x, y, _ = self.mouse_to_3d(x, y)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
257 if delta > 0:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
258 self.zoom(factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
259 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
260 self.zoom(1 / factor, (x, y))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
261
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
262 def wheel(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
263 """react to mouse wheel actions:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
264 without shift: set max layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
265 with shift: zoom viewport
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
266 """
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
267 self.handle_wheel(event)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
268 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
269
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
270 def fit(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
271 if not self.parent.model or not self.parent.model.loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
272 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
273 self.canvas.SetCurrent(self.context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
274 dims = gcode_dims(self.parent.model.gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
275 self.reset_mview(1.0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
276 center_x = (dims[0][0] + dims[0][1]) / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
277 center_y = (dims[1][0] + dims[1][1]) / 2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
278 center_x = self.build_dimensions[0] / 2 - center_x
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
279 center_y = self.build_dimensions[1] / 2 - center_y
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
280 if self.orthographic:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
281 ratio = float(self.dist) / max(dims[0][2], dims[1][2])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
282 glScalef(ratio, ratio, 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
283 glTranslatef(center_x, center_y, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
284 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
285
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
286 def keypress(self, event):
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
287 """gets keypress events and moves/rotates active shape"""
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
288 if event.HasModifiers():
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
289 # let alt+c bubble up
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
290 event.Skip()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
291 return
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
292 step = event.ControlDown() and 1.05 or 1.1
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
293 key = event.GetKeyCode()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
294 if key in LAYER_UP_KEYS:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
295 self.layerup()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
296 return # prevent shifting focus to other controls
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
297 elif key in LAYER_DOWN_KEYS:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
298 self.layerdown()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
299 return
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
300 # x, y, _ = self.mouse_to_3d(self.width / 2, self.height / 2)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
301 elif key in ZOOM_IN_KEYS:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
302 self.zoom_to_center(step)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
303 return
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
304 elif key in ZOOM_OUT_KEYS:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
305 self.zoom_to_center(1 / step)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
306 return
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
307 elif key in FIT_KEYS:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
308 self.fit()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
309 elif key in CURRENT_LAYER_KEYS:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
310 if not self.parent.model or not self.parent.model.loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
311 return
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
312 self.parent.model.only_current = not self.parent.model.only_current
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
313 wx.CallAfter(self.Refresh)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
314 elif key in RESET_KEYS:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
315 self.resetview()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
316 event.Skip()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
317
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
318 def resetview(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
319 self.canvas.SetCurrent(self.context)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
320 self.reset_mview(0.9)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
321 self.basequat = [0, 0, 0, 1]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
322 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
323
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
324 class GCObject:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
325
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
326 def __init__(self, model):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
327 self.offsets = [0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
328 self.centeroffset = [0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
329 self.rot = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
330 self.curlayer = 0.0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
331 self.scale = [1.0, 1.0, 1.0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
332 self.model = model
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
333
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
334 class GcodeViewLoader:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
335
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
336 path_halfwidth = 0.2
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
337 path_halfheight = 0.15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
338
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
339 def addfile_perlayer(self, gcode = None, showall = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
340 self.model = create_model(self.root.settings.light3d
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
341 if self.root else False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
342 if isinstance(self.model, actors.GcodeModel):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
343 self.model.set_path_size(self.path_halfwidth, self.path_halfheight)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
344 self.objects[-1].model = self.model
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
345 if self.root:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
346 set_model_colors(self.model, self.root)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
347 if gcode is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
348 generator = self.model.load_data(gcode)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
349 generator_output = next(generator)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
350 while generator_output is not None:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
351 yield generator_output
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
352 generator_output = next(generator)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
353 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
354 yield None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
355
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
356 def addfile(self, gcode = None, showall = False):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
357 generator = self.addfile_perlayer(gcode, showall)
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
358 while next(generator) is not None:
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
359 continue
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
360
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
361 def set_gcview_params(self, path_width, path_height):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
362 return set_gcview_params(self, path_width, path_height)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
363
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
364 from printrun.gviz import BaseViz
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
365 class GcodeViewMainWrapper(GcodeViewLoader, BaseViz):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
366
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
367 def __init__(self, parent, build_dimensions, root, circular, antialias_samples, grid):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
368 self.root = root
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
369 self.glpanel = GcodeViewPanel(parent, realparent = self,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
370 build_dimensions = build_dimensions,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
371 antialias_samples = antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
372 self.glpanel.SetMinSize((150, 150))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
373 if self.root and hasattr(self.root, "gcview_color_background"):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
374 self.glpanel.color_background = self.root.gcview_color_background
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
375 self.clickcb = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
376 self.widget = self.glpanel
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
377 self.refresh_timer = wx.CallLater(100, self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
378 self.p = self # Hack for backwards compatibility with gviz API
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
379 self.grid = grid
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
380 self.platform = actors.Platform(build_dimensions, circular = circular, grid = grid)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
381 self.model = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
382 self.objects = [GCObject(self.platform), GCObject(None)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
383
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
384 def __getattr__(self, name):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
385 return getattr(self.glpanel, name)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
386
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
387 def on_settings_change(self, changed_settings):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
388 if self.model:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
389 for s in changed_settings:
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
390 if s.name.startswith('gcview_color_'):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
391 self.model.update_colors()
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
392 break
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
393
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
394 def set_current_gline(self, gline):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
395 if gline.is_move and gline.gcview_end_vertex is not None \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
396 and self.model and self.model.loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
397 self.model.printed_until = gline.gcview_end_vertex
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
398 if not self.refresh_timer.IsRunning():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
399 self.refresh_timer.Start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
400
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
401 def recreate_platform(self, build_dimensions, circular, grid):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
402 return recreate_platform(self, build_dimensions, circular, grid)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
403
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
404 def setlayer(self, layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
405 if layer in self.model.layer_idxs_map:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
406 viz_layer = self.model.layer_idxs_map[layer]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
407 self.parent.model.num_layers_to_draw = viz_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
408 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
409
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
410 def clear(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
411 self.model = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
412 self.objects[-1].model = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
413 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
414
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
415 class GcodeViewFrame(GvizBaseFrame, GcodeViewLoader):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
416 '''A simple class for using OpenGL with wxPython.'''
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
417
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
418 def __init__(self, parent, ID, title, build_dimensions, objects = None,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
419 pos = wx.DefaultPosition, size = wx.DefaultSize,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
420 style = wx.DEFAULT_FRAME_STYLE, root = None, circular = False,
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
421 antialias_samples = 0,
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
422 grid = (1, 10)):
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
423 GvizBaseFrame.__init__(self, parent, ID, title,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
424 pos, size, style)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
425 self.root = root
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
426
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
427 panel, vbox = self.create_base_ui()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
428
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
429 self.refresh_timer = wx.CallLater(100, self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
430 self.p = self # Hack for backwards compatibility with gviz API
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
431 self.clonefrom = objects
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
432 self.platform = actors.Platform(build_dimensions, circular = circular, grid = grid)
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
433 self.model = objects[1].model if objects else None
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
434 self.objects = [GCObject(self.platform), GCObject(None)]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
435
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
436 fit_image = wx.Image(imagefile('fit.png'), wx.BITMAP_TYPE_PNG).ConvertToBitmap()
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
437 self.toolbar.InsertTool(6, 8, " " + _("Fit to plate"), fit_image,
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
438 shortHelp = _("Fit to plate [F]"),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
439 longHelp = '')
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
440 self.toolbar.Realize()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
441 self.glpanel = GcodeViewPanel(panel,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
442 build_dimensions = build_dimensions,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
443 realparent = self,
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
444 antialias_samples = antialias_samples)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
445 vbox.Add(self.glpanel, 1, flag = wx.EXPAND)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
446
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
447 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
448 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
449 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.layerup(), id = 3)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
450 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.layerdown(), id = 4)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
451 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.resetview(), id = 5)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
452 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.fit(), id = 8)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
453 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.inject(), id = 6)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
454 self.Bind(wx.EVT_TOOL, lambda x: self.glpanel.editlayer(), id = 7)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
455
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
456 def setlayercb(self, layer):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
457 self.layerslider.SetValue(layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
458 self.update_status("")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
459
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
460 def update_status(self, extra):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
461 layer = self.model.num_layers_to_draw
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
462 filtered = [k for k, v in self.model.layer_idxs_map.items() if v == layer]
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
463 if filtered:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
464 true_layer = filtered[0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
465 z = self.model.gcode.all_layers[true_layer].z
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
466 message = _("Layer %d -%s Z = %.03f mm") % (layer, extra, z)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
467 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
468 message = _("Entire object")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
469 wx.CallAfter(self.SetStatusText, message, 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
470
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
471 def process_slider(self, event):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
472 new_layer = self.layerslider.GetValue()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
473 new_layer = min(self.model.max_layers + 1, new_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
474 new_layer = max(1, new_layer)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
475 self.model.num_layers_to_draw = new_layer
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
476 self.update_status("")
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
477 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
478
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
479 def set_current_gline(self, gline):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
480 if gline.is_move and gline.gcview_end_vertex is not None \
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
481 and self.model and self.model.loaded:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
482 self.model.printed_until = gline.gcview_end_vertex
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
483 if not self.refresh_timer.IsRunning():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
484 self.refresh_timer.Start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
485
46
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
486 def recreate_platform(self, build_dimensions, circular, grid):
cce0af6351f0 updated and added new files for printrun
mdd
parents: 15
diff changeset
487 return recreate_platform(self, build_dimensions, circular, grid)
15
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
488
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
489 def addfile(self, gcode = None):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
490 if self.clonefrom:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
491 self.model = self.clonefrom[-1].model.copy()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
492 self.objects[-1].model = self.model
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
493 else:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
494 GcodeViewLoader.addfile(self, gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
495 self.layerslider.SetRange(1, self.model.max_layers + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
496 self.layerslider.SetValue(self.model.max_layers + 1)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
497 wx.CallAfter(self.SetStatusText, _("Entire object"), 0)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
498 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
499
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
500 def clear(self):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
501 self.model = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
502 self.objects[-1].model = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
503 wx.CallAfter(self.Refresh)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
504
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
505 if __name__ == "__main__":
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
506 import sys
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
507 app = wx.App(redirect = False)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
508 build_dimensions = [200, 200, 100, 0, 0, 0]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
509 title = 'Gcode view, shift to move view, mousewheel to set layer'
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
510 frame = GcodeViewFrame(None, wx.ID_ANY, title, size = (400, 400),
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
511 build_dimensions = build_dimensions)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
512 gcode = gcoder.GCode(open(sys.argv[1]), get_home_pos(build_dimensions))
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
513 frame.addfile(gcode)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
514
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
515 first_move = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
516 for i in range(len(gcode.lines)):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
517 if gcode.lines[i].is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
518 first_move = gcode.lines[i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
519 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
520 last_move = None
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
521 for i in range(len(gcode.lines) - 1, -1, -1):
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
522 if gcode.lines[i].is_move:
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
523 last_move = gcode.lines[i]
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
524 break
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
525 nsteps = 20
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
526 steptime = 500
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
527 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
528 current_line = 0
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
529
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
530 def setLine():
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
531 global current_line
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
532 frame.set_current_gline(lines[current_line])
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
533 current_line = (current_line + 1) % len(lines)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
534 timer.Start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
535 timer = wx.CallLater(steptime, setLine)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
536 timer.Start()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
537
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
538 frame.Show(True)
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
539 app.MainLoop()
0bbb006204fc Added printrun sourcecode from
mbayer
parents:
diff changeset
540 app.Destroy()

mercurial