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