forked from tpaviot/pythonocc-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwxDisplay.py
More file actions
453 lines (391 loc) · 12.8 KB
/
wxDisplay.py
File metadata and controls
453 lines (391 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#!/usr/bin/env python
##Copyright 2008-2017 Thomas Paviot (tpaviot@gmail.com)
##
##This file is part of pythonOCC.
##
##pythonOCC is free software: you can redistribute it and/or modify
##it under the terms of the GNU Lesser General Public License as published by
##the Free Software Foundation, either version 3 of the License, or
##(at your option) any later version.
##
##pythonOCC is distributed in the hope that it will be useful,
##but WITHOUT ANY WARRANTY; without even the implied warranty of
##MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
##GNU Lesser General Public License for more details.
##
##You should have received a copy of the GNU Lesser General Public License
##along with pythonOCC. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
import time
from typing import Any, Callable, Dict, Optional
try:
import wx
except ImportError:
raise ImportError("Please install wxPython.")
from OCC.Display import OCCViewer
class wxBaseViewer(wx.Panel):
"""
The base wx.Panel for an OCC viewer.
"""
def __init__(self, parent: Optional[Any] = None) -> None:
"""
Initializes the wxBaseViewer.
Args:
parent (wx.Window, optional): The parent window.
"""
wx.Panel.__init__(self, parent)
self.Bind(wx.EVT_SIZE, self.OnSize)
self.Bind(wx.EVT_IDLE, self.OnIdle)
self.Bind(wx.EVT_MOVE, self.OnMove)
self.Bind(wx.EVT_SET_FOCUS, self.OnFocus)
self.Bind(wx.EVT_KILL_FOCUS, self.OnLostFocus)
self.Bind(wx.EVT_MAXIMIZE, self.OnMaximize)
self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftDown)
self.Bind(wx.EVT_RIGHT_DOWN, self.OnRightDown)
self.Bind(wx.EVT_MIDDLE_DOWN, self.OnMiddleDown)
self.Bind(wx.EVT_LEFT_UP, self.OnLeftUp)
self.Bind(wx.EVT_RIGHT_UP, self.OnRightUp)
self.Bind(wx.EVT_MIDDLE_UP, self.OnMiddleUp)
self.Bind(wx.EVT_MOTION, self.OnMotion)
self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
self.Bind(wx.EVT_MOUSEWHEEL, self.OnWheelScroll)
self._display: Optional[OCCViewer.Viewer3d] = None
self._inited = False
def GetWinId(self) -> int:
"""
Returns the windows Id as an integer.
On Linux, the window must be displayed before GetHandle is called.
For that, we wait for a few milliseconds/seconds before calling InitDriver.
"""
timeout = 10 # 10 seconds
win_id = self.GetHandle()
init_time = time.time()
delta_t = 0.0 # elapsed time, initialized to 0 before the while loop
# if ever win_id is 0, enter the loop until it gets a value
while win_id == 0 and delta_t < timeout:
time.sleep(0.1)
wx.SafeYield()
win_id = self.GetHandle()
delta_t = time.time() - init_time
# check that win_id is different from 0
if win_id == 0:
raise AssertionError("Can't get win Id")
# otherwise returns the window Id
return win_id
def OnSize(self, event: Any) -> None:
"""
Called when the widget is resized.
"""
if self._inited:
self._display.OnResize()
def OnIdle(self, event: Any) -> None:
"""
Called when the application is idle.
"""
pass
def OnMove(self, event: Any) -> None:
"""
Called when the widget is moved.
"""
pass
def OnFocus(self, event: Any) -> None:
"""
Called when the widget gains focus.
"""
pass
def OnLostFocus(self, event: Any) -> None:
"""
Called when the widget loses focus.
"""
pass
def OnMaximize(self, event: Any) -> None:
"""
Called when the widget is maximized.
"""
pass
def OnLeftDown(self, event: Any) -> None:
"""
Called when the left mouse button is pressed.
"""
pass
def OnRightDown(self, event: Any) -> None:
"""
Called when the right mouse button is pressed.
"""
pass
def OnMiddleDown(self, event: Any) -> None:
"""
Called when the middle mouse button is pressed.
"""
pass
def OnLeftUp(self, event: Any) -> None:
"""
Called when the left mouse button is released.
"""
pass
def OnRightUp(self, event: Any) -> None:
"""
Called when the right mouse button is released.
"""
pass
def OnMiddleUp(self, event: Any) -> None:
"""
Called when the middle mouse button is released.
"""
pass
def OnMotion(self, event: Any) -> None:
"""
Called when the mouse is moved.
"""
pass
def OnKeyDown(self, event: Any) -> None:
"""
Called when a key is pressed.
"""
pass
class wxViewer3d(wxBaseViewer):
"""
A wx.Panel for an OCC viewer.
"""
def __init__(self, *kargs: Any) -> None:
"""
Initializes the wxViewer3d.
"""
wxBaseViewer.__init__(self, *kargs)
self._drawbox = False
self._zoom_area = False
self._select_area = False
self._inited = False
self._leftisdown = False
self._middleisdown = False
self._rightisdown = False
self._selection = None
self._scrollwheel = False
self._key_map: Dict[int, Callable] = {}
self.dragStartPos = None
def InitDriver(self) -> None:
"""
Initializes the driver.
"""
self._display = OCCViewer.Viewer3d()
self._display.Create(window_handle=self.GetWinId(), parent=self)
self._display.SetModeShaded()
self._inited = True
# dict mapping keys to functions
self._SetupKeyMap()
def _SetupKeyMap(self) -> None:
"""
Sets up the key map.
"""
def set_shade_mode():
self._display.DisableAntiAliasing()
self._display.SetModeShaded()
self._key_map = {
ord("W"): self._display.SetModeWireFrame,
ord("S"): set_shade_mode,
ord("A"): self._display.EnableAntiAliasing,
ord("B"): self._display.DisableAntiAliasing,
ord("H"): self._display.SetModeHLR,
ord("G"): self._display.SetSelectionModeVertex,
306: lambda: print("Shift pressed"),
}
def OnKeyDown(self, evt: Any) -> None:
"""
Called when a key is pressed.
"""
code = evt.GetKeyCode()
try:
self._key_map[code]()
print("Key pressed: %i" % code)
except KeyError:
print("Unrecognized key pressed %i" % code)
def OnMaximize(self, event: Any) -> None:
"""
Called when the widget is maximized.
"""
if self._inited:
self._display.Repaint()
def OnMove(self, event: Any) -> None:
"""
Called when the widget is moved.
"""
if self._inited:
self._display.Repaint()
def OnIdle(self, event: Any) -> None:
"""
Called when the application is idle.
"""
if self._drawbox:
pass
elif self._inited:
self._display.Repaint()
def Test(self) -> None:
"""
Runs a test.
"""
if self._inited:
self._display.Test()
def OnFocus(self, event: Any) -> None:
"""
Called when the widget gains focus.
"""
if self._inited:
self._display.Repaint()
def OnLostFocus(self, event: Any) -> None:
"""
Called when the widget loses focus.
"""
if self._inited:
self._display.Repaint()
def OnPaint(self, event: Any) -> None:
"""
Called when the widget is painted.
"""
if self._inited:
self._display.Repaint()
def ZoomAll(self, evt: Any) -> None:
"""
Zooms to fit all objects in the view.
"""
self._display.FitAll()
def Repaint(self, evt: Any) -> None:
"""
Repaints the view.
"""
if self._inited:
self._display.Repaint()
def OnLeftDown(self, evt: Any) -> None:
"""
Called when the left mouse button is pressed.
"""
self.SetFocus()
self.dragStartPos = evt.GetPosition()
self._display.StartRotation(self.dragStartPos.x, self.dragStartPos.y)
def OnLeftUp(self, evt: Any) -> None:
"""
Called when the left mouse button is released.
"""
pt = evt.GetPosition()
if self._select_area:
[Xmin, Ymin, dx, dy] = self._drawbox
self._display.SelectArea(Xmin, Ymin, Xmin + dx, Ymin + dy)
self._select_area = False
else:
self._display.Select(pt.x, pt.y)
def OnRightUp(self, evt: Any) -> None:
"""
Called when the right mouse button is released.
"""
if self._zoom_area:
[Xmin, Ymin, dx, dy] = self._drawbox
self._display.ZoomArea(Xmin, Ymin, Xmin + dx, Ymin + dy)
self._zoom_area = False
def OnMiddleUp(self, evt: Any) -> None:
"""
Called when the middle mouse button is released.
"""
pass
def OnRightDown(self, evt: Any) -> None:
"""
Called when the right mouse button is pressed.
"""
self.dragStartPos = evt.GetPosition()
self._display.StartRotation(self.dragStartPos.x, self.dragStartPos.y)
def OnMiddleDown(self, evt: Any) -> None:
"""
Called when the middle mouse button is pressed.
"""
self.dragStartPos = evt.GetPosition()
self._display.StartRotation(self.dragStartPos.x, self.dragStartPos.y)
def OnWheelScroll(self, evt: Any) -> None:
"""
Called when the mouse wheel is scrolled.
"""
# Zooming by wheel
zoom_factor = 2.0 if evt.GetWheelRotation() > 0 else 0.5
self._display.Repaint()
self._display.ZoomFactor(zoom_factor)
def DrawBox(self, event: Any) -> None:
"""
Draws a selection box.
"""
tolerance = 2
pt = event.GetPosition()
dx = pt.x - self.dragStartPos.x
dy = pt.y - self.dragStartPos.y
if abs(dx) <= tolerance and abs(dy) <= tolerance:
return
dc = wx.ClientDC(self)
dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT))
dc.SetBrush(wx.TRANSPARENT_BRUSH)
dc.SetLogicalFunction(wx.XOR)
if self._drawbox:
r = wx.Rect(*self._drawbox)
dc.DrawRectangle(r)
r = wx.Rect(self.dragStartPos.x, self.dragStartPos.y, dx, dy)
dc.DrawRectangle(r)
self._drawbox = [self.dragStartPos.x, self.dragStartPos.y, dx, dy]
def OnMotion(self, evt: Any) -> None:
"""
Called when the mouse is moved.
"""
pt = evt.GetPosition()
# ROTATE
if evt.LeftIsDown() and not evt.ShiftDown():
self._display.Rotation(pt.x, pt.y)
self._drawbox = False
# DYNAMIC ZOOM
elif evt.RightIsDown() and not evt.ShiftDown():
self._display.Repaint()
self._display.DynamicZoom(
abs(self.dragStartPos.x), abs(self.dragStartPos.y), abs(pt.x), abs(pt.y)
)
self.dragStartPos.x = pt.x
self.dragStartPos.y = pt.y
self._drawbox = False
# PAN
elif evt.MiddleIsDown():
dx = pt.x - self.dragStartPos.x
dy = pt.y - self.dragStartPos.y
self.dragStartPos.x = pt.x
self.dragStartPos.y = pt.y
self._display.Pan(dx, -dy)
self._drawbox = False
# DRAW BOX
elif evt.RightIsDown() and evt.ShiftDown(): # ZOOM WINDOW
self._zoom_area = True
self.DrawBox(evt)
elif evt.LeftIsDown() and evt.ShiftDown(): # SELECT AREA
self._select_area = True
self.DrawBox(evt)
else:
self._drawbox = False
self._display.MoveTo(pt.x, pt.y)
def TestWxDisplay() -> None:
"""
A test function for the wxViewer3d.
"""
class AppFrame(wx.Frame):
def __init__(self, parent: Optional[Any]) -> None:
wx.Frame.__init__(
self,
parent,
-1,
"wxDisplay3d sample",
style=wx.DEFAULT_FRAME_STYLE,
size=(640, 480),
)
self.canva = wxViewer3d(self)
def runTests(self) -> None:
self.canva._display.Test()
app = wx.App(False)
wx.InitAllImageHandlers()
frame = AppFrame(None)
frame.Show(True)
wx.SafeYield()
frame.canva.InitDriver()
frame.runTests()
app.SetTopWindow(frame)
app.MainLoop()
if __name__ == "__main__":
TestWxDisplay()