]> SALOME platform Git repositories - modules/kernel.git/blob - src/Communication_SWIG/libSALOME_Comm.i
Salome HOME
add make test for UNIT_TEST_PROG
[modules/kernel.git] / src / Communication_SWIG / libSALOME_Comm.i
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 %module libSALOME_Comm
24
25 %feature("autodoc", "1");
26
27 %{
28   #include "ReceiverFactory.hxx"
29   #include "MatrixClient.hxx"
30   #undef SEEK_SET
31   #undef SEEK_CUR
32   #undef SEEK_END
33   #include "SALOME_Comm_i.hxx"
34   #include "SALOMEMultiComm.hxx"
35   #include "SenderFactory.hxx"
36 %}
37
38 %typemap(python,in) SALOME::SenderDouble_ptr
39 {
40   PyObject* pdict = PyDict_New();
41   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
42   PyRun_String("import CORBA", Py_single_input, pdict, pdict);
43  
44   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
45                    pdict, pdict);
46  
47   PyObject* orb = PyDict_GetItemString(pdict, "o");
48
49   // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
50
51   PyObject* iorSupport = PyObject_CallMethod(orb, (char*)"object_to_string", (char*)"O", $input);
52  
53   if (iorSupport == Py_None)
54     return NULL;
55   char * s = PyString_AsString(PyObject_Str(iorSupport));
56  
57   // Ask omniORB to convert IOR string to SALOME::SenderDouble_ptr
58
59   int argc = 0;
60   char *xargv = (char*)"";
61   char **argv = &xargv;
62   CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
63   CORBA::Object_var O =  ORB->string_to_object(s);
64   SALOME::SenderDouble_ptr t = SALOME::SenderDouble::_narrow(O);
65   $1 = t;
66 }
67
68 %typemap(python,in) SALOME::SenderInt_ptr
69 {
70   PyObject* pdict = PyDict_New();
71   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
72   PyRun_String("import CORBA", Py_single_input, pdict, pdict);
73  
74   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
75                    pdict, pdict);
76  
77   PyObject* orb = PyDict_GetItemString(pdict, "o");
78
79   // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
80
81   PyObject* iorSupport = PyObject_CallMethod(orb, (char*)"object_to_string", (char*)"O", $input);
82  
83   if (iorSupport == Py_None)
84     return NULL;
85   char * s = PyString_AsString(PyObject_Str(iorSupport));
86  
87   // Ask omniORB to convert IOR string to SALOME::SenderInt_ptr
88
89   int argc = 0;
90   char *xargv = (char*)"";
91   char **argv = &xargv;
92   CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
93   CORBA::Object_var O =  ORB->string_to_object(s);
94   SALOME::SenderInt_ptr t = SALOME::SenderInt::_narrow(O);
95   $1 = t;
96 }
97
98 %typemap(python,out) SALOME::SenderDouble_ptr
99 {  
100    PyObject* pdict = PyDict_New();
101    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
102    PyRun_String("import CORBA", Py_single_input, pdict, pdict);
103    PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
104                    pdict, pdict);
105    PyObject* orb = PyDict_GetItemString(pdict, "o");
106    // Get the orb Corba C++
107    int argc = 0;
108    char *xargv = (char*)"";
109    char **argv = &xargv;
110    CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
111    std::string s =  ORB->object_to_string($1);
112    PyObject * tmp = PyString_FromString(s.c_str());
113    $result = PyObject_CallMethod(orb, (char*)"string_to_object", (char*)"O", tmp);
114 }
115
116 %typemap(python,out) SALOME::SenderInt_ptr
117 {  
118    PyObject* pdict = PyDict_New();
119    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
120    PyRun_String("import CORBA", Py_single_input, pdict, pdict);
121    PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
122                    pdict, pdict);
123    PyObject* orb = PyDict_GetItemString(pdict, "o");
124    // Get the orb Corba C++
125    int argc = 0;
126    char *xargv = (char*)"";
127    char **argv = &xargv;
128    CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
129    std::string s =  ORB->object_to_string($1);
130    PyObject * tmp = PyString_FromString(s.c_str());
131    $result = PyObject_CallMethod(orb, (char*)"string_to_object", (char*)"O", tmp);
132 }
133
134 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble);
135
136 %{
137 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble)
138 {
139   PyObject *py_list;
140   long size;
141   double *ret=ReceiverFactory::getValue(senderDouble,size);
142   py_list = PyList_New(size);
143   for (int i=0; i < size; i++)
144     {
145             int err = PyList_SetItem(py_list, i, Py_BuildValue("d", (double) ret[i]));
146             if(err)
147               {
148                 const char * message = "Error in SUPPORT::getTypes";
149                 PyErr_SetString(PyExc_RuntimeError, message);
150                 return NULL;
151               }
152     }
153   PyObject * result = Py_BuildValue("O", py_list);
154   delete [] ret;
155   Py_DECREF(py_list);
156   return result;
157 }
158 %}
159
160
161 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt);
162
163 %{
164 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt)
165 {
166   PyObject *py_list;
167   long size;
168   int *ret=ReceiverFactory::getValue(senderInt,size);
169   py_list = PyList_New(size);
170   for (int i=0; i < size; i++)
171     {
172             int err = PyList_SetItem(py_list, i, Py_BuildValue("i", (int) ret[i]));
173             if(err)
174               {
175                 const char * message = "Error in SUPPORT::getTypes";
176                 PyErr_SetString(PyExc_RuntimeError, message);
177                 return NULL;
178               }
179     }
180   PyObject * result = Py_BuildValue("O", py_list);
181   delete [] ret;
182   Py_DECREF(py_list);
183   return result;
184 }
185 %}
186
187 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix);
188 %{
189 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix)
190 {
191   PyObject *py_list;
192   int column,row;
193   double *ret=MatrixClient::getValue(matrix,column,row);
194   py_list = PyList_New(row);
195   for(int i=0;i<row;i++)
196     {
197        PyObject *tmpRow=PyList_New(column);
198        for(int j=0;j<column;j++)
199          {
200            int err = PyList_SetItem(tmpRow, j, Py_BuildValue("d", (double) ret[i*column+j]));
201             if(err)
202               {
203                 const char * message = "PyList_SetItem matrix sent may be invalid";
204                 PyErr_SetString(PyExc_RuntimeError, message);
205                 return NULL;
206               }
207          }
208        PyList_SetItem(py_list,i,tmpRow);
209        Py_DECREF(tmpRow);
210     }
211   delete [] ret;
212   Py_DECREF(py_list);
213   return py_list;
214 }
215 %}
216
217 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist);
218 %{
219 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist)
220 {
221   if (PyList_Check(pylist)) 
222   {
223     int listLgth = PyList_Size(pylist);
224     double *tab=new double[listLgth];
225     for (int i=0;i<listLgth;i++)
226         {
227           tab[i]=PyFloat_AsDouble(PyList_GetItem(pylist,i));
228         }
229     SALOMEMultiComm communicator;
230     return SenderFactory::buildSender(communicator,tab,listLgth,true);
231   }
232   else
233   { 
234     PyErr_SetString(PyExc_TypeError,"not a list");
235     return SALOME::SenderDouble::_nil();
236   }
237 }
238 %}
239
240 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist);
241 %{
242 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist)
243 {
244   if (PyList_Check(pylist)) 
245   {
246     int listLgth = PyList_Size(pylist);
247     int *tab=new int[listLgth];
248     for (int i=0;i<listLgth;i++)
249         {
250           tab[i]=PyInt_AsLong(PyList_GetItem(pylist,i));
251         }
252     SALOMEMultiComm communicator;
253     return SenderFactory::buildSender(communicator,tab,listLgth,true);
254   }
255   else
256   { 
257     PyErr_SetString(PyExc_TypeError,"not a list");
258     return SALOME::SenderInt::_nil();
259   }
260 }
261 %}