Salome HOME
Join modifications from branch BR_PR_V320b1
[modules/kernel.git] / src / Communication_SWIG / libSALOME_Comm.i
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either 
7 // version 2.1 of the License.
8 // 
9 // This library is distributed in the hope that it will be useful 
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
12 // Lesser General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public  
15 // License along with this library; if not, write to the Free Software 
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 //
18 // See http://www.salome-platform.org/
19 //
20 %module libSALOME_Comm
21
22 %{
23   #include "ReceiverFactory.hxx"
24   #include "MatrixClient.hxx"
25   #undef SEEK_SET
26   #undef SEEK_CUR
27   #undef SEEK_END
28   #include "SALOME_Comm_i.hxx"
29   #include "SALOMEMultiComm.hxx"
30   #include "SenderFactory.hxx"
31 %}
32
33 %typemap(python,in) SALOME::SenderDouble_ptr
34 {
35   PyObject* pdict = PyDict_New();
36   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
37   PyRun_String("import CORBA", Py_single_input, pdict, pdict);
38  
39   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
40                    pdict, pdict);
41  
42   PyObject* orb = PyDict_GetItemString(pdict, "o");
43
44   // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
45
46   PyObject* iorSupport = PyObject_CallMethod(orb, "object_to_string", "O", $input);
47  
48   if (iorSupport == Py_None)
49     return NULL;
50   char * s = PyString_AsString(PyObject_Str(iorSupport));
51  
52   // Ask omniORB to convert IOR string to SALOME::SenderDouble_ptr
53
54   int argc = 0;
55   char *xargv = "";
56   char **argv = &xargv;
57   CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
58   CORBA::Object_var O =  ORB->string_to_object(s);
59   SALOME::SenderDouble_ptr t = SALOME::SenderDouble::_narrow(O);
60   $1 = t;
61 }
62
63 %typemap(python,in) SALOME::SenderInt_ptr
64 {
65   PyObject* pdict = PyDict_New();
66   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
67   PyRun_String("import CORBA", Py_single_input, pdict, pdict);
68  
69   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
70                    pdict, pdict);
71  
72   PyObject* orb = PyDict_GetItemString(pdict, "o");
73
74   // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
75
76   PyObject* iorSupport = PyObject_CallMethod(orb, "object_to_string", "O", $input);
77  
78   if (iorSupport == Py_None)
79     return NULL;
80   char * s = PyString_AsString(PyObject_Str(iorSupport));
81  
82   // Ask omniORB to convert IOR string to SALOME::SenderInt_ptr
83
84   int argc = 0;
85   char *xargv = "";
86   char **argv = &xargv;
87   CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
88   CORBA::Object_var O =  ORB->string_to_object(s);
89   SALOME::SenderInt_ptr t = SALOME::SenderInt::_narrow(O);
90   $1 = t;
91 }
92
93 %typemap(python,out) SALOME::SenderDouble_ptr
94 {  
95    PyObject* pdict = PyDict_New();
96    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
97    PyRun_String("import CORBA", Py_single_input, pdict, pdict);
98    PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
99                    pdict, pdict);
100    PyObject* orb = PyDict_GetItemString(pdict, "o");
101    // Get the orb Corba C++
102    int argc = 0;
103    char *xargv = "";
104    char **argv = &xargv;
105    CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
106    std::string s =  ORB->object_to_string($1);
107    PyObject * tmp = PyString_FromString(s.c_str());
108    $result = PyObject_CallMethod(orb, "string_to_object", "O", tmp);
109 }
110
111 %typemap(python,out) SALOME::SenderInt_ptr
112 {  
113    PyObject* pdict = PyDict_New();
114    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
115    PyRun_String("import CORBA", Py_single_input, pdict, pdict);
116    PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
117                    pdict, pdict);
118    PyObject* orb = PyDict_GetItemString(pdict, "o");
119    // Get the orb Corba C++
120    int argc = 0;
121    char *xargv = "";
122    char **argv = &xargv;
123    CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
124    std::string s =  ORB->object_to_string($1);
125    PyObject * tmp = PyString_FromString(s.c_str());
126    $result = PyObject_CallMethod(orb, "string_to_object", "O", tmp);
127 }
128
129 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble);
130
131 %{
132 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble)
133 {
134   PyObject *py_list;
135   long size;
136   double *ret=ReceiverFactory::getValue(senderDouble,size);
137   py_list = PyList_New(size);
138   for (int i=0; i < size; i++)
139     {
140             int err = PyList_SetItem(py_list, i, Py_BuildValue("d", (double) ret[i]));
141             if(err)
142               {
143                 char * message = "Error in SUPPORT::getTypes";
144                 PyErr_SetString(PyExc_RuntimeError, message);
145                 return NULL;
146               }
147     }
148   PyObject * result = Py_BuildValue("O", py_list);
149   delete [] ret;
150   Py_DECREF(py_list);
151   return result;
152 }
153 %}
154
155
156 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt);
157
158 %{
159 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt)
160 {
161   PyObject *py_list;
162   long size;
163   int *ret=ReceiverFactory::getValue(senderInt,size);
164   py_list = PyList_New(size);
165   for (int i=0; i < size; i++)
166     {
167             int err = PyList_SetItem(py_list, i, Py_BuildValue("i", (int) ret[i]));
168             if(err)
169               {
170                 char * message = "Error in SUPPORT::getTypes";
171                 PyErr_SetString(PyExc_RuntimeError, message);
172                 return NULL;
173               }
174     }
175   PyObject * result = Py_BuildValue("O", py_list);
176   delete [] ret;
177   Py_DECREF(py_list);
178   return result;
179 }
180 %}
181
182 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix);
183 %{
184 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix)
185 {
186   PyObject *py_list;
187   int column,row;
188   double *ret=MatrixClient::getValue(matrix,column,row);
189   py_list = PyList_New(row);
190   for(int i=0;i<row;i++)
191     {
192        PyObject *tmpRow=PyList_New(column);
193        for(int j=0;j<column;j++)
194          {
195            int err = PyList_SetItem(tmpRow, j, Py_BuildValue("d", (double) ret[i*column+j]));
196             if(err)
197               {
198                 char * message = "PyList_SetItem matrix sent may be invalid";
199                 PyErr_SetString(PyExc_RuntimeError, message);
200                 return NULL;
201               }
202          }
203        PyList_SetItem(py_list,i,tmpRow);
204        Py_DECREF(tmpRow);
205     }
206   delete [] ret;
207   Py_DECREF(py_list);
208   return py_list;
209 }
210 %}
211
212 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist);
213 %{
214 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist)
215 {
216   if (PyList_Check(pylist)) 
217   {
218     int listLgth = PyList_Size(pylist);
219     double *tab=new double[listLgth];
220     for (int i=0;i<listLgth;i++)
221         {
222           tab[i]=PyFloat_AsDouble(PyList_GetItem(pylist,i));
223         }
224     SALOMEMultiComm communicator;
225     return SenderFactory::buildSender(communicator,tab,listLgth,true);
226   }
227   else
228   { 
229     PyErr_SetString(PyExc_TypeError,"not a list");
230     return SALOME::SenderDouble::_nil();
231   }
232 }
233 %}
234
235 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist);
236 %{
237 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist)
238 {
239   if (PyList_Check(pylist)) 
240   {
241     int listLgth = PyList_Size(pylist);
242     int *tab=new int[listLgth];
243     for (int i=0;i<listLgth;i++)
244         {
245           tab[i]=PyInt_AsLong(PyList_GetItem(pylist,i));
246         }
247     SALOMEMultiComm communicator;
248     return SenderFactory::buildSender(communicator,tab,listLgth,true);
249   }
250   else
251   { 
252     PyErr_SetString(PyExc_TypeError,"not a list");
253     return SALOME::SenderInt::_nil();
254   }
255 }
256 %}