Salome HOME
Removing hard coded meshname from Mesh import
[modules/smesh.git] / src / SMESH / SMESH_DriverMesh.cxx
1 // Copyright (C) 2007-2021  CEA/DEN, EDF R&D, 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 "SMESH_DriverMesh.hxx"
29
30 #include "SMESH_Mesh.hxx"
31 #include "SMESH_Gen.hxx"
32
33 #include <MEDFileMesh.hxx>
34 #include <MEDCouplingUMesh.hxx>
35
36 using namespace MEDCoupling;
37
38 /**
39  * @brief Compares the mesh from two mesh files (MED)
40  *
41  * @param mesh_file1 First file
42  * @param mesh_file2 Second file
43  * @param mesh_name Name of the mesh in the files
44  *
45  * @return true if the mesh within the files are identical
46  */
47 bool diffMEDFile(const std::string mesh_file1, const std::string mesh_file2, const std::string mesh_name){
48   MEDFileUMesh* medmesh1 = MEDFileUMesh::New(mesh_file1, mesh_name);
49   MEDFileUMesh* medmesh2 = MEDFileUMesh::New(mesh_file2, mesh_name);
50   MEDCouplingUMesh *m0_1=medmesh1->getMeshAtLevel(0,false);
51   MEDCouplingUMesh *m0_2=medmesh2->getMeshAtLevel(0,false);
52   return m0_1->isEqual(m0_2, 1e-12);
53 }
54
55 std::string getMeshName(std::string mesh_file){
56   std::unique_ptr<MEDFileUMesh> myMedMesh=MEDFileUMesh::New(mesh_file);
57
58   return myMedMesh->getLevel0Mesh()->getName();
59 }
60
61 /**
62  * @brief Import a mesh from a mesh file (MED) into a SMESH_Mesh object
63  *
64  * @param mesh_file the file
65  * @param aMesh the object
66  * @param mesh_name the name of the mesh in the file
67  *
68  * @return error code
69  */
70 int importMesh(const std::string mesh_file, SMESH_Mesh& aMesh){
71   // TODO: change that as it depends on the language
72   std::string mesh_name = getMeshName(mesh_file);
73   std::cout << "Importing mesh from " << mesh_file << " mesh " << mesh_name2 << std::endl;
74   int ret = aMesh.MEDToMesh(mesh_file.c_str(), mesh_name.c_str());
75   return ret;
76 }
77
78 /**
79  * @brief Export the content of a SMESH_Mesh into a mesh file (MED)
80  *
81  * @param mesh_file the file
82  * @param aMesh the object
83  * @param mesh_name name of the mesh in the file
84  *
85  * @return error code
86  */
87 int exportMesh(const std::string mesh_file, SMESH_Mesh& aMesh, const std::string mesh_name){
88
89   // TODO: See how to get the name of the mesh. Is it usefull ?
90   std::cout << "Exporting mesh to " << mesh_file << std::endl;
91   aMesh.ExportMED(mesh_file.c_str(), // theFile
92                   mesh_name.c_str(), // theMeshName
93                   false, // theAutoGroups
94                   -1, // theVersion
95                   nullptr, // theMeshPart
96                   true, // theAutoDimension
97                   true, // theAddODOnVertices
98                   1e-8, // theZTolerance
99                   true // theSaveNumbers
100                   );
101   return true;
102 }