📄 vtktkrenderwidget.py
字号:
"""A simple VTK input file for Python, which includesa vtkTkRenderWidget for Tkinter. The widget can beaccessed under the names 'vtkTkRenderWidget' or 'vtkRenderWidget' Created by David Gobbi, April 1999May ??, 1999 - Modifications peformed by Heather Drury, to rewrite _pan to match method in TkInteractor.tclMay 11, 1999 - Major rewrite by David Gobbi to make the interactor bindings identical to the TkInteractor.tcl bindings.July 14, 1999 - Added modification by Ken Martin for VTK 2.4, to use vtk widgets instead of Togl.Aug 29, 1999 - Renamed file to vtkRenderWidget.pyNov 14, 1999 - Added support for keyword 'rw'Mar 23, 2000 - Extensive but backwards compatible changes, improved documentation""""""A few important notes:This class is meant to be used as a base-class widget fordoing VTK rendering in Python.In VTK (and C++) there is a very important distinction betweenpublic ivars (attributes in pythonspeak), protected ivars, andprivate ivars. When you write a python class that you wantto 'look and feel' like a VTK class, you should follow these rules.1) Attributes should never be public. Attributes should always be either protected (prefixed with a single underscore) or private (prefixed with a double underscore). You can provide access to attributes through public Set/Get methods (same as VTK).2) Use a single underscore to denote a protected attribute, e.g. self._RenderWindow is protected (can be accessed from this class or a derived class).3) Use a double underscore to denote a private attribute, e.g. self.__InExpose cannot be accessed outside of this class.All attributes should be 'declared' in the __init__() functioni.e. set to some initial value. Don't forget that 'None' means'NULL' - the python/vtk wrappers guarantee their equivalence."""import Tkinterfrom Tkinter import *import math, os, sysfrom vtkpython import *from vtkLoadPythonTkWidgets import vtkLoadPythonTkWidgetsclass vtkTkRenderWidget(Tkinter.Widget): """ A vtkTkRenderWidget for Python. Use GetRenderWindow() to get the vtkRenderWindow. Create with the keyword stereo=1 in order to generate a stereo-capable window. Create with the keyword focus_on_enter=1 to enable focus-follows-mouse. The default is for a click-to-focus mode. """ def __init__(self, master, cnf={}, **kw): """ Constructor. Keyword arguments: rw -- Use passed render window instead of creating a new one. stereo -- If True, generate a stereo-capable window. Defaults to False. focus_on_enter -- If True, use a focus-follows-mouse mode. Defaults to False where the widget will use a click-to-focus mode. """ # load the necessary extensions into tk vtkLoadPythonTkWidgets(master.tk) try: # check to see if a render window was specified renderWindow = kw['rw'] except KeyError: renderWindow = vtkRenderWindow() try: # was a stereo rendering context requested? if kw['stereo']: renderWindow.StereoCapableWindowOn() del kw['stereo'] except KeyError: pass # check if focus should follow mouse if kw.get('focus_on_enter'): self._FocusOnEnter = 1 del kw['focus_on_enter'] else: self._FocusOnEnter = 0 kw['rw'] = renderWindow.GetAddressAsString("vtkRenderWindow") Tkinter.Widget.__init__(self, master, 'vtkTkRenderWidget', cnf, kw) 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 # used by the LOD actors self._DesiredUpdateRate = 15 self._StillUpdateRate = 0.0001 # these record the previous mouse position self._LastX = 0 self._LastY = 0 # private attributes self.__InExpose = 0 # create the Tk bindings self.BindTkRenderWidget() def __getattr__(self,attr): # because the tk part of vtkTkRenderWidget must have # the only remaining reference to the RenderWindow when # it is destroyed, we can't actually store the RenderWindow # as an attribute but instead have to get it from the tk-side if attr == '_RenderWindow': return self.GetRenderWindow() raise AttributeError, self.__class__.__name__ + \ " has no attribute named " + attr def BindTkRenderWidget(self): """ Bind some default actions. """ self.bind("<ButtonPress>", lambda e,s=self: s.StartMotion(e.x,e.y)) self.bind("<ButtonRelease>", lambda e,s=self: s.EndMotion(e.x,e.y)) self.bind("<B1-Motion>", lambda e,s=self: s.Rotate(e.x,e.y)) self.bind("<B2-Motion>", lambda e,s=self: s.Pan(e.x,e.y)) self.bind("<B3-Motion>", lambda e,s=self: s.Zoom(e.x,e.y)) self.bind("<Shift-B1-Motion>", lambda e,s=self: s.Pan(e.x,e.y)) self.bind("<KeyPress-r>", lambda e,s=self: s.Reset(e.x,e.y)) self.bind("<KeyPress-u>", lambda e,s=self: s.deiconify()) self.bind("<KeyPress-w>", lambda e,s=self: s.Wireframe()) self.bind("<KeyPress-s>", lambda e,s=self: s.Surface()) self.bind("<KeyPress-p>", lambda e,s=self: s.PickActor(e.x,e.y)) if self._FocusOnEnter: self.bind("<Enter>", lambda e,s=self: s.Enter(e.x,e.y)) self.bind("<Leave>", lambda e,s=self: s.Leave(e.x,e.y)) else: self.bind("<ButtonPress>", lambda e,s=self: s.Enter(e.x,e.y)) self.bind("<Expose>", lambda e,s=self: s.Expose()) def GetZoomFactor(self): return self._CurrentZoom def SetDesiredUpdateRate(self, rate): """Mirrors the method with the same name in vtkRenderWindowInteractor.""" self._DesiredUpdateRate = rate def GetDesiredUpdateRate(self): """Mirrors the method with the same name in vtkRenderWindowInteractor.""" return self._DesiredUpdateRate def SetStillUpdateRate(self, rate): """Mirrors the method with the same name in vtkRenderWindowInteractor.""" self._StillUpdateRate = rate def GetStillUpdateRate(self): """Mirrors the method with the same name in vtkRenderWindowInteractor.""" return self._StillUpdateRate def GetRenderWindow(self): addr = self.tk.call(self._w, 'GetRenderWindow')[5:] return vtkRenderWindow('_%s_vtkRenderWindow_p' % addr) def GetPicker(self): return self._Picker def Expose(self): if (not self.__InExpose): self.__InExpose = 1 self.update() self._RenderWindow.Render() self.__InExpose = 0 def Render(self): if (self._CurrentLight): light = self._CurrentLight light.SetPosition(self._CurrentCamera.GetPosition()) light.SetFocalPoint(self._CurrentCamera.GetFocalPoint()) self._RenderWindow.Render() def UpdateRenderer(self,x,y): """ UpdateRenderer will identify the renderer under the mouse and set up _CurrentRenderer, _CurrentCamera, and _CurrentLight. """ windowX = self.winfo_width() windowY = self.winfo_height()
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -