1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 %module libSALOME_Comm
23 #include "ReceiverFactory.hxx"
24 #include "MatrixClient.hxx"
28 #include "SALOME_Comm_i.hxx"
29 #include "SALOMEMultiComm.hxx"
30 #include "SenderFactory.hxx"
33 %typemap(python,in) SALOME::SenderDouble_ptr
35 PyObject* pdict = PyDict_New();
36 PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
37 PyRun_String("import CORBA", Py_single_input, pdict, pdict);
39 PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
42 PyObject* orb = PyDict_GetItemString(pdict, "o");
44 // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
46 PyObject* iorSupport = PyObject_CallMethod(orb, "object_to_string", "O", $input);
48 if (iorSupport == Py_None)
50 char * s = PyString_AsString(PyObject_Str(iorSupport));
52 // Ask omniORB to convert IOR string to SALOME::SenderDouble_ptr
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);
63 %typemap(python,in) SALOME::SenderInt_ptr
65 PyObject* pdict = PyDict_New();
66 PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
67 PyRun_String("import CORBA", Py_single_input, pdict, pdict);
69 PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
72 PyObject* orb = PyDict_GetItemString(pdict, "o");
74 // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
76 PyObject* iorSupport = PyObject_CallMethod(orb, "object_to_string", "O", $input);
78 if (iorSupport == Py_None)
80 char * s = PyString_AsString(PyObject_Str(iorSupport));
82 // Ask omniORB to convert IOR string to SALOME::SenderInt_ptr
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);
93 %typemap(python,out) SALOME::SenderDouble_ptr
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,
100 PyObject* orb = PyDict_GetItemString(pdict, "o");
101 // Get the orb Corba C++
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);
111 %typemap(python,out) SALOME::SenderInt_ptr
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,
118 PyObject* orb = PyDict_GetItemString(pdict, "o");
119 // Get the orb Corba C++
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);
129 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble);
132 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble)
136 double *ret=ReceiverFactory::getValue(senderDouble,size);
137 py_list = PyList_New(size);
138 for (int i=0; i < size; i++)
140 int err = PyList_SetItem(py_list, i, Py_BuildValue("d", (double) ret[i]));
143 char * message = "Error in SUPPORT::getTypes";
144 PyErr_SetString(PyExc_RuntimeError, message);
148 PyObject * result = Py_BuildValue("O", py_list);
156 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt);
159 PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt)
163 int *ret=ReceiverFactory::getValue(senderInt,size);
164 py_list = PyList_New(size);
165 for (int i=0; i < size; i++)
167 int err = PyList_SetItem(py_list, i, Py_BuildValue("i", (int) ret[i]));
170 char * message = "Error in SUPPORT::getTypes";
171 PyErr_SetString(PyExc_RuntimeError, message);
175 PyObject * result = Py_BuildValue("O", py_list);
182 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix);
184 PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix)
188 double *ret=MatrixClient::getValue(matrix,column,row);
189 py_list = PyList_New(row);
190 for(int i=0;i<row;i++)
192 PyObject *tmpRow=PyList_New(column);
193 for(int j=0;j<column;j++)
195 int err = PyList_SetItem(tmpRow, j, Py_BuildValue("d", (double) ret[i*column+j]));
198 char * message = "PyList_SetItem matrix sent may be invalid";
199 PyErr_SetString(PyExc_RuntimeError, message);
203 PyList_SetItem(py_list,i,tmpRow);
212 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist);
214 SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist)
216 if (PyList_Check(pylist))
218 int listLgth = PyList_Size(pylist);
219 double *tab=new double[listLgth];
220 for (int i=0;i<listLgth;i++)
222 tab[i]=PyFloat_AsDouble(PyList_GetItem(pylist,i));
224 SALOMEMultiComm communicator;
225 return SenderFactory::buildSender(communicator,tab,listLgth,true);
229 PyErr_SetString(PyExc_TypeError,"not a list");
230 return SALOME::SenderDouble::_nil();
235 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist);
237 SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist)
239 if (PyList_Check(pylist))
241 int listLgth = PyList_Size(pylist);
242 int *tab=new int[listLgth];
243 for (int i=0;i<listLgth;i++)
245 tab[i]=PyInt_AsLong(PyList_GetItem(pylist,i));
247 SALOMEMultiComm communicator;
248 return SenderFactory::buildSender(communicator,tab,listLgth,true);
252 PyErr_SetString(PyExc_TypeError,"not a list");
253 return SALOME::SenderInt::_nil();