printrun-src/printrun/stlview.py

changeset 46
cce0af6351f0
parent 15
0bbb006204fc
equal deleted inserted replaced
45:c82943fb205f 46:cce0af6351f0
1 #!/usr/bin/env python 1 #!/usr/bin/env python3
2 2
3 # This file is part of the Printrun suite. 3 # This file is part of the Printrun suite.
4 # 4 #
5 # Printrun is free software: you can redistribute it and/or modify 5 # Printrun is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by 6 # it under the terms of the GNU General Public License as published by
40 from .gl.libtatlin import actors 40 from .gl.libtatlin import actors
41 41
42 def vec(*args): 42 def vec(*args):
43 return (GLfloat * len(args))(*args) 43 return (GLfloat * len(args))(*args)
44 44
45 class stlview(object): 45 class stlview:
46 def __init__(self, facets, batch): 46 def __init__(self, facets, batch):
47 # Create the vertex and normal arrays. 47 # Create the vertex and normal arrays.
48 vertices = [] 48 vertices = []
49 normals = [] 49 normals = []
50 50
52 for j in i[1]: 52 for j in i[1]:
53 vertices.extend(j) 53 vertices.extend(j)
54 normals.extend(i[0]) 54 normals.extend(i[0])
55 55
56 # Create a list of triangle indices. 56 # Create a list of triangle indices.
57 indices = range(3 * len(facets)) # [[3*i, 3*i+1, 3*i+2] for i in xrange(len(facets))] 57 indices = list(range(3 * len(facets))) # [[3*i, 3*i+1, 3*i+2] for i in xrange(len(facets))]
58 self.vertex_list = batch.add_indexed(len(vertices) // 3, 58 self.vertex_list = batch.add_indexed(len(vertices) // 3,
59 GL_TRIANGLES, 59 GL_TRIANGLES,
60 None, # group, 60 None, # group,
61 indices, 61 indices,
62 ('v3f/static', vertices), 62 ('v3f/static', vertices),
67 67
68 class StlViewPanel(wxGLPanel): 68 class StlViewPanel(wxGLPanel):
69 69
70 do_lights = False 70 do_lights = False
71 71
72 def __init__(self, parent, size, id = wx.ID_ANY, 72 def __init__(self, parent, size,
73 build_dimensions = None, circular = False, 73 build_dimensions = None, circular = False,
74 antialias_samples = 0): 74 antialias_samples = 0,
75 super(StlViewPanel, self).__init__(parent, id, wx.DefaultPosition, size, 0, 75 grid = (1, 10)):
76 super().__init__(parent, wx.DefaultPosition, size, 0,
76 antialias_samples = antialias_samples) 77 antialias_samples = antialias_samples)
77 self.batches = [] 78 self.batches = []
78 self.rot = 0 79 self.rot = 0
79 self.canvas.Bind(wx.EVT_MOUSE_EVENTS, self.move) 80 self.canvas.Bind(wx.EVT_MOUSE_EVENTS, self.move)
80 self.canvas.Bind(wx.EVT_MOUSEWHEEL, self.wheel) 81 self.canvas.Bind(wx.EVT_MOUSEWHEEL, self.wheel)
85 if build_dimensions: 86 if build_dimensions:
86 self.build_dimensions = build_dimensions 87 self.build_dimensions = build_dimensions
87 else: 88 else:
88 self.build_dimensions = [200, 200, 100, 0, 0, 0] 89 self.build_dimensions = [200, 200, 100, 0, 0, 0]
89 self.platform = actors.Platform(self.build_dimensions, 90 self.platform = actors.Platform(self.build_dimensions,
90 circular = circular) 91 circular = circular,
92 grid = grid)
91 self.dist = max(self.build_dimensions[0], self.build_dimensions[1]) 93 self.dist = max(self.build_dimensions[0], self.build_dimensions[1])
92 self.basequat = [0, 0, 0, 1] 94 self.basequat = [0, 0, 0, 1]
93 wx.CallAfter(self.forceresize) 95 wx.CallAfter(self.forceresize) #why needed
94 self.mousepos = (0, 0) 96 self.mousepos = (0, 0)
95 97
96 def OnReshape(self): 98 def OnReshape(self):
97 self.mview_initialized = False 99 self.mview_initialized = False
98 super(StlViewPanel, self).OnReshape() 100 super(StlViewPanel, self).OnReshape()
153 def double_click(self, event): 155 def double_click(self, event):
154 if hasattr(self.parent, "clickcb") and self.parent.clickcb: 156 if hasattr(self.parent, "clickcb") and self.parent.clickcb:
155 self.parent.clickcb(event) 157 self.parent.clickcb(event)
156 158
157 def forceresize(self): 159 def forceresize(self):
158 self.SetClientSize((self.GetClientSize()[0], self.GetClientSize()[1] + 1)) 160 #print('forceresize')
159 self.SetClientSize((self.GetClientSize()[0], self.GetClientSize()[1] - 1)) 161 x, y = self.GetClientSize()
162 #TODO: probably not needed
163 self.SetClientSize((x, y+1))
164 self.SetClientSize((x, y))
160 self.initialized = False 165 self.initialized = False
161 166
162 def move(self, event): 167 def move(self, event):
163 """react to mouse actions: 168 """react to mouse actions:
164 no mouse: show red mousedrop 169 no mouse: show red mousedrop
165 LMB: move active object, 170 LMB: move active object,
166 with shift rotate viewport 171 with shift rotate viewport
167 RMB: nothing 172 RMB: nothing
168 with shift move viewport 173 with shift move viewport
169 """ 174 """
170 self.mousepos = event.GetPositionTuple() 175 self.mousepos = event.GetPosition()
171 if event.Dragging() and event.LeftIsDown(): 176 if event.Dragging():
172 self.handle_rotation(event) 177 if event.LeftIsDown():
173 elif event.Dragging() and event.RightIsDown(): 178 self.handle_rotation(event)
174 self.handle_translation(event) 179 elif event.RightIsDown():
175 elif event.ButtonUp(wx.MOUSE_BTN_LEFT): 180 self.handle_translation(event)
176 if self.initpos is not None: 181 self.Refresh(False)
177 self.initpos = None 182 elif event.ButtonUp(wx.MOUSE_BTN_LEFT) or \
178 elif event.ButtonUp(wx.MOUSE_BTN_RIGHT): 183 event.ButtonUp(wx.MOUSE_BTN_RIGHT):
179 if self.initpos is not None: 184 self.initpos = None
180 self.initpos = None
181 else:
182 event.Skip()
183 return
184 event.Skip() 185 event.Skip()
185 wx.CallAfter(self.Refresh)
186 186
187 def handle_wheel(self, event): 187 def handle_wheel(self, event):
188 delta = event.GetWheelRotation() 188 delta = event.GetWheelRotation()
189 factor = 1.05 189 factor = 1.05
190 x, y = event.GetPositionTuple() 190 x, y = event.GetPosition()
191 x, y, _ = self.mouse_to_3d(x, y, local_transform = True) 191 x, y, _ = self.mouse_to_3d(x, y, local_transform = True)
192 if delta > 0: 192 if delta > 0:
193 self.zoom(factor, (x, y)) 193 self.zoom(factor, (x, y))
194 else: 194 else:
195 self.zoom(1 / factor, (x, y)) 195 self.zoom(1 / factor, (x, y))
255 def create_objects(self): 255 def create_objects(self):
256 '''create opengl objects when opengl is initialized''' 256 '''create opengl objects when opengl is initialized'''
257 if not self.platform.initialized: 257 if not self.platform.initialized:
258 self.platform.init() 258 self.platform.init()
259 self.initialized = 1 259 self.initialized = 1
260 #TODO: this probably creates constant redraw
261 # create_objects is called during OnDraw, remove
260 wx.CallAfter(self.Refresh) 262 wx.CallAfter(self.Refresh)
261 263
262 def prepare_model(self, m, scale): 264 def prepare_model(self, m, scale):
263 batch = pyglet.graphics.Batch() 265 batch = pyglet.graphics.Batch()
264 stlview(m.facets, batch = batch) 266 stlview(m.facets, batch = batch)

mercurial