Salome HOME
On the road of last imps for MEDReader
[modules/med.git] / src / MEDLoader / Swig / MEDLoaderTypemaps.i
1 // Copyright (C) 2007-2013  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.
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(ParaMEDMEM::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<ParaMEDMEM::MEDFileUMesh *>(mesh))
32     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDFileUMesh,owner);
33   if(dynamic_cast<ParaMEDMEM::MEDFileCMesh *>(mesh))
34     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDFileCMesh,owner);
35   if(dynamic_cast<ParaMEDMEM::MEDFileCurveLinearMesh *>(mesh))
36     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__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(ParaMEDMEM::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_ParaMEDMEM__MEDFileParameterDouble1TS,owner);
52   if(dynamic_cast<MEDFileParameterDouble1TSWTI *>(p1ts))
53     ret=SWIG_NewPointerObj((void*)p1ts,SWIGTYPE_p_ParaMEDMEM__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(ParaMEDMEM::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_ParaMEDMEM__MEDFileField1TS,owner);
69   if(dynamic_cast<MEDFileIntField1TS *>(p))
70     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_ParaMEDMEM__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(ParaMEDMEM::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_ParaMEDMEM__MEDFileFieldMultiTS,owner);
86   if(dynamic_cast<MEDFileIntFieldMultiTS *>(p))
87     ret=SWIG_NewPointerObj((void*)p,SWIGTYPE_p_ParaMEDMEM__MEDFileIntFieldMultiTS,owner);
88   if(!ret)
89     throw INTERP_KERNEL::Exception("Not recognized type of MEDFileAnyTypeFieldMultiTS on downcast !");
90   return ret;
91 }
92
93 static std::vector<std::pair<int,int> > convertTimePairIdsFromPy(PyObject *pyLi) throw(INTERP_KERNEL::Exception)
94 {
95   std::vector<std::pair<int,int> > ret;
96   if(PyList_Check(pyLi))
97     {
98       int size=PyList_Size(pyLi);
99       ret.resize(size);
100       for(int i=0;i<size;i++)
101         {
102           PyObject *o=PyList_GetItem(pyLi,i);
103           if(PyTuple_Check(o))
104             {
105               std::pair<int,int> p;
106               int size2=PyTuple_Size(o);
107               if(size2!=2)
108                 throw INTERP_KERNEL::Exception("tuples in list must be of size 2 (dt,it) !");
109               PyObject *o0=PyTuple_GetItem(o,0);
110               if(PyInt_Check(o0))
111                 p.first=(int)PyInt_AS_LONG(o0);
112               else
113                 throw INTERP_KERNEL::Exception("First elem of tuples in list must be integer : dt !");
114               PyObject *o1=PyTuple_GetItem(o,1);
115               if(PyInt_Check(o1))
116                 p.second=(int)PyInt_AS_LONG(o1);
117               else
118                 throw INTERP_KERNEL::Exception("Second elem of tuples in list must be integer : dt !");
119               ret[i]=p;
120             }
121           else
122             throw INTERP_KERNEL::Exception("list must contain tuples only");
123         }
124     }
125   else
126     throw INTERP_KERNEL::Exception("convertTimePairIdsFromPy : not a list");
127   return ret;
128 }
129
130 static void converPyListToVecString(PyObject *pyLi, std::vector<std::string>& v)
131 {
132   if(PyList_Check(pyLi))
133     {
134       int size=PyList_Size(pyLi);
135       v.resize(size);
136       for(int i=0;i<size;i++)
137         {
138           PyObject *o=PyList_GetItem(pyLi,i);
139           if(!PyString_Check(o))
140             throw INTERP_KERNEL::Exception("In list passed in argument some elements are NOT strings ! Expected a list containing only strings !");
141           const char *st=PyString_AsString(o);
142           v[i]=std::string(st);
143         }
144     }
145   else if(PyTuple_Check(pyLi))
146     {
147       int size=PyTuple_Size(pyLi);
148       v.resize(size);
149       for(int i=0;i<size;i++)
150         {
151           PyObject *o=PyTuple_GetItem(pyLi,i);
152           if(!PyString_Check(o))
153             throw INTERP_KERNEL::Exception("In tuple passed in argument some elements are NOT strings ! Expected a tuple containing only strings !");
154           const char *st=PyString_AsString(o);
155           v[i]=std::string(st);
156         }
157     }
158   else if(PyString_Check(pyLi))
159     {
160       v.resize(1);
161       v[0]=std::string((const char *)PyString_AsString(pyLi));
162     }
163   else
164     {
165       throw INTERP_KERNEL::Exception("Unrecognized python argument : expected a list of string or tuple of string or string !");
166     }
167 }
168
169 static PyObject *convertFieldDoubleVecToPy(const std::vector<ParaMEDMEM::MEDCouplingFieldDouble *>& li)
170 {
171   int sz=li.size();
172   PyObject *ret=PyList_New(sz);
173   for(int i=0;i<sz;i++)
174     {
175       PyObject *o=SWIG_NewPointerObj((void*)li[i],SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDouble,SWIG_POINTER_OWN | 0);
176       PyList_SetItem(ret,i,o);
177     }
178   return ret;
179 }
180
181 PyObject *convertVecPairVecStToPy(const std::vector< std::pair<std::vector<std::string>, std::string > >& vec)
182 {
183   int sz=(int)vec.size();
184   PyObject *ret=PyList_New(sz);
185   for(int i=0;i<sz;i++)
186     {
187       PyObject *t=PyTuple_New(2);
188       int sz2=(int)vec[i].first.size();
189       PyObject *ll=PyList_New(sz2);
190       for(int j=0;j<sz2;j++)
191         PyList_SetItem(ll,j,PyString_FromString(vec[i].first[j].c_str()));
192       PyTuple_SetItem(t,0,ll);
193       PyTuple_SetItem(t,1,PyString_FromString(vec[i].second.c_str()));
194       PyList_SetItem(ret,i,t);
195     }
196   return ret;
197 }
198
199 std::vector< std::pair<std::string, std::string > > convertVecPairStStFromPy(PyObject *pyLi)
200 {
201   std::vector< std::pair<std::string, std::string > > ret;
202   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 !";
203   if(PyList_Check(pyLi))
204     {
205       int size=PyList_Size(pyLi);
206       ret.resize(size);
207       for(int i=0;i<size;i++)
208         {
209           PyObject *o=PyList_GetItem(pyLi,i);
210           if(PyTuple_Check(o))
211             {
212               std::pair<std::string, std::string> p;
213               int size2=PyTuple_Size(o);
214               if(size2!=2)
215                 throw INTERP_KERNEL::Exception(msg);
216               PyObject *o0=PyTuple_GetItem(o,0);
217               if(PyString_Check(o0))
218                 p.first=std::string(PyString_AsString(o0));
219               else
220                 throw INTERP_KERNEL::Exception(msg);
221               PyObject *o1=PyTuple_GetItem(o,1);
222               if(PyString_Check(o1))
223                 p.second=std::string(PyString_AsString(o1));
224               else
225                 throw INTERP_KERNEL::Exception(msg);
226               ret[i]=p;
227             }
228           else
229             throw INTERP_KERNEL::Exception(msg);
230         }
231       return ret;
232     }
233   throw INTERP_KERNEL::Exception(msg);
234 }
235
236 std::vector< std::pair<std::vector<std::string>, std::string > > convertVecPairVecStFromPy(PyObject *pyLi)
237 {
238   std::vector< std::pair<std::vector<std::string>, std::string > > ret;
239   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 !";
240   if(PyList_Check(pyLi))
241     {
242       int size=PyList_Size(pyLi);
243       ret.resize(size);
244       for(int i=0;i<size;i++)
245         {
246           PyObject *o=PyList_GetItem(pyLi,i);
247           if(PyTuple_Check(o))
248             {
249               std::pair<std::vector<std::string>, std::string> p;
250               int size2=PyTuple_Size(o);
251               if(size2!=2)
252                 throw INTERP_KERNEL::Exception(msg);
253               PyObject *o0=PyTuple_GetItem(o,0);
254               if(PyList_Check(o0))
255                 {
256                   int size3=PyList_Size(o0);
257                   p.first.resize(size3);
258                   for(int j=0;j<size3;j++)
259                     {
260                       PyObject *o0j=PyList_GetItem(o0,j);
261                       if(PyString_Check(o0j))
262                         {
263                           p.first[j]=std::string(PyString_AsString(o0j));
264                         }
265                       else
266                         throw INTERP_KERNEL::Exception(msg);
267                     }
268                 }
269               else
270                 throw INTERP_KERNEL::Exception(msg);
271               PyObject *o1=PyTuple_GetItem(o,1);
272               if(PyString_Check(o1))
273                 p.second=std::string(PyString_AsString(o1));
274               else
275                 throw INTERP_KERNEL::Exception(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 /*!
287  * Called by MEDFileAnyTypeFieldMultiTS::__getitem__ when \a elt0 is neither a list nor a slice.
288  * In this case a MEDFileAnyTypeField1TS object is returned.
289  */
290 int MEDFileAnyTypeFieldMultiTSgetitemSingleTS__(const MEDFileAnyTypeFieldMultiTS *self, PyObject *elt0) throw(INTERP_KERNEL::Exception)
291 {
292   if(elt0 && PyInt_Check(elt0))
293     {//fmts[3]
294       return PyInt_AS_LONG(elt0);
295     }
296   else if(elt0 && PyTuple_Check(elt0))
297     {
298       if(PyTuple_Size(elt0)==2)
299         {
300           PyObject *o0=PyTuple_GetItem(elt0,0);
301           PyObject *o1=PyTuple_GetItem(elt0,1);
302           if(PyInt_Check(o0) && PyInt_Check(o1))
303             {//fmts(1,-1)
304               int iter=PyInt_AS_LONG(o0);
305               int order=PyInt_AS_LONG(o1);
306               return self->getPosOfTimeStep(iter,order);
307             }
308           else
309             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 !");
310         }
311       else
312         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 !");
313     }
314   else if(elt0 && PyFloat_Check(elt0))
315     {
316       double val=PyFloat_AS_DOUBLE(elt0);
317       return self->getPosGivenTime(val);
318     }
319   else
320     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] !");
321 }
322
323 /*!
324  * Called by MEDFileAnyTypeFieldMultiTS::__getitem__ when \a obj is neither a list nor a slice.
325  * In this case a MEDFileAnyTypeField1TS object is returned.
326  */
327 int MEDFileFieldsgetitemSingleTS__(const MEDFileFields *self, PyObject *obj) throw(INTERP_KERNEL::Exception)
328 {
329   if(PyInt_Check(obj))
330     {
331       return (int)PyInt_AS_LONG(obj);
332     }
333   else if(PyString_Check(obj))
334     {
335       return self->getPosFromFieldName(PyString_AsString(obj));
336     }
337   else
338     throw INTERP_KERNEL::Exception("MEDFileFields::__getitem__ : only integer or string with fieldname supported !");
339 }