From: karadaniz Date: Wed, 22 May 2024 11:46:54 +0000 (+0200) Subject: [#spns_40523] Ignore operation on field if no field exists X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=dff8f4093d00165dc95904c06476dbad66e2be08;p=tools%2Fmedcoupling.git [#spns_40523] Ignore operation on field if no field exists --- diff --git a/src/MEDLoader/MeshFormatWriter.cxx b/src/MEDLoader/MeshFormatWriter.cxx index a7c679d52..439cfa730 100644 --- a/src/MEDLoader/MeshFormatWriter.cxx +++ b/src/MEDLoader/MeshFormatWriter.cxx @@ -74,13 +74,15 @@ void MeshFormatWriter::setMEDFileDS(MEDCoupling::MEDFileData* mfd) _mesh->incrRef(); MEDCoupling::MEDFileFields* fields = mfd->getFields(); + if (fields) + { for (int i = 0; igetNumberOfFields(); i++ ) { MEDCoupling::MEDFileAnyTypeFieldMultiTS* field = fields->getFieldAtPos(i); MEDCoupling::MEDFileFieldMultiTS * f = dynamic_cast(field); _fields.push_back(f); } - + } } diff --git a/src/MEDLoader/Swig/MEDWriterTest1.py b/src/MEDLoader/Swig/MEDWriterTest1.py new file mode 100644 index 000000000..807c0c87c --- /dev/null +++ b/src/MEDLoader/Swig/MEDWriterTest1.py @@ -0,0 +1,116 @@ +# -*- coding: iso-8859-1 -*- +# Copyright (C) 2007-2024 CEA, EDF +# +# This library is free software; you can redistribute it and/or +# modify it under the terms of the GNU Lesser General Public +# License as published by the Free Software Foundation; either +# version 2.1 of the License, or (at your option) any later version. +# +# This library is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Lesser General Public License for more details. +# +# You should have received a copy of the GNU Lesser General Public +# License along with this library; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +# See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com + +from MEDLoader import * +import unittest +from MEDLoaderDataForTest import MEDLoaderDataForTest,WriteInTmpDir + + +class MEDWriterTest1(unittest.TestCase): + @WriteInTmpDir + def testMesh1D(self): + # setup + mesh=MEDLoaderDataForTest.build1DMesh_1(); + mesh.checkConsistencyLight(); + meshMEDFile = MEDFileUMesh() + meshMEDFile.setMeshAtLevel(0, mesh) + # create groups + grp0 = DataArrayInt([0, 1]) + grp0.setName("grp0") + grp1 = DataArrayInt([0, 1, 2]) + grp1.setName("grp1") + meshMEDFile.setGroupsAtLevel(0,[grp0, grp1]) + # write mesh file + ms=MEDFileMeshes() + ms.pushMesh(meshMEDFile) + medFileData = MEDFileData() + medFileData.setMeshes(ms) + tmpMeshWriter = MeshFormatWriter() + tmpMeshWriter.setMeshFileName("Meshfile") + tmpMeshWriter.setMEDFileDS(medFileData) + meshMEDFile.write("/tmp/Mesh1D.med",2) + # Read MEDFile and test equality + meshMEDFileRead = MEDFileMesh.New("/tmp/Mesh1D.med") + # assert groups + self.assertEqual(meshMEDFileRead.getGroupArr(0,"grp0").getValues(), [0, 1]) + self.assertEqual(meshMEDFileRead.getGroupArr(0,"grp1").getValues(), [0, 1, 2]) + +class MEDWriterTest2(unittest.TestCase): + @WriteInTmpDir + def testMesh2D(self): + # setup + mesh=MEDLoaderDataForTest.build2DCurveMesh_1(); + mesh.checkConsistencyLight(); + meshMEDFile = MEDFileUMesh() + meshMEDFile.setMeshAtLevel(0, mesh) + # create groups + grp0 = DataArrayInt([0, 3]) + grp0.setName("grp0") + grp1 = DataArrayInt([0, 1, 3, 2]) + grp1.setName("grp1") + meshMEDFile.setGroupsAtLevel(0,[grp0, grp1]) + # write mesh file + ms=MEDFileMeshes() + ms.pushMesh(meshMEDFile) + medFileData = MEDFileData() + medFileData.setMeshes(ms) + tmpMeshWriter = MeshFormatWriter() + tmpMeshWriter.setMeshFileName("Meshfile") + tmpMeshWriter.setMEDFileDS(medFileData) + meshMEDFile.write("/tmp/Mesh2D.med",2) + # Read MEDFile and test equality + meshMEDFileRead = MEDFileMesh.New("/tmp/Mesh2D.med") + # assert groups + self.assertEqual(meshMEDFileRead.getGroupArr(0,"grp0").getValues(), [0, 3]) + self.assertEqual(meshMEDFileRead.getGroupArr(0,"grp1").getValues(), [0, 1, 2, 3]) + +class MEDWriterTest3(unittest.TestCase): + @WriteInTmpDir + def testMesh3D(self): + # setup + mesh=MEDLoaderDataForTest.build3DMesh_1(); + mesh.checkConsistencyLight(); + meshMEDFile = MEDFileUMesh() + meshMEDFile.setMeshAtLevel(0, mesh) + # create groups + grp0 = DataArrayInt([0, 1]) + grp0.setName("grp0") + grp1 = DataArrayInt([0, 5, 7, 8]) + grp1.setName("grp1") + meshMEDFile.setGroupsAtLevel(0,[grp0, grp1]) + # write mesh file + ms=MEDFileMeshes() + ms.pushMesh(meshMEDFile) + medFileData = MEDFileData() + medFileData.setMeshes(ms) + tmpMeshWriter = MeshFormatWriter() + tmpMeshWriter.setMeshFileName("Meshfile") + tmpMeshWriter.setMEDFileDS(medFileData) + meshMEDFile.write("/tmp/Mesh3D.med",2) + # Read MEDFile and test equality + meshMEDFileRead = MEDFileMesh.New("/tmp/Mesh3D.med") + # assert groups + self.assertEqual(meshMEDFileRead.getGroupArr(0,"grp0").getValues(), [0, 1]) + self.assertEqual(meshMEDFileRead.getGroupArr(0,"grp1").getValues(), [0, 5, 7, 8]) + + + + +if __name__ == '__main__': + unittest.main() \ No newline at end of file