Salome HOME
[Bug PAL7900] g++ 3.3 compatibility
[modules/yacs.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 "QAD_Application.h"
38 #include "QAD_Desktop.h"
39 #include "QAD_Study.h"
40 #include "QAD_RightFrame.h"
41
42 #include "VTKViewer_ViewFrame.h"
43 #include "VTKViewer_RenderWindow.h"
44 #include "VTKViewer_RenderWindowInteractor.h"
45
46 #include "utilities.h"
47
48 using namespace std;
49
50
51 PyObject* GetPyClass(const char* theClassName){
52   static PyObject *aVTKModule = NULL;
53   if(!aVTKModule){
54     if (VTK_MAJOR_VERSION > 3)
55       aVTKModule = PyImport_ImportModule("libvtkRenderingPython"); 
56     else
57       aVTKModule = PyImport_ImportModule("libVTKGraphicsPython"); 
58     if(PyErr_Occurred()){
59       PyErr_Print();
60       return NULL;
61     }
62   }
63   PyObject* aVTKDict = PyModule_GetDict(aVTKModule);
64   char* aClassName = const_cast<char*>(theClassName);
65   PyObject* aPyClass = PyDict_GetItemString(aVTKDict,aClassName);
66   //Py_DECREF(aVTKModule);
67   return aPyClass;
68 }
69
70
71 VTKViewer_ViewFrame* GetVTKViewFrame(){
72   QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
73   QAD_StudyFrame* aStudyFrame = aStudy->getActiveStudyFrame();
74   QAD_ViewFrame* aViewFrame = aStudyFrame->getRightFrame()->getViewFrame();
75   return dynamic_cast<VTKViewer_ViewFrame*>(aViewFrame);
76 }
77
78
79 extern "C" PyObject *libSalomePy_getRenderer(PyObject *self, PyObject *args)
80 {
81   if(VTKViewer_ViewFrame* aVTKViewFrame = GetVTKViewFrame()){
82     PyObject* aPyClass = GetPyClass("vtkRenderer");
83     vtkRenderer* aVTKObject = aVTKViewFrame->getRenderer();
84     return PyVTKObject_New(aPyClass,aVTKObject);
85   }
86   return NULL;
87 }
88
89
90 extern "C" PyObject *libSalomePy_getRenderWindow(PyObject *self, PyObject *args)
91 {
92   if(VTKViewer_ViewFrame* aVTKViewFrame = GetVTKViewFrame()){
93     PyObject* aPyClass = GetPyClass("vtkRenderWindow");
94     vtkRenderWindow* aVTKObject = aVTKViewFrame->getRW()->getRenderWindow();
95     return PyVTKObject_New(aPyClass,aVTKObject);
96   }
97   return NULL;
98 }
99
100
101 extern "C" PyObject *libSalomePy_getRenderWindowInteractor(PyObject *self, PyObject *args)
102 {
103   if(VTKViewer_ViewFrame* aVTKViewFrame = GetVTKViewFrame()){
104     PyObject* aPyClass = GetPyClass("vtkRenderWindowInteractor");
105     vtkRenderWindowInteractor* aVTKObject = aVTKViewFrame->getRWInteractor();
106     return PyVTKObject_New(aPyClass,aVTKObject);
107   }
108   return NULL;
109 }
110
111
112 static PyMethodDef Module_Methods[] = 
113   {
114     {"getRenderer",libSalomePy_getRenderer,METH_NOARGS},
115     {"getRenderWindow",libSalomePy_getRenderWindow,METH_NOARGS},
116     {"getRenderWindowInteractor",libSalomePy_getRenderWindow,METH_NOARGS},
117     {NULL, NULL}
118   };
119
120
121 extern "C" void initlibSalomePy()
122 {
123   static char modulename[] = "libSalomePy";
124   PyObject* aModule = Py_InitModule(modulename, Module_Methods);
125   PyObject* aDict = PyModule_GetDict(aModule);
126   if(PyErr_Occurred()){
127     PyErr_Print();
128     return;
129   }
130   PyObject* anObj = libSalomePy_getRenderer(NULL,NULL);
131   PyDict_SetItemString(aDict,"renderer",anObj);
132   Py_DECREF(anObj);
133 }