1 // Copyright (C) 2007-2013 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "InterpKernelAutoPtr.hxx"
23 #include <numpy/arrayobject.h>
25 // specific DataArray deallocator callback. This deallocator is used both in the constructor of DataArray and in the toNumPyArr
26 // method. This dellocator uses weakref to determine if the linked numArr is still alive or not. If alive the ownership is given to it.
27 // if no more alive the "standart" DataArray deallocator is called.
28 void numarrdeal(void *pt, void *wron)
30 void **wronc=(void **)wron;
31 PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(wronc[0]);
32 PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
36 PyArrayObject *objC=reinterpret_cast<PyArrayObject *>(obj);
37 objC->flags|=NPY_OWNDATA;
38 Py_XDECREF(weakRefOnOwner);
43 typedef void (*MyDeallocator)(void *,void *);
44 MyDeallocator deall=(MyDeallocator)wronc[1];
46 Py_XDECREF(weakRefOnOwner);
51 template<class MCData>
52 struct PyCallBackDataArraySt {
57 typedef struct PyCallBackDataArraySt<ParaMEDMEM::DataArrayInt> PyCallBackDataArrayInt;
58 typedef struct PyCallBackDataArraySt<ParaMEDMEM::DataArrayDouble> PyCallBackDataArrayDouble;
62 static int callbackmcdataarray___init__(PyObject *self, PyObject *args, PyObject *kwargs) { return 0; }
64 static PyObject *callbackmcdataarrayint___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
66 PyCallBackDataArrayInt *self = (PyCallBackDataArrayInt *) ( type->tp_alloc(type, 0) );
67 return (PyObject *)self;
70 static PyObject *callbackmcdataarraydouble___new__(PyTypeObject *type, PyObject *args, PyObject *kwargs)
72 PyCallBackDataArrayDouble *self = (PyCallBackDataArrayDouble *) ( type->tp_alloc(type, 0) );
73 return (PyObject *)self;
76 static void callbackmcdataarray_dealloc(PyObject *self)
78 Py_TYPE(self)->tp_free(self);
81 // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
82 // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
83 static PyObject *callbackmcdataarrayint_call(PyCallBackDataArrayInt *self, PyObject *args, PyObject *kw)
87 ParaMEDMEM::MemArray<int>& mma=self->_pt_mc->accessToMemArray();
94 // real callback called when a numpy arr having more than one DataArray instance client on it is destroyed.
95 // In this case, all the "weak" clients, except the first one, invoke this call back that desable the content of these "weak" clients.
96 static PyObject *callbackmcdataarraydouble_call(PyCallBackDataArrayDouble *self, PyObject *args, PyObject *kw)
100 ParaMEDMEM::MemArray<double>& mma=self->_pt_mc->accessToMemArray();
108 PyTypeObject PyCallBackDataArrayInt_RefType = {
109 PyVarObject_HEAD_INIT(&PyType_Type, 0)
110 "callbackmcdataarrayint",
111 sizeof(PyCallBackDataArrayInt),
113 callbackmcdataarray_dealloc, /*tp_dealloc*/
120 0, /*tp_as_sequence*/
123 (ternaryfunc)callbackmcdataarrayint_call, /*tp_call*/
128 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
132 0, /*tp_richcompare*/
133 0, /*tp_weaklistoffset*/
144 callbackmcdataarray___init__, /*tp_init*/
145 PyType_GenericAlloc, /*tp_alloc*/
146 callbackmcdataarrayint___new__, /*tp_new*/
147 PyObject_GC_Del, /*tp_free*/
150 PyTypeObject PyCallBackDataArrayDouble_RefType = {
151 PyVarObject_HEAD_INIT(&PyType_Type, 0)
152 "callbackmcdataarraydouble",
153 sizeof(PyCallBackDataArrayDouble),
155 callbackmcdataarray_dealloc, /*tp_dealloc*/
162 0, /*tp_as_sequence*/
165 (ternaryfunc)callbackmcdataarraydouble_call, /*tp_call*/
170 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_BASETYPE, /*tp_flags*/
174 0, /*tp_richcompare*/
175 0, /*tp_weaklistoffset*/
186 callbackmcdataarray___init__, /*tp_init*/
187 PyType_GenericAlloc, /*tp_alloc*/
188 callbackmcdataarraydouble___new__, /*tp_new*/
189 PyObject_GC_Del, /*tp_free*/
192 // this is the second type of specific deallocator, only valid for the constructor of DataArrays taking numpy array
193 // in input when an another DataArray is already client of this.
194 template<class MCData>
195 void numarrdeal2(void *pt, void *obj)
197 typedef struct PyCallBackDataArraySt<MCData> PyCallBackDataArray;
198 void **obj1=(void **)obj;
199 PyCallBackDataArray *cbdaic=reinterpret_cast<PyCallBackDataArray *>(obj1[0]);
200 PyObject *weakRefOnOwner=reinterpret_cast<PyObject *>(obj1[1]);
202 Py_XDECREF(weakRefOnOwner);
207 template<class MCData, class T>
208 MCData *BuildNewInstance(PyObject *elt0, int npyObjectType, PyTypeObject *pytype, const char *msg)
210 int ndim=PyArray_NDIM(elt0);
211 if(ndim!=1 && ndim!=2)
212 throw INTERP_KERNEL::Exception("Input numpy array should have dimension equal to 1 or 2 !");
213 if(PyArray_DESCR(elt0)->type_num != npyObjectType)
215 std::ostringstream oss; oss << "Input numpy array has not the type " << msg << "!";
216 throw INTERP_KERNEL::Exception(oss.str().c_str());
218 npy_intp sz0=PyArray_DIM(elt0,0);
219 npy_intp sz1=ndim==2?PyArray_DIM(elt0,1):1;
221 int itemSize=PyArray_ITEMSIZE(elt0);
222 if(itemSize!=sizeof(T))
224 std::ostringstream oss; oss << "Input numpy array has not itemSize set to " << sizeof(T) << " !";
225 throw INTERP_KERNEL::Exception(oss.str().c_str());
227 if(itemSize*sz1!=PyArray_STRIDE(elt0,0))
228 throw INTERP_KERNEL::Exception("Input numpy array has stride that mismatches the item size ! Data are not packed in the right way for DataArrays !");
230 if(itemSize!=PyArray_STRIDE(elt0,1))
231 throw INTERP_KERNEL::Exception("Input numpy array has stride that mismatches the item size ! Data are not packed in the right way for DataArrays for component #1 !");
232 const char *data=PyArray_BYTES(elt0);
233 typename ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<MCData> ret=MCData::New();
234 if(PyArray_ISBEHAVED(elt0))//aligned and writeable and in machine byte-order
236 PyArrayObject *elt0C=reinterpret_cast<PyArrayObject *>(elt0);
237 PyArrayObject *eltOwning=(PyArray_FLAGS(elt0C) & NPY_OWNDATA)?elt0C:NULL;
238 int mask=NPY_OWNDATA; mask=~mask;
240 PyObject *deepestObj=elt0;
241 PyObject *base=elt0C->base;
242 if(base) deepestObj=base;
245 if(PyArray_Check(base))
247 PyArrayObject *baseC=reinterpret_cast<PyArrayObject *>(base);
248 eltOwning=(PyArray_FLAGS(baseC) & NPY_OWNDATA)?baseC:eltOwning;
251 if(base) deepestObj=base;
256 typename ParaMEDMEM::MemArray<T>& mma=ret->accessToMemArray();
259 PyCallBackDataArraySt<MCData> *cb=PyObject_GC_New(PyCallBackDataArraySt<MCData>,pytype);
261 ret->useArray(reinterpret_cast<const T *>(data),true,ParaMEDMEM::C_DEALLOC,sz0,sz1);
262 PyObject *ref=PyWeakref_NewRef(deepestObj,(PyObject *)cb);
263 void **objs=new void *[2]; objs[0]=cb; objs[1]=ref;
264 mma.setParameterForDeallocator(objs);
265 mma.setSpecificDeallocator(numarrdeal2<MCData>);
266 //"Impossible to share this numpy array chunk of data, because already shared by an another non numpy array object (maybe an another DataArrayInt instance) ! Release it, or perform a copy on the input array !");
270 ret->useArray(reinterpret_cast<const T *>(data),true,ParaMEDMEM::C_DEALLOC,sz0,sz1);
271 PyObject *ref=PyWeakref_NewRef(reinterpret_cast<PyObject *>(eltOwning),NULL);
272 void **objs=new void *[2]; objs[0]=ref; objs[1]=(void*) ParaMEDMEM::MemArray<T>::CDeallocator;
273 mma.setParameterForDeallocator(objs);
274 mma.setSpecificDeallocator(numarrdeal);
277 else if(PyArray_ISBEHAVED_RO(elt0))
278 ret->useArray(reinterpret_cast<const T *>(data),false,ParaMEDMEM::CPP_DEALLOC,sz0,sz1);
283 int NumpyArrSetBaseObjectExt(PyArrayObject *arr, PyObject *obj)
286 PyErr_SetString(PyExc_ValueError,
287 "Cannot set the NumPy array 'base' "
288 "dependency to NULL after initialization");
292 * Allow the base to be set only once. Once the object which
293 * owns the data is set, it doesn't make sense to change it.
295 if (PyArray_BASE(arr) != NULL) {
297 PyErr_SetString(PyExc_ValueError,
298 "Cannot set the NumPy array 'base' "
299 "dependency more than once");
304 * Don't allow infinite chains of views, always set the base
305 * to the first owner of the data.
306 * That is, either the first object which isn't an array,
307 * or the first object which owns its own data.
310 while (PyArray_Check(obj) && (PyObject *)arr != obj) {
311 PyArrayObject *obj_arr = (PyArrayObject *)obj;
315 /* If this array owns its own data, stop collapsing */
316 if (PyArray_CHKFLAGS(obj_arr, NPY_OWNDATA)) {
320 tmp = PyArray_BASE(obj_arr);
321 /* If there's no base, stop collapsing */
325 /* Stop the collapse new base when the would not be of the same
326 * type (i.e. different subclass).
328 if (Py_TYPE(tmp) != Py_TYPE(arr)) {
338 /* Disallow circular references */
339 if ((PyObject *)arr == obj) {
341 PyErr_SetString(PyExc_ValueError,
342 "Cannot create a circular NumPy array 'base' dependency");
351 template<class MCData, class T>
352 PyObject *ToNumPyArray(MCData *self, int npyObjectType, const char *MCDataStr)
354 if(!self->isAllocated())
356 std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : this is not allocated !";
357 throw INTERP_KERNEL::Exception(oss.str().c_str());
359 ParaMEDMEM::MemArray<T>& mem=self->accessToMemArray();
360 int nbComp=self->getNumberOfComponents();
363 std::ostringstream oss; oss << MCDataStr << "::toNumPyArray : number of components of this is 0 ! Should be > 0 !";
364 throw INTERP_KERNEL::Exception(oss.str().c_str());
366 int nbDims=nbComp==1?1:2;
368 dim[0]=(npy_intp)self->getNumberOfTuples(); dim[1]=nbComp;
369 const T *bg=self->getConstPointer();
370 PyObject *ret=PyArray_SimpleNewFromData(nbDims,dim,npyObjectType,const_cast<T *>(bg));
371 if(mem.isDeallocatorCalled())
373 if(mem.getDeallocator()!=numarrdeal)
374 {// case for the first call of toNumPyArray
375 PyObject *ref=PyWeakref_NewRef(ret,NULL);
376 void **objs=new void *[2]; objs[0]=ref; objs[1]=(void*) mem.getDeallocator();
377 mem.setParameterForDeallocator(objs);
378 mem.setSpecificDeallocator(numarrdeal);
382 {// case for the second and other call of toNumPyArray
383 void **objs=(void **)mem.getParameterForDeallocator();
384 PyObject *weakRefOnOwner=(PyObject *)objs[0];
385 PyObject *obj=PyWeakref_GetObject(weakRefOnOwner);
387 {//the previous numArray exists let numpy deals the numpy array each other by declaring the still alive instance as base
389 NumpyArrSetBaseObjectExt((PyArrayObject*)ret,obj);
392 {//the previous numArray no more exists -> declare the newly created numpy array as the first one.
393 Py_XDECREF(weakRefOnOwner);
394 PyObject *ref=PyWeakref_NewRef(ret,NULL);
402 SWIGINTERN PyObject *ParaMEDMEM_DataArrayInt_toNumPyArray(ParaMEDMEM::DataArrayInt *self);
403 SWIGINTERN PyObject *ParaMEDMEM_DataArrayDouble_toNumPyArray(ParaMEDMEM::DataArrayDouble *self);
405 PyObject *ToCSRMatrix(const std::vector<std::map<int,double> >& m, int nbCols) throw(INTERP_KERNEL::Exception)
407 int nbRows((int)m.size());
408 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayInt> indPtr(ParaMEDMEM::DataArrayInt::New()),indices(ParaMEDMEM::DataArrayInt::New());
409 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> data(ParaMEDMEM::DataArrayDouble::New());
410 indPtr->alloc(nbRows+1,1);
411 int *intPtr_ptr(indPtr->getPointer()); intPtr_ptr[0]=0; intPtr_ptr++;
413 for(std::vector<std::map<int,double> >::const_iterator it0=m.begin();it0!=m.end();it0++,intPtr_ptr++)
415 sz2+=(int)(*it0).size();
418 indices->alloc(sz2,1); data->alloc(sz2,1);
419 int *indices_ptr(indices->getPointer());
420 double *data_ptr(data->getPointer());
421 for(std::vector<std::map<int,double> >::const_iterator it0=m.begin();it0!=m.end();it0++)
422 for(std::map<int,double>::const_iterator it1=(*it0).begin();it1!=(*it0).end();it1++,indices_ptr++,data_ptr++)
424 *indices_ptr=(*it1).first;
425 *data_ptr=(*it1).second;
427 PyObject *a(ParaMEDMEM_DataArrayDouble_toNumPyArray(data)),*b(ParaMEDMEM_DataArrayInt_toNumPyArray(indices)),*c(ParaMEDMEM_DataArrayInt_toNumPyArray(indPtr));
429 PyObject *args(PyTuple_New(1)),*args0(PyTuple_New(3)),*kw(PyDict_New()),*kw1(PyTuple_New(2));
430 PyTuple_SetItem(args0,0,a); PyTuple_SetItem(args0,1,b); PyTuple_SetItem(args0,2,c); PyTuple_SetItem(args,0,args0);
431 PyTuple_SetItem(kw1,0,PyInt_FromLong(nbRows)); PyTuple_SetItem(kw1,1,PyInt_FromLong(nbCols));
432 PyObject *tmp1(PyString_FromString("shape"));
433 PyDict_SetItem(kw,tmp1,kw1); Py_DECREF(tmp1); Py_DECREF(kw1);
434 PyObject* pdict=PyDict_New();
435 PyDict_SetItemString(pdict, "__builtins__", PyEval_GetBuiltins());
436 PyObject *tmp(PyRun_String("from scipy.sparse import csr_matrix", Py_single_input, pdict, pdict));
437 PyObject *csrMatrixCls=PyDict_GetItemString(pdict,"csr_matrix");
438 PyObject *ret(PyObject_Call(csrMatrixCls,args,kw));
439 Py_DECREF(pdict); Py_XDECREF(tmp); Py_DECREF(args); Py_DECREF(kw);
445 static PyObject *convertMesh(ParaMEDMEM::MEDCouplingMesh *mesh, int owner) throw(INTERP_KERNEL::Exception)
453 if(dynamic_cast<ParaMEDMEM::MEDCouplingUMesh *>(mesh))
454 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDCouplingUMesh,owner);
455 if(dynamic_cast<ParaMEDMEM::MEDCoupling1SGTUMesh *>(mesh))
456 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDCoupling1SGTUMesh,owner);
457 if(dynamic_cast<ParaMEDMEM::MEDCoupling1DGTUMesh *>(mesh))
458 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDCoupling1DGTUMesh,owner);
459 if(dynamic_cast<ParaMEDMEM::MEDCouplingExtrudedMesh *>(mesh))
460 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDCouplingExtrudedMesh,owner);
461 if(dynamic_cast<ParaMEDMEM::MEDCouplingCMesh *>(mesh))
462 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDCouplingCMesh,owner);
463 if(dynamic_cast<ParaMEDMEM::MEDCouplingCurveLinearMesh *>(mesh))
464 ret=SWIG_NewPointerObj((void*)mesh,SWIGTYPE_p_ParaMEDMEM__MEDCouplingCurveLinearMesh,owner);
466 throw INTERP_KERNEL::Exception("Not recognized type of mesh on downcast !");
470 static PyObject *convertFieldDiscretization(ParaMEDMEM::MEDCouplingFieldDiscretization *fd, int owner) throw(INTERP_KERNEL::Exception)
478 if(dynamic_cast<ParaMEDMEM::MEDCouplingFieldDiscretizationP0 *>(fd))
479 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDiscretizationP0,owner);
480 if(dynamic_cast<ParaMEDMEM::MEDCouplingFieldDiscretizationP1 *>(fd))
481 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDiscretizationP1,owner);
482 if(dynamic_cast<ParaMEDMEM::MEDCouplingFieldDiscretizationGauss *>(fd))
483 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDiscretizationGauss,owner);
484 if(dynamic_cast<ParaMEDMEM::MEDCouplingFieldDiscretizationGaussNE *>(fd))
485 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDiscretizationGaussNE,owner);
486 if(dynamic_cast<ParaMEDMEM::MEDCouplingFieldDiscretizationKriging *>(fd))
487 ret=SWIG_NewPointerObj(reinterpret_cast<void*>(fd),SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDiscretizationKriging,owner);
489 throw INTERP_KERNEL::Exception("Not recognized type of field discretization on downcast !");
493 static PyObject *convertDataArrayChar(ParaMEDMEM::DataArrayChar *dac, int owner) throw(INTERP_KERNEL::Exception)
501 if(dynamic_cast<ParaMEDMEM::DataArrayByte *>(dac))
502 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayByte,owner);
503 if(dynamic_cast<ParaMEDMEM::DataArrayAsciiChar *>(dac))
504 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayAsciiChar,owner);
506 throw INTERP_KERNEL::Exception("Not recognized type of DataArrayChar on downcast !");
510 static PyObject *convertDataArray(ParaMEDMEM::DataArray *dac, int owner) throw(INTERP_KERNEL::Exception)
518 if(dynamic_cast<ParaMEDMEM::DataArrayDouble *>(dac))
519 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,owner);
520 if(dynamic_cast<ParaMEDMEM::DataArrayInt *>(dac))
521 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,owner);
522 if(dynamic_cast<ParaMEDMEM::DataArrayByte *>(dac))
523 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayByte,owner);
524 if(dynamic_cast<ParaMEDMEM::DataArrayAsciiChar *>(dac))
525 ret=SWIG_NewPointerObj((void*)dac,SWIGTYPE_p_ParaMEDMEM__DataArrayAsciiChar,owner);
527 throw INTERP_KERNEL::Exception("Not recognized type of DataArray on downcast !");
531 static PyObject* convertMultiFields(ParaMEDMEM::MEDCouplingMultiFields *mfs, int owner) throw(INTERP_KERNEL::Exception)
539 if(dynamic_cast<ParaMEDMEM::MEDCouplingFieldOverTime *>(mfs))
540 ret=SWIG_NewPointerObj((void*)mfs,SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldOverTime,owner);
542 ret=SWIG_NewPointerObj((void*)mfs,SWIGTYPE_p_ParaMEDMEM__MEDCouplingMultiFields,owner);
546 static PyObject *convertIntArrToPyList(const int *ptr, int size) throw(INTERP_KERNEL::Exception)
548 PyObject *ret=PyList_New(size);
549 for(int i=0;i<size;i++)
550 PyList_SetItem(ret,i,PyInt_FromLong(ptr[i]));
554 static PyObject *convertIntArrToPyList2(const std::vector<int>& v) throw(INTERP_KERNEL::Exception)
557 PyObject *ret=PyList_New(size);
558 for(int i=0;i<size;i++)
559 PyList_SetItem(ret,i,PyInt_FromLong(v[i]));
563 static PyObject *convertIntArrToPyList3(const std::set<int>& v) throw(INTERP_KERNEL::Exception)
566 PyObject *ret=PyList_New(size);
567 std::set<int>::const_iterator it=v.begin();
568 for(int i=0;i<size;i++,it++)
569 PyList_SetItem(ret,i,PyInt_FromLong(*it));
573 static PyObject *convertIntArrToPyListOfTuple(const int *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
575 PyObject *ret=PyList_New(nbOfTuples);
576 for(int i=0;i<nbOfTuples;i++)
578 PyObject *t=PyTuple_New(nbOfComp);
579 for(int j=0;j<nbOfComp;j++)
580 PyTuple_SetItem(t,j,PyInt_FromLong(vals[i*nbOfComp+j]));
581 PyList_SetItem(ret,i,t);
586 static int *convertPyToNewIntArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL::Exception)
588 if(PyList_Check(pyLi))
590 *size=PyList_Size(pyLi);
591 int *tmp=new int[*size];
592 for(int i=0;i<*size;i++)
594 PyObject *o=PyList_GetItem(pyLi,i);
597 int val=(int)PyInt_AS_LONG(o);
603 throw INTERP_KERNEL::Exception("list must contain integers only");
608 else if(PyTuple_Check(pyLi))
610 *size=PyTuple_Size(pyLi);
611 int *tmp=new int[*size];
612 for(int i=0;i<*size;i++)
614 PyObject *o=PyTuple_GetItem(pyLi,i);
617 int val=(int)PyInt_AS_LONG(o);
623 throw INTERP_KERNEL::Exception("tuple must contain integers only");
630 throw INTERP_KERNEL::Exception("convertPyToNewIntArr2 : not a list");
634 static void convertPyToVectorPairInt(PyObject *pyLi, std::vector< std::pair<int,int> >& arr) throw(INTERP_KERNEL::Exception)
636 const char msg[]="list must contain tuples of 2 integers only or tuple must contain tuples of 2 integers only !";
637 if(PyList_Check(pyLi))
639 int size=PyList_Size(pyLi);
641 for(int i=0;i<size;i++)
643 PyObject *o=PyList_GetItem(pyLi,i);
646 int sz2=PyTuple_Size(o);
648 throw INTERP_KERNEL::Exception(msg);
649 PyObject *o_0=PyTuple_GetItem(o,0);
650 if(!PyInt_Check(o_0))
651 throw INTERP_KERNEL::Exception(msg);
652 PyObject *o_1=PyTuple_GetItem(o,1);
653 if(!PyInt_Check(o_1))
654 throw INTERP_KERNEL::Exception(msg);
655 arr[i].first=(int)PyInt_AS_LONG(o_0);
656 arr[i].second=(int)PyInt_AS_LONG(o_1);
659 throw INTERP_KERNEL::Exception(msg);
662 else if(PyTuple_Check(pyLi))
664 int size=PyTuple_Size(pyLi);
666 for(int i=0;i<size;i++)
668 PyObject *o=PyTuple_GetItem(pyLi,i);
671 int sz2=PyTuple_Size(o);
673 throw INTERP_KERNEL::Exception(msg);
674 PyObject *o_0=PyTuple_GetItem(o,0);
675 if(!PyInt_Check(o_0))
676 throw INTERP_KERNEL::Exception(msg);
677 PyObject *o_1=PyTuple_GetItem(o,1);
678 if(!PyInt_Check(o_1))
679 throw INTERP_KERNEL::Exception(msg);
680 arr[i].first=(int)PyInt_AS_LONG(o_0);
681 arr[i].second=(int)PyInt_AS_LONG(o_1);
684 throw INTERP_KERNEL::Exception(msg);
688 throw INTERP_KERNEL::Exception(msg);
691 static void convertPyToNewIntArr3(PyObject *pyLi, std::vector<int>& arr) throw(INTERP_KERNEL::Exception)
693 if(PyList_Check(pyLi))
695 int size=PyList_Size(pyLi);
697 for(int i=0;i<size;i++)
699 PyObject *o=PyList_GetItem(pyLi,i);
702 int val=(int)PyInt_AS_LONG(o);
706 throw INTERP_KERNEL::Exception("list must contain integers only");
709 else if(PyTuple_Check(pyLi))
711 int size=PyTuple_Size(pyLi);
713 for(int i=0;i<size;i++)
715 PyObject *o=PyTuple_GetItem(pyLi,i);
718 int val=(int)PyInt_AS_LONG(o);
722 throw INTERP_KERNEL::Exception("tuple must contain integers only");
727 throw INTERP_KERNEL::Exception("convertPyToNewIntArr3 : not a list nor a tuple");
731 static void convertPyToNewIntArr4(PyObject *pyLi, int recurseLev, int nbOfSubPart, std::vector<int>& arr) throw(INTERP_KERNEL::Exception)
734 throw INTERP_KERNEL::Exception("convertPyToNewIntArr4 : invalid list of integers level of recursion !");
736 if(PyList_Check(pyLi))
738 int size=PyList_Size(pyLi);
739 for(int i=0;i<size;i++)
741 PyObject *o=PyList_GetItem(pyLi,i);
744 int val=(int)PyInt_AS_LONG(o);
749 std::vector<int> arr2;
750 convertPyToNewIntArr4(o,recurseLev-1,nbOfSubPart,arr2);
751 if(nbOfSubPart>=1 && nbOfSubPart!=(int)arr2.size())
753 std::ostringstream oss; oss << "convertPyToNewIntArr4 : input list at lev " << recurseLev << " invalid nb of subpart elts expected " << nbOfSubPart << " having " << arr2.size() << " !";
754 throw INTERP_KERNEL::Exception(oss.str().c_str());
756 arr.insert(arr.end(),arr2.begin(),arr2.end());
760 else if(PyTuple_Check(pyLi))
762 int size=PyTuple_Size(pyLi);
763 for(int i=0;i<size;i++)
765 PyObject *o=PyTuple_GetItem(pyLi,i);
768 int val=(int)PyInt_AS_LONG(o);
773 std::vector<int> arr2;
774 convertPyToNewIntArr4(o,recurseLev-1,nbOfSubPart,arr2);
775 if(nbOfSubPart>=1 && nbOfSubPart!=(int)arr2.size())
777 std::ostringstream oss; oss << "convertPyToNewIntArr4 : input list at lev " << recurseLev << " invalid nb of subpart elts expected " << nbOfSubPart << " having " << arr2.size() << " !";
778 throw INTERP_KERNEL::Exception(oss.str().c_str());
780 arr.insert(arr.end(),arr2.begin(),arr2.end());
785 throw INTERP_KERNEL::Exception("convertPyToNewIntArr4 : not a list nor a tuple recursively !");
788 static void checkFillArrayWithPyList(int size1, int size2, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
792 if(nbOfComp==-1) { nbOfTuples=size1; nbOfComp=size2; }
793 else { if(nbOfComp==size2) { nbOfTuples=size1; } else
795 std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
796 oss << " whereas nb of components expected is " << nbOfComp << " !";
797 throw INTERP_KERNEL::Exception(oss.str().c_str());
804 if((nbOfTuples!=size1 || nbOfComp!=size2))
806 if(size2!=1 || size1!=nbOfComp*nbOfTuples)
808 std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
809 oss << " whereas nb of tuples expected is " << nbOfTuples << " and number of components expected is " << nbOfComp << " !";
810 throw INTERP_KERNEL::Exception(oss.str().c_str());
816 if(nbOfTuples==size1)
820 std::ostringstream oss; oss << "fillArrayWithPyListDbl2 : mismatch between nb of elemts : Input has " << size1 << " tuples and " << size2 << " components";
821 oss << " whereas nb of tuples expected is " << nbOfTuples << " !";
822 throw INTERP_KERNEL::Exception(oss.str().c_str());
828 static void fillArrayWithPyListInt3(PyObject *pyLi, int& nbOfElt, std::vector<int>& ret)
830 static const char MSG[]="fillArrayWithPyListInt3 : It appears that the input list or tuple is composed by elts having different sizes !";
831 if(PyInt_Check(pyLi))
833 long val=PyInt_AS_LONG(pyLi);
838 throw INTERP_KERNEL::Exception(MSG);
841 else if(PyList_Check(pyLi))
843 int size=PyList_Size(pyLi);
845 for(int i=0;i<size;i++)
847 PyObject *o=PyList_GetItem(pyLi,i);
849 fillArrayWithPyListInt3(o,tmp1,ret);
857 throw INTERP_KERNEL::Exception(MSG);
860 else if(PyTuple_Check(pyLi))
862 int size=PyTuple_Size(pyLi);
864 for(int i=0;i<size;i++)
866 PyObject *o=PyTuple_GetItem(pyLi,i);
868 fillArrayWithPyListInt3(o,tmp1,ret);
876 throw INTERP_KERNEL::Exception(MSG);
880 throw INTERP_KERNEL::Exception("fillArrayWithPyListInt3 : Unrecognized type ! Should be a composition of tuple,list,int !");
883 static std::vector<int> fillArrayWithPyListInt2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
885 std::vector<int> ret;
886 int size1=-1,size2=-1;
887 if(PyList_Check(pyLi))
889 size1=PyList_Size(pyLi);
890 for(int i=0;i<size1;i++)
892 PyObject *o=PyList_GetItem(pyLi,i);
893 fillArrayWithPyListInt3(o,size2,ret);
898 else if(PyTuple_Check(pyLi))
900 size1=PyTuple_Size(pyLi);
901 for(int i=0;i<size1;i++)
903 PyObject *o=PyTuple_GetItem(pyLi,i);
904 fillArrayWithPyListInt3(o,size2,ret);
910 throw INTERP_KERNEL::Exception("fillArrayWithPyListInt2 : Unrecognized type ! Should be a tuple or a list !");
912 checkFillArrayWithPyList(size1,size2,nbOfTuples,nbOfComp);
916 static bool fillStringVector(PyObject *pyLi, std::vector<std::string>& vec) throw(INTERP_KERNEL::Exception)
918 if(PyList_Check(pyLi))
920 Py_ssize_t sz=PyList_Size(pyLi);
922 for(int i=0;i<sz;i++)
924 PyObject *o=PyList_GetItem(pyLi,i);
925 if(PyString_Check(o))
926 vec[i]=PyString_AsString(o);
932 else if(PyTuple_Check(pyLi))
934 Py_ssize_t sz=PyTuple_Size(pyLi);
936 for(int i=0;i<sz;i++)
938 PyObject *o=PyTuple_GetItem(pyLi,i);
939 if(PyString_Check(o))
940 vec[i]=PyString_AsString(o);
950 static PyObject *convertDblArrToPyList(const double *ptr, int size) throw(INTERP_KERNEL::Exception)
952 PyObject *ret=PyList_New(size);
953 for(int i=0;i<size;i++)
954 PyList_SetItem(ret,i,PyFloat_FromDouble(ptr[i]));
958 static PyObject *convertDblArrToPyList2(const std::vector<double>& v) throw(INTERP_KERNEL::Exception)
961 PyObject *ret=PyList_New(size);
962 for(int i=0;i<size;i++)
963 PyList_SetItem(ret,i,PyFloat_FromDouble(v[i]));
967 static PyObject *convertDblArrToPyListOfTuple(const double *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
969 PyObject *ret=PyList_New(nbOfTuples);
970 for(int i=0;i<nbOfTuples;i++)
972 PyObject *t=PyTuple_New(nbOfComp);
973 for(int j=0;j<nbOfComp;j++)
974 PyTuple_SetItem(t,j,PyFloat_FromDouble(vals[i*nbOfComp+j]));
975 PyList_SetItem(ret,i,t);
980 static PyObject *convertCharArrToPyListOfTuple(const char *vals, int nbOfComp, int nbOfTuples) throw(INTERP_KERNEL::Exception)
982 PyObject *ret=PyList_New(nbOfTuples);
983 INTERP_KERNEL::AutoPtr<char> tmp=new char[nbOfComp+1]; tmp[nbOfComp]='\0';
984 for(int i=0;i<nbOfTuples;i++)
986 std::copy(vals+i*nbOfComp,vals+(i+1)*nbOfComp,(char *)tmp);
987 PyList_SetItem(ret,i,PyString_FromString(tmp));
992 static double *convertPyToNewDblArr2(PyObject *pyLi, int *size) throw(INTERP_KERNEL::Exception)
994 if(PyList_Check(pyLi))
996 *size=PyList_Size(pyLi);
997 double *tmp=(double *)malloc((*size)*sizeof(double));
998 for(int i=0;i<*size;i++)
1000 PyObject *o=PyList_GetItem(pyLi,i);
1001 if(PyFloat_Check(o))
1003 double val=PyFloat_AS_DOUBLE(o);
1006 else if(PyInt_Check(o))
1008 long val0=PyInt_AS_LONG(o);
1015 throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : list must contain floats/integers only");
1020 else if(PyTuple_Check(pyLi))
1022 *size=PyTuple_Size(pyLi);
1023 double *tmp=(double *)malloc((*size)*sizeof(double));
1024 for(int i=0;i<*size;i++)
1026 PyObject *o=PyTuple_GetItem(pyLi,i);
1027 if(PyFloat_Check(o))
1029 double val=PyFloat_AS_DOUBLE(o);
1032 else if(PyInt_Check(o))
1034 long val0=PyInt_AS_LONG(o);
1041 throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : tuple must contain floats/integers only");
1047 throw INTERP_KERNEL::Exception("convertPyToNewDblArr2 : not a list");
1050 static void fillArrayWithPyListDbl3(PyObject *pyLi, int& nbOfElt, std::vector<double>& ret)
1052 static const char MSG[]="fillArrayWithPyListDbl3 : It appears that the input list or tuple is composed by elts having different sizes !";
1053 if(PyFloat_Check(pyLi))
1059 throw INTERP_KERNEL::Exception(MSG);
1060 double val=PyFloat_AS_DOUBLE(pyLi);
1063 else if(PyInt_Check(pyLi))
1065 long val0=PyInt_AS_LONG(pyLi);
1071 throw INTERP_KERNEL::Exception(MSG);
1074 else if(PyList_Check(pyLi))
1076 int size=PyList_Size(pyLi);
1078 for(int i=0;i<size;i++)
1080 PyObject *o=PyList_GetItem(pyLi,i);
1082 fillArrayWithPyListDbl3(o,tmp1,ret);
1090 throw INTERP_KERNEL::Exception(MSG);
1093 else if(PyTuple_Check(pyLi))
1095 int size=PyTuple_Size(pyLi);
1097 for(int i=0;i<size;i++)
1099 PyObject *o=PyTuple_GetItem(pyLi,i);
1101 fillArrayWithPyListDbl3(o,tmp1,ret);
1109 throw INTERP_KERNEL::Exception(MSG);
1113 throw INTERP_KERNEL::Exception("fillArrayWithPyListDbl3 : Unrecognized type ! Should be a composition of tuple,list,int and float !");
1116 static std::vector<double> fillArrayWithPyListDbl2(PyObject *pyLi, int& nbOfTuples, int& nbOfComp) throw(INTERP_KERNEL::Exception)
1118 std::vector<double> ret;
1119 int size1=-1,size2=-1;
1120 if(PyList_Check(pyLi))
1122 size1=PyList_Size(pyLi);
1123 for(int i=0;i<size1;i++)
1125 PyObject *o=PyList_GetItem(pyLi,i);
1126 fillArrayWithPyListDbl3(o,size2,ret);
1131 else if(PyTuple_Check(pyLi))
1133 size1=PyTuple_Size(pyLi);
1134 for(int i=0;i<size1;i++)
1136 PyObject *o=PyTuple_GetItem(pyLi,i);
1137 fillArrayWithPyListDbl3(o,size2,ret);
1143 throw INTERP_KERNEL::Exception("fillArrayWithPyListDbl2 : Unrecognized type ! Should be a tuple or a list !");
1145 checkFillArrayWithPyList(size1,size2,nbOfTuples,nbOfComp);
1149 //convertFromPyObjVectorOfObj<const ParaMEDMEM::MEDCouplingUMesh *>(pyLi,SWIGTYPE_p_ParaMEDMEM__MEDCouplingUMesh,"MEDCouplingUMesh")
1151 static void convertFromPyObjVectorOfObj(PyObject *pyLi, swig_type_info *ty, const char *typeStr, typename std::vector<T>& ret)
1154 if(PyList_Check(pyLi))
1156 int size=PyList_Size(pyLi);
1158 for(int i=0;i<size;i++)
1160 PyObject *obj=PyList_GetItem(pyLi,i);
1161 int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
1162 if(!SWIG_IsOK(status))
1164 std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : list is excepted to contain only " << typeStr << " instances !";
1165 throw INTERP_KERNEL::Exception(oss.str().c_str());
1167 T arg=reinterpret_cast< T >(argp);
1171 else if(PyTuple_Check(pyLi))
1173 int size=PyTuple_Size(pyLi);
1175 for(int i=0;i<size;i++)
1177 PyObject *obj=PyTuple_GetItem(pyLi,i);
1178 int status=SWIG_ConvertPtr(obj,&argp,ty,0|0);
1179 if(!SWIG_IsOK(status))
1181 std::ostringstream oss; oss << "convertFromPyObjVectorOfObj : tuple is excepted to contain only " << typeStr << " instances !";
1182 throw INTERP_KERNEL::Exception(oss.str().c_str());
1184 T arg=reinterpret_cast< T >(argp);
1188 else if(SWIG_IsOK(SWIG_ConvertPtr(pyLi,&argp,ty,0|0)))
1191 T arg=reinterpret_cast< T >(argp);
1195 throw INTERP_KERNEL::Exception("convertFromPyObjVectorOfObj : not a list nor a tuple");
1199 * if python int -> cpp int sw=1
1200 * if python list[int] -> cpp vector<int> sw=2
1201 * if python tuple[int] -> cpp vector<int> sw=2
1202 * if python DataArrayInt -> cpp DataArrayInt sw=3
1203 * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4
1205 * switch between (int,vector<int>,DataArrayInt)
1207 static void convertObjToPossibleCpp1(PyObject *value, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, ParaMEDMEM::DataArrayInt *& daIntTyypp, ParaMEDMEM::DataArrayIntTuple *&daIntTuple) throw(INTERP_KERNEL::Exception)
1210 if(PyInt_Check(value))
1212 iTyypp=(int)PyInt_AS_LONG(value);
1216 if(PyTuple_Check(value))
1218 int size=PyTuple_Size(value);
1219 stdvecTyypp.resize(size);
1220 for(int i=0;i<size;i++)
1222 PyObject *o=PyTuple_GetItem(value,i);
1224 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1227 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1228 throw INTERP_KERNEL::Exception(oss.str().c_str());
1234 if(PyList_Check(value))
1236 int size=PyList_Size(value);
1237 stdvecTyypp.resize(size);
1238 for(int i=0;i<size;i++)
1240 PyObject *o=PyList_GetItem(value,i);
1242 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1245 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1246 throw INTERP_KERNEL::Exception(oss.str().c_str());
1253 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
1254 if(SWIG_IsOK(status))
1256 daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayInt * >(argp);
1260 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
1261 if(SWIG_IsOK(status))
1263 daIntTuple=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
1267 throw INTERP_KERNEL::Exception("5 types accepted : integer, tuple of integer, list of integer, DataArrayInt, DataArrayIntTuple");
1271 * if python double -> cpp double sw=1
1272 * if python int -> cpp double sw=1
1273 * if python list[double] -> cpp vector<double> sw=2
1274 * if python list[int] -> cpp vector<double> sw=2
1275 * if python tuple[double] -> cpp vector<double> sw=2
1276 * if python tuple[int] -> cpp vector<double> sw=2
1277 * if python DataArrayDouble -> cpp DataArrayDouble sw=3
1279 * switch between (int,vector<int>,DataArrayInt)
1281 static void convertObjToPossibleCpp4(PyObject *value, int& sw, double& iTyypp, std::vector<double>& stdvecTyypp, ParaMEDMEM::DataArrayDouble *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1284 if(PyFloat_Check(value))
1286 iTyypp=PyFloat_AS_DOUBLE(value);
1290 if(PyInt_Check(value))
1292 iTyypp=(double)PyInt_AS_LONG(value);
1296 if(PyTuple_Check(value))
1298 int size=PyTuple_Size(value);
1299 stdvecTyypp.resize(size);
1300 for(int i=0;i<size;i++)
1302 PyObject *o=PyTuple_GetItem(value,i);
1303 if(PyFloat_Check(o))
1304 stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1305 else if(PyInt_Check(o))
1306 stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1309 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1310 throw INTERP_KERNEL::Exception(oss.str().c_str());
1316 if(PyList_Check(value))
1318 int size=PyList_Size(value);
1319 stdvecTyypp.resize(size);
1320 for(int i=0;i<size;i++)
1322 PyObject *o=PyList_GetItem(value,i);
1323 if(PyFloat_Check(o))
1324 stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1325 else if(PyInt_Check(o))
1326 stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1329 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1330 throw INTERP_KERNEL::Exception(oss.str().c_str());
1337 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
1338 if(!SWIG_IsOK(status))
1339 throw INTERP_KERNEL::Exception("5 types accepted : double float, integer, tuple of double float or int, list of double float or int, DataArrayDouble");
1340 daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
1345 * if python double -> cpp double sw=1
1346 * if python int -> cpp double sw=1
1347 * if python list[double] -> cpp vector<double> sw=2
1348 * if python list[int] -> cpp vector<double> sw=2
1349 * if python tuple[double] -> cpp vector<double> sw=2
1350 * if python tuple[int] -> cpp vector<double> sw=2
1351 * if python DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1353 * switch between (int,vector<int>,DataArrayInt)
1355 static void convertObjToPossibleCpp44(PyObject *value, int& sw, double& iTyypp, std::vector<double>& stdvecTyypp, ParaMEDMEM::DataArrayDoubleTuple *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1358 if(PyFloat_Check(value))
1360 iTyypp=PyFloat_AS_DOUBLE(value);
1364 if(PyInt_Check(value))
1366 iTyypp=(double)PyInt_AS_LONG(value);
1370 if(PyTuple_Check(value))
1372 int size=PyTuple_Size(value);
1373 stdvecTyypp.resize(size);
1374 for(int i=0;i<size;i++)
1376 PyObject *o=PyTuple_GetItem(value,i);
1377 if(PyFloat_Check(o))
1378 stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1379 else if(PyInt_Check(o))
1380 stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1383 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1384 throw INTERP_KERNEL::Exception(oss.str().c_str());
1390 if(PyList_Check(value))
1392 int size=PyList_Size(value);
1393 stdvecTyypp.resize(size);
1394 for(int i=0;i<size;i++)
1396 PyObject *o=PyList_GetItem(value,i);
1397 if(PyFloat_Check(o))
1398 stdvecTyypp[i]=PyFloat_AS_DOUBLE(o);
1399 else if(PyInt_Check(o))
1400 stdvecTyypp[i]=(double)PyInt_AS_LONG(o);
1403 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1404 throw INTERP_KERNEL::Exception(oss.str().c_str());
1411 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
1412 if(!SWIG_IsOK(status))
1413 throw INTERP_KERNEL::Exception("5 types accepted : double float, integer, tuple of double float or int, list of double float or int, DataArrayDoubleTuple");
1414 daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
1419 * if python int -> cpp int sw=1
1420 * if python list[int] -> cpp vector<int> sw=2
1421 * if python tuple[int] -> cpp vector<int> sw=2
1422 * if python slicp -> cpp pair sw=3 (begin,end,step)
1423 * if python DataArrayInt -> cpp DataArrayInt sw=4 . The returned pointer cannot be the null pointer ! If null an exception is thrown.
1425 * switch between (int,vector<int>,DataArrayInt)
1427 static void convertObjToPossibleCpp2(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, std::pair<int, std::pair<int,int> >& p, ParaMEDMEM::DataArrayInt *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1429 const char *msg="5 types accepted : integer, tuple of integer, list of integer, slice, DataArrayInt, DataArrayIntTuple";
1431 if(PyInt_Check(value))
1433 iTyypp=(int)PyInt_AS_LONG(value);
1437 if(PyTuple_Check(value))
1439 int size=PyTuple_Size(value);
1440 stdvecTyypp.resize(size);
1441 for(int i=0;i<size;i++)
1443 PyObject *o=PyTuple_GetItem(value,i);
1445 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1448 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1449 throw INTERP_KERNEL::Exception(oss.str().c_str());
1455 if(PyList_Check(value))
1457 int size=PyList_Size(value);
1458 stdvecTyypp.resize(size);
1459 for(int i=0;i<size;i++)
1461 PyObject *o=PyList_GetItem(value,i);
1463 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1466 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1467 throw INTERP_KERNEL::Exception(oss.str().c_str());
1473 if(PySlice_Check(value))
1475 Py_ssize_t strt=2,stp=2,step=2;
1476 PySliceObject *oC=reinterpret_cast<PySliceObject *>(value);
1477 if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0)
1478 if(nbelem!=0 || strt!=0 || stp!=0)
1480 std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem;
1481 throw INTERP_KERNEL::Exception(oss.str().c_str());
1485 p.second.second=step;
1490 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
1491 if(SWIG_IsOK(status))
1493 daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayInt * >(argp);
1496 std::ostringstream oss; oss << msg << " Instance in null !";
1497 throw INTERP_KERNEL::Exception(oss.str().c_str());
1502 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
1503 if(SWIG_IsOK(status))
1505 ParaMEDMEM::DataArrayIntTuple *tmp=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
1508 std::ostringstream oss; oss << msg << " Instance in null !";
1509 throw INTERP_KERNEL::Exception(oss.str().c_str());
1511 stdvecTyypp.resize(tmp->getNumberOfCompo());
1512 std::copy(tmp->getConstPointer(),tmp->getConstPointer()+tmp->getNumberOfCompo(),stdvecTyypp.begin());
1516 throw INTERP_KERNEL::Exception(msg);
1520 * if python int -> cpp int sw=1
1521 * if python tuple[int] -> cpp vector<int> sw=2
1522 * if python list[int] -> cpp vector<int> sw=2
1523 * if python slice -> cpp pair sw=3
1524 * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4 . WARNING The returned pointer can be the null pointer !
1526 static void convertObjToPossibleCpp22(PyObject *value, int nbelem, int& sw, int& iTyypp, std::vector<int>& stdvecTyypp, std::pair<int, std::pair<int,int> >& p, ParaMEDMEM::DataArrayIntTuple *& daIntTyypp) throw(INTERP_KERNEL::Exception)
1529 if(PyInt_Check(value))
1531 iTyypp=(int)PyInt_AS_LONG(value);
1535 if(PyTuple_Check(value))
1537 int size=PyTuple_Size(value);
1538 stdvecTyypp.resize(size);
1539 for(int i=0;i<size;i++)
1541 PyObject *o=PyTuple_GetItem(value,i);
1543 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1546 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
1547 throw INTERP_KERNEL::Exception(oss.str().c_str());
1553 if(PyList_Check(value))
1555 int size=PyList_Size(value);
1556 stdvecTyypp.resize(size);
1557 for(int i=0;i<size;i++)
1559 PyObject *o=PyList_GetItem(value,i);
1561 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
1564 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
1565 throw INTERP_KERNEL::Exception(oss.str().c_str());
1571 if(PySlice_Check(value))
1573 Py_ssize_t strt=2,stp=2,step=2;
1574 PySliceObject *oC=reinterpret_cast<PySliceObject *>(value);
1575 if(PySlice_GetIndices(oC,nbelem,&strt,&stp,&step)!=0)
1576 if(nbelem!=0 || strt!=0 || stp!=0)
1578 std::ostringstream oss; oss << "Slice in subscriptable object DataArray invalid : number of elements is : " << nbelem;
1579 throw INTERP_KERNEL::Exception(oss.str().c_str());
1583 p.second.second=step;
1588 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
1589 if(!SWIG_IsOK(status))
1590 throw INTERP_KERNEL::Exception("4 types accepted : integer, tuple of integer, list of integer, slice, DataArrayIntTuple");
1591 daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
1596 * if python string with size one -> cpp char sw=1
1597 * if python string with size different from one -> cpp string sw=2
1598 * if python tuple[string] or list[string] -> vector<string> sw=3
1599 * if python not null pointer of DataArrayChar -> cpp DataArrayChar sw=4
1600 * switch between (int,string,vector<string>,DataArrayChar)
1602 static void convertObjToPossibleCpp6(PyObject *value, int& sw, char& cTyp, std::string& sType, std::vector<std::string>& vsType, ParaMEDMEM::DataArrayChar *& dacType) throw(INTERP_KERNEL::Exception)
1604 const char *msg="4 types accepted : string, list or tuple of strings having same size, not null DataArrayChar instance.";
1606 if(PyString_Check(value))
1608 const char *pt=PyString_AsString(value);
1609 Py_ssize_t sz=PyString_Size(value);
1623 if(PyTuple_Check(value))
1625 int size=PyTuple_Size(value);
1626 vsType.resize(size);
1627 for(int i=0;i<size;i++)
1629 PyObject *o=PyTuple_GetItem(value,i);
1630 if(PyString_Check(o))
1631 vsType[i]=PyString_AsString(o);
1634 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not a string ! only tuples of strings accepted !";
1635 throw INTERP_KERNEL::Exception(oss.str().c_str());
1641 if(PyList_Check(value))
1643 int size=PyList_Size(value);
1644 vsType.resize(size);
1645 for(int i=0;i<size;i++)
1647 PyObject *o=PyList_GetItem(value,i);
1648 if(PyString_Check(o))
1649 vsType[i]=PyString_AsString(o);
1652 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not string ! only lists of strings accepted !";
1653 throw INTERP_KERNEL::Exception(oss.str().c_str());
1660 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayChar,0|0);
1661 if(SWIG_IsOK(status))
1663 dacType=reinterpret_cast< ParaMEDMEM::DataArrayChar * >(argp);
1666 std::ostringstream oss; oss << msg << " Instance in null !";
1667 throw INTERP_KERNEL::Exception(oss.str().c_str());
1672 throw INTERP_KERNEL::Exception(msg);
1676 * if value int -> cpp it sw=1
1677 * if value list[int] -> vt sw=2
1678 * if value tuple[int] -> vt sw=2
1679 * if value slice -> pt sw=3
1680 * if value DataArrayInt -> dt sw=4
1681 * if value tuple [int,int] -> cpp it,ip sw=5
1682 * if value tuple [list[int],int] -> cpp vt,ip sw=6
1683 * if value tuple [tuple[int],int] -> cpp vt,ip sw=6
1684 * if value tuple [slice,int] -> cpp pt,ip sw=7
1685 * if value tuple [DaI,int] -> cpp dt,ip sw=8
1686 * if value tuple [int,list[int]] -> cpp it,vc sw=9
1687 * if value tuple [list[int],list[int]] -> cpp vt,vc sw=10
1688 * if value tuple [tuple[int],list[int]] -> cpp vt,vc sw=10
1689 * if value tuple [slice,list[int]] -> cpp pt,vc sw=11
1690 * if value tuple [DaI,list[int]] -> cpp dt,vc sw=12
1691 * if value tuple [int,tuple[int]] -> cpp it,vc sw=9
1692 * if value tuple [list[int],tuple[int]] -> cpp vt,vc sw=10
1693 * if value tuple [tuple[int],tuple[int]] -> cpp vt,vc sw=10
1694 * if value tuple [slice,tuple[int]] -> cpp pt,vc sw=11
1695 * if value tuple [DaI,tuple[int]] -> cpp dt,vc sw=12
1696 * if value tuple [int,slice] -> cpp it,pc sw=13
1697 * if value tuple [list[int],slice] -> cpp vt,pc sw=14
1698 * if value tuple [tuple[int],slice] -> cpp vt,pc sw=14
1699 * if value tuple [slice,slice] -> cpp pt,pc sw=15
1700 * if value tuple [DaI,slice] -> cpp dt,pc sw=16
1702 * switch between (int,vector<int>,DataArrayInt)
1704 static void convertObjToPossibleCpp3(PyObject *value, int nbTuple, int nbCompo, int& sw, int& it, int& ic, std::vector<int>& vt, std::vector<int>& vc,
1705 std::pair<int, std::pair<int,int> >& pt, std::pair<int, std::pair<int,int> >& pc,
1706 ParaMEDMEM::DataArrayInt *&dt, ParaMEDMEM::DataArrayInt *&dc) throw(INTERP_KERNEL::Exception)
1708 if(!PyTuple_Check(value))
1710 convertObjToPossibleCpp2(value,nbTuple,sw,it,vt,pt,dt);
1715 int sz=PyTuple_Size(value);
1717 throw INTERP_KERNEL::Exception("Unexpected nb of slice element : 1 or 2 expected !\n1st is for tuple selection, 2nd for component selection !");
1718 PyObject *ob0=PyTuple_GetItem(value,0);
1720 convertObjToPossibleCpp2(ob0,nbTuple,sw1,it,vt,pt,dt);
1721 PyObject *ob1=PyTuple_GetItem(value,1);
1722 convertObjToPossibleCpp2(ob1,nbCompo,sw2,ic,vc,pc,dc);
1728 * if value int -> cpp val sw=1
1729 * if value double -> cpp val sw=1
1730 * if value DataArrayDouble -> cpp DataArrayDouble sw=2
1731 * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1732 * if value list[int,double] -> cpp std::vector<double> sw=4
1733 * if value tuple[int,double] -> cpp std::vector<double> sw=4
1735 static void convertObjToPossibleCpp5(PyObject *value, int& sw, double& val, ParaMEDMEM::DataArrayDouble *&d, ParaMEDMEM::DataArrayDoubleTuple *&e, std::vector<double>& f)
1738 if(PyFloat_Check(value))
1740 val=PyFloat_AS_DOUBLE(value);
1744 if(PyInt_Check(value))
1746 val=(double)PyInt_AS_LONG(value);
1750 if(PyTuple_Check(value))
1752 int size=PyTuple_Size(value);
1754 for(int i=0;i<size;i++)
1756 PyObject *o=PyTuple_GetItem(value,i);
1757 if(PyFloat_Check(o))
1758 f[i]=PyFloat_AS_DOUBLE(o);
1759 else if(PyInt_Check(o))
1760 f[i]=(double)PyInt_AS_LONG(o);
1763 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1764 throw INTERP_KERNEL::Exception(oss.str().c_str());
1770 if(PyList_Check(value))
1772 int size=PyList_Size(value);
1774 for(int i=0;i<size;i++)
1776 PyObject *o=PyList_GetItem(value,i);
1777 if(PyFloat_Check(o))
1778 f[i]=PyFloat_AS_DOUBLE(o);
1779 else if(PyInt_Check(o))
1780 f[i]=(double)PyInt_AS_LONG(o);
1783 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1784 throw INTERP_KERNEL::Exception(oss.str().c_str());
1791 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
1792 if(SWIG_IsOK(status))
1794 d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
1798 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
1799 if(SWIG_IsOK(status))
1801 e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
1805 throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
1809 * if value int -> cpp val sw=1
1810 * if value double -> cpp val sw=1
1811 * if value DataArrayDouble -> cpp DataArrayDouble sw=2
1812 * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1813 * if value list[int,double] -> cpp std::vector<double> sw=4
1814 * if value tuple[int,double] -> cpp std::vector<double> sw=4
1816 static const double *convertObjToPossibleCpp5_Safe(PyObject *value, int& sw, double& val, ParaMEDMEM::DataArrayDouble *&d, ParaMEDMEM::DataArrayDoubleTuple *&e, std::vector<double>& f,
1817 const char *msg, int nbTuplesExpected, int nbCompExpected, bool throwIfNullPt) throw(INTERP_KERNEL::Exception)
1820 if(PyFloat_Check(value))
1822 val=PyFloat_AS_DOUBLE(value);
1824 if(nbTuplesExpected*nbCompExpected!=1)
1826 std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyFloat) !";
1827 throw INTERP_KERNEL::Exception(oss.str().c_str());
1831 if(PyInt_Check(value))
1833 val=(double)PyInt_AS_LONG(value);
1835 if(nbTuplesExpected*nbCompExpected!=1)
1837 std::ostringstream oss; oss << msg << "dimension expected to be " << nbTuplesExpected*nbCompExpected << " , and your data in input has dimension one (single PyInt) !";
1838 throw INTERP_KERNEL::Exception(oss.str().c_str());
1842 if(PyTuple_Check(value) || PyList_Check(value))
1846 int tmp1=nbTuplesExpected,tmp2=nbCompExpected;
1847 std::vector<double> ret=fillArrayWithPyListDbl2(value,tmp1,tmp2);
1852 catch(INTERP_KERNEL::Exception& exc) { throw exc; }
1855 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
1856 if(SWIG_IsOK(status))
1858 d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
1862 if(d->getNumberOfTuples()==nbTuplesExpected)
1864 if(d->getNumberOfComponents()==nbCompExpected)
1866 return d->getConstPointer();
1870 std::ostringstream oss; oss << msg << "nb of components expected to be " << nbCompExpected << " , and input has " << d->getNumberOfComponents() << " components !";
1871 throw INTERP_KERNEL::Exception(oss.str().c_str());
1876 std::ostringstream oss; oss << msg << " input DataArrayDouble should have a number of tuples equal to " << nbTuplesExpected << " and there are " << d->getNumberOfTuples() << " tuples !";
1877 throw INTERP_KERNEL::Exception(oss.str().c_str());
1884 std::ostringstream oss; oss << msg << " null pointer not accepted!";
1885 throw INTERP_KERNEL::Exception(oss.str().c_str());
1891 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
1892 if(SWIG_IsOK(status))
1894 e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
1896 if(e->getNumberOfCompo()==nbCompExpected)
1898 if(nbTuplesExpected==1)
1899 return e->getConstPointer();
1902 std::ostringstream oss; oss << msg << "nb of tuples expected to be " << nbTuplesExpected << " , and input DataArrayDoubleTuple has always one tuple by contruction !";
1903 throw INTERP_KERNEL::Exception(oss.str().c_str());
1908 std::ostringstream oss; oss << msg << "nb of components expected to be " << nbCompExpected << " , and input DataArrayDoubleTuple has " << e->getNumberOfCompo() << " components !";
1909 throw INTERP_KERNEL::Exception(oss.str().c_str());
1912 throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
1916 * if value int -> cpp val sw=1
1917 * if value double -> cpp val sw=1
1918 * if value DataArrayDouble -> cpp DataArrayDouble sw=2
1919 * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
1920 * if value list[int,double] -> cpp std::vector<double> sw=4
1921 * if value tuple[int,double] -> cpp std::vector<double> sw=4
1923 static const double *convertObjToPossibleCpp5_Safe2(PyObject *value, int& sw, double& val, ParaMEDMEM::DataArrayDouble *&d, ParaMEDMEM::DataArrayDoubleTuple *&e, std::vector<double>& f,
1924 const char *msg, int nbCompExpected, bool throwIfNullPt, int& nbTuples) throw(INTERP_KERNEL::Exception)
1927 if(PyFloat_Check(value))
1929 val=PyFloat_AS_DOUBLE(value);
1931 if(nbCompExpected!=1)
1933 std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyFloat) !";
1934 throw INTERP_KERNEL::Exception(oss.str().c_str());
1939 if(PyInt_Check(value))
1941 val=(double)PyInt_AS_LONG(value);
1943 if(nbCompExpected!=1)
1945 std::ostringstream oss; oss << msg << "dimension expected to be " << nbCompExpected << " , and your data in input has dimension one (single PyInt) !";
1946 throw INTERP_KERNEL::Exception(oss.str().c_str());
1951 if(PyTuple_Check(value))
1953 int size=PyTuple_Size(value);
1955 for(int i=0;i<size;i++)
1957 PyObject *o=PyTuple_GetItem(value,i);
1958 if(PyFloat_Check(o))
1959 f[i]=PyFloat_AS_DOUBLE(o);
1960 else if(PyInt_Check(o))
1961 f[i]=(double)PyInt_AS_LONG(o);
1964 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
1965 throw INTERP_KERNEL::Exception(oss.str().c_str());
1969 if(size%nbCompExpected!=0)
1971 std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !";
1972 throw INTERP_KERNEL::Exception(oss.str().c_str());
1974 nbTuples=size/nbCompExpected;
1977 if(PyList_Check(value))
1979 int size=PyList_Size(value);
1981 for(int i=0;i<size;i++)
1983 PyObject *o=PyList_GetItem(value,i);
1984 if(PyFloat_Check(o))
1985 f[i]=PyFloat_AS_DOUBLE(o);
1986 else if(PyInt_Check(o))
1987 f[i]=(double)PyInt_AS_LONG(o);
1990 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
1991 throw INTERP_KERNEL::Exception(oss.str().c_str());
1995 if(size%nbCompExpected!=0)
1997 std::ostringstream oss; oss << msg << "dimension expected to be a multiple of " << nbCompExpected << " , and your data in input has dimension " << f.size() << " !";
1998 throw INTERP_KERNEL::Exception(oss.str().c_str());
2000 nbTuples=size/nbCompExpected;
2004 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
2005 if(SWIG_IsOK(status))
2007 d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
2011 if(d->getNumberOfComponents()==nbCompExpected)
2013 nbTuples=d->getNumberOfTuples();
2014 return d->getConstPointer();
2018 std::ostringstream oss; oss << msg << "nb of components expected to be a multiple of " << nbCompExpected << " , and input has " << d->getNumberOfComponents() << " components !";
2019 throw INTERP_KERNEL::Exception(oss.str().c_str());
2026 std::ostringstream oss; oss << msg << " null pointer not accepted!";
2027 throw INTERP_KERNEL::Exception(oss.str().c_str());
2030 { nbTuples=0; return 0; }
2033 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
2034 if(SWIG_IsOK(status))
2036 e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
2040 if(e->getNumberOfCompo()==nbCompExpected)
2043 return e->getConstPointer();
2047 std::ostringstream oss; oss << msg << "nb of components expected to be " << nbCompExpected << " , and input DataArrayDoubleTuple has " << e->getNumberOfCompo() << " components !";
2048 throw INTERP_KERNEL::Exception(oss.str().c_str());
2055 std::ostringstream oss; oss << msg << " null pointer not accepted!";
2056 throw INTERP_KERNEL::Exception(oss.str().c_str());
2059 { nbTuples=0; return 0; }
2062 throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
2066 * if value int -> cpp val sw=1
2067 * if value double -> cpp val sw=1
2068 * if value DataArrayDouble -> cpp DataArrayDouble sw=2
2069 * if value DataArrayDoubleTuple -> cpp DataArrayDoubleTuple sw=3
2070 * if value list[int,double] -> cpp std::vector<double> sw=4
2071 * if value tuple[int,double] -> cpp std::vector<double> sw=4
2073 static const double *convertObjToPossibleCpp5_SingleCompo(PyObject *value, int& sw, double& val, std::vector<double>& f,
2074 const char *msg, bool throwIfNullPt, int& nbTuples) throw(INTERP_KERNEL::Exception)
2076 ParaMEDMEM::DataArrayDouble *d=0;
2077 ParaMEDMEM::DataArrayDoubleTuple *e=0;
2079 if(PyFloat_Check(value))
2081 val=PyFloat_AS_DOUBLE(value);
2086 if(PyInt_Check(value))
2088 val=(double)PyInt_AS_LONG(value);
2093 if(PyTuple_Check(value))
2095 int size=PyTuple_Size(value);
2097 for(int i=0;i<size;i++)
2099 PyObject *o=PyTuple_GetItem(value,i);
2100 if(PyFloat_Check(o))
2101 f[i]=PyFloat_AS_DOUBLE(o);
2102 else if(PyInt_Check(o))
2103 f[i]=(double)PyInt_AS_LONG(o);
2106 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not double ! only tuples of doubles accepted or integer !";
2107 throw INTERP_KERNEL::Exception(oss.str().c_str());
2114 if(PyList_Check(value))
2116 int size=PyList_Size(value);
2118 for(int i=0;i<size;i++)
2120 PyObject *o=PyList_GetItem(value,i);
2121 if(PyFloat_Check(o))
2122 f[i]=PyFloat_AS_DOUBLE(o);
2123 else if(PyInt_Check(o))
2124 f[i]=(double)PyInt_AS_LONG(o);
2127 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not double ! only lists of doubles accepted or integer !";
2128 throw INTERP_KERNEL::Exception(oss.str().c_str());
2136 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
2137 if(SWIG_IsOK(status))
2139 d=reinterpret_cast< ParaMEDMEM::DataArrayDouble * >(argp);
2143 if(d->getNumberOfComponents()==1)
2145 nbTuples=d->getNumberOfTuples();
2146 return d->getConstPointer();
2150 std::ostringstream oss; oss << msg << "nb of components expected to be one, and input has " << d->getNumberOfComponents() << " components !";
2151 throw INTERP_KERNEL::Exception(oss.str().c_str());
2158 std::ostringstream oss; oss << msg << " null pointer not accepted!";
2159 throw INTERP_KERNEL::Exception(oss.str().c_str());
2162 { nbTuples=0; return 0; }
2165 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayDoubleTuple,0|0);
2166 if(SWIG_IsOK(status))
2168 e=reinterpret_cast< ParaMEDMEM::DataArrayDoubleTuple * >(argp);
2172 nbTuples=e->getNumberOfCompo();
2173 return e->getConstPointer();
2179 std::ostringstream oss; oss << msg << " null pointer not accepted!";
2180 throw INTERP_KERNEL::Exception(oss.str().c_str());
2183 { nbTuples=0; return 0; }
2186 throw INTERP_KERNEL::Exception("4 types accepted : integer, double, DataArrayDouble, DataArrayDoubleTuple");
2190 * if python int -> cpp int sw=1
2191 * if python list[int] -> cpp vector<int> sw=2
2192 * if python tuple[int] -> cpp vector<int> sw=2
2193 * if python DataArrayInt -> cpp DataArrayInt sw=3
2194 * if python DataArrayIntTuple -> cpp DataArrayIntTuple sw=4
2196 * switch between (int,vector<int>,DataArrayInt)
2198 static const int *convertObjToPossibleCpp1_Safe(PyObject *value, int& sw, int& sz, int& iTyypp, std::vector<int>& stdvecTyypp) throw(INTERP_KERNEL::Exception)
2201 if(PyInt_Check(value))
2203 iTyypp=(int)PyInt_AS_LONG(value);
2207 if(PyTuple_Check(value))
2209 int size=PyTuple_Size(value);
2210 stdvecTyypp.resize(size);
2211 for(int i=0;i<size;i++)
2213 PyObject *o=PyTuple_GetItem(value,i);
2215 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
2218 std::ostringstream oss; oss << "Tuple as been detected but element #" << i << " is not integer ! only tuples of integers accepted !";
2219 throw INTERP_KERNEL::Exception(oss.str().c_str());
2223 return &stdvecTyypp[0];
2225 if(PyList_Check(value))
2227 int size=PyList_Size(value);
2228 stdvecTyypp.resize(size);
2229 for(int i=0;i<size;i++)
2231 PyObject *o=PyList_GetItem(value,i);
2233 stdvecTyypp[i]=(int)PyInt_AS_LONG(o);
2236 std::ostringstream oss; oss << "List as been detected but element #" << i << " is not integer ! only lists of integers accepted !";
2237 throw INTERP_KERNEL::Exception(oss.str().c_str());
2241 return &stdvecTyypp[0];
2244 int status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
2245 if(SWIG_IsOK(status))
2247 ParaMEDMEM::DataArrayInt *daIntTyypp=reinterpret_cast< ParaMEDMEM::DataArrayInt * >(argp);
2250 sw=3; sz=daIntTyypp->getNbOfElems();
2251 return daIntTyypp->begin();
2259 status=SWIG_ConvertPtr(value,&argp,SWIGTYPE_p_ParaMEDMEM__DataArrayIntTuple,0|0);
2260 if(SWIG_IsOK(status))
2262 ParaMEDMEM::DataArrayIntTuple *daIntTuple=reinterpret_cast< ParaMEDMEM::DataArrayIntTuple * >(argp);
2263 sw=4; sz=daIntTuple->getNumberOfCompo();
2264 return daIntTuple->getConstPointer();
2266 throw INTERP_KERNEL::Exception("5 types accepted : integer, tuple of integer, list of integer, DataArrayInt, DataArrayIntTuple");
2269 static ParaMEDMEM::MEDCouplingFieldDouble *ParaMEDMEM_MEDCouplingFieldDouble___add__Impl(ParaMEDMEM::MEDCouplingFieldDouble *self, PyObject *obj) throw(INTERP_KERNEL::Exception)
2271 const char msg[]="Unexpected situation in MEDCouplingFieldDouble.__add__ ! Expecting a not null MEDCouplingFieldDouble or DataArrayDouble or DataArrayDoubleTuple instance, or a list of double, or a double.";
2272 const char msg2[]="in MEDCouplingFieldDouble.__add__ : self field has no Array of values set !";
2275 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDouble,0|0)))
2277 ParaMEDMEM::MEDCouplingFieldDouble *other=reinterpret_cast< ParaMEDMEM::MEDCouplingFieldDouble * >(argp);
2279 return (*self)+(*other);
2281 throw INTERP_KERNEL::Exception(msg);
2285 ParaMEDMEM::DataArrayDouble *a;
2286 ParaMEDMEM::DataArrayDoubleTuple *aa;
2287 std::vector<double> bb;
2289 convertObjToPossibleCpp5(obj,sw,val,a,aa,bb);
2294 if(!self->getArray())
2295 throw INTERP_KERNEL::Exception(msg2);
2296 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=self->getArray()->deepCpy();
2297 ret->applyLin(1.,val);
2298 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2299 ret2->setArray(ret);
2304 if(!self->getArray())
2305 throw INTERP_KERNEL::Exception(msg2);
2306 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Add(self->getArray(),a);
2307 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2308 ret2->setArray(ret);
2313 if(!self->getArray())
2314 throw INTERP_KERNEL::Exception(msg2);
2315 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
2316 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Add(self->getArray(),aaa);
2317 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2318 ret2->setArray(ret);
2323 if(!self->getArray())
2324 throw INTERP_KERNEL::Exception(msg2);
2325 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> aaa=ParaMEDMEM::DataArrayDouble::New(); aaa->useArray(&bb[0],false,ParaMEDMEM::CPP_DEALLOC,1,(int)bb.size());
2326 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Add(self->getArray(),aaa);
2327 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2328 ret2->setArray(ret);
2332 { throw INTERP_KERNEL::Exception(msg); }
2336 static ParaMEDMEM::MEDCouplingFieldDouble *ParaMEDMEM_MEDCouplingFieldDouble___radd__Impl(ParaMEDMEM::MEDCouplingFieldDouble *self, PyObject *obj) throw(INTERP_KERNEL::Exception)
2338 return ParaMEDMEM_MEDCouplingFieldDouble___add__Impl(self,obj);
2341 static ParaMEDMEM::MEDCouplingFieldDouble *ParaMEDMEM_MEDCouplingFieldDouble___rsub__Impl(ParaMEDMEM::MEDCouplingFieldDouble *self, PyObject *obj) throw(INTERP_KERNEL::Exception)
2343 const char msg[]="Unexpected situation in MEDCouplingFieldDouble.__rsub__ ! Expecting a not null MEDCouplingFieldDouble or DataArrayDouble or DataArrayDoubleTuple instance, or a list of double, or a double.";
2344 const char msg2[]="in MEDCouplingFieldDouble.__rsub__ : self field has no Array of values set !";
2347 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDouble,0|0)))
2349 ParaMEDMEM::MEDCouplingFieldDouble *other=reinterpret_cast< ParaMEDMEM::MEDCouplingFieldDouble * >(argp);
2351 return (*other)-(*self);
2353 throw INTERP_KERNEL::Exception(msg);
2357 ParaMEDMEM::DataArrayDouble *a;
2358 ParaMEDMEM::DataArrayDoubleTuple *aa;
2359 std::vector<double> bb;
2361 convertObjToPossibleCpp5(obj,sw,val,a,aa,bb);
2366 if(!self->getArray())
2367 throw INTERP_KERNEL::Exception(msg2);
2368 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=self->getArray()->deepCpy();
2369 ret->applyLin(-1.,val);
2370 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2371 ret2->setArray(ret);
2376 if(!self->getArray())
2377 throw INTERP_KERNEL::Exception(msg2);
2378 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Substract(a,self->getArray());
2379 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2380 ret2->setArray(ret);
2385 if(!self->getArray())
2386 throw INTERP_KERNEL::Exception(msg2);
2387 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
2388 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Substract(aaa,self->getArray());
2389 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2390 ret2->setArray(ret);
2395 if(!self->getArray())
2396 throw INTERP_KERNEL::Exception(msg2);
2397 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> aaa=ParaMEDMEM::DataArrayDouble::New(); aaa->useArray(&bb[0],false,ParaMEDMEM::CPP_DEALLOC,1,(int)bb.size());
2398 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Substract(aaa,self->getArray());
2399 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2400 ret2->setArray(ret);
2404 { throw INTERP_KERNEL::Exception(msg); }
2408 static ParaMEDMEM::MEDCouplingFieldDouble *ParaMEDMEM_MEDCouplingFieldDouble___mul__Impl(ParaMEDMEM::MEDCouplingFieldDouble *self, PyObject *obj) throw(INTERP_KERNEL::Exception)
2410 const char msg[]="Unexpected situation in MEDCouplingFieldDouble.__mul__ ! Expecting a not null MEDCouplingFieldDouble or DataArrayDouble or DataArrayDoubleTuple instance, or a list of double, or a double.";
2411 const char msg2[]="in MEDCouplingFieldDouble.__mul__ : self field has no Array of values set !";
2414 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDouble,0|0)))
2416 ParaMEDMEM::MEDCouplingFieldDouble *other=reinterpret_cast< ParaMEDMEM::MEDCouplingFieldDouble * >(argp);
2418 return (*self)*(*other);
2420 throw INTERP_KERNEL::Exception(msg);
2424 ParaMEDMEM::DataArrayDouble *a;
2425 ParaMEDMEM::DataArrayDoubleTuple *aa;
2426 std::vector<double> bb;
2428 convertObjToPossibleCpp5(obj,sw,val,a,aa,bb);
2433 if(!self->getArray())
2434 throw INTERP_KERNEL::Exception(msg2);
2435 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=self->getArray()->deepCpy();
2436 ret->applyLin(val,0.);
2437 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2438 ret2->setArray(ret);
2443 if(!self->getArray())
2444 throw INTERP_KERNEL::Exception(msg2);
2445 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Multiply(self->getArray(),a);
2446 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2447 ret2->setArray(ret);
2452 if(!self->getArray())
2453 throw INTERP_KERNEL::Exception(msg2);
2454 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
2455 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Multiply(self->getArray(),aaa);
2456 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2457 ret2->setArray(ret);
2462 if(!self->getArray())
2463 throw INTERP_KERNEL::Exception(msg2);
2464 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> aaa=ParaMEDMEM::DataArrayDouble::New(); aaa->useArray(&bb[0],false,ParaMEDMEM::CPP_DEALLOC,1,(int)bb.size());
2465 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Multiply(self->getArray(),aaa);
2466 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2467 ret2->setArray(ret);
2471 { throw INTERP_KERNEL::Exception(msg); }
2475 ParaMEDMEM::MEDCouplingFieldDouble *ParaMEDMEM_MEDCouplingFieldDouble___rmul__Impl(ParaMEDMEM::MEDCouplingFieldDouble *self, PyObject *obj) throw(INTERP_KERNEL::Exception)
2477 return ParaMEDMEM_MEDCouplingFieldDouble___mul__Impl(self,obj);
2480 ParaMEDMEM::MEDCouplingFieldDouble *ParaMEDMEM_MEDCouplingFieldDouble___rdiv__Impl(ParaMEDMEM::MEDCouplingFieldDouble *self, PyObject *obj) throw(INTERP_KERNEL::Exception)
2482 const char msg[]="Unexpected situation in MEDCouplingFieldDouble.__rdiv__ ! Expecting a not null MEDCouplingFieldDouble or DataArrayDouble or DataArrayDoubleTuple instance, or a list of double, or a double.";
2483 const char msg2[]="in MEDCouplingFieldDouble.__div__ : self field has no Array of values set !";
2486 if(SWIG_IsOK(SWIG_ConvertPtr(obj,&argp,SWIGTYPE_p_ParaMEDMEM__MEDCouplingFieldDouble,0|0)))
2488 ParaMEDMEM::MEDCouplingFieldDouble *other=reinterpret_cast< ParaMEDMEM::MEDCouplingFieldDouble * >(argp);
2490 return (*other)/(*self);
2492 throw INTERP_KERNEL::Exception(msg);
2496 ParaMEDMEM::DataArrayDouble *a;
2497 ParaMEDMEM::DataArrayDoubleTuple *aa;
2498 std::vector<double> bb;
2500 convertObjToPossibleCpp5(obj,sw,val,a,aa,bb);
2505 if(!self->getArray())
2506 throw INTERP_KERNEL::Exception(msg2);
2507 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=self->getArray()->deepCpy();
2509 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2510 ret2->setArray(ret);
2515 if(!self->getArray())
2516 throw INTERP_KERNEL::Exception(msg2);
2517 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Divide(a,self->getArray());
2518 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2519 ret2->setArray(ret);
2524 if(!self->getArray())
2525 throw INTERP_KERNEL::Exception(msg2);
2526 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> aaa=aa->buildDADouble(1,self->getNumberOfComponents());
2527 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Divide(aaa,self->getArray());
2528 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2529 ret2->setArray(ret);
2534 if(!self->getArray())
2535 throw INTERP_KERNEL::Exception(msg2);
2536 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> aaa=ParaMEDMEM::DataArrayDouble::New(); aaa->useArray(&bb[0],false,ParaMEDMEM::CPP_DEALLOC,1,(int)bb.size());
2537 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::DataArrayDouble> ret=ParaMEDMEM::DataArrayDouble::Divide(aaa,self->getArray());
2538 ParaMEDMEM::MEDCouplingAutoRefCountObjectPtr<ParaMEDMEM::MEDCouplingFieldDouble> ret2=self->clone(false);
2539 ret2->setArray(ret);
2543 { throw INTERP_KERNEL::Exception(msg); }
2547 static ParaMEDMEM::DataArray *CheckAndRetrieveDataArrayInstance(PyObject *obj, const char *msg)
2550 int status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArray,0|0);
2551 if(!SWIG_IsOK(status))
2553 status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayDouble,0|0);
2554 if(!SWIG_IsOK(status))
2556 status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayInt,0|0);
2557 if(!SWIG_IsOK(status))
2559 status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayAsciiChar,0|0);
2560 if(!SWIG_IsOK(status))
2562 status=SWIG_ConvertPtr(obj,&aBasePtrVS,SWIGTYPE_p_ParaMEDMEM__DataArrayByte,0|0);
2563 std::ostringstream oss; oss << msg << " ! Accepted instances are DataArrayDouble, DataArrayInt, DataArrayAsciiChar, DataArrayByte !";
2564 throw INTERP_KERNEL::Exception(oss.str().c_str());
2569 return reinterpret_cast< ParaMEDMEM::DataArray * >(aBasePtrVS);