Salome HOME
e087bf63bb8695731382e7c7e4fdd1923100f798
[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 using namespace std;
28 #include "DriverDAT_W_SMDS_Mesh.h"
29
30 #include "SMDS_MeshElement.hxx"
31 #include "SMDS_MeshNode.hxx"
32 #include "SMDS_MeshEdgesIterator.hxx"
33 #include "SMDS_MeshFacesIterator.hxx"
34 #include "SMDS_MeshNodesIterator.hxx"
35 #include "SMDS_MeshVolumesIterator.hxx"
36
37 #include "utilities.h"
38
39 DriverDAT_W_SMDS_Mesh::DriverDAT_W_SMDS_Mesh() {
40 ;
41 }
42
43 DriverDAT_W_SMDS_Mesh::~DriverDAT_W_SMDS_Mesh() {
44 ;
45 }
46
47 void DriverDAT_W_SMDS_Mesh::SetMesh(Handle(SMDS_Mesh)& aMesh) {
48   myMesh = aMesh;
49 }
50
51 void DriverDAT_W_SMDS_Mesh::SetFile(string aFile) {
52   myFile = aFile;
53 }
54
55 void DriverDAT_W_SMDS_Mesh::SetFileId(FILE* aFileId) {
56   myFileId = aFileId;
57 }
58
59 void DriverDAT_W_SMDS_Mesh::SetMeshId(int aMeshId) {
60   myMeshId = aMeshId;
61 }
62
63 void DriverDAT_W_SMDS_Mesh::Add() {
64   ;
65 }
66
67 void DriverDAT_W_SMDS_Mesh::Write() {
68
69   int nbNodes,nbCells;
70   int i;
71
72   char* file2Read = (char*)myFile.c_str();
73   myFileId = fopen(file2Read,"w+");
74   if (myFileId < 0)
75     {
76       fprintf(stderr,">> ERREUR : ouverture du fichier %s \n",file2Read);
77       exit(EXIT_FAILURE);
78     }
79   SCRUTE(myMesh);
80   /****************************************************************************
81   *                       NOMBRES D'OBJETS                                    *
82   ****************************************************************************/
83   fprintf(stdout,"\n(****************************)\n");
84   fprintf(stdout,"(* INFORMATIONS GENERALES : *)\n");
85   fprintf(stdout,"(****************************)\n");
86
87   /* Combien de noeuds ? */
88   nbNodes = myMesh->NbNodes();
89
90   /* Combien de mailles, faces ou aretes ? */
91   Standard_Integer nb_of_nodes, nb_of_edges,nb_of_faces, nb_of_volumes;
92   nb_of_edges = myMesh->NbEdges();
93   nb_of_faces = myMesh->NbFaces();
94   nb_of_volumes = myMesh->NbVolumes();
95   nbCells = nb_of_edges + nb_of_faces + nb_of_volumes;
96   SCRUTE(nb_of_edges);
97   SCRUTE(nb_of_faces);
98   SCRUTE(nb_of_volumes);
99
100   fprintf(stdout,"%d %d\n",nbNodes,nbCells);
101   fprintf(myFileId,"%d %d\n",nbNodes,nbCells);
102
103   /****************************************************************************
104   *                       ECRITURE DES NOEUDS                                 *
105   ****************************************************************************/
106   fprintf(stdout,"\n(************************)\n");
107   fprintf(stdout,"(* NOEUDS DU MAILLAGE : *)\n");
108   fprintf(stdout,"(************************)\n");
109
110   SMDS_MeshNodesIterator itNodes(myMesh);
111   for (;itNodes.More();itNodes.Next()) {
112     const Handle(SMDS_MeshElement)& elem = itNodes.Value();
113     const Handle(SMDS_MeshNode)& node = myMesh->GetNode(1,elem);
114  
115     fprintf(myFileId,"%d %e %e %e\n",node->GetID(),node->X(),node->Y(),node->Z());
116   }
117
118   /****************************************************************************
119   *                       ECRITURE DES ELEMENTS                                *
120   ****************************************************************************/
121   fprintf(stdout,"\n(**************************)\n");
122   fprintf(stdout,"(* ELEMENTS DU MAILLAGE : *)\n");
123   fprintf(stdout,"(**************************)");
124   /* Ecriture des connectivites, noms, numeros des mailles */
125
126   SMDS_MeshEdgesIterator itEdges(myMesh);
127   for (;itEdges.More();itEdges.Next()) {
128     const Handle(SMDS_MeshElement)& elem = itEdges.Value();
129
130     switch (elem->NbNodes()) {
131     case 2 : {
132       fprintf(myFileId,"%d %d ",elem->GetID(),102);
133       break;
134     } 
135     case 3 : {
136       fprintf(myFileId,"%d %d ",elem->GetID(),103);
137       break;
138     }
139     }
140
141     for (i=0;i<elem->NbNodes();i++)
142       fprintf(myFileId,"%d ",elem->GetConnection(i+1));
143     
144     fprintf(myFileId,"\n");
145   }
146
147   SMDS_MeshFacesIterator itFaces(myMesh);
148   for (;itFaces.More();itFaces.Next()) {
149     const Handle(SMDS_MeshElement)& elem = itFaces.Value();
150
151     switch (elem->NbNodes()) {
152     case 3 : {
153       fprintf(myFileId,"%d %d ",elem->GetID(),203);
154       break;
155     }
156     case 4 : {
157       fprintf(myFileId,"%d %d ",elem->GetID(),204);
158       break;
159     }
160     case 6 : {
161       fprintf(myFileId,"%d %d ",elem->GetID(),206);
162       break;
163     }
164     }
165
166     for (i=0;i<elem->NbNodes();i++)
167       fprintf(myFileId,"%d ",elem->GetConnection(i+1));
168     
169     fprintf(myFileId,"\n");
170   }
171
172   SMDS_MeshVolumesIterator itVolumes(myMesh);
173   for (;itVolumes.More();itVolumes.Next()) {
174     const Handle(SMDS_MeshElement)& elem = itVolumes.Value();
175
176     switch (elem->NbNodes()) {
177     case 8 : {
178       fprintf(myFileId,"%d %d ",elem->GetID(),308);
179       break;
180     }
181     }
182
183     for (i=0;i<elem->NbNodes();i++)
184       fprintf(myFileId,"%d ",elem->GetConnection(i+1));
185     
186     fprintf(myFileId,"\n");
187   }
188
189   fclose (myFileId);
190 }