1 // Copyright (C) 2007-2016 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, or (at your option) any later version.
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(MEDCoupling::MEDFileMesh* mesh, int owner) throw(INTERP_KERNEL::Exception)
31 if(dynamic_cast<MEDCoupling::MEDFileUMesh *>(mesh))
32 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDFileUMesh,owner);
33 if(dynamic_cast<MEDCoupling::MEDFileCMesh *>(mesh))
34 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDFileCMesh,owner);
35 if(dynamic_cast<MEDCoupling::MEDFileCurveLinearMesh *>(mesh))
36 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_MEDCoupling__MEDFileCurveLinearMesh,owner);
38 throw INTERP_KERNEL::Exception("Not recognized type of MEDFileMesh on downcast !");
42 static PyObject *convertMEDFileParameter1TS(MEDCoupling::MEDFileParameter1TS* p1ts, int owner) throw(INTERP_KERNEL::Exception)
50 if(dynamic_cast<MEDFileParameterDouble1TS *>(p1ts))
51 ret=SWIG_NewPointerObj((void*)p1ts,SWIGTYPE_p_MEDCoupling__MEDFileParameterDouble1TS,owner);
52 if(dynamic_cast<MEDFileParameterDouble1TSWTI *>(p1ts))
53 ret=SWIG_NewPointerObj((void*)p1ts,SWIGTYPE_p_MEDCoupling__MEDFileParameterDouble1TSWTI,owner);
55 throw INTERP_KERNEL::Exception("Not recognized type of MEDFileParameter1TS on downcast !");
59 static PyObject *convertMEDFileField1TS(MEDCoupling::MEDFileAnyTypeField1TS *p, int owner) throw(INTERP_KERNEL::Exception)
67 if(dynamic_cast<MEDFileField1TS *>(p))
68 ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileField1TS,owner);
69 if(dynamic_cast<MEDFileIntField1TS *>(p))
70 ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileIntField1TS,owner);
71 if(dynamic_cast<MEDFileFloatField1TS *>(p))
72 ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileFloatField1TS,owner);
74 throw INTERP_KERNEL::Exception("Not recognized type of MEDFileAnyTypeField1TS on downcast !");
78 static PyObject *convertMEDFileFieldMultiTS(MEDCoupling::MEDFileAnyTypeFieldMultiTS *p, int owner) throw(INTERP_KERNEL::Exception)
86 if(dynamic_cast<MEDFileFieldMultiTS *>(p))
87 ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileFieldMultiTS,owner);
88 if(dynamic_cast<MEDFileIntFieldMultiTS *>(p))
89 ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileIntFieldMultiTS,owner);
90 if(dynamic_cast<MEDFileFloatFieldMultiTS *>(p))
91 ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileFloatFieldMultiTS,owner);
93 throw INTERP_KERNEL::Exception("Not recognized type of MEDFileAnyTypeFieldMultiTS on downcast !");
97 static PyObject *convertMEDMeshMultiLev(MEDCoupling::MEDMeshMultiLev *p, int owner) throw(INTERP_KERNEL::Exception)
105 if(dynamic_cast<MEDUMeshMultiLev *>(p))
106 ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDUMeshMultiLev,owner);
107 if(dynamic_cast<MEDCMeshMultiLev *>(p))
108 ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDCMeshMultiLev,owner);
109 if(dynamic_cast<MEDCurveLinearMeshMultiLev *>(p))
110 ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDCurveLinearMeshMultiLev,owner);
112 throw INTERP_KERNEL::Exception("Not recognized type of MEDMeshMultiLev on downcast !");
116 static std::vector<std::pair<int,int> > convertTimePairIdsFromPy(PyObject *pyLi) throw(INTERP_KERNEL::Exception)
118 std::vector<std::pair<int,int> > ret;
119 if(PyList_Check(pyLi))
121 int size=PyList_Size(pyLi);
123 for(int i=0;i<size;i++)
125 PyObject *o=PyList_GetItem(pyLi,i);
128 std::pair<int,int> p;
129 int size2=PyTuple_Size(o);
131 throw INTERP_KERNEL::Exception("tuples in list must be of size 2 (dt,it) !");
132 PyObject *o0=PyTuple_GetItem(o,0);
134 p.first=(int)PyInt_AS_LONG(o0);
136 throw INTERP_KERNEL::Exception("First elem of tuples in list must be integer : dt !");
137 PyObject *o1=PyTuple_GetItem(o,1);
139 p.second=(int)PyInt_AS_LONG(o1);
141 throw INTERP_KERNEL::Exception("Second elem of tuples in list must be integer : dt !");
145 throw INTERP_KERNEL::Exception("list must contain tuples only");
149 throw INTERP_KERNEL::Exception("convertTimePairIdsFromPy : not a list");
153 static std::vector< std::pair<TypeOfField,INTERP_KERNEL::NormalizedCellType> > convertVecPairIntToVecPairTOFCT(const std::vector<std::pair<int,int> >& tmp)
155 std::size_t sz(tmp.size());
156 std::vector< std::pair<TypeOfField,INTERP_KERNEL::NormalizedCellType> > entitiesCpp(sz);
157 for(std::size_t i=0;i<sz;i++)
159 entitiesCpp[i].first=(TypeOfField)tmp[i].first;
160 entitiesCpp[i].second=(INTERP_KERNEL::NormalizedCellType)tmp[i].second;
165 static void converPyListToVecString(PyObject *pyLi, std::vector<std::string>& v)
167 if(PyList_Check(pyLi))
169 int size=PyList_Size(pyLi);
171 for(int i=0;i<size;i++)
173 PyObject *o=PyList_GetItem(pyLi,i);
174 if(!PyString_Check(o))
175 throw INTERP_KERNEL::Exception("In list passed in argument some elements are NOT strings ! Expected a list containing only strings !");
176 const char *st=PyString_AsString(o);
177 v[i]=std::string(st);
180 else if(PyTuple_Check(pyLi))
182 int size=PyTuple_Size(pyLi);
184 for(int i=0;i<size;i++)
186 PyObject *o=PyTuple_GetItem(pyLi,i);
187 if(!PyString_Check(o))
188 throw INTERP_KERNEL::Exception("In tuple passed in argument some elements are NOT strings ! Expected a tuple containing only strings !");
189 const char *st=PyString_AsString(o);
190 v[i]=std::string(st);
193 else if(PyString_Check(pyLi))
196 v[0]=std::string((const char *)PyString_AsString(pyLi));
200 throw INTERP_KERNEL::Exception("Unrecognized python argument : expected a list of string or tuple of string or string !");
204 static PyObject *convertFieldDoubleVecToPy(const std::vector<MEDCoupling::MEDCouplingFieldDouble *>& li)
207 PyObject *ret=PyList_New(sz);
208 for(int i=0;i<sz;i++)
210 PyObject *o=SWIG_NewPointerObj((void*)li[i],SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,SWIG_POINTER_OWN | 0);
211 PyList_SetItem(ret,i,o);
216 PyObject *convertVecPairIntToPy(const std::vector< std::pair<int,int> >& vec)
218 PyObject *ret(PyList_New(vec.size()));
220 for(std::vector< std::pair<int,int> >::const_iterator iter=vec.begin();iter!=vec.end();iter++,rk++)
222 PyObject *elt=PyTuple_New(2);
223 PyTuple_SetItem(elt,0,SWIG_From_int((*iter).first));
224 PyTuple_SetItem(elt,1,SWIG_From_int((*iter).second));
225 PyList_SetItem(ret,rk,elt);
230 PyObject *convertVecPairVecStToPy(const std::vector< std::pair<std::vector<std::string>, std::string > >& vec)
232 int sz=(int)vec.size();
233 PyObject *ret=PyList_New(sz);
234 for(int i=0;i<sz;i++)
236 PyObject *t=PyTuple_New(2);
237 int sz2=(int)vec[i].first.size();
238 PyObject *ll=PyList_New(sz2);
239 for(int j=0;j<sz2;j++)
240 PyList_SetItem(ll,j,PyString_FromString(vec[i].first[j].c_str()));
241 PyTuple_SetItem(t,0,ll);
242 PyTuple_SetItem(t,1,PyString_FromString(vec[i].second.c_str()));
243 PyList_SetItem(ret,i,t);
248 PyObject *convertVectPairStToPy(const std::vector< std::pair<std::string, std::string > >& vec)
250 int sz=(int)vec.size();
251 PyObject *ret=PyList_New(sz);
252 for(int i=0;i<sz;i++)
254 PyObject *t=PyTuple_New(2);
255 PyTuple_SetItem(t,0,PyString_FromString(vec[i].first.c_str()));
256 PyTuple_SetItem(t,1,PyString_FromString(vec[i].second.c_str()));
257 PyList_SetItem(ret,i,t);
262 std::vector< std::pair<std::string, std::string > > convertVecPairStStFromPy(PyObject *pyLi)
264 std::vector< std::pair<std::string, std::string > > ret;
265 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 !";
266 if(PyList_Check(pyLi))
268 int size=PyList_Size(pyLi);
270 for(int i=0;i<size;i++)
272 PyObject *o=PyList_GetItem(pyLi,i);
275 std::pair<std::string, std::string> p;
276 int size2=PyTuple_Size(o);
278 throw INTERP_KERNEL::Exception(msg);
279 PyObject *o0=PyTuple_GetItem(o,0);
280 if(PyString_Check(o0))
281 p.first=std::string(PyString_AsString(o0));
283 throw INTERP_KERNEL::Exception(msg);
284 PyObject *o1=PyTuple_GetItem(o,1);
285 if(PyString_Check(o1))
286 p.second=std::string(PyString_AsString(o1));
288 throw INTERP_KERNEL::Exception(msg);
292 throw INTERP_KERNEL::Exception(msg);
296 throw INTERP_KERNEL::Exception(msg);
299 std::vector< std::pair<std::vector<std::string>, std::string > > convertVecPairVecStFromPy(PyObject *pyLi)
301 std::vector< std::pair<std::vector<std::string>, std::string > > ret;
302 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 !";
303 if(PyList_Check(pyLi))
305 int size=PyList_Size(pyLi);
307 for(int i=0;i<size;i++)
309 PyObject *o=PyList_GetItem(pyLi,i);
312 std::pair<std::vector<std::string>, std::string> p;
313 int size2=PyTuple_Size(o);
315 throw INTERP_KERNEL::Exception(msg);
316 PyObject *o0=PyTuple_GetItem(o,0);
319 int size3=PyList_Size(o0);
320 p.first.resize(size3);
321 for(int j=0;j<size3;j++)
323 PyObject *o0j=PyList_GetItem(o0,j);
324 if(PyString_Check(o0j))
326 p.first[j]=std::string(PyString_AsString(o0j));
329 throw INTERP_KERNEL::Exception(msg);
333 throw INTERP_KERNEL::Exception(msg);
334 PyObject *o1=PyTuple_GetItem(o,1);
335 if(PyString_Check(o1))
336 p.second=std::string(PyString_AsString(o1));
338 throw INTERP_KERNEL::Exception(msg);
342 throw INTERP_KERNEL::Exception(msg);
346 throw INTERP_KERNEL::Exception(msg);
350 * Called by MEDFileAnyTypeFieldMultiTS::__getitem__ when \a elt0 is neither a list nor a slice.
351 * In this case a MEDFileAnyTypeField1TS object is returned.
353 int MEDFileAnyTypeFieldMultiTSgetitemSingleTS__(const MEDFileAnyTypeFieldMultiTS *self, PyObject *elt0) throw(INTERP_KERNEL::Exception)
355 if(elt0 && PyInt_Check(elt0))
357 return InterpreteNegativeInt(PyInt_AS_LONG(elt0),self->getNumberOfTS());
359 else if(elt0 && PyTuple_Check(elt0))
361 if(PyTuple_Size(elt0)==2)
363 PyObject *o0=PyTuple_GetItem(elt0,0);
364 PyObject *o1=PyTuple_GetItem(elt0,1);
365 if(PyInt_Check(o0) && PyInt_Check(o1))
367 int iter=PyInt_AS_LONG(o0);
368 int order=PyInt_AS_LONG(o1);
369 return self->getPosOfTimeStep(iter,order);
372 throw INTERP_KERNEL::Exception("MEDFileAnyTypeFieldMultiTS::__getitem__ : invalid input param ! input is a tuple of size 2 but two integers are expected in this tuple to request a time steps !");
375 throw INTERP_KERNEL::Exception("MEDFileAnyTypeFieldMultiTS::__getitem__ : invalid input param ! input is a tuple of size != 2 ! two integers are expected in this tuple to request a time steps !");
377 else if(elt0 && PyFloat_Check(elt0))
379 double val=PyFloat_AS_DOUBLE(elt0);
380 return self->getPosGivenTime(val);
383 throw INTERP_KERNEL::Exception("MEDFileAnyTypeFieldMultiTS::__getitem__ : invalid input params ! expected fmts[int], fmts[int,int], or fmts[double] to request one time step ! To request a series of time steps invoke fmts[slice], fmts[list of int], fmts[list of double], or fmts[list of int,int] !");
387 * Called by MEDFileAnyTypeFieldMultiTS::__getitem__ when \a obj is neither a list nor a slice.
388 * In this case a MEDFileAnyTypeField1TS object is returned.
390 int MEDFileFieldsgetitemSingleTS__(const MEDFileFields *self, PyObject *obj) throw(INTERP_KERNEL::Exception)
394 return InterpreteNegativeInt((int)PyInt_AS_LONG(obj),self->getNumberOfFields());
396 else if(PyString_Check(obj))
398 return self->getPosFromFieldName(PyString_AsString(obj));
401 throw INTERP_KERNEL::Exception("MEDFileFields::__getitem__ : only integer or string with fieldname supported !");
404 void convertToMapIntDataArrayInt(PyObject *pyMap, std::map<int, MCAuto<DataArrayInt> >& cppMap)
406 if(!PyDict_Check(pyMap))
407 throw INTERP_KERNEL::Exception("convertToMapIntDataArrayInt : input is not a python map !");
408 PyObject *key, *value;
411 while (PyDict_Next(pyMap,&pos,&key,&value))
413 if(!PyInt_Check(key))
414 throw INTERP_KERNEL::Exception("convertToMapIntDataArrayInt : keys in map must be PyInt !");
415 long k(PyInt_AS_LONG(key));
417 int status(SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_MEDCoupling__DataArrayInt,0|0));
418 if(!SWIG_IsOK(status))
420 std::ostringstream oss; oss << "convertToMapIntDataArrayInt : values in map must be DataArrayInt !";
421 throw INTERP_KERNEL::Exception(oss.str().c_str());
423 DataArrayInt *arg(reinterpret_cast<DataArrayInt*>(argp));
424 MCAuto<DataArrayInt> arg2(arg);