Salome HOME
Initial version
[modules/gui.git] / src / SALOME_PY / SalomePy.cxx
1 //  SALOME SALOME_PY : binding of VTK graphics and Python
2 //
3 //  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
5 // 
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. 
10 // 
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. 
15 // 
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 
19 // 
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SalomePy.cxx
25 //  Author : Paul RASCLE, EDF
26 //  Module : SALOME
27 //  $Header$
28
29 #include <Python.h>
30 #include <vtkPythonUtil.h>
31
32 #include <vtkVersion.h>
33 #include <vtkRenderer.h>
34 #include <vtkRenderWindow.h>
35 #include <vtkRenderWindowInteractor.h>
36
37 #include "SALOME_Event.hxx"
38
39 #include "SUIT_Session.h"
40 #include "SalomeApp_Application.h"
41 #include "SalomeApp_Study.h"
42
43 #include "VTKViewer_ViewManager.h"
44 #include "VTKViewer_ViewWindow.h"
45 #include "VTKViewer_RenderWindow.h"
46 #include "VTKViewer_RenderWindowInteractor.h"
47
48 using namespace std;
49
50 //////////////////////////////////////////////////////////////////////////////
51 // VSR : 19.04.05 : Reimplemented for new SALOME GUI (SUIT-based)
52 // All methods are implemented using Event mechanism:
53 // - getRenderer()
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 //////////////////////////////////////////////////////////////////////////////
59
60 static PyObject* GetPyClass(const char* theClassName){
61   static PyObject *aVTKModule = NULL;
62   if(!aVTKModule){
63     if (VTK_MAJOR_VERSION > 3)
64       aVTKModule = PyImport_ImportModule("libvtkRenderingPython"); 
65     else
66       aVTKModule = PyImport_ImportModule("libVTKGraphicsPython"); 
67     if(PyErr_Occurred()){
68       PyErr_Print();
69       return NULL;
70     }
71   }
72   PyObject* aVTKDict = PyModule_GetDict(aVTKModule);
73   char* aClassName = const_cast<char*>(theClassName);
74   PyObject* aPyClass = PyDict_GetItemString(aVTKDict,aClassName);
75   //Py_DECREF(aVTKModule);
76   return aPyClass;
77 }
78
79 static VTKViewer_ViewWindow* GetVTKViewWindow() {
80   if ( SUIT_Session::session() ) {
81     // get application
82     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
83     if ( anApp ) {
84       // get active study
85       SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
86       if ( aStudy ) {
87         // find or create VTK view manager
88         VTKViewer_ViewManager* aVM = dynamic_cast<VTKViewer_ViewManager*>( anApp->getViewManager( "VTKViewer", true ) );
89         if ( aVM ) {
90           VTKViewer_ViewWindow* aVW = dynamic_cast<VTKViewer_ViewWindow*>( aVM->getActiveView() );
91           // VSR : When new view window is created it can be not active yet at this moment,
92           // so the following is a some workaround
93           if ( !aVW && !aVM->getViews().isEmpty() )
94             return dynamic_cast<VTKViewer_ViewWindow*>( aVM->getViews()[ 0 ] );
95         }
96       }
97     }
98   }
99   return NULL;
100 }
101
102 /*!
103   Get VTK renderer (opens new VTK window if there is no one opened)
104 */
105 class TGetRendererEvent: public SALOME_Event {
106 public:
107   typedef PyObject* TResult;
108   TResult myResult;
109   TGetRendererEvent() : myResult( Py_None ) {}
110   virtual void Execute() {
111     if( VTKViewer_ViewWindow* aVTKViewWindow = GetVTKViewWindow() ) {
112       PyObject* aPyClass = GetPyClass("vtkRenderer");
113       vtkRenderer* aVTKObject = aVTKViewWindow->getRenderer();
114       myResult = PyVTKObject_New(aPyClass,aVTKObject);
115     }
116   }
117 };
118 extern "C" PyObject *libSalomePy_getRenderer(PyObject *self, PyObject *args)
119 {
120   return ProcessEvent( new TGetRendererEvent() );
121 }
122
123 /*!
124   Get VTK render window (opens new VTK window if there is no one opened)
125 */
126 class TGetRenderWindowEvent: public SALOME_Event {
127 public:
128   typedef PyObject* TResult;
129   TResult myResult;
130   TGetRenderWindowEvent() : myResult( Py_None ) {}
131   virtual void Execute() {
132     if( VTKViewer_ViewWindow* aVTKViewWindow = GetVTKViewWindow() ) {
133       PyObject* aPyClass = GetPyClass("vtkRenderWindow");
134       vtkRenderWindow* aVTKObject = aVTKViewWindow->getRenderWindow()->getRenderWindow();
135       myResult = PyVTKObject_New(aPyClass,aVTKObject);
136     }
137   }
138 };
139 extern "C" PyObject *libSalomePy_getRenderWindow(PyObject *self, PyObject *args)
140 {
141   return ProcessEvent( new TGetRenderWindowEvent() );
142 }
143
144 /*!
145   Get VTK render window interactor (opens new VTK window if there is no one opened)
146 */
147 class TGetRenderWindowInteractorEvent: public SALOME_Event {
148 public:
149   typedef PyObject* TResult;
150   TResult myResult;
151   TGetRenderWindowInteractorEvent() : myResult( Py_None ) {}
152   virtual void Execute() {
153     if( VTKViewer_ViewWindow* aVTKViewWindow = GetVTKViewWindow() ) {
154       PyObject* aPyClass = GetPyClass("vtkRenderWindowInteractor");
155       vtkRenderWindowInteractor* aVTKObject = aVTKViewWindow->getRWInteractor();
156       myResult = PyVTKObject_New(aPyClass,aVTKObject);
157     }
158   }
159 };
160 extern "C" PyObject *libSalomePy_getRenderWindowInteractor(PyObject *self, PyObject *args)
161 {
162   return ProcessEvent( new TGetRenderWindowInteractorEvent() );
163 }
164
165 /*!
166   Library initialization
167 */
168 static PyMethodDef Module_Methods[] = 
169 {
170   { "getRenderer",               libSalomePy_getRenderer,     METH_NOARGS },
171   { "getRenderWindow",           libSalomePy_getRenderWindow, METH_NOARGS },
172   { "getRenderWindowInteractor", libSalomePy_getRenderWindow, METH_NOARGS },
173   { NULL, NULL }
174 };
175
176 extern "C" void initlibSalomePy()
177 {
178   static char modulename[] = "libSalomePy";
179   /*PyObject* aModule = */Py_InitModule(modulename, Module_Methods);
180   if(PyErr_Occurred()){
181     PyErr_Print();
182     return;
183   }
184 }