]> SALOME platform Git repositories - modules/yacs.git/blob - src/SALOME_PY/SalomePy.cxx
Salome HOME
PR: mergefrom_BSEC_br1_14Mar04
[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 <vtkRenderer.h>
31 #include <vtkRenderWindowInteractor.h>
32 #include <vtkPythonUtil.h>
33 #include <vtkVersion.h>
34 #include "utilities.h"
35 #include "QAD_Study.h"
36 #include "QAD_RightFrame.h"
37 #include "QAD_Application.h"
38 #include "QAD_Desktop.h"
39 #include "VTKViewer_ViewFrame.h"
40 using namespace std;
41
42 extern "C"
43
44   static PyObject * libSalomePy_getRenderer(PyObject *self, PyObject *args);
45 }
46
47 static PyObject *libSalomePy_getRenderer(PyObject *self, PyObject *args)
48 {
49   if (!PyArg_ParseTuple(args, ":getRenderer"))
50     return NULL;
51   // exemple retournant systematiquement  Py_None
52   Py_INCREF(Py_None);
53   return Py_None;
54 }
55
56 static PyMethodDef Module_Methods[] = 
57   {
58     {"getRenderer",         libSalomePy_getRenderer,         METH_VARARGS},
59     {NULL, NULL}
60   };
61
62 extern "C" { void initlibSalomePy();}
63
64 void initlibSalomePy()
65 {
66   PyObject *m, *d, *c, *md, *obj, *vtkclass;
67
68   static char modulename[] = "libSalomePy";
69   MESSAGE("---");
70   m = Py_InitModule(modulename, Module_Methods);
71   MESSAGE("---");
72   d = PyModule_GetDict(m);
73   MESSAGE("---");
74   // DANGEROUS : if (!d) Py_FatalError("can't get dictionary for module SalomePy!");
75   if (PyErr_Occurred())
76     {
77       MESSAGE("---");
78       return;
79     }
80
81   if (VTK_MAJOR_VERSION > 3)
82     m=PyImport_ImportModule("libvtkRenderingPython"); // import module if not already imported (new ref)
83   else
84     m=PyImport_ImportModule("libVTKGraphicsPython"); // import module if not already imported (new ref)
85     
86   MESSAGE("---");
87
88   if (PyErr_Occurred())
89     {
90       PyErr_Print();
91       MESSAGE("---");
92       return;
93     }
94
95   if ( m ) {
96     md = PyModule_GetDict(m);                        // dict of libvtkGraphicsPython (borrowed ref ; not decref)
97     MESSAGE("---");
98
99     vtkclass=PyDict_GetItemString(md,"vtkRenderer"); // (borrowed ref ; not decref)
100     Py_DECREF(m);                                    // no more need of m
101     MESSAGE("---");
102   }
103 //NRI
104   //san:T3.13 - move getRenderer() implementation here
105   //vtkRenderer *renderer = SALOMEGUI_Swig::getRenderer();
106   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
107   int nbStudyFrames = myStudy->getStudyFramesCount();
108   vtkRenderer *renderer = NULL;
109   int viewId = -1;
110   if (viewId == -1) // find the first frame with VTK viewer & get renderer
111     {
112       int i=0;
113       for(i=0; i<nbStudyFrames; i++)
114         {
115           if ( myStudy->getStudyFrame(i)->getTypeView() == VIEW_VTK )
116             {
117               renderer = ((VTKViewer_ViewFrame*)myStudy->getStudyFrame(i)->getRightFrame()->getViewFrame())->getRenderer();
118               break;
119             }
120         }
121     }
122   else     // get the VTK renderer of a given frame
123     {
124       SCRUTE(viewId);
125       if ((viewId >=0) && (viewId <nbStudyFrames))
126         renderer = ((VTKViewer_ViewFrame*)myStudy->getStudyFrame(viewId)->getRightFrame()->getViewFrame())->getRenderer();
127     }
128   if (renderer == NULL) INFOS("No VTK Renderer available !");
129   //san:T3.13 - move getRenderer() implementation here
130
131   MESSAGE("---");
132   obj = PyVTKObject_New(vtkclass,renderer);        // (new ref)
133   MESSAGE( "Nombre de references sur obj : " << obj->ob_refcnt ); // sys.getrefcount(o) gives ref count + 1 in Python interp
134   PyDict_SetItemString(d,"renderer",obj);                              // (add a ref to obj ; has to be  decref)
135   MESSAGE( "Nombre de references sur obj : " << obj->ob_refcnt ); // sys.getrefcount(o) gives ref count + 1 in Python interp
136   Py_DECREF(obj);                                                      // only one ref is sufficient
137   MESSAGE( "Nombre de references sur obj : " << obj->ob_refcnt ); // sys.getrefcount(o) gives ref count + 1 in Python interp
138 //NRI
139
140 //   vtkclass=PyDict_GetItemString(md,"vtkRenderWindowInteractor"); // (borrowed ref ; not decref)
141 //   Py_DECREF(m);                                    // no more need of m
142 //   MESSAGE("---");
143 //   vtkRenderWindowInteractor *RWInteractor = SALOMEGUI_Swig::getRWInteractor();
144 //   MESSAGE("---");
145 //   obj = PyVTKObject_New(vtkclass,RWInteractor);        // (new ref)
146 //   MESSAGE( "Nombre de references sur obj : " << obj->ob_refcnt ); // sys.getrefcount(o) gives ref count + 1 in Python interp
147 //   PyDict_SetItemString(d,"interactor",obj);                              // (add a ref to obj ; has to be  decref)
148 //   MESSAGE( "Nombre de references sur obj : " << obj->ob_refcnt ); // sys.getrefcount(o) gives ref count + 1 in Python interp
149 //   Py_DECREF(obj);                                                      // only one ref is sufficient
150 //   MESSAGE( "Nombre de references sur obj : " << obj->ob_refcnt ); // sys.getrefcount(o) gives ref count + 1 in Python interp
151 }
152
153