📄 vtk.py
字号:
"""
VTK is now including a package for using VTK with wxPython, so this
module is now officially nothing but ancient history. If for some
strange reason you really need this code (I don't know why, it didn't
work all that well anyway,) then just remove the triple quotes below.
I'm told that the module from Kitware is excellent and so you should
really use it. See the URL below to get a copy from CVS.
http://public.kitware.com/cgi-bin/cvsweb.cgi/VTK/Wrapping/Python/vtk/wx/
"""
print __doc__
'''
#----------------------------------------------------------------------
# Name: wxPython.lib.vtk
# Purpose: Provides a wrapper around the vtkRenderWindow from the
# VTK Visualization Toolkit. Requires the VTK Python
# extensions from http://www.kitware.com/
#
# Author: Robin Dunn
#
# Created: 16-Nov-1999
# RCS-ID: $Id: vtk.py,v 1.4 2003/11/12 21:29:05 RD Exp $
# Copyright: (c) 1999 by Total Control Software
# Licence: wxWindows license
#----------------------------------------------------------------------
# This class has been rewritten and improved by Prabhu Ramachandran
# <prabhu@aero.iitm.ernet.in>. It has been tested under Win32 and
# Linux. Many thanks to Eric Boix <frog@creatis.insa-lyon.fr> for
# testing it under Windows and finding and fixing many errors.
# Thanks also to Sebastien BARRE <sebastien@barre.nom.fr> for his
# suggestions.
try:
from vtkpython import *
except ImportError:
raise ImportError, "VTK extension module not found"
from wxPython.wx import *
import math
#----------------------------------------------------------------------
DEBUG = 0
def debug(msg):
if DEBUG:
print msg
class wxVTKRenderWindowBase(wxWindow):
"""
A base class that enables one to embed a vtkRenderWindow into
a wxPython widget. This class embeds the RenderWindow correctly
under different platforms. Provided are some empty methods that
can be overloaded to provide a user defined interaction behaviour.
The event handling functions have names that are similar to the
ones in the vtkInteractorStyle class included with VTK.
"""
def __init__(self, parent, id, position=wxDefaultPosition,
size=wxDefaultSize, style=0):
wxWindow.__init__(self, parent, id, position, size, style | wxWANTS_CHARS)
self._RenderWindow = vtkRenderWindow()
self.__InExpose = 0
self.__Created = 0
if wxPlatform != '__WXMSW__':
# We can't get the handle in wxGTK until after the widget
# is created, the window create event happens later so we'll
# catch it there
EVT_WINDOW_CREATE(self, self.OnCreateWindow)
EVT_PAINT (self, self.OnExpose)
else:
# but in MSW, the window create event happens durring the above
# call to __init__ so we have to do it here.
hdl = self.GetHandle()
self._RenderWindow.SetWindowInfo(str(hdl))
EVT_PAINT (self, self.OnExpose)
self.__Created = 1
# common for all platforms
EVT_SIZE (self, self.OnConfigure)
# setup the user defined events.
self.SetupEvents()
def SetupEvents(self):
"Setup the user defined event bindings."
# Remember to bind everything to self.box and NOT self
EVT_LEFT_DOWN (self, self.OnLeftButtonDown)
EVT_MIDDLE_DOWN (self, self.OnMiddleButtonDown)
EVT_RIGHT_DOWN (self, self.OnRightButtonDown)
EVT_LEFT_UP (self, self.OnLeftButtonUp)
EVT_MIDDLE_UP (self, self.OnMiddleButtonUp)
EVT_RIGHT_UP (self, self.OnRightButtonUp)
EVT_MOTION (self, self.OnMouseMove)
EVT_ENTER_WINDOW (self, self.OnEnter)
EVT_LEAVE_WINDOW (self, self.OnLeave)
EVT_CHAR (self, self.OnChar)
# Add your bindings if you want them in the derived class.
def GetRenderer(self):
self._RenderWindow.GetRenderers().InitTraversal()
return self._RenderWindow.GetRenderers().GetNextItem()
def GetRenderWindow(self):
return self._RenderWindow
def Render(self):
if self.__Created:
# if block needed because calls to render before creation
# will prevent the renderwindow from being embedded into a
# wxPython widget.
self._RenderWindow.Render()
def OnExpose(self, event):
# I need this for the MDIDemo. Somehow OnCreateWindow was
# not getting called.
if not self.__Created:
self.OnCreateWindow(event)
if (not self.__InExpose):
self.__InExpose = 1
dc = wxPaintDC(self)
self._RenderWindow.Render()
self.__InExpose = 0
def OnCreateWindow(self, event):
hdl = self.GetHandle()
try:
self._RenderWindow.SetParentInfo(str(hdl))
except:
self._RenderWindow.SetWindowInfo(str(hdl))
msg = "Warning:\n "\
"Unable to call vtkRenderWindow.SetParentInfo\n\n"\
"Using the SetWindowInfo method instead. This\n"\
"is likely to cause a lot of flicker when\n"\
"rendering in the vtkRenderWindow. Please\n"\
"use a recent Nightly VTK release (later than\n"\
"March 10 2001) to eliminate this problem."
dlg = wxMessageDialog(NULL, msg, "Warning!",
wxOK |wxICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
self.__Created = 1
def OnConfigure(self, event):
sz = self.GetSize()
self.SetSize(sz)
# Ugly hack that according to Eric Boix is necessary under
# Windows. If possible Try commenting this out and test under
# Windows.
#self._RenderWindow.GetSize()
#
self._RenderWindow.SetSize(sz.width, sz.height)
def OnLeftButtonDown(self, event):
"Left mouse button pressed."
pass
def OnMiddleButtonDown(self, event):
"Middle mouse button pressed."
pass
def OnRightButtonDown(self, event):
"Right mouse button pressed."
pass
def OnLeftButtonUp(self, event):
"Left mouse button released."
pass
def OnMiddleButtonUp(self, event):
"Middle mouse button released."
pass
def OnRightButtonUp(self, event):
"Right mouse button released."
pass
def OnMouseMove(self, event):
"Mouse has moved."
pass
def OnEnter(self, event):
"Entering the vtkRenderWindow."
pass
def OnLeave(self, event):
"Leaving the vtkRenderWindow."
pass
def OnChar(self, event):
"Process Key events."
pass
def OnKeyDown(self, event):
"Key pressed down."
pass
def OnKeyUp(self, event):
"Key released."
pass
class wxVTKRenderWindow(wxVTKRenderWindowBase):
"""
An example of a fully functional wxVTKRenderWindow that is
based on the vtkRenderWidget.py provided with the VTK sources.
"""
def __init__(self, parent, id, position=wxDefaultPosition,
size=wxDefaultSize, style=0):
wxVTKRenderWindowBase.__init__(self, parent, id, position, size,
style)
self._CurrentRenderer = None
self._CurrentCamera = None
self._CurrentZoom = 1.0
self._CurrentLight = None
self._ViewportCenterX = 0
self._ViewportCenterY = 0
self._Picker = vtkCellPicker()
self._PickedAssembly = None
self._PickedProperty = vtkProperty()
self._PickedProperty.SetColor(1,0,0)
self._PrePickedProperty = None
self._OldFocus = None
# these record the previous mouse position
self._LastX = 0
self._LastY = 0
def OnLeftButtonDown(self, event):
"Left mouse button pressed."
self.StartMotion(event)
def OnMiddleButtonDown(self, event):
"Middle mouse button pressed."
self.StartMotion(event)
def OnRightButtonDown(self, event):
"Right mouse button pressed."
self.StartMotion(event)
def OnLeftButtonUp(self, event):
"Left mouse button released."
self.EndMotion(event)
def OnMiddleButtonUp(self, event):
"Middle mouse button released."
self.EndMotion(event)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -