Salome HOME
updated copyright message
[modules/kernel.git] / src / Communication_SWIG / libSALOME_Comm.i
index e96cc3eb04d31389cecc0b363dbd4d9d1bf4c120..ba0d6b14b754f80447df2a36b1e5f62d2b1e3fd9 100644 (file)
@@ -1,18 +1,43 @@
+// Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
 %module libSALOME_Comm
 
+%feature("autodoc", "1");
+
 %{
   #include "ReceiverFactory.hxx"
-  #undef SEEK_SET
-  #undef SEEK_CUR
-  #undef SEEK_END
+  #include "MatrixClient.hxx"
   #include "SALOME_Comm_i.hxx"
+  #include "SALOMEMultiComm.hxx"
+  #include "SenderFactory.hxx"
+  #include "OpUtil.hxx"
 %}
 
-%typemap(python,in) SALOME::SenderDouble_ptr
+%typemap(in) SALOME::SenderDouble_ptr
 {
   PyObject* pdict = PyDict_New();
   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
-  PyRun_String("import CORBA", Py_single_input, pdict, pdict);
+  PyRun_String("from omniORB import CORBA", Py_single_input, pdict, pdict);
  
   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
                    pdict, pdict);
@@ -21,7 +46,7 @@
 
   // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
 
-  PyObject* iorSupport = PyObject_CallMethod(orb, "object_to_string", "O", $input);
+  PyObject* iorSupport = PyObject_CallMethod(orb, (char*)"object_to_string", (char*)"O", $input);
  
   if (iorSupport == Py_None)
     return NULL;
  
   // Ask omniORB to convert IOR string to SALOME::SenderDouble_ptr
 
-  int argc = 0;
-  char *xargv = "";
-  char **argv = &xargv;
-  CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
+  CORBA::ORB_var ORB = KERNEL::GetRefToORB();
   CORBA::Object_var O =  ORB->string_to_object(s);
   SALOME::SenderDouble_ptr t = SALOME::SenderDouble::_narrow(O);
   $1 = t;
 }
 
-%typemap(python,in) SALOME::SenderInt_ptr
+%typemap(in) SALOME::SenderInt_ptr
 {
   PyObject* pdict = PyDict_New();
   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
-  PyRun_String("import CORBA", Py_single_input, pdict, pdict);
+  PyRun_String("from omniORB import CORBA", Py_single_input, pdict, pdict);
  
   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
                    pdict, pdict);
@@ -51,7 +73,7 @@
 
   // Ask omniORBpy to transform SUPPORT (python Corba) ptr to IOR string
 
-  PyObject* iorSupport = PyObject_CallMethod(orb, "object_to_string", "O", $input);
+  PyObject* iorSupport = PyObject_CallMethod(orb, (char*)"object_to_string", (char*)"O", $input);
  
   if (iorSupport == Py_None)
     return NULL;
  
   // Ask omniORB to convert IOR string to SALOME::SenderInt_ptr
 
-  int argc = 0;
-  char *xargv = "";
-  char **argv = &xargv;
-  CORBA::ORB_var ORB = CORBA::ORB_init(argc, argv);
+  CORBA::ORB_var ORB = KERNEL::GetRefToORB();
   CORBA::Object_var O =  ORB->string_to_object(s);
   SALOME::SenderInt_ptr t = SALOME::SenderInt::_narrow(O);
   $1 = t;
 }
 
