Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[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 "SVTK_ViewManager.h"
44 #include "SVTK_ViewWindow.h"
45
46 using namespace std;
47
48 /*!
49   VSR : 19.04.05 : Reimplemented for new SALOME GUI (SUIT-based)
50   All methods are implemented using Event mechanism:
51   - getRenderer()
52   - getRenderWindow()
53   - getRenderWindowInteractor()
54   These methods open new VTK viewer if there is no one opened.
55   In case of error methods return None object in Python.
56 */
57
58 static PyObject* GetPyClass(const char* theClassName){
59   static PyObject *aVTKModule = NULL;
60   if(!aVTKModule){
61     if (VTK_MAJOR_VERSION > 3)
62       aVTKModule = PyImport_ImportModule("libvtkRenderingPython"); 
63     else
64       aVTKModule = PyImport_ImportModule("libVTKGraphicsPython"); 
65     if(PyErr_Occurred()){
66       PyErr_Print();
67       return NULL;
68     }
69   }
70   PyObject* aVTKDict = PyModule_GetDict(aVTKModule);
71   char* aClassName = const_cast<char*>(theClassName);
72   PyObject* aPyClass = PyDict_GetItemString(aVTKDict,aClassName);
73   //Py_DECREF(aVTKModule);
74   return aPyClass;
75 }
76
77 static SVTK_ViewWindow* GetVTKViewWindow() {
78   SVTK_ViewWindow* aVW = NULL;
79   if ( SUIT_Session::session() ) {
80     // get application
81     SalomeApp_Application* anApp = dynamic_cast<SalomeApp_Application*>( SUIT_Session::session()->activeApplication() );
82     if ( anApp ) {
83       // get active study
84       SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() );
85       if ( aStudy ) {
86         // find or create VTK view manager
87         SVTK_ViewManager* aVM = dynamic_cast<SVTK_ViewManager*>( anApp->getViewManager( "VTKViewer", true ) );
88         if ( aVM ) {
89           aVW = dynamic_cast<SVTK_ViewWindow*>( aVM->getActiveView() );
90           // VSR : When new view window is created it can be not active yet at this moment,
91           // so the following is a some workaround
92           if ( !aVW && !aVM->getViews().isEmpty() )
93             aVW = dynamic_cast<SVTK_ViewWindow*>( aVM->getViews()[ 0 ] );
94         }
95       }
96     }
97   }
98   return aVW;
99 }
100
101 /*!
102   Get VTK renderer (opens new VTK window if there is no one opened)
103 */
104 class TGetRendererEvent: public SALOME_Event {
105 public:
106   typedef PyObject* TResult;
107   TResult myResult;
108   TGetRendererEvent() : myResult( Py_None ) {}
109   virtual void Execute() {
110     if( SVTK_ViewWindow* aVTKViewWindow = GetVTKViewWindow() ) {
111       PyObject* aPyClass = GetPyClass("vtkRenderer");
112       vtkRenderer* aVTKObject = aVTKViewWindow->getRenderer();
113       myResult = PyVTKObject_New(aPyClass,aVTKObject);
114     }
115   }
116 };
117 extern "C" PyObject *libSalomePy_getRenderer(PyObject *self, PyObject *args)
118 {
119   return ProcessEvent( new TGetRendererEvent() );
120 }
121
122 /*!
123   Get VTK render window (opens new VTK window if there is no one opened)
124 */
125 class TGetRenderWindowEvent: public SALOME_Event {
126 public:
127   typedef PyObject* TResult;
128   TResult myResult;
129   TGetRenderWindowEvent() : myResult( Py_None ) {}
130   virtual void Execute() {
131     if( SVTK_ViewWindow* aVTKViewWindow = GetVTKViewWindow() ) {
132       PyObject* aPyClass = GetPyClass("vtkRenderWindow");
133       vtkRenderWindow* aVTKObject = aVTKViewWindow->getRenderWindow();
134       myResult = PyVTKObject_New(aPyClass,aVTKObject);
135     }
136   }
137 };
138 extern "C" PyObject *libSalomePy_getRenderWindow(PyObject *self, PyObject *args)
139 {
140   return ProcessEvent( new TGetRenderWindowEvent() );
141 }
142
143 /*!
144   Get VTK render window interactor (opens new VTK window if there is no one opened)
145 */
146 class TGetRenderWindowInteractorEvent: public SALOME_Event {
147 public:
148   typedef PyObject* TResult;
149   TResult myResult;
150   TGetRenderWindowInteractorEvent() : myResult( Py_None ) {}
151   virtual void Execute() {
152     if( SVTK_ViewWindow* aVTKViewWindow = GetVTKViewWindow() ) {
153       PyObject* aPyClass = GetPyClass("vtkRenderWindowInteractor");
154       vtkRenderWindowInteractor* aVTKObject = aVTKViewWindow->getInteractor();
155       myResult = PyVTKObject_New(aPyClass,aVTKObject);
156     }
157   }
158 };
159 extern "C" PyObject *libSalomePy_getRenderWindowInteractor(PyObject *self, PyObject *args)
160 {
161   return ProcessEvent( new TGetRenderWindowInteractorEvent() );
162 }
163
164 /*!
165   Library initialization
166 */
167 static PyMethodDef Module_Methods[] = 
168 {
169   { "getRenderer",               libSalomePy_getRenderer,     METH_NOARGS },
170   { "getRenderWindow",           libSalomePy_getRenderWindow, METH_NOARGS },
171   { "getRenderWindowInteractor", libSalomePy_getRenderWindow, METH_NOARGS },
172   { NULL, NULL }
173 };
174
175 extern "C" void initlibSalomePy()
176 {
177   static char modulename[] = "libSalomePy";
178   /*PyObject* aModule = */Py_InitModule(modulename, Module_Methods);
179   if(PyErr_Occurred()){
180     PyErr_Print();
181     return;
182   }
183 }