Salome HOME
Copyright update 2021
[tools/medcoupling.git] / src / MEDLoader / Swig / MEDLoaderTypemaps.i
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 // Author : Anthony Geay (EDF R&D)
20
21 #include <vector>
22
23 static PyObject *convertMEDFileMesh(MEDCoupling::MEDFileMesh* mesh, int owner)
24 {
25   PyObject *ret=0;
26   if(!mesh)
27     {
28       Py_XINCREF(Py_None);
29       return Py_None;
30     }
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);
37   if(!ret)
38     throw INTERP_KERNEL::Exception("Not recognized type of MEDFileMesh on downcast !");
39   return ret;
40 }
41
42 static PyObject *convertMEDFileParameter1TS(MEDCoupling::MEDFileParameter1TS* p1ts, int owner)
43 {
44   PyObject *ret=0;
45   if(!p1ts)
46     {
47       Py_XINCREF(Py_None);
48       return Py_None;
49     }
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);
54   if(!ret)
55     throw INTERP_KERNEL::Exception("Not recognized type of MEDFileParameter1TS on downcast !");
56   return ret;
57 }
58
59 static PyObject *convertMEDFileField1TS(MEDCoupling::MEDFileAnyTypeField1TS *p, int owner)
60 {
61   PyObject *ret=0;
62   if(!p)
63     {
64       Py_XINCREF(Py_None);
65       return Py_None;
66     }
67   if(dynamic_cast<MEDFileField1TS *>(p))
68     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileField1TS,owner);
69   if(dynamic_cast<MEDFileInt32Field1TS *>(p))
70     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileInt32Field1TS,owner);
71   if(dynamic_cast<MEDFileInt64Field1TS *>(p))
72     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileInt64Field1TS,owner);
73   if(dynamic_cast<MEDFileFloatField1TS *>(p))
74     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileFloatField1TS,owner);
75   if(!ret)
76     throw INTERP_KERNEL::Exception("Not recognized type of MEDFileAnyTypeField1TS on downcast !");
77   return ret;
78 }
79
80 static PyObject *convertMEDFileFieldMultiTS(MEDCoupling::MEDFileAnyTypeFieldMultiTS *p, int owner)
81 {
82   PyObject *ret=0;
83   if(!p)
84     {
85       Py_XINCREF(Py_None);
86       return Py_None;
87     }
88   if(dynamic_cast<MEDFileFieldMultiTS *>(p))
89     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileFieldMultiTS,owner);
90   if(dynamic_cast<MEDFileInt32FieldMultiTS *>(p))
91     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileInt32FieldMultiTS,owner);
92   if(dynamic_cast<MEDFileInt64FieldMultiTS *>(p))
93     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileInt64FieldMultiTS,owner);
94   if(dynamic_cast<MEDFileFloatFieldMultiTS *>(p))
95     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDFileFloatFieldMultiTS,owner);
96   if(!ret)
97     throw INTERP_KERNEL::Exception("Not recognized type of MEDFileAnyTypeFieldMultiTS on downcast !");
98   return ret;
99 }
100
101 static PyObject *convertMEDMeshMultiLev(MEDCoupling::MEDMeshMultiLev *p, int owner)
102 {
103   PyObject *ret=0;
104   if(!p)
105     {
106       Py_XINCREF(Py_None);
107       return Py_None;
108     }
109   if(dynamic_cast<MEDUMeshMultiLev *>(p))
110     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDUMeshMultiLev,owner);
111   if(dynamic_cast<MEDCMeshMultiLev *>(p))
112     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDCMeshMultiLev,owner);
113   if(dynamic_cast<MEDCurveLinearMeshMultiLev *>(p))
114     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_MEDCoupling__MEDCurveLinearMeshMultiLev,owner);
115   if(!ret)
116     throw INTERP_KERNEL::Exception("Not recognized type of MEDMeshMultiLev on downcast !");
117   return ret;
118 }
119
120 static std::vector<std::pair<int,int> > convertTimePairIdsFromPy(PyObject *pyLi)
121 {
122   std::vector<std::pair<int,int> > ret;
123   if(PyList_Check(pyLi))
124     {
125       std::size_t size=PyList_Size(pyLi);
126       ret.resize(size);
127       for(std::size_t i=0;i<size;i++)
128         {
129           PyObject *o=PyList_GetItem(pyLi,i);
130           if(PyTuple_Check(o))
131             {
132               std::pair<int,int> p;
133               std::size_t size2=PyTuple_Size(o);
134               if(size2!=2)
135                 throw INTERP_KERNEL::Exception("tuples in list must be of size 2 (dt,it) !");
136               PyObject *o0=PyTuple_GetItem(o,0);
137               if(PyInt_Check(o0))
138                 p.first=(int)PyInt_AS_LONG(o0);
139               else
140                 throw INTERP_KERNEL::Exception("First elem of tuples in list must be integer : dt !");
141               PyObject *o1=PyTuple_GetItem(o,1);
142               if(PyInt_Check(o1))
143                 p.second=(int)PyInt_AS_LONG(o1);
144               else
145                 throw INTERP_KERNEL::Exception("Second elem of tuples in list must be integer : dt !");
146               ret[i]=p;
147             }
148           else
149             throw INTERP_KERNEL::Exception("list must contain tuples only");
150         }
151     }
152   else
153     throw INTERP_KERNEL::Exception("convertTimePairIdsFromPy : not a list");
154   return ret;
155 }
156
157 static std::vector< std::pair<TypeOfField,INTERP_KERNEL::NormalizedCellType> > convertVecPairIntToVecPairTOFCT(const std::vector<std::pair<int,int> >& tmp)
158 {
159   std::size_t sz(tmp.size());
160   std::vector< std::pair<TypeOfField,INTERP_KERNEL::NormalizedCellType> > entitiesCpp(sz);
161   for(std::size_t i=0;i<sz;i++)
162     {
163       entitiesCpp[i].first=(TypeOfField)tmp[i].first;
164       entitiesCpp[i].second=(INTERP_KERNEL::NormalizedCellType)tmp[i].second;
165     }
166   return entitiesCpp;
167 }
168
169 static void converPyListToVecString(PyObject *pyLi, std::vector<std::string>& v)
170 {
171   static const char msg0[]="In list passed in argument some elements are NOT strings ! Expected a list containing only strings !";
172   static const char msg1[]="In tuple passed in argument some elements are NOT strings ! Expected a list containing only strings !";
173   static const char msg2[]="Unrecognized python argument : expected a list of string or tuple of string or string !";
174   if(PyList_Check(pyLi))
175     {
176       std::size_t size=PyList_Size(pyLi);
177       v.resize(size);
178       for(std::size_t i=0;i<size;i++)
179         {
180           PyObject *o=PyList_GetItem(pyLi,i);
181           v[i]=convertPyObjectToStr(o,msg0);
182         }
183       return ;
184     }
185   else if(PyTuple_Check(pyLi))
186     {
187       std::size_t size=PyTuple_Size(pyLi);
188       v.resize(size);
189       for(std::size_t i=0;i<size;i++)
190         {
191           PyObject *o=PyTuple_GetItem(pyLi,i);
192           v[i]=convertPyObjectToStr(o,msg1);
193         }
194       return ;
195     }
196   v.resize(1);
197   v[0]=convertPyObjectToStr(pyLi,msg2);
198 }
199
200 static PyObject *convertFieldDoubleVecToPy(const std::vector<MEDCoupling::MEDCouplingFieldDouble *>& li)
201 {
202   std::size_t sz=li.size();
203   PyObject *ret=PyList_New(sz);
204   for(std::size_t i=0;i<sz;i++)
205     {
206       PyObject *o=SWIG_NewPointerObj((void*)li[i],SWIGTYPE_p_MEDCoupling__MEDCouplingFieldDouble,SWIG_POINTER_OWN | 0);
207       PyList_SetItem(ret,i,o);
208     }
209   return ret;
210 }
211
212 template< class T >
213 PyObject *convertVecPairIntToPy(const std::vector< std::pair<int,T> >& vec)
214 {
215   PyObject *ret(PyList_New(vec.size()));
216   int rk=0;
217   for(typename std::vector< std::pair<int,T> >::const_iterator iter=vec.begin();iter!=vec.end();iter++,rk++)
218     {
219       PyObject *elt=PyTuple_New(2);
220       PyTuple_SetItem(elt,0,SWIG_From_int((*iter).first));
221       PyTuple_SetItem(elt,1,PyInt_FromLong((*iter).second));
222       PyList_SetItem(ret,rk,elt);
223     }
224   return ret;
225 }
226
227 PyObject *convertVecPairVecStToPy(const std::vector< std::pair<std::vector<std::string>, std::string > >& vec)
228 {
229   std::size_t sz=vec.size();
230   PyObject *ret=PyList_New(sz);
231   for(std::size_t i=0;i<sz;i++)
232     {
233       PyObject *t=PyTuple_New(2);
234       int sz2=(int)vec[i].first.size();
235       PyObject *ll=PyList_New(sz2);
236       for(int j=0;j<sz2;j++)
237         PyList_SetItem(ll,j,PyString_FromString(vec[i].first[j].c_str()));
238       PyTuple_SetItem(t,0,ll);
239       PyTuple_SetItem(t,1,PyString_FromString(vec[i].second.c_str()));
240       PyList_SetItem(ret,i,t);
241     }
242   return ret;
243 }
244
245 PyObject *convertVectPairStToPy(const std::vector< std::pair<std::string, std::string > >& vec)
246 {
247   std::size_t sz=vec.size();
248   PyObject *ret=PyList_New(sz);
249   for(std::size_t i=0;i<sz;i++)
250     {
251       PyObject *t=PyTuple_New(2);
252       PyTuple_SetItem(t,0,PyString_FromString(vec[i].first.c_str()));
253       PyTuple_SetItem(t,1,PyString_FromString(vec[i].second.c_str()));
254       PyList_SetItem(ret,i,t);
255     }
256   return ret;
257 }
258
259 std::vector< std::pair<std::string, std::string > > convertVecPairStStFromPy(PyObject *pyLi)
260 {
261   std::vector< std::pair<std::string, std::string > > ret;
262   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 !";
263   if(PyList_Check(pyLi))
264     {
265       std::size_t size=PyList_Size(pyLi);
266       ret.resize(size);
267       for(std::size_t i=0;i<size;i++)
268         {
269           PyObject *o=PyList_GetItem(pyLi,i);
270           if(PyTuple_Check(o))
271             {
272               std::pair<std::string, std::string> p;
273               std::size_t size2=PyTuple_Size(o);
274               if(size2!=2)
275                 throw INTERP_KERNEL::Exception(msg);
276               PyObject *o0=PyTuple_GetItem(o,0);
277               p.first=convertPyObjectToStr(o0,msg);
278               PyObject *o1=PyTuple_GetItem(o,1);
279               p.second=convertPyObjectToStr(o1,msg);
280               ret[i]=p;
281             }
282           else
283             throw INTERP_KERNEL::Exception(msg);
284         }
285       return ret;
286     }
287   throw INTERP_KERNEL::Exception(msg);
288 }
289
290 std::vector< std::pair<std::vector<std::string>, std::string > > convertVecPairVecStFromPy(PyObject *pyLi)
291 {
292   std::vector< std::pair<std::vector<std::string>, std::string > > ret;
293   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 !";
294   if(PyList_Check(pyLi))
295     {
296       std::size_t size=PyList_Size(pyLi);
297       ret.resize(size);
298       for(std::size_t i=0;i<size;i++)
299         {
300           PyObject *o=PyList_GetItem(pyLi,i);
301           if(PyTuple_Check(o))
302             {
303               std::pair<std::vector<std::string>, std::string> p;
304               std::size_t size2=PyTuple_Size(o);
305               if(size2!=2)
306                 throw INTERP_KERNEL::Exception(msg);
307               PyObject *o0=PyTuple_GetItem(o,0);
308               if(PyList_Check(o0))
309                 {
310                   std::size_t size3=PyList_Size(o0);
311                   p.first.resize(size3);
312                   for(std::size_t j=0;j<size3;j++)
313                     {
314                       PyObject *o0j=PyList_GetItem(o0,j);
315                       p.first[j]=convertPyObjectToStr(o0j,msg);
316                     }
317                 }
318               else
319                 throw INTERP_KERNEL::Exception(msg);
320               PyObject *o1=PyTuple_GetItem(o,1);
321               p.second=convertPyObjectToStr(o1,msg);
322               ret[i]=p;
323             }
324           else
325             throw INTERP_KERNEL::Exception(msg);
326         }
327       return ret;
328     }
329   throw INTERP_KERNEL::Exception(msg);
330 }
331
332 /*!
333  * Called by MEDFileAnyTypeFieldMultiTS::__getitem__ when \a elt0 is neither a list nor a slice.
334  * In this case a MEDFileAnyTypeField1TS object is returned.
335  */
336 int MEDFileAnyTypeFieldMultiTSgetitemSingleTS__(const MEDFileAnyTypeFieldMultiTS *self, PyObject *elt0)
337 {
338   if(elt0 && PyInt_Check(elt0))
339     {//fmts[3]
340       return InterpreteNegativeInt(PyInt_AS_LONG(elt0),self->getNumberOfTS());
341     }
342   else if(elt0 && PyTuple_Check(elt0))
343     {
344       if(PyTuple_Size(elt0)==2)
345         {
346           PyObject *o0=PyTuple_GetItem(elt0,0);
347           PyObject *o1=PyTuple_GetItem(elt0,1);
348           if(PyInt_Check(o0) && PyInt_Check(o1))
349             {//fmts(1,-1)
350               int iter=(int)PyInt_AS_LONG(o0);
351               int order=(int)PyInt_AS_LONG(o1);
352               return self->getPosOfTimeStep(iter,order);
353             }
354           else
355             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 !");
356         }
357       else
358         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 !");
359     }
360   else if(elt0 && PyFloat_Check(elt0))
361     {
362       double val=PyFloat_AS_DOUBLE(elt0);
363       return self->getPosGivenTime(val);
364     }
365   else
366     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] !");
367 }
368
369 /*!
370  * Called by MEDFileAnyTypeFieldMultiTS::__getitem__ when \a obj is neither a list nor a slice.
371  * In this case a MEDFileAnyTypeField1TS object is returned.
372  */
373 int MEDFileFieldsgetitemSingleTS__(const MEDFileFields *self, PyObject *obj)
374 {
375   static const char msg[]="MEDFileFields::__getitem__ : only integer or string with fieldname supported !";
376   if(PyInt_Check(obj))
377     {
378       return InterpreteNegativeInt(PyInt_AS_LONG(obj),self->getNumberOfFields());
379     }
380   return self->getPosFromFieldName(convertPyObjectToStr(obj,msg));
381 }
382
383 void convertToMapIntDataArrayInt(PyObject *pyMap, std::map<int, MCAuto<DataArrayIdType> >& cppMap)
384 {
385   if(!PyDict_Check(pyMap))
386     throw INTERP_KERNEL::Exception("convertToMapIntDataArrayInt : input is not a python map !");
387   PyObject *key, *value;
388   Py_ssize_t pos(0);
389   cppMap.clear();
390   while (PyDict_Next(pyMap,&pos,&key,&value))
391     {
392       if(!PyInt_Check(key))
393         throw INTERP_KERNEL::Exception("convertToMapIntDataArrayInt : keys in map must be PyInt !");
394       int k((int)PyInt_AS_LONG(key));
395       void *argp(0);
396       int status(SWIG_ConvertPtr(value,&argp,SWIGTITraits<mcIdType>::TI,0|0));
397       if(!SWIG_IsOK(status))
398         {
399           std::ostringstream oss; oss << "convertToMapIntDataArrayInt : values in map must be DataArrayInt !";
400           throw INTERP_KERNEL::Exception(oss.str().c_str());
401         }
402       DataArrayIdType *arg(reinterpret_cast<DataArrayIdType*>(argp));
403       MCAuto<DataArrayIdType> arg2(arg);
404       if(arg)
405         arg->incrRef();
406       cppMap[k]=arg2;
407     }
408 }
409
410 template<class T>
411 PyObject *MEDFileField1TS_getFieldWithProfile(const typename MLFieldTraits<T>::F1TSType *self, TypeOfField type, int meshDimRelToMax, const MEDFileMesh *mesh) 
412 {
413   DataArrayIdType *ret1(NULL);
414   typename MEDCoupling::Traits<T>::ArrayType *ret0(self->getFieldWithProfile(type,meshDimRelToMax,mesh,ret1));
415   PyObject *ret(PyTuple_New(2));
416   PyTuple_SetItem(ret,0,SWIG_NewPointerObj(SWIG_as_voidptr(ret0),SWIGTITraits<T>::TI, SWIG_POINTER_OWN | 0 ));
417   PyTuple_SetItem(ret,1,SWIG_NewPointerObj(SWIG_as_voidptr(ret1),SWIGTITraits<mcIdType>::TI, SWIG_POINTER_OWN | 0 ));
418   return ret;
419 }
420
421 template<class T>
422 PyObject *MEDFileField1TS_getUndergroundDataArrayExt(const typename MLFieldTraits<T>::F1TSType *self)
423 {
424   std::vector< std::pair<std::pair<INTERP_KERNEL::NormalizedCellType,int>,std::pair<mcIdType,mcIdType> > > elt1Cpp;
425   typename MEDCoupling::Traits<T>::ArrayType *elt0=self->getUndergroundDataArrayExt(elt1Cpp);
426   if(elt0)
427     elt0->incrRef();
428   PyObject *ret=PyTuple_New(2);
429   PyTuple_SetItem(ret,0,SWIG_NewPointerObj(SWIG_as_voidptr(elt0),SWIGTITraits<T>::TI, SWIG_POINTER_OWN | 0 ));
430   std::size_t sz=elt1Cpp.size();
431   PyObject *elt=PyList_New(sz);
432   for(std::size_t i=0;i<sz;i++)
433     {
434       PyObject *elt1=PyTuple_New(2);
435       PyObject *elt2=PyTuple_New(2);
436       PyTuple_SetItem(elt2,0,SWIG_From_int((int)elt1Cpp[i].first.first));
437       PyTuple_SetItem(elt2,1,SWIG_From_int(elt1Cpp[i].first.second));
438       PyObject *elt3=PyTuple_New(2);
439       PyTuple_SetItem(elt3,0,PyInt_FromLong(elt1Cpp[i].second.first));
440       PyTuple_SetItem(elt3,1,PyInt_FromLong(elt1Cpp[i].second.second));
441       PyTuple_SetItem(elt1,0,elt2);
442       PyTuple_SetItem(elt1,1,elt3);
443       PyList_SetItem(elt,i,elt1);
444     }
445   PyTuple_SetItem(ret,1,elt);
446   return ret;
447 }