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
331 lines (275 loc) · 9.79 KB
/
wxDisplay.py
File metadata and controls
331 lines (275 loc) · 9.79 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
#!/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
try:
import wx
except ImportError:
raise ImportError("Please install wxPython.")
from OCC.Display import OCCViewer
class wxBaseViewer(wx.Panel):
def __init__(self, parent=None):
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 = None
self._inited = False
def GetWinId(self):
"""Returns the windows Id as an integer.
issue with GetHandle on Linux for wx versions
>3 or 4. Window must be displayed before GetHandle is
called. For that, just wait for a few milliseconds/seconds
before calling InitDriver
a solution is given here
see https://github.com/cztomczak/cefpython/issues/349
but raises an issue with wxPython 4.x
finally, it seems that the sleep function does the job
reported as a pythonocc issue
https://github.com/tpaviot/pythonocc-core/476
"""
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):
if self._inited:
self._display.OnResize()
def OnIdle(self, event):
pass
def OnMove(self, event):
pass
def OnFocus(self, event):
pass
def OnLostFocus(self, event):
pass
def OnMaximize(self, event):
pass
def OnLeftDown(self, event):
pass
def OnRightDown(self, event):
pass
def OnMiddleDown(self, event):
pass
def OnLeftUp(self, event):
pass
def OnRightUp(self, event):
pass
def OnMiddleUp(self, event):
pass
def OnMotion(self, event):
pass
def OnKeyDown(self, event):
pass
class wxViewer3d(wxBaseViewer):
def __init__(self, *kargs):
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 = {}
self.dragStartPos = None
def InitDriver(self):
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):
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):
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):
if self._inited:
self._display.Repaint()
def OnMove(self, event):
if self._inited:
self._display.Repaint()
def OnIdle(self, event):
if self._drawbox:
pass
elif self._inited:
self._display.Repaint()
def Test(self):
if self._inited:
self._display.Test()
def OnFocus(self, event):
if self._inited:
self._display.Repaint()
def OnLostFocus(self, event):
if self._inited:
self._display.Repaint()
def OnPaint(self, event):
if self._inited:
self._display.Repaint()
def ZoomAll(self, evt):
self._display.FitAll()
def Repaint(self, evt):
if self._inited:
self._display.Repaint()
def OnLeftDown(self, evt):
self.SetFocus()
self.dragStartPos = evt.GetPosition()
self._display.StartRotation(self.dragStartPos.x, self.dragStartPos.y)
def OnLeftUp(self, evt):
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):
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):
pass
def OnRightDown(self, evt):
self.dragStartPos = evt.GetPosition()
self._display.StartRotation(self.dragStartPos.x, self.dragStartPos.y)
def OnMiddleDown(self, evt):
self.dragStartPos = evt.GetPosition()
self._display.StartRotation(self.dragStartPos.x, self.dragStartPos.y)
def OnWheelScroll(self, evt):
# 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):
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):
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():
class AppFrame(wx.Frame):
def __init__(self, parent):
wx.Frame.__init__(
self,
parent,
-1,
"wxDisplay3d sample",
style=wx.DEFAULT_FRAME_STYLE,
size=(640, 480),
)
self.canva = wxViewer3d(self)
def runTests(self):
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()