Salome HOME
Merge from V6_main 01/04/2013
[tools/medcoupling.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(dynamic_cast<ParaMEDMEM::MEDFileUMesh *>(mesh))
27     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDFileUMesh,owner);
28   if(dynamic_cast<ParaMEDMEM::MEDFileCMesh *>(mesh))
29     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDFileCMesh,owner);
30   if(dynamic_cast<ParaMEDMEM::MEDFileCurveLinearMesh *>(mesh))
31     ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDFileCurveLinearMesh,owner);
32   if(!ret)
33     throw INTERP_KERNEL::Exception("Not recognized type of MEDFileMesh on downcast !");
34   return ret;
35 }
36
37 static PyObject* convertMEDFileParameter1TS(ParaMEDMEM::MEDFileParameter1TS* p1ts, int owner) throw(INTERP_KERNEL::Exception)
38 {
39   PyObject *ret=0;
40   if(dynamic_cast<MEDFileParameterDouble1TS *>(p1ts))
41     ret=SWIG_NewPointerObj((void*)p1ts,SWIGTYPE_p_ParaMEDMEM__MEDFileParameterDouble1TS,owner);
42   if(dynamic_cast<MEDFileParameterDouble1TSWTI *>(p1ts))
43     ret=SWIG_NewPointerObj((void*)p1ts,SWIGTYPE_p_ParaMEDMEM__MEDFileParameterDouble1TSWTI,owner);
44   if(!ret)
45     throw INTERP_KERNEL::Exception("Not recognized type of MEDFileParameter1TS on downcast !");
46   return ret;
47 }
48
49 static std::vector<std::pair<int,int> > convertTimePairIdsFromPy(PyObject *pyLi) throw(INTERP_KERNEL::Exception)
50 {
51   std::vector<std::pair<int,int> > ret;
52   if(PyList_Check(pyLi))
53     {
54       int size=PyList_Size(pyLi);
55       ret.resize(size);
56       for(int i=0;i<size;i++)
57         {
58           PyObject *o=PyList_GetItem(pyLi,i);
59           if(PyTuple_Check(o))
60             {
61               std::pair<int,int> p;
62               int size2=PyTuple_Size(o);
63               if(size2!=2)
64                 throw INTERP_KERNEL::Exception("tuples in list must be of size 2 (dt,it) !");
65               PyObject *o0=PyTuple_GetItem(o,0);
66               if(PyInt_Check(o0))
67                 p.first=(int)PyInt_AS_LONG(o0);
68               else
69                 throw INTERP_KERNEL::Exception("First elem of tuples in list must be integer : dt !");
70               PyObject *o1=PyTuple_GetItem(o,1);
71               if(PyInt_Check(o1))
72                 p.second=(int)PyInt_AS_LONG(o1);
73               else
74                 throw INTERP_KERNEL::Exception("Second elem of tuples in list must be integer : dt !");
75               ret[i]=p;
76             }
77           else
78             throw INTERP_KERNEL::Exception("list must contain tuples only");
79         }
80     }
81   else
82     throw INTERP_KERNEL::Exception("convertTimePairIdsFromPy : not a list");
83   return ret;
84 }
85
86 static void converPyListToVecString(PyObject *pyLi, std::vector<std::string>& v)
87 {
88   if(PyList_Check(pyLi))
89     {
90       int size=PyList_Size(pyLi);
91       v.resize(size);
92       for(int i=0;i<size;i++)
93         {
94           PyObject *o=PyList_GetItem(pyLi,i);
95           if(!PyString_Check(o))
96             throw INTERP_KERNEL::Exception("In list passed in argument some elements are NOT strings ! Expected a list containing only strings !");
97           const char *st=PyString_AsString(o);
98           v[i]=std::string(st);
99         }
100     }
101   else if(PyTuple_Check(pyLi))
102     {
103       int size=PyTuple_Size(pyLi);
104       v.resize(size);
105       for(int i=0;i<size;i++)
106         {
107           PyObject *o=PyTuple_GetItem(pyLi,i);
108           if(!PyString_Check(o))
109             throw INTERP_KERNEL::Exception("In tuple passed in argument some elements are NOT strings ! Expected a tuple containing only strings !");
110           const char *st=PyString_AsString(o);
111           v[i]=std::string(st);
112         }
113     }
114   else if(PyString_Check(pyLi))
115     {
116       v.resize(1);
117       v[0]=std::string((const char *)PyString_AsString(pyLi));
118     }
119   else
120     {
121       throw INTERP_KERNEL::Exception("Unrecognized python argument : expected a list of string or tuple of string or string !");
122     }
123 }
124
125 static PyObject *convertFieldDoubleVecToPy(const std::vector<ParaMEDMEM::MEDCouplingFieldDouble *>& li)
126 {
127   int sz=li.size();
128   PyObject *ret=PyList_New(sz);
129   for(int i=0;i<sz;i++)
130     {
131       PyObject *o=SWIG_NewPointerObj((void*)li[i],SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDouble,SWIG_POINTER_OWN | 0);
132       PyList_SetItem(ret,i,o);
133     }
134   return ret;
135 }
136
137 PyObject *convertVecPairVecStToPy(const std::vector< std::pair<std::vector<std::string>, std::string > >& vec)
138 {
139   int sz=(int)vec.size();
140   PyObject *ret=PyList_New(sz);
141   for(int i=0;i<sz;i++)
142     {
143       PyObject *t=PyTuple_New(2);
144       int sz2=(int)vec[i].first.size();
145       PyObject *ll=PyList_New(sz2);
146       for(int j=0;j<sz2;j++)
147         PyList_SetItem(ll,j,PyString_FromString(vec[i].first[j].c_str()));
148       PyTuple_SetItem(t,0,ll);
149       PyTuple_SetItem(t,1,PyString_FromString(vec[i].second.c_str()));
150       PyList_SetItem(ret,i,t);
151     }
152   return ret;
153 }
154
155 std::vector< std::pair<std::string, std::string > > convertVecPairStStFromPy(PyObject *pyLi)
156 {
157   std::vector< std::pair<std::string, std::string > > ret;
158   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 !";
159   if(PyList_Check(pyLi))
160     {
161       int size=PyList_Size(pyLi);
162       ret.resize(size);
163       for(int i=0;i<size;i++)
164         {
165           PyObject *o=PyList_GetItem(pyLi,i);
166           if(PyTuple_Check(o))
167             {
168               std::pair<std::string, std::string> p;
169               int size2=PyTuple_Size(o);
170               if(size2!=2)
171                 throw INTERP_KERNEL::Exception(msg);
172               PyObject *o0=PyTuple_GetItem(o,0);
173               if(PyString_Check(o0))
174                 p.second=std::string(PyString_AsString(o0));
175               else
176                 throw INTERP_KERNEL::Exception(msg);
177               PyObject *o1=PyTuple_GetItem(o,1);
178               if(PyString_Check(o1))
179                 p.second=std::string(PyString_AsString(o1));
180               else
181                 throw INTERP_KERNEL::Exception(msg);
182               ret[i]=p;
183             }
184           else
185             throw INTERP_KERNEL::Exception(msg);
186         }
187       return ret;
188     }
189   throw INTERP_KERNEL::Exception(msg);
190 }
191
192 std::vector< std::pair<std::vector<std::string>, std::string > > convertVecPairVecStFromPy(PyObject *pyLi)
193 {
194   std::vector< std::pair<std::vector<std::string>, std::string > > ret;
195   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 !";
196   if(PyList_Check(pyLi))
197     {
198       int size=PyList_Size(pyLi);
199       ret.resize(size);
200       for(int i=0;i<size;i++)
201         {
202           PyObject *o=PyList_GetItem(pyLi,i);
203           if(PyTuple_Check(o))
204             {
205               std::pair<std::vector<std::string>, std::string> p;
206               int size2=PyTuple_Size(o);
207               if(size2!=2)
208                 throw INTERP_KERNEL::Exception(msg);
209               PyObject *o0=PyTuple_GetItem(o,0);
210               if(PyList_Check(o0))
211                 {
212                   int size2=PyList_Size(o0);
213                   p.first.resize(size2);
214                   for(int j=0;j<size2;j++)
215                     {
216                       PyObject *o0j=PyList_GetItem(o0,j);
217                       if(PyString_Check(o0j))
218                         {
219                           p.first[j]=std::string(PyString_AsString(o0j));
220                         }
221                       else
222                         throw INTERP_KERNEL::Exception(msg);
223                     }
224                 }
225               else
226                 throw INTERP_KERNEL::Exception(msg);
227               PyObject *o1=PyTuple_GetItem(o,1);
228               if(PyString_Check(o1))
229                 p.second=std::string(PyString_AsString(o1));
230               else
231                 throw INTERP_KERNEL::Exception(msg);
232               ret[i]=p;
233             }
234           else
235             throw INTERP_KERNEL::Exception(msg);
236         }
237       return ret;
238     }
239   throw INTERP_KERNEL::Exception(msg);
240 }