1 // SALOME SALOME_PY : binding of VTK graphics and Python
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SalomePy.cxx
25 // Author : Paul RASCLE, EDF
30 #include <vtkPythonUtil.h>
32 #include <vtkVersion.h>
33 #include <vtkRenderer.h>
34 #include <vtkRenderWindow.h>
35 #include <vtkRenderWindowInteractor.h>
37 #include "SALOME_Event.hxx"
39 #include "SUIT_Session.h"
40 #include "SalomeApp_Application.h"
41 #include "SalomeApp_Study.h"
43 #include "SVTK_ViewManager.h"
44 #include "SVTK_ViewWindow.h"
45 #include "SVTK_RenderWindow.h"
46 #include "SVTK_RenderWindowInteractor.h"
50 //////////////////////////////////////////////////////////////////////////////
51 // VSR : 19.04.05 : Reimplemented for new SALOME GUI (SUIT-based)
52 // All methods are implemented using Event mechanism:
54 // - getRenderWindow()
55 // - getRenderWindowInteractor()
56 // These methods open new VTK viewer if there is no one opened.
57 // In case of error methods return None object in Python.
58 //////////////////////////////////////////////////////////////////////////////
60 static PyObject* GetPyClass(const char* theClassName){
61 static PyObject *aVTKModule = NULL;
63 if (VTK_MAJOR_VERSION > 3)
64 aVTKModule = PyImport_ImportModule("libvtkRenderingPython");
66 aVTKModule = PyImport_ImportModule("libVTKGraphicsPython");
72 PyObject* aVTKDict = PyModule_GetDict(aVTKModule);
73 char* aClassName = const_cast<char*>(theClassName);
74 PyObject* aPyClass = PyDict_GetItemString(aVTKDict,aClassName);
75 //Py_DECREF(aVTKModule);
79 static SVTK_ViewWindow* GetVTKViewWindow() {
80 SVTK_ViewWindow* aVW = NULL;
81 if ( SUIT_Session::session() ) {
83 SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
86 SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
88 // find or create VTK view manager
89 SVTK_ViewManager* aVM = dynamic_cast<SVTK_ViewManager*>( anApp->getViewManager( "VTKViewer", true ) );
91 aVW = dynamic_cast<SVTK_ViewWindow*>( aVM->getActiveView() );
92 // VSR : When new view window is created it can be not active yet at this moment,
93 // so the following is a some workaround
94 if ( !aVW && !aVM->getViews().isEmpty() )
95 aVW = dynamic_cast<SVTK_ViewWindow*>( aVM->getViews()[ 0 ] );
104 Get VTK renderer (opens new VTK window if there is no one opened)
106 class TGetRendererEvent: public SALOME_Event {
108 typedef PyObject* TResult;
110 TGetRendererEvent() : myResult( Py_None ) {}
111 virtual void Execute() {
112 if( SVTK_ViewWindow* aVTKViewWindow = GetVTKViewWindow() ) {
113 PyObject* aPyClass = GetPyClass("vtkRenderer");
114 vtkRenderer* aVTKObject = aVTKViewWindow->getRenderer();
115 myResult = PyVTKObject_New(aPyClass,aVTKObject);
119 extern "C" PyObject *libSalomePy_getRenderer(PyObject *self, PyObject *args)
121 return ProcessEvent( new TGetRendererEvent() );
125 Get VTK render window (opens new VTK window if there is no one opened)
127 class TGetRenderWindowEvent: public SALOME_Event {
129 typedef PyObject* TResult;
131 TGetRenderWindowEvent() : myResult( Py_None ) {}
132 virtual void Execute() {
133 if( SVTK_ViewWindow* aVTKViewWindow = GetVTKViewWindow() ) {
134 PyObject* aPyClass = GetPyClass("vtkRenderWindow");
135 vtkRenderWindow* aVTKObject = aVTKViewWindow->getRenderWindow()->getRenderWindow();
136 myResult = PyVTKObject_New(aPyClass,aVTKObject);
140 extern "C" PyObject *libSalomePy_getRenderWindow(PyObject *self, PyObject *args)
142 return ProcessEvent( new TGetRenderWindowEvent() );
146 Get VTK render window interactor (opens new VTK window if there is no one opened)
148 class TGetRenderWindowInteractorEvent: public SALOME_Event {
150 typedef PyObject* TResult;
152 TGetRenderWindowInteractorEvent() : myResult( Py_None ) {}
153 virtual void Execute() {
154 if( SVTK_ViewWindow* aVTKViewWindow = GetVTKViewWindow() ) {
155 PyObject* aPyClass = GetPyClass("vtkRenderWindowInteractor");
156 vtkRenderWindowInteractor* aVTKObject = aVTKViewWindow->getRWInteractor();
157 myResult = PyVTKObject_New(aPyClass,aVTKObject);
161 extern "C" PyObject *libSalomePy_getRenderWindowInteractor(PyObject *self, PyObject *args)
163 return ProcessEvent( new TGetRenderWindowInteractorEvent() );
167 Library initialization
169 static PyMethodDef Module_Methods[] =
171 { "getRenderer", libSalomePy_getRenderer, METH_NOARGS },
172 { "getRenderWindow", libSalomePy_getRenderWindow, METH_NOARGS },
173 { "getRenderWindowInteractor", libSalomePy_getRenderWindow, METH_NOARGS },
177 extern "C" void initlibSalomePy()
179 static char modulename[] = "libSalomePy";
180 /*PyObject* aModule = */Py_InitModule(modulename, Module_Methods);
181 if(PyErr_Occurred()){