From 412dee9ae935f49d8bf89d760e188e31c11f69ce Mon Sep 17 00:00:00 2001 From: ageay Date: Wed, 4 Jul 2012 08:19:06 +0000 Subject: [PATCH] Iterators on MEDFileFields, MEDFileFieldMultiTS and MEDFileMeshes --- src/MEDLoader/MEDFileField.cxx | 56 ++++++++++++++++++++ src/MEDLoader/MEDFileField.hxx | 28 ++++++++++ src/MEDLoader/MEDFileMesh.cxx | 28 ++++++++++ src/MEDLoader/MEDFileMesh.hxx | 14 +++++ src/MEDLoader/Swig/MEDLoaderCommon.i | 78 ++++++++++++++++++++++++++++ src/MEDLoader/Swig/MEDLoaderTest3.py | 12 +++++ 6 files changed, 216 insertions(+) diff --git a/src/MEDLoader/MEDFileField.cxx b/src/MEDLoader/MEDFileField.cxx index 1c3036f32..6a6b8755e 100644 --- a/src/MEDLoader/MEDFileField.cxx +++ b/src/MEDLoader/MEDFileField.cxx @@ -4077,6 +4077,11 @@ MEDFileField1TS *MEDFileFieldMultiTS::getTimeStepGivenTime(double time, double e return getTimeStepAtPos(pos); } +MEDFileFieldMultiTSIterator *MEDFileFieldMultiTS::iterator() throw(INTERP_KERNEL::Exception) +{ + return new MEDFileFieldMultiTSIterator(this); +} + std::string MEDFileFieldMultiTS::simpleRepr() const { std::ostringstream oss; @@ -4309,6 +4314,29 @@ void MEDFileFieldMultiTS::changeLocsRefsNamesGen(const std::vector< std::pairincrRef(); + _nb_iter=fmts->getNumberOfTS(); + } +} + +MEDFileField1TS *MEDFileFieldMultiTSIterator::nextt() +{ + if(_iter_id<_nb_iter) + { + MEDFileFieldMultiTS *fmts(_fmts); + if(fmts) + return fmts->getTimeStepAtPos(_iter_id++); + else + return 0; + } + else + return 0; +} + MEDFileFields *MEDFileFields::New() { return new MEDFileFields; @@ -4571,6 +4599,11 @@ MEDFileFieldMultiTS *MEDFileFields::getFieldWithName(const char *fieldName) cons return getFieldAtPos(getPosFromFieldName(fieldName)); } +MEDFileFieldsIterator *MEDFileFields::iterator() throw(INTERP_KERNEL::Exception) +{ + return new MEDFileFieldsIterator(this); +} + int MEDFileFields::getPosFromFieldName(const char *fieldName) const throw(INTERP_KERNEL::Exception) { std::string tmp(fieldName); @@ -4592,3 +4625,26 @@ int MEDFileFields::getPosFromFieldName(const char *fieldName) const throw(INTERP oss << " !"; throw INTERP_KERNEL::Exception(oss.str().c_str()); } + +MEDFileFieldsIterator::MEDFileFieldsIterator(MEDFileFields *fs):_fs(fs),_iter_id(0),_nb_iter(0) +{ + if(fs) + { + fs->incrRef(); + _nb_iter=fs->getNumberOfFields(); + } +} + +MEDFileFieldMultiTS *MEDFileFieldsIterator::nextt() +{ + if(_iter_id<_nb_iter) + { + MEDFileFields *fs(_fs); + if(fs) + return fs->getFieldAtPos(_iter_id++); + else + return 0; + } + else + return 0; +} diff --git a/src/MEDLoader/MEDFileField.hxx b/src/MEDLoader/MEDFileField.hxx index 95bba60f5..53dd8e500 100644 --- a/src/MEDLoader/MEDFileField.hxx +++ b/src/MEDLoader/MEDFileField.hxx @@ -531,6 +531,8 @@ namespace ParaMEDMEM std::vector< MEDCouplingAutoRefCountObjectPtr > _time_steps; }; + class MEDFileFieldMultiTSIterator; + /*! * User class. */ @@ -545,6 +547,7 @@ namespace ParaMEDMEM MEDFileField1TS *getTimeStepAtPos(int pos) const throw(INTERP_KERNEL::Exception); MEDFileField1TS *getTimeStep(int iteration, int order) const throw(INTERP_KERNEL::Exception); MEDFileField1TS *getTimeStepGivenTime(double time, double eps=1e-8) const throw(INTERP_KERNEL::Exception); + MEDFileFieldMultiTSIterator *iterator() throw(INTERP_KERNEL::Exception); // std::string simpleRepr() const; void write(const char *fileName, int mode) const throw(INTERP_KERNEL::Exception); @@ -572,6 +575,19 @@ namespace ParaMEDMEM MEDFileFieldMultiTS(const char *fileName, const char *fieldName) throw(INTERP_KERNEL::Exception); }; + class MEDCOUPLING_EXPORT MEDFileFieldMultiTSIterator + { + public: + MEDFileFieldMultiTSIterator(MEDFileFieldMultiTS *fmts); + MEDFileField1TS *nextt(); + private: + MEDCouplingAutoRefCountObjectPtr _fmts; + int _iter_id; + int _nb_iter; + }; + + class MEDFileFieldsIterator; + /*! * Use class. */ @@ -592,6 +608,7 @@ namespace ParaMEDMEM void setFieldAtPos(int i, MEDFileFieldMultiTS *field) throw(INTERP_KERNEL::Exception); MEDFileFieldMultiTS *getFieldAtPos(int i) const throw(INTERP_KERNEL::Exception); MEDFileFieldMultiTS *getFieldWithName(const char *fieldName) const throw(INTERP_KERNEL::Exception); + MEDFileFieldsIterator *iterator() throw(INTERP_KERNEL::Exception); void destroyFieldAtPos(int i) throw(INTERP_KERNEL::Exception); private: int getPosFromFieldName(const char *fieldName) const throw(INTERP_KERNEL::Exception); @@ -607,6 +624,17 @@ namespace ParaMEDMEM private: std::vector< MEDCouplingAutoRefCountObjectPtr > _fields; }; + + class MEDCOUPLING_EXPORT MEDFileFieldsIterator + { + public: + MEDFileFieldsIterator(MEDFileFields *fs); + MEDFileFieldMultiTS *nextt(); + private: + MEDCouplingAutoRefCountObjectPtr _fs; + int _iter_id; + int _nb_iter; + }; } #endif diff --git a/src/MEDLoader/MEDFileMesh.cxx b/src/MEDLoader/MEDFileMesh.cxx index 19f65abd4..1d818de6a 100644 --- a/src/MEDLoader/MEDFileMesh.cxx +++ b/src/MEDLoader/MEDFileMesh.cxx @@ -2926,6 +2926,11 @@ int MEDFileMeshes::getNumberOfMeshes() const throw(INTERP_KERNEL::Exception) return _meshes.size(); } +MEDFileMeshesIterator *MEDFileMeshes::iterator() throw(INTERP_KERNEL::Exception) +{ + return new MEDFileMeshesIterator(this); +} + MEDFileMesh *MEDFileMeshes::getMeshAtPos(int i) const throw(INTERP_KERNEL::Exception) { if(i<0 || i>=(int)_meshes.size()) @@ -3065,3 +3070,26 @@ void MEDFileMeshes::checkCoherency() const throw(INTERP_KERNEL::Exception) } } } + +MEDFileMeshesIterator::MEDFileMeshesIterator(MEDFileMeshes *ms):_ms(ms),_iter_id(0),_nb_iter(0) +{ + if(ms) + { + ms->incrRef(); + _nb_iter=ms->getNumberOfMeshes(); + } +} + +MEDFileMesh *MEDFileMeshesIterator::nextt() +{ + if(_iter_id<_nb_iter) + { + MEDFileMeshes *ms(_ms); + if(ms) + return ms->getMeshAtPos(_iter_id++); + else + return 0; + } + else + return 0; +} diff --git a/src/MEDLoader/MEDFileMesh.hxx b/src/MEDLoader/MEDFileMesh.hxx index 83ff5c90c..fbe279d5a 100644 --- a/src/MEDLoader/MEDFileMesh.hxx +++ b/src/MEDLoader/MEDFileMesh.hxx @@ -283,6 +283,8 @@ namespace ParaMEDMEM std::vector< MEDCouplingAutoRefCountObjectPtr > _mesh_one_ts; }; + class MEDFileMeshesIterator; + class MEDLOADER_EXPORT MEDFileMeshes : public RefCountObject, public MEDFileWritable { public: @@ -293,6 +295,7 @@ namespace ParaMEDMEM void write(const char *fileName, int mode) const throw(INTERP_KERNEL::Exception); void write(med_idt fid) const throw(INTERP_KERNEL::Exception); int getNumberOfMeshes() const throw(INTERP_KERNEL::Exception); + MEDFileMeshesIterator *iterator() throw(INTERP_KERNEL::Exception); MEDFileMesh *getMeshAtPos(int i) const throw(INTERP_KERNEL::Exception); MEDFileMesh *getMeshWithName(const char *mname) const throw(INTERP_KERNEL::Exception); std::vector getMeshesNames() const throw(INTERP_KERNEL::Exception); @@ -309,6 +312,17 @@ namespace ParaMEDMEM private: std::vector< MEDCouplingAutoRefCountObjectPtr > _meshes; }; + + class MEDCOUPLING_EXPORT MEDFileMeshesIterator + { + public: + MEDFileMeshesIterator(MEDFileMeshes *ms); + MEDFileMesh *nextt(); + private: + MEDCouplingAutoRefCountObjectPtr _ms; + int _iter_id; + int _nb_iter; + }; } #endif diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 93ebbc8b1..c6d62dec8 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -82,16 +82,19 @@ using namespace ParaMEDMEM; %newobject ParaMEDMEM::MEDFileMeshes::getMeshAtPos; %newobject ParaMEDMEM::MEDFileMeshes::getMeshWithName; %newobject ParaMEDMEM::MEDFileMeshes::__getitem__; +%newobject ParaMEDMEM::MEDFileMeshes::__iter__; %newobject ParaMEDMEM::MEDFileFields::New; %newobject ParaMEDMEM::MEDFileFields::getFieldWithName; %newobject ParaMEDMEM::MEDFileFields::getFieldAtPos; %newobject ParaMEDMEM::MEDFileFields::__getitem__; +%newobject ParaMEDMEM::MEDFileFields::__iter__; %newobject ParaMEDMEM::MEDFileFieldMultiTS::New; %newobject ParaMEDMEM::MEDFileFieldMultiTS::getTimeStepAtPos; %newobject ParaMEDMEM::MEDFileFieldMultiTS::getTimeStep; %newobject ParaMEDMEM::MEDFileFieldMultiTS::getTimeStepGivenTime; %newobject ParaMEDMEM::MEDFileFieldMultiTS::__getitem__; +%newobject ParaMEDMEM::MEDFileFieldMultiTS::__iter__; %newobject ParaMEDMEM::MEDFileFieldMultiTS::getFieldAtLevel; %newobject ParaMEDMEM::MEDFileFieldMultiTS::getFieldAtTopLevel; %newobject ParaMEDMEM::MEDFileFieldMultiTS::getFieldOnMeshAtLevel; @@ -657,6 +660,28 @@ namespace ParaMEDMEM } }; + class MEDFileMeshesIterator + { + public: + %extend + { + PyObject *next() throw(INTERP_KERNEL::Exception) + { + MEDFileMesh *ret=self->nextt(); + if(ret) + { + ret->incrRef(); + return convertMEDFileMesh(ret,SWIG_POINTER_OWN | 0 ); + } + else + { + PyErr_SetString(PyExc_StopIteration,"No more data."); + return 0; + } + } + } + }; + class MEDFileMeshes : public RefCountObject, public MEDFileWritable { public: @@ -712,6 +737,11 @@ namespace ParaMEDMEM self->setMeshAtPos(obj,mesh); return self; } + + MEDFileMeshesIterator *__iter__() throw(INTERP_KERNEL::Exception) + { + return self->iterator(); + } MEDFileMesh *getMeshAtPos(int i) const throw(INTERP_KERNEL::Exception) { @@ -1329,6 +1359,25 @@ namespace ParaMEDMEM } }; + class MEDFileFieldMultiTSIterator + { + public: + %extend + { + PyObject *next() throw(INTERP_KERNEL::Exception) + { + MEDFileField1TS *ret=self->nextt(); + if(ret) + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__MEDFileField1TS,SWIG_POINTER_OWN | 0); + else + { + PyErr_SetString(PyExc_StopIteration,"No more data."); + return 0; + } + } + } + }; + class MEDFileFieldMultiTS : public MEDFileFieldMultiTSWithoutDAS, public MEDFileFieldGlobsReal, public MEDFileWritable { public: @@ -1405,6 +1454,11 @@ namespace ParaMEDMEM throw INTERP_KERNEL::Exception("MEDFileFieldMultiTS::__getitem__ : invalid input params ! expected fmts[int], fmts[int,int] or fmts[double] to request time step !"); } + MEDFileFieldMultiTSIterator *__iter__() throw(INTERP_KERNEL::Exception) + { + return self->iterator(); + } + PyObject *getFieldWithProfile(TypeOfField type, int iteration, int order, int meshDimRelToMax, const MEDFileMesh *mesh) const throw(INTERP_KERNEL::Exception) { DataArrayInt *ret1=0; @@ -1417,6 +1471,25 @@ namespace ParaMEDMEM } }; + class MEDFileFieldsIterator + { + public: + %extend + { + PyObject *next() throw(INTERP_KERNEL::Exception) + { + MEDFileFieldMultiTS *ret=self->nextt(); + if(ret) + return SWIG_NewPointerObj(SWIG_as_voidptr(ret),SWIGTYPE_p_ParaMEDMEM__MEDFileFieldMultiTS,SWIG_POINTER_OWN | 0); + else + { + PyErr_SetString(PyExc_StopIteration,"No more data."); + return 0; + } + } + } + }; + class MEDFileFields : public RefCountObject, public MEDFileFieldGlobsReal, public MEDFileWritable { public: @@ -1468,6 +1541,11 @@ namespace ParaMEDMEM self->setFieldAtPos(obj,field); return self; } + + MEDFileFieldsIterator *__iter__() throw(INTERP_KERNEL::Exception) + { + return self->iterator(); + } } }; diff --git a/src/MEDLoader/Swig/MEDLoaderTest3.py b/src/MEDLoader/Swig/MEDLoaderTest3.py index 1c977148f..ebf4b695e 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest3.py +++ b/src/MEDLoader/Swig/MEDLoaderTest3.py @@ -382,6 +382,11 @@ class MEDLoaderTest(unittest.TestCase): mm=MEDFileMesh.New("Pyfile17.med") mm.write("Pyfile17_bis.med",2) ff=MEDFileFieldMultiTS("Pyfile17.med") + tsExpected=[[1,2],[3,4],[5,6]] + for pos,f1ts in enumerate(ff): + self.assertEqual(tsExpected[pos],f1ts.getTime()[:2]) + self.assertEqual(type(f1ts),MEDFileField1TS) + pass self.assertEqual("MeasureOfMesh_Extruded",ff.getName()) self.assertEqual([3,4],ff[1].getTime()[:-1]) self.assertEqual([3,4],ff[3,4].getTime()[:-1]) @@ -578,6 +583,10 @@ class MEDLoaderTest(unittest.TestCase): mmm2=MEDFileMeshMultiTS.New() ; mmm2.setOneTimeStep(mm2) ms=MEDFileMeshes.New(); ms.setMeshAtPos(0,mm1) ; ms.setMeshAtPos(1,mm2) d.setMeshes(ms) + for name,mmm in zip(["1DMesh_1","2DCurveMesh_1"],ms): + self.assertEqual(name,mmm.getName()) + self.assertEqual(type(mmm),MEDFileUMesh) + pass self.assertEqual(('1DMesh_1', '2DCurveMesh_1'),d.getMeshes().getMeshesNames()) # ff1=MEDFileFieldMultiTS.New() @@ -595,6 +604,9 @@ class MEDLoaderTest(unittest.TestCase): ff22.appendFieldNoProfileSBT(f22) fs=MEDFileFields.New() fs.pushField(ff1) ; fs.pushField(ff21) ; fs.pushField(ff22) + for name,fmts in zip(["f1","f21","f22"],fs): + self.assertEqual(name,fmts.getName()) + pass d.setFields(fs) # fname2="Pyfile29_2.med" -- 2.39.2