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