Salome HOME
Merge branch 'V9_8_BR'
[modules/kernel.git] / src / Communication_SWIG / libSALOME_Comm.i
1 // Copyright (C) 2007-2021  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, or (at your option) any later version.
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   #include "SALOME_Comm_i.hxx"
31   #include "SALOMEMultiComm.hxx"
32   #include "SenderFactory.hxx"
33   #include "OpUtil.hxx"
34 %}
35
36 %typemap(in) SALOME::SenderDouble_ptr
37 {
38   PyObject* pdict = PyDict_New();
39   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
40   PyRun_String("from omniORB import CORBA", Py_single_input, pdict, pdict);
41  
42   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
43                    pdict, pdict);
44  
45   PyObject* orb = PyDict_GetItemString(pdict, "o");
46
47   // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
48
49   PyObject* iorSupport = PyObject_CallMethod(orb, (char*)"object_to_string", (char*)"O", $input);
50  
51   if (iorSupport == Py_None)
52     return NULL;
53   char * s = PyString_AsString(PyObject_Str(iorSupport));
54  
55   // Ask omniORB to convert IOR string to SALOME::SenderDouble_ptr
56
57   CORBA::ORB_var ORB = KERNEL::GetRefToORB();
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(in) SALOME::SenderInt_ptr
64 {
65   PyObject* pdict = PyDict_New();
66   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
67   PyRun_String("from omniORB 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, (char*)"object_to_string", (char*)"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   CORBA::ORB_var ORB = KERNEL::GetRefToORB();
85   CORBA::Object_var O =  ORB->string_to_object(s);
86   SALOME::SenderInt_ptr t = SALOME::SenderInt::_narrow(O);
87   $1 = t;
88 }
89
90 %typemap(out) SALOME::SenderDouble_ptr
91 {  
92    PyObject* pdict = PyDict_New();
93    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
94    PyRun_String("from omniORB import CORBA", Py_single_input, pdict, pdict);
95    PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
96                    pdict, pdict);
97    PyObject* orb = PyDict_GetItemString(pdict, "o");
98    // Get the orb Corba C++
99    CORBA::ORB_var ORB = KERNEL::GetRefToORB();
100    std::string s =  ORB->object_to_string($1);
101    PyObject * tmp = PyString_FromString(s.c_str());
102    $result = PyObject_CallMethod(orb, (char*)"string_to_object", (char*)"O", tmp);
103 }
104
105 %typemap(out) SALOME::SenderInt_ptr
106 {  
107    PyObject* pdict = PyDict_New();
108    PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
109    PyRun_String("from omniORB import CORBA", Py_single_input, pdict, pdict);
110    PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
111                    pdict, pdict);
112    PyObject* orb = PyDict_GetItemString(pdict, "o");
113    // Get the orb Corba C++
114    CORBA::ORB_var ORB = KERNEL::GetRefToORB();
115    std::string s =  ORB->object_to_string($1);
116    PyObject * tmp = PyString_FromString(s.c_str());
117    $result = PyObject_CallMethod(orb, (char*)"string_to_object", (char*)"O", tmp);
118 }
119
120 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble);
121
122 %{
123 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble)
124 {
125   PyObject *py_list;
126   long size;
127   double *ret=ReceiverFactory::getValue(senderDouble,size);
128   py_list = PyList_New(size);
129   for (int i=0; i < size; i++)
130     {
131             int err = PyList_SetItem(py_list, i, Py_BuildValue("d", (double) ret[i]));
132             if(err)
133               {
134                 const char * message = "Error in SUPPORT::getTypes";
135                 PyErr_SetString(PyExc_RuntimeError, message);
136                 return NULL;
137               }
138     }
139   PyObject * result = Py_BuildValue("O", py_list);
140   delete [] ret;
141   Py_DECREF(py_list);
142   return result;
143 }
144 %}
145
146
147 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt);
148
149 %{
150 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt)
151 {
152   PyObject *py_list;
153   long size;
154   int *ret=ReceiverFactory::getValue(senderInt,size);
155   py_list = PyList_New(size);
156   for (int i=0; i < size; i++)
157     {
158             int err = PyList_SetItem(py_list, i, Py_BuildValue("i", (int) ret[i]));
159             if(err)
160               {
161                 const char * message = "Error in SUPPORT::getTypes";
162                 PyErr_SetString(PyExc_RuntimeError, message);
163                 return NULL;
164               }
165     }
166   PyObject * result = Py_BuildValue("O", py_list);
167   delete [] ret;
168   Py_DECREF(py_list);
169   return result;
170 }
171 %}
172
173 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix);
174 %{
175 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix)
176 {
177   PyObject *py_list;
178   int column,row;
179   double *ret=MatrixClient::getValue(matrix,column,row);
180   py_list = PyList_New(row);
181   for(int i=0;i<row;i++)
182     {
183        PyObject *tmpRow=PyList_New(column);
184        for(int j=0;j<column;j++)
185          {
186            int err = PyList_SetItem(tmpRow, j, Py_BuildValue("d", (double) ret[i*column+j]));
187             if(err)
188               {
189                 const char * message = "PyList_SetItem matrix sent may be invalid";
190                 PyErr_SetString(PyExc_RuntimeError, message);
191                 return NULL;
192               }
193          }
194        PyList_SetItem(py_list,i,tmpRow);
195        Py_DECREF(tmpRow);
196     }
197   delete [] ret;
198   Py_DECREF(py_list);
199   return py_list;
200 }
201 %}
202
203 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist);
204 %{
205 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist)
206 {
207   if (PyList_Check(pylist)) 
208   {
209     int listLgth = PyList_Size(pylist);
210     double *tab=new double[listLgth];
211     for (int i=0;i<listLgth;i++)
212         {
213           tab[i]=PyFloat_AsDouble(PyList_GetItem(pylist,i));
214         }
215     SALOMEMultiComm communicator;
216     return SenderFactory::buildSender(communicator,tab,listLgth,true);
217   }
218   else
219   { 
220     PyErr_SetString(PyExc_TypeError,"not a list");
221     return SALOME::SenderDouble::_nil();
222   }
223 }
224 %}
225
226 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist);
227 %{
228 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist)
229 {
230   if (PyList_Check(pylist)) 
231   {
232     int listLgth = PyList_Size(pylist);
233     int *tab=new int[listLgth];
234     for (int i=0;i<listLgth;i++)
235         {
236           tab[i]=PyInt_AsLong(PyList_GetItem(pylist,i));
237         }
238     SALOMEMultiComm communicator;
239     return SenderFactory::buildSender(communicator,tab,listLgth,true);
240   }
241   else
242   { 
243     PyErr_SetString(PyExc_TypeError,"not a list");
244     return SALOME::SenderInt::_nil();
245   }
246 }
247 %}