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