From: ageay Date: Tue, 3 Dec 2013 09:23:25 +0000 (+0000) Subject: New SauvWriter option for copy group lying on a single family X-Git-Tag: V7_3_1b1~32 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=2cc71da34294e89574d39c0ab108fd3c59cffb23;p=tools%2Fmedcoupling.git New SauvWriter option for copy group lying on a single family --- diff --git a/src/MEDLoader/SauvWriter.cxx b/src/MEDLoader/SauvWriter.cxx index 697847bb6..6478c2bbe 100644 --- a/src/MEDLoader/SauvWriter.cxx +++ b/src/MEDLoader/SauvWriter.cxx @@ -212,11 +212,9 @@ namespace } } -//================================================================================ -/*! - * \brief Creates SauvWriter - */ -//================================================================================ +SauvWriter::SauvWriter():_cpy_grp_if_on_single_family(false) +{ +} SauvWriter* SauvWriter::New() { @@ -233,6 +231,16 @@ std::vector SauvWriter::getDirectChildren() const return std::vector(); } +void SauvWriter::setCpyGrpIfOnASingleFamilyStatus(bool status) +{ + _cpy_grp_if_on_single_family=status; +} + +bool SauvWriter::getCpyGrpIfOnASingleFamilyStatus() const +{ + return _cpy_grp_if_on_single_family; +} + //================================================================================ /*! * \brief Fills own DS by MEDFileData @@ -447,7 +455,23 @@ void SauvWriter::fillGroupSubMeshes() if (k != famNames.size()) famSubMeshes.resize(k); SubMesh* grpSubMesh = addSubMesh( groupName, famSubMeshes[0]->_dimRelExt ); - grpSubMesh->_subs.swap( famSubMeshes ); + if(!_cpy_grp_if_on_single_family) + grpSubMesh->_subs.swap( famSubMeshes ); + else + { + /* If a group sub mesh consists of only one family, the group is written as + * a copy of this family. + * A mesh composed of only one submesh may cause an issue with some Gibi operators.*/ + if (famSubMeshes.size() == 1) + { + for(int i = 0; i < famSubMeshes[0]->cellIDsByTypeSize() ; i++) + { + grpSubMesh->_cellIDsByType[i] = famSubMeshes[0]->_cellIDsByType[i]; + } + } + else + grpSubMesh->_subs.swap( famSubMeshes ); + } } } diff --git a/src/MEDLoader/SauvWriter.hxx b/src/MEDLoader/SauvWriter.hxx index d6e732c1c..bbbdce98d 100644 --- a/src/MEDLoader/SauvWriter.hxx +++ b/src/MEDLoader/SauvWriter.hxx @@ -46,11 +46,13 @@ namespace ParaMEDMEM class SauvWriter : public ParaMEDMEM::RefCountObject { public: - MEDLOADER_EXPORT static SauvWriter * New(); + MEDLOADER_EXPORT static SauvWriter *New(); MEDLOADER_EXPORT void setMEDFileDS(const MEDFileData* medData, unsigned meshIndex = 0); MEDLOADER_EXPORT void write(const char* fileName); - + MEDLOADER_EXPORT void setCpyGrpIfOnASingleFamilyStatus(bool status); + MEDLOADER_EXPORT bool getCpyGrpIfOnASingleFamilyStatus() const; private: + SauvWriter(); std::size_t getHeapMemorySizeWithoutChildren() const; std::vector getDirectChildren() const; /*! @@ -109,6 +111,7 @@ namespace ParaMEDMEM std::vector _longNames[ LN_NB ]; std::fstream* _sauvFile; + bool _cpy_grp_if_on_single_family; }; } diff --git a/src/MEDLoader/Swig/MEDLoaderCommon.i b/src/MEDLoader/Swig/MEDLoaderCommon.i index 750847a55..cfac2c6e0 100644 --- a/src/MEDLoader/Swig/MEDLoaderCommon.i +++ b/src/MEDLoader/Swig/MEDLoaderCommon.i @@ -2822,6 +2822,8 @@ namespace ParaMEDMEM static SauvWriter * New(); void setMEDFileDS(const MEDFileData* medData, unsigned meshIndex = 0) throw(INTERP_KERNEL::Exception); void write(const char* fileName) throw(INTERP_KERNEL::Exception); + void setCpyGrpIfOnASingleFamilyStatus(bool status) throw(INTERP_KERNEL::Exception); + bool getCpyGrpIfOnASingleFamilyStatus() const throw(INTERP_KERNEL::Exception); }; /////////////// diff --git a/src/MEDLoader/Swig/MEDLoaderDataForTest.py b/src/MEDLoader/Swig/MEDLoaderDataForTest.py index 22e8cfc28..8a0aee21c 100644 --- a/src/MEDLoader/Swig/MEDLoaderDataForTest.py +++ b/src/MEDLoader/Swig/MEDLoaderDataForTest.py @@ -657,6 +657,55 @@ class MEDLoaderDataForTest: # data=MEDFileData() ; data.setMeshes(ms) ; data.setFields(fs) return data + + def buildAMEDFileDataWithGroupOnOneFamilyForSauv(self): + # Coordinates + coords = [0.,0., 0.,1., 1.,1., 1.,0.] + # lvl 0 connectivity + conn2D = [1,2,3,4] + # lvl -1 connectivity + conn1D = [0,1, 1,2, 2,3, 4,1] + # lvl 0 mesh + mesh2D=MEDCouplingUMesh.New() + mesh2D.setMeshDimension(2) + mesh2D.allocateCells(1) + mesh2D.insertNextCell(NORM_QUAD4,4,conn2D) + mesh2D.finishInsertingCells() + # lvl -1 mesh + mesh1D=MEDCouplingUMesh.New() + mesh1D.setMeshDimension(1) + mesh1D.allocateCells(4) + mesh1D.insertNextCell(NORM_SEG2,2,conn1D[0:2]) + mesh1D.insertNextCell(NORM_SEG2,2,conn1D[2:4]) + mesh1D.insertNextCell(NORM_SEG2,2,conn1D[4:6]) + mesh1D.insertNextCell(NORM_SEG2,2,conn1D[6:8]) + mesh1D.finishInsertingCells() + # assigning coordinates + meshCoords=DataArrayDouble.New() + meshCoords.setValues(coords, 4, 2) + mesh2D.setCoords(meshCoords) + mesh1D.setCoords(meshCoords) + # Creating a multi level mesh + mm = MEDFileUMesh.New() + mm.setMeshAtLevel(0, mesh2D) + mm.setMeshAtLevel(-1, mesh1D) + mm.setName("carre") + # Creating groups + # Creating a group with an element on level -1 + grp0_LM1 = DataArrayInt.New([0]) + grp0_LM1.setName("grp0_LM1") + # Creating a group with all elements on level -1 + grp1_LM1 = DataArrayInt.New([0,1,2,3]) + grp1_LM1.setName("grp1_LM1") + # + mm.setGroupsAtLevel(-1,[grp0_LM1,grp1_LM1]) + # + ms=MEDFileMeshes.New() + ms.setMeshAtPos(0,mm) + mfd=MEDFileData.New() + mfd.setMeshes(ms) + # + return mfd build1DMesh_1=classmethod(build1DMesh_1) build2DCurveMesh_1=classmethod(build2DCurveMesh_1) @@ -675,4 +724,5 @@ class MEDLoaderDataForTest: buildVecFieldOnGauss_2_Simpler=classmethod(buildVecFieldOnGauss_2_Simpler) buildVecFieldOnGaussNE_1=classmethod(buildVecFieldOnGaussNE_1) buildACompleteMEDDataStructureWithFieldsOnCells_1=classmethod(buildACompleteMEDDataStructureWithFieldsOnCells_1) + buildAMEDFileDataWithGroupOnOneFamilyForSauv=classmethod(buildAMEDFileDataWithGroupOnOneFamilyForSauv) pass diff --git a/src/MEDLoader/Swig/MEDLoaderTest.py b/src/MEDLoader/Swig/MEDLoaderTest.py index 344023135..f04d53d95 100644 --- a/src/MEDLoader/Swig/MEDLoaderTest.py +++ b/src/MEDLoader/Swig/MEDLoaderTest.py @@ -658,6 +658,15 @@ class MEDLoaderTest(unittest.TestCase): self.assertTrue(m0.isEqual(mRead,1e-12)) pass + def testChangeGroupName(self): + """ This test is a non regression test on MEDFileUMesh.changeGroupName thanks to Alliance. + """ + mfd=MEDLoaderDataForTest.buildAMEDFileDataWithGroupOnOneFamilyForSauv() + mesh = mfd.getMeshes().getMeshAtPos(0) + mesh.changeGroupName("grp0_LM1", "xonall1") + self.assertTrue("xonall1" in mesh.getGroupsNames()) + pass + pass unittest.main() diff --git a/src/MEDLoader/Swig/SauvLoaderTest.py b/src/MEDLoader/Swig/SauvLoaderTest.py index 0da6104cd..8cd667ffd 100644 --- a/src/MEDLoader/Swig/SauvLoaderTest.py +++ b/src/MEDLoader/Swig/SauvLoaderTest.py @@ -321,9 +321,70 @@ class SauvLoaderTest(unittest.TestCase): self.assertTrue( f2.isEqual( f, 1e-12, 1e-12 )) del sr os.remove( sauvFile ) + pass + def testSauvWriterGroupWithOneFamily(self): + """ + This test checks an option for sauv writing. It is requested here to copy a group from a family if a group is lying on a single family. + """ + import re + mfd=MEDLoaderDataForTest.buildAMEDFileDataWithGroupOnOneFamilyForSauv() + sauvFile = "mesh.sauv" + sw=SauvWriter.New() + sw.setMEDFileDS(mfd) + self.assertTrue(not sw.getCpyGrpIfOnASingleFamilyStatus()) + sw.setCpyGrpIfOnASingleFamilyStatus(True) + self.assertTrue(sw.getCpyGrpIfOnASingleFamilyStatus()) + sw.write(sauvFile) + + f = open(sauvFile) + # String pattern for the header of the sub meshes record ("PILE" number, number of named objects, number of objects) + pattern_pile= re.compile(r'\sPILE\sNUMERO\s+(?P[0-9]+)NBRE\sOBJETS\sNOMMES\s+(?P[0-9]+)NBRE\sOBJETS\s+(?P[0-9]+)') + # String pattern for a sub mesh header (cell type, number of components and three numbers) + pattern_header=re.compile(r'\s+(?P[0-9]+)\s+(?P[0-9]+)\s+[0-9]+\s+[0-9]+\s+[0-9]+') + + nbobjects=0 + line = f.readline() + while(line): + match_pile = pattern_pile.match(line) + if match_pile: + number=int(match_pile.group("number")) + if number == 1: + nbnamed=int(match_pile.group("nbnamed")) + nbobjects=int(match_pile.group("nbobjects")) + break + pass + line=f.readline() + pass + + # Skipping the objects names + f.readline() + # Skipping the objects ids + f.readline() + # Looking for each sub-mesh header + line = f.readline() + cur_object=0 + while(line and cur_object < nbobjects): + match_header=pattern_header.match(line) + if match_header: + cell_type=int(match_header.group("type")) + nb_subs=int(match_header.group("nbsubs")) + # Looking for a compound object + if cell_type == 0: + # Testing if there is only one component + self.assertTrue(nb_subs > 1) + else: + f.readline() + f.readline() + cur_object = cur_object + 1 + pass + pass + line=f.readline() + pass + os.remove(sauvFile) pass + pass unittest.main()