Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
MshMeshWriter.cc
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2//-----------------------------------------------------------------------------
3// Copyright 2000-2022 CEA (www.cea.fr) IFPEN (www.ifpenergiesnouvelles.com)
4// See the top-level COPYRIGHT file for details.
5// SPDX-License-Identifier: Apache-2.0
6//-----------------------------------------------------------------------------
7/*---------------------------------------------------------------------------*/
8/* MshMeshWriter.cc (C) 2000-2021 */
9/* */
10/* Lecture/Ecriture d'un fichier au format MSH. */
11/*---------------------------------------------------------------------------*/
12/*---------------------------------------------------------------------------*/
13
14#include "arcane/utils/Iostream.h"
15#include "arcane/utils/StdHeader.h"
16#include "arcane/utils/HashTableMap.h"
17#include "arcane/utils/ValueConvert.h"
18#include "arcane/utils/ScopedPtr.h"
19#include "arcane/utils/ITraceMng.h"
20#include "arcane/utils/String.h"
21#include "arcane/utils/IOException.h"
22#include "arcane/utils/Collection.h"
23#include "arcane/utils/Enumerator.h"
24#include "arcane/utils/NotImplementedException.h"
25#include "arcane/utils/Real3.h"
26
27#include "arcane/FactoryService.h"
28#include "arcane/IMainFactory.h"
29#include "arcane/IMeshReader.h"
30#include "arcane/ISubDomain.h"
31#include "arcane/IMesh.h"
32#include "arcane/IMeshSubMeshTransition.h"
33#include "arcane/IItemFamily.h"
34#include "arcane/Item.h"
35#include "arcane/ItemEnumerator.h"
36#include "arcane/VariableTypes.h"
37#include "arcane/IVariableAccessor.h"
38#include "arcane/IParallelMng.h"
39#include "arcane/IIOMng.h"
40#include "arcane/IXmlDocumentHolder.h"
41#include "arcane/XmlNodeList.h"
42#include "arcane/XmlNode.h"
43#include "arcane/IMeshUtilities.h"
44#include "arcane/IMeshWriter.h"
45#include "arcane/BasicService.h"
46#include "arcane/SharedVariable.h"
47
48#include "arcane/AbstractService.h"
49
50/*****************************************************************************\
51* DEFINES *
52* Element types in .msh file format, found in gmsh-2.0.4/Common/GmshDefines.h *
53\*****************************************************************************/
54
55#include "arcane/std/internal/IosGmsh.h"
56
57/*---------------------------------------------------------------------------*/
58/*---------------------------------------------------------------------------*/
59
60namespace Arcane
61{
62
63/*---------------------------------------------------------------------------*/
64/*---------------------------------------------------------------------------*/
69: public AbstractService
70, public IMeshWriter
71{
72 public:
73
75 virtual void build() {}
76 virtual bool writeMeshToFile(IMesh* mesh,const String& file_name);
77
78 private:
79
80 Integer _switchMshType(Integer mshElemType);
81};
82
83/*---------------------------------------------------------------------------*/
84/*---------------------------------------------------------------------------*/
85
86ARCANE_REGISTER_SUB_DOMAIN_FACTORY(MshMeshWriter,IMeshWriter,MshNewMeshWriter);
87
88/*---------------------------------------------------------------------------*/
89/*---------------------------------------------------------------------------*/
90
91/*****************************************************************************\
92* [_switchMshType] *
93\*****************************************************************************/
94/*!/brief Selon le type MSH passé en argument, cette fonction retourne l'Integer
95correspondant type d'ARCANE. Une exception est levée dans le cas d'une inadéquation.
96\param msh_type Type MSH proposé au décodage
97\retrun Le type ARCANE trouvé en correspondance
98*/
100 switch (msh_type){
101// case (IT_NullType): return MSH_LIN_2; //case (0) is not used
102 case (IT_Vertex): return MSH_PNT; //printf("1-node point");
103 case (IT_Line2): return MSH_LIN_2; //printf("2-node line");
104 case (IT_Triangle3): return MSH_TRI_3; //printf("3-node triangle");
105 case (IT_Quad4): return MSH_QUA_4; //printf("4-node quadrangle");
106 case (IT_Tetraedron4): return MSH_TET_4; //printf("4-node tetrahedron");
107 case (IT_Hexaedron8): return MSH_HEX_8; //printf("8-node hexahedron");
108 case (IT_Pentaedron6): return MSH_PRI_6; //printf("6-node prism");
109 case (IT_Pyramid5): return MSH_PYR_5; //printf("5-node pyramid");
110 // Beneath, are some meshes that have been tried to match gmsh's ones
111 // Other 5-nodes
112 case (IT_Pentagon5): return MSH_PYR_5; // Could use a tag to encode these
113 case (IT_HemiHexa5): return MSH_PYR_5;
114 case (IT_DiTetra5): return MSH_PYR_5;
115 // Other 6-nodes
116 case (IT_Hexagon6): return MSH_PRI_6;
117 case (IT_HemiHexa6): return MSH_PRI_6;
118 case (IT_AntiWedgeLeft6): return MSH_PRI_6;
119 case (IT_AntiWedgeRight6): return MSH_PRI_6;
120 // Other 10-nodes
121 case (IT_Heptaedron10): return MSH_TRI_10;
122 // Other 12-nodes
123 case (IT_Octaedron12): return MSH_TRI_12;
124 // Other ?-nodes, have to work with another field (nNodes)
125 case (IT_HemiHexa7): return IT_NullType; // This IT_NullType will be associated with its number of nodes
126 // Others ar still considered as default, rising an exception
127 case (IT_DualNode):
128 case (IT_DualEdge):
129 case (IT_DualFace):
130 case (IT_DualCell):
131 default:
132 info() << "_switchMshType Non supporté (" << msh_type << ")";
133 throw IOException("_switchMshType Non supporté");
134 }// Not found: IT_Pentagon5, IT_Hexagon6, IT_Octaedron12
135 info() << "_switchMshType non switché (" << msh_type << ")";
136 throw IOException("_switchMshType non switché");
137 return 0;
138}
139
140
141
142/**********************************************************************\
143* [writeMeshToFile] *
144\**********************************************************************/
170{
172 std::ofstream ofile(mshFileName.localstr());
173 if (!ofile)
174 throw IOException("VtkMeshIOService::writeMeshToFile(): Unable to open file");
175
176 info() << "[writNodes=" << mesh->nbNode() << " nCells="<< mesh->nbCell();
177
178 ofile << "$MeshFormat\n";
179 ofile << "2.0 0 " << (int) sizeof(double) << "\n";
180 ofile << "$EndMeshFormat\n";
181 ofile << "$Nodes\n";
182 ofile << mesh->nbNode() << "\n";
183
185 ItemGroup all_nodes = mesh->allNodes();
186
188 Node node = *inode;
190 double vtkXyz[3];
191
195 ofile << node.uniqueId() << " " << vtkXyz[0] << " " << vtkXyz[1] << " " <<vtkXyz[2] << "\n";
196 //info() << "[writeMeshToFile] Adding node[" << node.uniqueId().asInt64() << "]";
197 }
198
199 ofile << "$EndNodes\n";
200 ofile << "$Elements\n";
201 ofile << mesh->nbCell() << "\n";
202
203 // Scanning the cells' nodes to get type and connectivity
204 // elm-number elm-type number-of-tags < tag > ... node-number-list
206 Cell cell = *iCell;
207 int nbNodes = cell.nbNode();
208 ofile << cell.uniqueId().asInt64() << " " << _switchMshType(cell.type());
209 if (_switchMshType(cell.type()) == IT_NullType)
210 ofile << " 1 " << nbNodes << " ";
211 else
212 ofile << " 0 ";
213 for(Integer j=0; j<nbNodes;++j)
214 ofile << cell.node(j).uniqueId().asInt64() << " ";
215 ofile << "\n";
216 }
217
218 ofile << "$EndElements\n";
219 return false;
220}
221
222/*---------------------------------------------------------------------------*/
223/*---------------------------------------------------------------------------*/
224
225} // End namespace Arcane
226
227/*---------------------------------------------------------------------------*/
228/*---------------------------------------------------------------------------*/
#define ENUMERATE_CELL(name, group)
Enumérateur générique d'un groupe de mailles.
#define ENUMERATE_NODE(name, group)
Enumérateur générique d'un groupe de noeuds.
#define ARCANE_REGISTER_SUB_DOMAIN_FACTORY(aclass, ainterface, aname)
Enregistre un service de fabrique pour la classe aclass.
Classe de base d'un service.
Maille d'un maillage.
Definition Item.h:1178
virtual Integer nbCell()=0
Nombre de mailles du maillage.
virtual Integer nbNode()=0
Nombre de noeuds du maillage.
virtual NodeGroup allNodes()=0
Groupe de tous les noeuds.
virtual CellGroup allCells()=0
Groupe de toutes les mailles.
Interface d'un service d'écriture d'un maillage.
Definition IMeshWriter.h:36
virtual SharedVariableNodeReal3 sharedNodesCoordinates()=0
Coordonnées des noeuds.
Exception lorsqu'une erreur d'entrée/sortie est détectée.
Definition IOException.h:32
Groupe d'entités de maillage.
Definition ItemGroup.h:49
Node node(Int32 i) const
i-ème noeud de l'entité
Definition Item.h:768
Int32 nbNode() const
Nombre de noeuds de l'entité
Definition Item.h:765
ItemUniqueId uniqueId() const
Identifiant unique sur tous les domaines.
Definition Item.h:216
Int16 type() const
Type de l'entité
Definition Item.h:232
Lecteur des fichiers de maillage via la bibliothèque LIMA.
Definition Lima.cc:120
Ecriture des fichiers de maillage aux format msh.
Integer _switchMshType(Integer mshElemType)
virtual void build()
Construction de niveau build du service.
virtual bool writeMeshToFile(IMesh *mesh, const String &file_name)
writeMeshToFile écrit au format gmsh tel que spécifié ci-dessous:
Noeud d'un maillage.
Definition Dom.h:204
Classe gérant un vecteur de réel de dimension 3.
Definition Real3.h:132
Structure contenant les informations pour créer un service.
Chaîne de caractères unicode.
TraceMessage info() const
Flot pour un message d'information.
double toDouble(Real r)
Converti un Real en double.
Definition Convert.h:40
-*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-