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