Salome HOME
beeced2361d41cda08edd7022e561542348966e0
[modules/smesh.git] / src / SMESH / SMESH_DriverMesh.cxx
1 // Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 //  File   : SMESH_DriverMesh.cxx
24 //  Author : Yoann AUDOUIN, EDF
25 //  Module : SMESH
26 //
27
28 #include "utilities.h"
29
30 #include "SMESH_DriverMesh.hxx"
31
32 #include "SMESH_Mesh.hxx"
33 #include "SMESH_Gen.hxx"
34
35 #include <MEDFileMesh.hxx>
36 #include <MEDCouplingUMesh.hxx>
37
38 using namespace MEDCoupling;
39
40 /**
41  * @brief Compares the mesh from two mesh files (MED)
42  *
43  * @param mesh_file1 First file
44  * @param mesh_file2 Second file
45  * @param mesh_name Name of the mesh in the files
46  *
47  * @return true if the mesh within the files are identical
48  */
49 bool SMESH_DriverMesh::diffMEDFile(const std::string mesh_file1, const std::string mesh_file2, const std::string mesh_name){
50   MEDFileUMesh* medmesh1 = MEDFileUMesh::New(mesh_file1, mesh_name);
51   MEDFileUMesh* medmesh2 = MEDFileUMesh::New(mesh_file2, mesh_name);
52   MEDCouplingUMesh *m0_1=medmesh1->getMeshAtLevel(0,false);
53   MEDCouplingUMesh *m0_2=medmesh2->getMeshAtLevel(0,false);
54   return m0_1->isEqual(m0_2, 1e-12);
55 }
56
57 std::string getMeshName(std::string mesh_file){
58   // TODO: Memory leak but desctructor private check with AG
59   MEDFileUMesh * myMedMesh = MEDFileUMesh::New(mesh_file);
60
61   return myMedMesh->getLevel0Mesh()->getName();
62 }
63
64 /**
65  * @brief Import a mesh from a mesh file (MED) into a SMESH_Mesh object
66  *
67  * @param mesh_file the file
68  * @param aMesh the object
69  * @param mesh_name the name of the mesh in the file
70  *
71  * @return error code
72  */
73 int SMESH_DriverMesh::importMesh(const std::string mesh_file, SMESH_Mesh& aMesh){
74   // TODO: change that as it depends on the language
75   std::string mesh_name = getMeshName(mesh_file);
76   MESSAGE("Importing mesh from " << mesh_file << " mesh " << mesh_name);
77   int ret = aMesh.MEDToMesh(mesh_file.c_str(), mesh_name.c_str());
78   return ret;
79 }
80
81 /**
82  * @brief Export the content of a SMESH_Mesh into a mesh file (MED)
83  *
84  * @param mesh_file the file
85  * @param aMesh the object
86  * @param mesh_name name of the mesh in the file
87  *
88  * @return error code
89  */
90  
91 int SMESH_DriverMesh::exportMesh(const std::string mesh_file, SMESH_Mesh& aMesh, const std::string mesh_name){
92
93   MESSAGE("Exporting mesh to " << mesh_file);
94   aMesh.ExportMED(mesh_file.c_str(), // theFile
95                   mesh_name.c_str(), // theMeshName
96                   false, // theAutoGroups
97                   -1, // theVersion
98                   nullptr, // theMeshPart
99                   true, // theAutoDimension
100                   true, // theAddODOnVertices
101                   1e-8, // theZTolerance
102                   true // theSaveNumbers
103                   );
104   return true;
105 }