Salome HOME
Merge br_enable_import_mesh. Enable import mesh and save/load SMESH study.
[modules/smesh.git] / src / DriverDAT / DriverDAT_W_SMDS_Mesh.cxx
1 //  SMESH DriverDAT : driver to read and write 'dat' files
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : DriverDAT_W_SMDS_Mesh.cxx
25 //  Module : SMESH
26
27 #include "DriverDAT_W_SMDS_Mesh.h"
28 #include "SMDS_MeshElement.hxx"
29 #include "SMDS_MeshNode.hxx"
30 #include "utilities.h"
31
32 extern "C"
33 {
34
35 /**
36  * Factory function which will be called by SMESHDriver
37  */
38 void * SMESH_createDATMeshWriter()
39 {
40         return new DriverDAT_W_SMDS_Mesh();
41 }
42 }
43
44 DriverDAT_W_SMDS_Mesh::DriverDAT_W_SMDS_Mesh()
45 {
46 }
47
48 DriverDAT_W_SMDS_Mesh::~DriverDAT_W_SMDS_Mesh()
49 {
50         ;
51 }
52
53 void DriverDAT_W_SMDS_Mesh::SetMesh(SMDS_Mesh * aMesh)
54 {
55         myMesh = aMesh;
56 }
57
58 void DriverDAT_W_SMDS_Mesh::SetFile(string aFile)
59 {
60         myFile = aFile;
61 }
62
63 void DriverDAT_W_SMDS_Mesh::SetFileId(FILE * aFileId)
64 {
65         myFileId = aFileId;
66 }
67
68 void DriverDAT_W_SMDS_Mesh::SetMeshId(int aMeshId)
69 {
70         myMeshId = aMeshId;
71 }
72
73 void DriverDAT_W_SMDS_Mesh::Add()
74 {
75         MESSAGE("Adding a mesh to a DAT document. As DAT do not support more than one mesh in a file, the previous mesh is deleted");
76         Write();
77 }
78
79 void DriverDAT_W_SMDS_Mesh::Write()
80 {
81         int nbNodes, nbCells;
82         int i;
83
84         char *file2Read = (char *)myFile.c_str();
85         myFileId = fopen(file2Read, "w+");
86         if (myFileId < 0)
87         {
88                 fprintf(stderr, ">> ERREUR : ouverture du fichier %s \n", file2Read);
89                 exit(EXIT_FAILURE);
90         }
91
92   /****************************************************************************
93   *                       NOMBRES D'OBJETS                                    *
94   ****************************************************************************/
95         fprintf(stdout, "\n(****************************)\n");
96         fprintf(stdout, "(* INFORMATIONS GENERALES : *)\n");
97         fprintf(stdout, "(****************************)\n");
98
99         /* Combien de noeuds ? */
100         nbNodes = myMesh->NbNodes();
101
102         /* Combien de mailles, faces ou aretes ? */
103         int nb_of_nodes, nb_of_edges, nb_of_faces, nb_of_volumes;
104         nb_of_edges = myMesh->NbEdges();
105         nb_of_faces = myMesh->NbFaces();
106         nb_of_volumes = myMesh->NbVolumes();
107         nbCells = nb_of_edges + nb_of_faces + nb_of_volumes;
108
109         fprintf(stdout, "%d %d\n", nbNodes, nbCells);
110         fprintf(myFileId, "%d %d\n", nbNodes, nbCells);
111
112   /****************************************************************************
113   *                       ECRITURE DES NOEUDS                                 *
114   ****************************************************************************/
115         fprintf(stdout, "\n(************************)\n");
116         fprintf(stdout, "(* NOEUDS DU MAILLAGE : *)\n");
117         fprintf(stdout, "(************************)\n");
118
119         SMDS_Iterator<const SMDS_MeshNode *> * itNodes=myMesh->nodesIterator();
120         while(itNodes->more())
121         {               
122                 const SMDS_MeshNode * node = itNodes->next();
123                 fprintf(myFileId, "%d %e %e %e\n", node->GetID(), node->X(),
124                         node->Y(), node->Z());
125         }
126         delete itNodes;
127
128   /****************************************************************************
129   *                       ECRITURE DES ELEMENTS                                *
130   ****************************************************************************/
131         fprintf(stdout, "\n(**************************)\n");
132         fprintf(stdout, "(* ELEMENTS DU MAILLAGE : *)\n");
133         fprintf(stdout, "(**************************)");
134         /* Ecriture des connectivites, noms, numeros des mailles */
135
136         SMDS_Iterator<const SMDS_MeshEdge*> * itEdges=myMesh->edgesIterator();
137         while(itEdges->more())
138         {
139                 const SMDS_MeshEdge * elem = itEdges->next();
140
141                 switch (elem->NbNodes())
142                 {
143                 case 2:
144                 {
145                         fprintf(myFileId, "%d %d ", elem->GetID(), 102);
146                         break;
147                 }
148                 case 3:
149                 {
150                         fprintf(myFileId, "%d %d ", elem->GetID(), 103);
151                         break;
152                 }
153                 }
154
155                 SMDS_Iterator<const SMDS_MeshElement *> * itNodes=elem->nodesIterator();
156                 while(itNodes->more())
157                         fprintf(myFileId, "%d ", itNodes->next()->GetID());
158                 
159                 fprintf(myFileId, "\n");
160         }
161         delete itEdges;
162
163         SMDS_Iterator<const SMDS_MeshFace *> * itFaces=myMesh->facesIterator();
164         while(itFaces->more())
165         {
166                 const SMDS_MeshElement * elem = itFaces->next();
167
168                 switch (elem->NbNodes())
169                 {
170                 case 3:
171                 {
172                         fprintf(myFileId, "%d %d ", elem->GetID(), 203);
173                         break;
174                 }
175                 case 4:
176                 {
177                         fprintf(myFileId, "%d %d ", elem->GetID(), 204);
178                         break;
179                 }
180                 case 6:
181                 {
182                         fprintf(myFileId, "%d %d ", elem->GetID(), 206);
183                         break;
184                 }
185                 }
186
187                 SMDS_Iterator<const SMDS_MeshElement *> * itNodes=elem->nodesIterator();
188                 while(itNodes->more())
189                         fprintf(myFileId, "%d ", itNodes->next()->GetID());
190                 delete itNodes;
191         
192                 fprintf(myFileId, "\n");
193         }
194         delete itFaces;
195
196         SMDS_Iterator<const SMDS_MeshVolume*> * itVolumes=myMesh->volumesIterator();
197         while(itVolumes->more())
198         {
199                 const SMDS_MeshElement * elem = itVolumes->next();
200                 
201                 switch (elem->NbNodes())
202                 {
203                 case 8:
204                 {
205                         fprintf(myFileId, "%d %d ", elem->GetID(), 308);
206                         break;
207                 }
208                 }
209
210                 SMDS_Iterator<const SMDS_MeshElement *> * itNodes=elem->nodesIterator();
211                 while(itNodes->more())
212                         fprintf(myFileId, "%d ", itNodes->next()->GetID());
213                 delete itNodes; 
214
215                 fprintf(myFileId, "\n");
216         }
217         delete itVolumes;
218
219         fclose(myFileId);
220 }