+%typemap(out) SALOME::SenderDouble_ptr
+{  
+   PyObject* pdict = PyDict_New();
+   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
+   PyRun_String("from omniORB import CORBA", Py_single_input, pdict, pdict);
+   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
+                   pdict, pdict);
+   PyObject* orb = PyDict_GetItemString(pdict, "o");
+   // Get the orb Corba C++
+   CORBA::ORB_var ORB = KERNEL::GetRefToORB();
+   std::string s =  ORB->object_to_string($1);
+   PyObject * tmp = PyString_FromString(s.c_str());
+   $result = PyObject_CallMethod(orb, (char*)"string_to_object", (char*)"O", tmp);
+}
+
+%typemap(out) SALOME::SenderInt_ptr
+{  
+   PyObject* pdict = PyDict_New();
+   PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
+   PyRun_String("from omniORB import CORBA", Py_single_input, pdict, pdict);
+   PyRun_String("o = CORBA.ORB_init([''], CORBA.ORB_ID);", Py_single_input,
+                   pdict, pdict);
+   PyObject* orb = PyDict_GetItemString(pdict, "o");
+   // Get the orb Corba C++
+   CORBA::ORB_var ORB = KERNEL::GetRefToORB();
+   std::string s =  ORB->object_to_string($1);
+   PyObject * tmp = PyString_FromString(s.c_str());
+   $result = PyObject_CallMethod(orb, (char*)"string_to_object", (char*)"O", tmp);
+}
+
 PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble);
 
 %{
@@ -82,7 +131,7 @@ PyObject * getValueForSenderDouble(SALOME::SenderDouble_ptr senderDouble)
             int err = PyList_SetItem(py_list, i, Py_BuildValue("d", (double) ret[i]));
             if(err)
               {
-                char * message = "Error in SUPPORT::getTypes";
+                const char * message = "Error in SUPPORT::getTypes";
                 PyErr_SetString(PyExc_RuntimeError, message);
                 return NULL;
               }
@@ -109,7 +158,7 @@ PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt)
             int err = PyList_SetItem(py_list, i, Py_BuildValue("i", (int) ret[i]));
             if(err)
               {
-                char * message = "Error in SUPPORT::getTypes";
+                const char * message = "Error in SUPPORT::getTypes";
                 PyErr_SetString(PyExc_RuntimeError, message);
                 return NULL;
               }
@@ -120,3 +169,79 @@ PyObject * getValueForSenderInt(SALOME::SenderInt_ptr senderInt)
   return result;
 }
 %}
+
+PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix);
+%{
+PyObject * getValueForMatrix(SALOME::Matrix_ptr matrix)
+{
+  PyObject *py_list;
+  int column,row;
+  double *ret=MatrixClient::getValue(matrix,column,row);
+  py_list = PyList_New(row);
+  for(int i=0;i<row;i++)
+    {
+       PyObject *tmpRow=PyList_New(column);
+       for(int j=0;j<column;j++)
+         {
+           int err = PyList_SetItem(tmpRow, j, Py_BuildValue("d", (double) ret[i*column+j]));
+            if(err)
+              {
+                const char * message = "PyList_SetItem matrix sent may be invalid";
+                PyErr_SetString(PyExc_RuntimeError, message);
+                return NULL;
+              }
+         }
+       PyList_SetItem(py_list,i,tmpRow);
+       Py_DECREF(tmpRow);
+    }
+  delete [] ret;
+  Py_DECREF(py_list);
+  return py_list;
+}
+%}
+
+SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist);
+%{
+SALOME::SenderDouble_ptr buildSenderDoubleFromList(PyObject *pylist)
+{
+  if (PyList_Check(pylist)) 
+  {
+    int listLgth = PyList_Size(pylist);
+    double *tab=new double[listLgth];
+    for (int i=0;i<listLgth;i++)
+       {
+         tab[i]=PyFloat_AsDouble(PyList_GetItem(pylist,i));
+       }
+    SALOMEMultiComm communicator;
+    return SenderFactory::buildSender(communicator,tab,listLgth,true);
+  }
+  else
+  { 
+    PyErr_SetString(PyExc_TypeError,"not a list");
+    return SALOME::SenderDouble::_nil();
+  }
+}
+%}
+
+SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist);
+%{
+SALOME::SenderInt_ptr buildSenderIntFromList(PyObject *pylist)
+{
+  if (PyList_Check(pylist)) 
+  {
+    int listLgth = PyList_Size(pylist);
+    int *tab=new int[listLgth];
+    for (int i=0;i<listLgth;i++)
+       {
+         tab[i]=PyInt_AsLong(PyList_GetItem(pylist,i));
+       }
+    SALOMEMultiComm communicator;
+    return SenderFactory::buildSender(communicator,tab,listLgth,true);
+  }
+  else
+  { 
+    PyErr_SetString(PyExc_TypeError,"not a list");
+    return SALOME::SenderInt::_nil();
+  }
+}
+%}