1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 // Author : Anthony Geay (CEA/DEN)
23 static PyObject* convertMEDFileMesh(ParaMEDMEM::MEDFileMesh* mesh, int owner) throw(INTERP_KERNEL::Exception)
26 if(dynamic_cast<ParaMEDMEM::MEDFileUMesh *>(mesh))
27 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDFileUMesh,owner);
28 if(dynamic_cast<ParaMEDMEM::MEDFileCMesh *>(mesh))
29 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDFileCMesh,owner);
31 throw INTERP_KERNEL::Exception("Not recognized type of MEDFileMesh on downcast !");
35 static std::vector<std::pair<int,int> > convertTimePairIdsFromPy(PyObject *pyLi) throw(INTERP_KERNEL::Exception)
37 std::vector<std::pair<int,int> > ret;
38 if(PyList_Check(pyLi))
40 int size=PyList_Size(pyLi);
42 for(int i=0;i<size;i++)
44 PyObject *o=PyList_GetItem(pyLi,i);
48 int size2=PyTuple_Size(o);
50 throw INTERP_KERNEL::Exception("tuples in list must be of size 2 (dt,it) !");
51 PyObject *o0=PyTuple_GetItem(o,0);
53 p.first=(int)PyInt_AS_LONG(o0);
55 throw INTERP_KERNEL::Exception("First elem of tuples in list must be integer : dt !");
56 PyObject *o1=PyTuple_GetItem(o,1);
58 p.second=(int)PyInt_AS_LONG(o1);
60 throw INTERP_KERNEL::Exception("Second elem of tuples in list must be integer : dt !");
64 throw INTERP_KERNEL::Exception("list must contain tuples only");
68 throw INTERP_KERNEL::Exception("convertTimePairIdsFromPy : not a list");
72 static void converPyListToVecString(PyObject *pyLi, std::vector<std::string>& v)
74 if(PyList_Check(pyLi))
76 int size=PyList_Size(pyLi);
78 for(int i=0;i<size;i++)
80 PyObject *o=PyList_GetItem(pyLi,i);
81 if(!PyString_Check(o))
82 throw INTERP_KERNEL::Exception("In list passed in argument some elements are NOT strings ! Expected a list containing only strings !");
83 const char *st=PyString_AsString(o);
87 else if(PyTuple_Check(pyLi))
89 int size=PyTuple_Size(pyLi);
91 for(int i=0;i<size;i++)
93 PyObject *o=PyTuple_GetItem(pyLi,i);
94 if(!PyString_Check(o))
95 throw INTERP_KERNEL::Exception("In tuple passed in argument some elements are NOT strings ! Expected a tuple containing only strings !");
96 const char *st=PyString_AsString(o);
100 else if(PyString_Check(pyLi))
103 v[0]=std::string((const char *)PyString_AsString(pyLi));
107 throw INTERP_KERNEL::Exception("Unrecognized python argument : expected a list of string or tuple of string or string !");
111 static PyObject *convertFieldDoubleVecToPy(const std::vector<ParaMEDMEM::MEDCouplingFieldDouble *>& li)
114 PyObject *ret=PyList_New(sz);
115 for(int i=0;i<sz;i++)
117 PyObject *o=SWIG_NewPointerObj((void*)li[i],SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDouble,SWIG_POINTER_OWN | 0);
118 PyList_SetItem(ret,i,o);
123 PyObject *convertVecPairVecStToPy(const std::vector< std::pair<std::vector<std::string>, std::string > >& vec)
125 int sz=(int)vec.size();
126 PyObject *ret=PyList_New(sz);
127 for(int i=0;i<sz;i++)
129 PyObject *t=PyTuple_New(2);
130 int sz2=(int)vec[i].first.size();
131 PyObject *ll=PyList_New(sz2);
132 for(int j=0;j<sz2;j++)
133 PyList_SetItem(ll,j,PyString_FromString(vec[i].first[j].c_str()));
134 PyTuple_SetItem(t,0,ll);
135 PyTuple_SetItem(t,1,PyString_FromString(vec[i].second.c_str()));
136 PyList_SetItem(ret,i,t);
141 std::vector< std::pair<std::string, std::string > > convertVecPairStStFromPy(PyObject *pyLi)
143 std::vector< std::pair<std::string, std::string > > ret;
144 const char *msg="convertVecPairStStFromPy : Expecting PyList of Tuples of size 2 ! The first elt in tuple is one string and the 2nd one a string !";
145 if(PyList_Check(pyLi))
147 int size=PyList_Size(pyLi);
149 for(int i=0;i<size;i++)
151 PyObject *o=PyList_GetItem(pyLi,i);
154 std::pair<std::string, std::string> p;
155 int size2=PyTuple_Size(o);
157 throw INTERP_KERNEL::Exception(msg);
158 PyObject *o0=PyTuple_GetItem(o,0);
159 if(PyString_Check(o0))
160 p.second=std::string(PyString_AsString(o0));
162 throw INTERP_KERNEL::Exception(msg);
163 PyObject *o1=PyTuple_GetItem(o,1);
164 if(PyString_Check(o1))
165 p.second=std::string(PyString_AsString(o1));
167 throw INTERP_KERNEL::Exception(msg);
171 throw INTERP_KERNEL::Exception(msg);
175 throw INTERP_KERNEL::Exception(msg);
178 std::vector< std::pair<std::vector<std::string>, std::string > > convertVecPairVecStFromPy(PyObject *pyLi)
180 std::vector< std::pair<std::vector<std::string>, std::string > > ret;
181 const char *msg="convertVecPairVecStFromPy : Expecting PyList of Tuples of size 2 ! The first elt in tuple is a list of strings and the 2nd one a string !";
182 if(PyList_Check(pyLi))
184 int size=PyList_Size(pyLi);
186 for(int i=0;i<size;i++)
188 PyObject *o=PyList_GetItem(pyLi,i);
191 std::pair<std::vector<std::string>, std::string> p;
192 int size2=PyTuple_Size(o);
194 throw INTERP_KERNEL::Exception(msg);
195 PyObject *o0=PyTuple_GetItem(o,0);
198 int size2=PyList_Size(o0);
199 p.first.resize(size2);
200 for(int j=0;j<size2;j++)
202 PyObject *o0j=PyList_GetItem(o0,j);
203 if(PyString_Check(o0j))
205 p.first[j]=std::string(PyString_AsString(o0j));
208 throw INTERP_KERNEL::Exception(msg);
212 throw INTERP_KERNEL::Exception(msg);
213 PyObject *o1=PyTuple_GetItem(o,1);
214 if(PyString_Check(o1))
215 p.second=std::string(PyString_AsString(o1));
217 throw INTERP_KERNEL::Exception(msg);
221 throw INTERP_KERNEL::Exception(msg);
225 throw INTERP_KERNEL::Exception(msg);