wxVTKRenderWindow.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  1. """
  2. A simple VTK widget for wxPython.
  3. Find wxPython info at http://wxPython.org
  4. Created by David Gobbi, December 2001
  5. Based on vtkTkRenderWindget.py
  6. Updated to new wx namespace and some cleaning by Andrea Gavana,
  7. December 2006
  8. """
  9. """
  10. Please see the example at the end of this file.
  11. ----------------------------------------
  12. Creation:
  13. wxVTKRenderWindow(parent, ID, stereo=0, [wx keywords]):
  14. You should create a wx.App(False) or some other wx.App subclass
  15. before creating the window.
  16. ----------------------------------------
  17. Methods:
  18. Render()
  19. AddRenderer(ren)
  20. GetRenderers()
  21. GetRenderWindow()
  22. ----------------------------------------
  23. Methods to override (all take a wx.Event):
  24. OnButtonDown(event) default: propagate event to Left, Right, Middle
  25. OnLeftDown(event) default: set _Mode to 'Rotate'
  26. OnRightDown(event) default: set _Mode to 'Zoom'
  27. OnMiddleDown(event) default: set _Mode to 'Pan'
  28. OnButtonUp(event) default: propagate event to L, R, M and unset _Mode
  29. OnLeftUp(event)
  30. OnRightUp(event)
  31. OnMiddleUp(event)
  32. OnMotion(event) default: call appropriate handler for _Mode
  33. OnEnterWindow(event) default: set focus to this window
  34. OnLeaveWindow(event) default: release focus
  35. OnKeyDown(event) default: [R]eset, [W]irefreme, [S]olid, [P]ick
  36. OnKeyUp(event)
  37. OnChar(event)
  38. OnSetFocus(event)
  39. OnKillFocus(event)
  40. OnSize(event)
  41. OnMove(event)
  42. OnPaint(event) default: Render()
  43. ----------------------------------------
  44. Protected Members:
  45. _Mode: Current mode: 'Rotate', 'Zoom', 'Pan'
  46. _LastX, _LastY: The (x,y) coordinates of the previous event
  47. _CurrentRenderer: The renderer that was most recently clicked in
  48. _CurrentCamera: The camera for the current renderer
  49. ----------------------------------------
  50. Private Members:
  51. __Handle: Handle to the window containing the vtkRenderWindow
  52. """
  53. # import usual libraries
  54. import math, os, sys
  55. import wx
  56. from vtkmodules.vtkRenderingCore import vtkCellPicker, vtkProperty, vtkRenderWindow
  57. # a few configuration items, see what works best on your system
  58. # Use GLCanvas as base class instead of wx.Window.
  59. # This is sometimes necessary under wxGTK or the image is blank.
  60. # (in wxWindows 2.3.1 and earlier, the GLCanvas had scroll bars)
  61. baseClass = wx.Window
  62. if wx.Platform == "__WXGTK__":
  63. import wx.glcanvas
  64. baseClass = wx.glcanvas.GLCanvas
  65. # Keep capturing mouse after mouse is dragged out of window
  66. # (in wxGTK 2.3.2 there is a bug that keeps this from working,
  67. # but it is only relevant in wxGTK if there are multiple windows)
  68. _useCapture = (wx.Platform == "__WXMSW__")
  69. # end of configuration items
  70. class wxVTKRenderWindow(baseClass):
  71. """
  72. A wxRenderWindow for wxPython.
  73. Use GetRenderWindow() to get the vtkRenderWindow.
  74. Create with the keyword stereo=1 in order to
  75. generate a stereo-capable window.
  76. """
  77. def __init__(self, parent, ID, *args, **kw):
  78. """Default class constructor.
  79. @param parent: parent window
  80. @param ID: window id
  81. @param **kw: wxPython keywords (position, size, style) plus the
  82. 'stereo' keyword
  83. """
  84. # miscellaneous protected variables
  85. self._CurrentRenderer = None
  86. self._CurrentCamera = None
  87. self._CurrentZoom = 1.0
  88. self._CurrentLight = None
  89. self._ViewportCenterX = 0
  90. self._ViewportCenterY = 0
  91. self._Picker = vtkCellPicker()
  92. self._PickedActor = None
  93. self._PickedProperty = vtkProperty()
  94. self._PickedProperty.SetColor(1,0,0)
  95. self._PrePickedProperty = None
  96. # these record the previous mouse position
  97. self._LastX = 0
  98. self._LastY = 0
  99. # the current interaction mode (Rotate, Pan, Zoom, etc)
  100. self._Mode = None
  101. self._ActiveButton = None
  102. # private attributes
  103. self.__OldFocus = None
  104. # used by the LOD actors
  105. self._DesiredUpdateRate = 15
  106. self._StillUpdateRate = 0.0001
  107. # First do special handling of some keywords:
  108. # stereo, position, size, width, height, style
  109. try:
  110. stereo = bool(kw['stereo'])
  111. del kw['stereo']
  112. except KeyError:
  113. stereo = False
  114. try:
  115. position = kw['position']
  116. del kw['position']
  117. except KeyError:
  118. position = wx.DefaultPosition
  119. try:
  120. size = kw['size']
  121. del kw['size']
  122. except KeyError:
  123. try:
  124. size = parent.GetSize()
  125. except AttributeError:
  126. size = wx.DefaultSize
  127. # wx.WANTS_CHARS says to give us e.g. TAB
  128. # wx.NO_FULL_REPAINT_ON_RESIZE cuts down resize flicker under GTK
  129. style = wx.WANTS_CHARS | wx.NO_FULL_REPAINT_ON_RESIZE
  130. try:
  131. style = style | kw['style']
  132. del kw['style']
  133. except KeyError:
  134. pass
  135. # the enclosing frame must be shown under GTK or the windows
  136. # don't connect together properly
  137. l = []
  138. p = parent
  139. while p: # make a list of all parents
  140. l.append(p)
  141. p = p.GetParent()
  142. l.reverse() # sort list into descending order
  143. for p in l:
  144. p.Show(1)
  145. # initialize the wx.Window
  146. if baseClass.__name__ == 'GLCanvas':
  147. # Set the doublebuffer attribute of the GL canvas.
  148. baseClass.__init__(self, parent, ID, pos=position, size=size,
  149. style=style,
  150. attribList=[wx.glcanvas.WX_GL_DOUBLEBUFFER])
  151. else:
  152. baseClass.__init__(self, parent, ID, pos=position, size=size,
  153. style=style)
  154. # create the RenderWindow and initialize it
  155. self._RenderWindow = vtkRenderWindow()
  156. self._RenderWindow.SetSize(size.width, size.height)
  157. if stereo:
  158. self._RenderWindow.StereoCapableWindowOn()
  159. self._RenderWindow.SetStereoTypeToCrystalEyes()
  160. self.__handle = None
  161. # refresh window by doing a Render
  162. self.Bind(wx.EVT_PAINT, self.OnPaint)
  163. # turn off background erase to reduce flicker
  164. self.Bind(wx.EVT_ERASE_BACKGROUND, lambda e: None)
  165. # Bind the events to the event converters
  166. self.Bind(wx.EVT_RIGHT_DOWN, self._OnButtonDown)
  167. self.Bind(wx.EVT_LEFT_DOWN, self._OnButtonDown)
  168. self.Bind(wx.EVT_MIDDLE_DOWN, self._OnButtonDown)
  169. self.Bind(wx.EVT_RIGHT_UP, self._OnButtonUp)
  170. self.Bind(wx.EVT_LEFT_UP, self._OnButtonUp)
  171. self.Bind(wx.EVT_MIDDLE_UP, self._OnButtonUp)
  172. self.Bind(wx.EVT_MOTION, self.OnMotion)
  173. self.Bind(wx.EVT_ENTER_WINDOW, self._OnEnterWindow)
  174. self.Bind(wx.EVT_LEAVE_WINDOW, self._OnLeaveWindow)
  175. self.Bind(wx.EVT_CHAR, self.OnChar)
  176. # If we use EVT_KEY_DOWN instead of EVT_CHAR, capital versions
  177. # of all characters are always returned. EVT_CHAR also performs
  178. # other necessary keyboard-dependent translations.
  179. self.Bind(wx.EVT_CHAR, self.OnKeyDown)
  180. self.Bind(wx.EVT_KEY_UP, self.OnKeyUp)
  181. self.Bind(wx.EVT_SIZE, self._OnSize)
  182. self.Bind(wx.EVT_MOVE, self.OnMove)
  183. self.Bind(wx.EVT_SET_FOCUS, self.OnSetFocus)
  184. self.Bind(wx.EVT_KILL_FOCUS, self.OnKillFocus)
  185. def SetDesiredUpdateRate(self, rate):
  186. """Mirrors the method with the same name in
  187. vtkRenderWindowInteractor.
  188. """
  189. self._DesiredUpdateRate = rate
  190. def GetDesiredUpdateRate(self):
  191. """Mirrors the method with the same name in
  192. vtkRenderWindowInteractor.
  193. """
  194. return self._DesiredUpdateRate
  195. def SetStillUpdateRate(self, rate):
  196. """Mirrors the method with the same name in
  197. vtkRenderWindowInteractor.
  198. """
  199. self._StillUpdateRate = rate
  200. def GetStillUpdateRate(self):
  201. """Mirrors the method with the same name in
  202. vtkRenderWindowInteractor.
  203. """
  204. return self._StillUpdateRate
  205. def OnPaint(self, event):
  206. """Handles the wx.EVT_PAINT event for wxVTKRenderWindow.
  207. """
  208. dc = wx.PaintDC(self)
  209. self.Render()
  210. def _OnSize(self, event):
  211. """Handles the wx.EVT_SIZE event for wxVTKRenderWindow.
  212. """
  213. if wx.Platform != '__WXMSW__':
  214. width, height = event.GetSize()
  215. self._RenderWindow.SetSize(width, height)
  216. self.OnSize(event)
  217. self.Render()
  218. def OnSize(self, event):
  219. """Overridable event.
  220. """
  221. pass
  222. def OnMove(self, event):
  223. """Overridable event.
  224. """
  225. pass
  226. def _OnEnterWindow(self, event):
  227. """Handles the wx.EVT_ENTER_WINDOW event for
  228. wxVTKRenderWindow.
  229. """
  230. self.UpdateRenderer(event)
  231. self.OnEnterWindow(event)
  232. def OnEnterWindow(self, event):
  233. """Overridable event.
  234. """
  235. if self.__OldFocus == None:
  236. self.__OldFocus = wx.Window.FindFocus()
  237. self.SetFocus()
  238. def _OnLeaveWindow(self, event):
  239. """Handles the wx.EVT_LEAVE_WINDOW event for
  240. wxVTKRenderWindow.
  241. """
  242. self.OnLeaveWindow(event)
  243. def OnLeaveWindow(self, event):
  244. """Overridable event.
  245. """
  246. if self.__OldFocus:
  247. self.__OldFocus.SetFocus()
  248. self.__OldFocus = None
  249. def OnSetFocus(self, event):
  250. """Overridable event.
  251. """
  252. pass
  253. def OnKillFocus(self, event):
  254. """Overridable event.
  255. """
  256. pass
  257. def _OnButtonDown(self, event):
  258. """Handles the wx.EVT_LEFT/RIGHT/MIDDLE_DOWN events for
  259. wxVTKRenderWindow.
  260. """
  261. # helper function for capturing mouse until button released
  262. self._RenderWindow.SetDesiredUpdateRate(self._DesiredUpdateRate)
  263. if event.RightDown():
  264. button = "Right"
  265. elif event.LeftDown():
  266. button = "Left"
  267. elif event.MiddleDown():
  268. button = "Middle"
  269. else:
  270. button = None
  271. # save the button and capture mouse until the button is released
  272. if button and not self._ActiveButton:
  273. self._ActiveButton = button
  274. if _useCapture:
  275. self.CaptureMouse()
  276. self.OnButtonDown(event)
  277. def OnButtonDown(self, event):
  278. """Overridable event.
  279. """
  280. if not self._Mode:
  281. # figure out what renderer the mouse is over
  282. self.UpdateRenderer(event)
  283. if event.LeftDown():
  284. self.OnLeftDown(event)
  285. elif event.RightDown():
  286. self.OnRightDown(event)
  287. elif event.MiddleDown():
  288. self.OnMiddleDown(event)
  289. def OnLeftDown(self, event):
  290. """Overridable event.
  291. """
  292. if not self._Mode:
  293. if event.ControlDown():
  294. self._Mode = "Zoom"
  295. elif event.ShiftDown():
  296. self._Mode = "Pan"
  297. else:
  298. self._Mode = "Rotate"
  299. def OnRightDown(self, event):
  300. """Overridable event.
  301. """
  302. if not self._Mode:
  303. self._Mode = "Zoom"
  304. def OnMiddleDown(self, event):
  305. """Overridable event.
  306. """
  307. if not self._Mode:
  308. self._Mode = "Pan"
  309. def _OnButtonUp(self, event):
  310. """Handles the wx.EVT_LEFT/RIGHT/MIDDLE_UP events for
  311. wxVTKRenderWindow.
  312. """
  313. # helper function for releasing mouse capture
  314. self._RenderWindow.SetDesiredUpdateRate(self._StillUpdateRate)
  315. if event.RightUp():
  316. button = "Right"
  317. elif event.LeftUp():
  318. button = "Left"
  319. elif event.MiddleUp():
  320. button = "Middle"
  321. else:
  322. button = None
  323. # if the ActiveButton is released, then release mouse capture
  324. if self._ActiveButton and button == self._ActiveButton:
  325. if _useCapture:
  326. self.ReleaseMouse()
  327. self._ActiveButton = None
  328. self.OnButtonUp(event)
  329. def OnButtonUp(self, event):
  330. """Overridable event.
  331. """
  332. if event.LeftUp():
  333. self.OnLeftUp(event)
  334. elif event.RightUp():
  335. self.OnRightUp(event)
  336. elif event.MiddleUp():
  337. self.OnMiddleUp(event)
  338. # if not interacting, then do nothing more
  339. if self._Mode:
  340. if self._CurrentRenderer:
  341. self.Render()
  342. self._Mode = None
  343. def OnLeftUp(self, event):
  344. """Overridable event.
  345. """
  346. pass
  347. def OnRightUp(self, event):
  348. """Overridable event.
  349. """
  350. pass
  351. def OnMiddleUp(self, event):
  352. """Overridable event.
  353. """
  354. pass
  355. def OnMotion(self, event):
  356. """Overridable event.
  357. """
  358. if self._Mode == "Pan":
  359. self.Pan(event)
  360. elif self._Mode == "Rotate":
  361. self.Rotate(event)
  362. elif self._Mode == "Zoom":
  363. self.Zoom(event)
  364. def OnChar(self, event):
  365. """Overridable event.
  366. """
  367. pass
  368. def OnKeyDown(self, event):
  369. """Handles the wx.EVT_KEY_DOWN events for wxVTKRenderWindow.
  370. """
  371. if event.GetKeyCode() == ord('r'):
  372. self.Reset(event)
  373. if event.GetKeyCode() == ord('w'):
  374. self.Wireframe()
  375. if event.GetKeyCode() == ord('s'):
  376. self.Surface()
  377. if event.GetKeyCode() == ord('p'):
  378. self.PickActor(event)
  379. if event.GetKeyCode() < 256:
  380. self.OnChar(event)
  381. def OnKeyUp(self, event):
  382. """Overridable event.
  383. """
  384. pass
  385. def GetZoomFactor(self):
  386. """Returns the current zoom factor.
  387. """
  388. return self._CurrentZoom
  389. def GetRenderWindow(self):
  390. """Returns the render window (vtkRenderWindow).
  391. """
  392. return self._RenderWindow
  393. def GetPicker(self):
  394. """Returns the current picker (vtkCellPicker).
  395. """
  396. return self._Picker
  397. def Render(self):
  398. """Actually renders the VTK scene on screen.
  399. """
  400. if self._CurrentLight:
  401. light = self._CurrentLight
  402. light.SetPosition(self._CurrentCamera.GetPosition())
  403. light.SetFocalPoint(self._CurrentCamera.GetFocalPoint())
  404. if not self.GetUpdateRegion().IsEmpty() or self.__handle:
  405. if self.__handle and self.__handle == self.GetHandle():
  406. self._RenderWindow.Render()
  407. elif self.GetHandle():
  408. # this means the user has reparented us
  409. # let's adapt to the new situation by doing the WindowRemap
  410. # dance
  411. self._RenderWindow.SetNextWindowInfo(str(self.GetHandle()))
  412. self._RenderWindow.WindowRemap()
  413. # store the new situation
  414. self.__handle = self.GetHandle()
  415. self._RenderWindow.Render()
  416. def UpdateRenderer(self, event):
  417. """
  418. UpdateRenderer will identify the renderer under the mouse and set
  419. up _CurrentRenderer, _CurrentCamera, and _CurrentLight.
  420. """
  421. x = event.GetX()
  422. y = event.GetY()
  423. windowX, windowY = self._RenderWindow.GetSize()
  424. renderers = self._RenderWindow.GetRenderers()
  425. numRenderers = renderers.GetNumberOfItems()
  426. self._CurrentRenderer = None
  427. renderers.InitTraversal()
  428. for i in range(0,numRenderers):
  429. renderer = renderers.GetNextItem()
  430. vx,vy = (0,0)
  431. if (windowX > 1):
  432. vx = float(x)/(windowX-1)
  433. if (windowY > 1):
  434. vy = (windowY-float(y)-1)/(windowY-1)
  435. (vpxmin,vpymin,vpxmax,vpymax) = renderer.GetViewport()
  436. if (vx >= vpxmin and vx <= vpxmax and
  437. vy >= vpymin and vy <= vpymax):
  438. self._CurrentRenderer = renderer
  439. self._ViewportCenterX = float(windowX)*(vpxmax-vpxmin)/2.0\
  440. +vpxmin
  441. self._ViewportCenterY = float(windowY)*(vpymax-vpymin)/2.0\
  442. +vpymin
  443. self._CurrentCamera = self._CurrentRenderer.GetActiveCamera()
  444. lights = self._CurrentRenderer.GetLights()
  445. lights.InitTraversal()
  446. self._CurrentLight = lights.GetNextItem()
  447. break
  448. self._LastX = x
  449. self._LastY = y
  450. def GetCurrentRenderer(self):
  451. """Returns the current renderer.
  452. """
  453. return self._CurrentRenderer
  454. def Rotate(self, event):
  455. """Rotates the scene (camera).
  456. """
  457. if self._CurrentRenderer:
  458. x = event.GetX()
  459. y = event.GetY()
  460. self._CurrentCamera.Azimuth(self._LastX - x)
  461. self._CurrentCamera.Elevation(y - self._LastY)
  462. self._CurrentCamera.OrthogonalizeViewUp()
  463. self._LastX = x
  464. self._LastY = y
  465. self._CurrentRenderer.ResetCameraClippingRange()
  466. self.Render()
  467. def Pan(self, event):
  468. """Pans the scene (camera).
  469. """
  470. if self._CurrentRenderer:
  471. x = event.GetX()
  472. y = event.GetY()
  473. renderer = self._CurrentRenderer
  474. camera = self._CurrentCamera
  475. (pPoint0,pPoint1,pPoint2) = camera.GetPosition()
  476. (fPoint0,fPoint1,fPoint2) = camera.GetFocalPoint()
  477. if camera.GetParallelProjection():
  478. renderer.SetWorldPoint(fPoint0,fPoint1,fPoint2,1.0)
  479. renderer.WorldToDisplay()
  480. fx,fy,fz = renderer.GetDisplayPoint()
  481. renderer.SetDisplayPoint(fx-x+self._LastX,
  482. fy+y-self._LastY,
  483. fz)
  484. renderer.DisplayToWorld()
  485. fx,fy,fz,fw = renderer.GetWorldPoint()
  486. camera.SetFocalPoint(fx,fy,fz)
  487. renderer.SetWorldPoint(pPoint0,pPoint1,pPoint2,1.0)
  488. renderer.WorldToDisplay()
  489. fx,fy,fz = renderer.GetDisplayPoint()
  490. renderer.SetDisplayPoint(fx-x+self._LastX,
  491. fy+y-self._LastY,
  492. fz)
  493. renderer.DisplayToWorld()
  494. fx,fy,fz,fw = renderer.GetWorldPoint()
  495. camera.SetPosition(fx,fy,fz)
  496. else:
  497. (fPoint0,fPoint1,fPoint2) = camera.GetFocalPoint()
  498. # Specify a point location in world coordinates
  499. renderer.SetWorldPoint(fPoint0,fPoint1,fPoint2,1.0)
  500. renderer.WorldToDisplay()
  501. # Convert world point coordinates to display coordinates
  502. dPoint = renderer.GetDisplayPoint()
  503. focalDepth = dPoint[2]
  504. aPoint0 = self._ViewportCenterX + (x - self._LastX)
  505. aPoint1 = self._ViewportCenterY - (y - self._LastY)
  506. renderer.SetDisplayPoint(aPoint0,aPoint1,focalDepth)
  507. renderer.DisplayToWorld()
  508. (rPoint0,rPoint1,rPoint2,rPoint3) = renderer.GetWorldPoint()
  509. if (rPoint3 != 0.0):
  510. rPoint0 = rPoint0/rPoint3
  511. rPoint1 = rPoint1/rPoint3
  512. rPoint2 = rPoint2/rPoint3
  513. camera.SetFocalPoint((fPoint0 - rPoint0) + fPoint0,
  514. (fPoint1 - rPoint1) + fPoint1,
  515. (fPoint2 - rPoint2) + fPoint2)
  516. camera.SetPosition((fPoint0 - rPoint0) + pPoint0,
  517. (fPoint1 - rPoint1) + pPoint1,
  518. (fPoint2 - rPoint2) + pPoint2)
  519. self._LastX = x
  520. self._LastY = y
  521. self.Render()
  522. def Zoom(self, event):
  523. """Zooms the scene (camera).
  524. """
  525. if self._CurrentRenderer:
  526. x = event.GetX()
  527. y = event.GetY()
  528. renderer = self._CurrentRenderer
  529. camera = self._CurrentCamera
  530. zoomFactor = math.pow(1.02,(0.5*(self._LastY - y)))
  531. self._CurrentZoom = self._CurrentZoom * zoomFactor
  532. if camera.GetParallelProjection():
  533. parallelScale = camera.GetParallelScale()/zoomFactor
  534. camera.SetParallelScale(parallelScale)
  535. else:
  536. camera.Dolly(zoomFactor)
  537. renderer.ResetCameraClippingRange()
  538. self._LastX = x
  539. self._LastY = y
  540. self.Render()
  541. def Reset(self, event=None):
  542. """Resets the camera.
  543. """
  544. if self._CurrentRenderer:
  545. self._CurrentRenderer.ResetCamera()
  546. self.Render()
  547. def Wireframe(self):
  548. """Sets the current actor representation as wireframe.
  549. """
  550. actors = self._CurrentRenderer.GetActors()
  551. numActors = actors.GetNumberOfItems()
  552. actors.InitTraversal()
  553. for i in range(0,numActors):
  554. actor = actors.GetNextItem()
  555. actor.GetProperty().SetRepresentationToWireframe()
  556. self.Render()
  557. def Surface(self):
  558. """Sets the current actor representation as surface.
  559. """
  560. actors = self._CurrentRenderer.GetActors()
  561. numActors = actors.GetNumberOfItems()
  562. actors.InitTraversal()
  563. for i in range(0,numActors):
  564. actor = actors.GetNextItem()
  565. actor.GetProperty().SetRepresentationToSurface()
  566. self.Render()
  567. def PickActor(self, event):
  568. """Picks an actor.
  569. """
  570. if self._CurrentRenderer:
  571. x = event.GetX()
  572. y = event.GetY()
  573. renderer = self._CurrentRenderer
  574. picker = self._Picker
  575. windowX, windowY = self._RenderWindow.GetSize()
  576. picker.Pick(x,(windowY - y - 1),0.0,renderer)
  577. actor = picker.GetActor()
  578. if (self._PickedActor != None and
  579. self._PrePickedProperty != None):
  580. self._PickedActor.SetProperty(self._PrePickedProperty)
  581. # release hold of the property
  582. self._PrePickedProperty.UnRegister(self._PrePickedProperty)
  583. self._PrePickedProperty = None
  584. if (actor != None):
  585. self._PickedActor = actor
  586. self._PrePickedProperty = self._PickedActor.GetProperty()
  587. # hold onto the property
  588. self._PrePickedProperty.Register(self._PrePickedProperty)
  589. self._PickedActor.SetProperty(self._PickedProperty)
  590. self.Render()
  591. #----------------------------------------------------------------------------
  592. def wxVTKRenderWindowConeExample():
  593. """Like it says, just a simple example.
  594. """
  595. from vtkmodules.vtkFiltersSources import vtkConeSource
  596. from vtkmodules.vtkRenderingCore import vtkActor, vtkPolyDataMapper, vtkRenderer
  597. # load implementations for rendering and interaction factory classes
  598. import vtkmodules.vtkRenderingOpenGL2
  599. import vtkmodules.vtkInteractionStyle
  600. # every wx app needs an app
  601. app = wx.App(False)
  602. # create the widget
  603. frame = wx.Frame(None, -1, "wxVTKRenderWindow", size=(400,400))
  604. widget = wxVTKRenderWindow(frame, -1)
  605. ren = vtkRenderer()
  606. widget.GetRenderWindow().AddRenderer(ren)
  607. cone = vtkConeSource()
  608. cone.SetResolution(8)
  609. coneMapper = vtkPolyDataMapper()
  610. coneMapper.SetInputConnection(cone.GetOutputPort())
  611. coneActor = vtkActor()
  612. coneActor.SetMapper(coneMapper)
  613. ren.AddActor(coneActor)
  614. # show the window
  615. frame.Show()
  616. app.MainLoop()
  617. if __name__ == "__main__":
  618. wxVTKRenderWindowConeExample()