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