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