From: Anthony Geay Date: Fri, 28 Oct 2016 06:52:05 +0000 (+0200) Subject: Write into MEDfile version equal to 3.0 is now possible if using MEDfile library... X-Git-Tag: V8_2_0a1~7 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=0fb8ef9bb978b04e475c7f6d9ca5d8bd6f36bcf5;p=tools%2Fmedcoupling.git Write into MEDfile version equal to 3.0 is now possible if using MEDfile library >= 3.2.1 --- diff --git a/src/MEDLoader/MEDFileUtilities.cxx b/src/MEDLoader/MEDFileUtilities.cxx index 201388f6a..44c4a776b 100644 --- a/src/MEDLoader/MEDFileUtilities.cxx +++ b/src/MEDLoader/MEDFileUtilities.cxx @@ -20,6 +20,7 @@ #include "MEDFileUtilities.hxx" #include "MEDLoaderBase.hxx" +#include "MEDLoader.hxx" #include "InterpKernelAutoPtr.hxx" @@ -189,9 +190,13 @@ void MEDCoupling::MEDFileWritableStandAlone::write(const std::string& fileName, void MEDCoupling::MEDFileWritableStandAlone::write30(const std::string& fileName, int mode) const { med_access_mode medmod(MEDFileUtilities::TraduceWriteMode(mode)); - throw INTERP_KERNEL::Exception("MEDFileWritableStandAlone::write30 : will be implemented with MEDFile >= 3.2.1 !"); - //MEDFileUtilities::AutoFid fid(MEDfileVersionOpen(fileName.c_str(),medmod,3,0,0)); - //writeLL(fid); +#if MED_NUM_MAJEUR>=3 && MED_NUM_MINEUR>=2 && MED_NUM_RELEASE>=1 + MEDFileUtilities::AutoFid fid(MEDfileVersionOpen(fileName.c_str(),medmod,3,0,0)); + writeLL(fid); +#else + std::ostringstream oss; oss << "MEDFileWritableStandAlone::write30 : is implemented with MEDFile " << MEDFileVersionStr() << " ! If you need this feature please use version >= 3.2.1."; + throw INTERP_KERNEL::Exception(oss.str()); +#endif } MEDCoupling::MCAuto MEDCoupling::MEDFileWritableStandAlone::serialize() const diff --git a/src/MEDLoader/MEDLoader.cxx b/src/MEDLoader/MEDLoader.cxx index 50009ad4a..4cd9823b2 100644 --- a/src/MEDLoader/MEDLoader.cxx +++ b/src/MEDLoader/MEDLoader.cxx @@ -292,6 +292,24 @@ std::string MEDCoupling::MEDFileVersionStr() return std::string(MED_VERSION_STR); } +std::string MEDCoupling::MEDFileVersionOfFileStr(const std::string& fileName) +{ + MEDFileUtilities::AutoFid fid(MEDCoupling::OpenMEDFileForRead(fileName)); + const int SZ=20; + const char START_EXPECTED[]="MED-"; + char buf[SZ]; + std::fill(buf,buf+SZ,'\0'); + MEDFILESAFECALLERRD0(MEDfileStrVersionRd,(fid,buf)); + std::string ret(buf); + std::size_t pos(ret.find(START_EXPECTED,0)); + if(pos!=0) + { + std::ostringstream oss; oss << "MEDFileVersionOfFileStr : internal error ! The MEDFile returned version (\"" << ret << "\") has not the right pattern !"; + throw INTERP_KERNEL::Exception(oss.str()); + } + return ret.substr(sizeof(START_EXPECTED)-1,std::string::npos); +} + void MEDCoupling::MEDFileVersion(int& major, int& minor, int& release) { major=MED_NUM_MAJEUR; diff --git a/src/MEDLoader/MEDLoader.hxx b/src/MEDLoader/MEDLoader.hxx index ec445ac8c..a8c5eafed 100644 --- a/src/MEDLoader/MEDLoader.hxx +++ b/src/MEDLoader/MEDLoader.hxx @@ -42,6 +42,7 @@ namespace MEDCoupling MEDLOADER_EXPORT void SetTooLongStrPolicy(int val); MEDLOADER_EXPORT bool HasXDR(); MEDLOADER_EXPORT std::string MEDFileVersionStr(); + MEDLOADER_EXPORT std::string MEDFileVersionOfFileStr(const std::string& fileName); MEDLOADER_EXPORT void MEDFileVersion(int& major, int& minor, int& release); MEDLOADER_EXPORT void CheckFileForRead(const std::string& fileName); MEDLOADER_EXPORT std::vector GetMeshNames(const std::string& fileName); diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 45b90ddbd..810045d92 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -300,6 +300,7 @@ namespace MEDCoupling { bool HasXDR(); std::string MEDFileVersionStr(); + std::string MEDFileVersionOfFileStr(const std::string& fileName) throw(INTERP_KERNEL::Exception); void SetEpsilonForNodeComp(double val) throw(INTERP_KERNEL::Exception); void SetCompPolicyForCell(int val) throw(INTERP_KERNEL::Exception); void SetTooLongStrPolicy(int val) throw(INTERP_KERNEL::Exception); diff --git a/src/MEDLoader/Swig/MEDLoaderTest3.py b/src/MEDLoader/Swig/MEDLoaderTest3.py index 84c6eb84b..93aaf60aa 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest3.py +++ b/src/MEDLoader/Swig/MEDLoaderTest3.py @@ -24,6 +24,7 @@ import unittest import platform from math import pi,e,sqrt from MEDLoaderDataForTest import MEDLoaderDataForTest +from distutils.version import LooseVersion class MEDLoaderTest3(unittest.TestCase): def testMEDMesh1(self): @@ -5751,6 +5752,7 @@ class MEDLoaderTest3(unittest.TestCase): ex=MEDCouplingMappedExtrudedMesh(mesh3D) mm=MEDFileUMesh(ex) mm.write(fname,2) + assert(LooseVersion(MEDFileVersionOfFileStr(fname)).version[:2]==list(MEDFileVersion()[:2])) # checks that MED file version of written mesh is thoose of the current MED file lib ex2=mm.convertToExtrudedMesh() mm2=MEDFileMesh.New(fname) ex3=mm2.convertToExtrudedMesh() @@ -5758,6 +5760,18 @@ class MEDLoaderTest3(unittest.TestCase): self.assertTrue(ex.isEqual(ex3,1e-12)) pass + @unittest.skipUnless(LooseVersion(MEDFileVersionStr())>=LooseVersion('3.2.1'),"This test requires at least MEDFile version 3.2.1") + def testWriteInto30(self): + fname="Pyfile108.med" + m=MEDCouplingUMesh("mesh",1) ; m.setCoords(DataArrayDouble([0,0,1,1],2,2)) ; m.allocateCells() ; m.insertNextCell(NORM_SEG2,[1,0]) + mm=MEDFileUMesh() ; mm[0]=m + mm.setFamilyId("FAMILLE_ZERO",0) + # + mm.write30(fname,2) + assert(LooseVersion(MEDFileVersionOfFileStr(fname)).version[:2]==[3,0]) # checks that just written MED file has a version == 3.0.x + mm2=MEDFileUMesh(fname) + self.assertTrue(mm.isEqual(mm2,1e-12)) + pass pass if __name__ == "__main__":