Salome HOME
sources v1.2
[modules/smesh.git] / src / DriverUNV / DriverUNV_W_SMESHDS_Mesh.cxx
1 //  SMESH DriverUNV : driver to read and write 'unv' 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   : DriverUNV_W_SMESHDS_Mesh.cxx
25 //  Module : SMESH
26
27 using namespace std;
28 #include "DriverUNV_W_SMESHDS_Mesh.h"
29 #include "DriverUNV_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 #define sNODE_UNV_ID "  2411"
41 #define sELT_UNV_ID  "  2412"
42 #define sUNV_SEPARATOR "    -1"
43 #define sNODE_UNV_DESCR "%10d         1         1        11\n"
44 #define sELT_SURF_DESC  "%10d        %2d         1         1        11         %1d\n"
45 #define sELT_VOLU_DESC  "%10d        %2d         1         1         9         %1d\n"
46 #define sELT_BEAM_DESC1 "%10d        %2d         1         1         7         %1d\n"
47 #define sELT_BEAM_DESC2 "         0         1         1\n"
48
49 DriverUNV_W_SMESHDS_Mesh::DriverUNV_W_SMESHDS_Mesh() {
50 ;
51 }
52
53 DriverUNV_W_SMESHDS_Mesh::~DriverUNV_W_SMESHDS_Mesh() {
54 ;
55 }
56
57 void DriverUNV_W_SMESHDS_Mesh::SetMesh(Handle(SMDS_Mesh)& aMesh) {
58   //myMesh = Handle(SMESHDS_Mesh)::DownCast(aMesh);
59   myMesh = aMesh;
60 }
61
62 void DriverUNV_W_SMESHDS_Mesh::SetFile(string aFile) {
63   myFile = aFile;
64 }
65
66 void DriverUNV_W_SMESHDS_Mesh::SetFileId(FILE* aFileId) {
67   myFileId = aFileId;
68 }
69
70 void DriverUNV_W_SMESHDS_Mesh::SetMeshId(int aMeshId) {
71   myMeshId = aMeshId;
72 }
73
74 void DriverUNV_W_SMESHDS_Mesh::Write() {
75
76   string myClass = string("SMDS_Mesh");
77   string myExtension = string("UNV");
78
79   DriverUNV_W_SMDS_Mesh* myWriter = new DriverUNV_W_SMDS_Mesh;
80
81   myWriter->SetMesh(myMesh);
82   myWriter->SetFile(myFile);
83   myWriter->SetMeshId(myMeshId);
84   //myWriter->SetFileId(myFileId);
85
86   myWriter->Write();
87
88
89 }
90 void DriverUNV_W_SMESHDS_Mesh::Add() {
91   int nbNodes,nbCells;
92   int i;
93
94   char* file2Read = (char*)myFile.c_str();
95   myFileId = fopen(file2Read,"w+");
96   if (myFileId < 0)
97     {
98       fprintf(stderr,">> ERREUR : ouverture du fichier %s \n",file2Read);
99       exit(EXIT_FAILURE);
100     }
101   
102   /****************************************************************************
103   *                       NOMBRES D'OBJETS                                    *
104   ****************************************************************************/
105   fprintf(stdout,"\n(****************************)\n");
106   fprintf(stdout,"(* INFORMATIONS GENERALES : *)\n");
107   fprintf(stdout,"(****************************)\n");
108
109   /* Combien de noeuds ? */
110   nbNodes = myMesh->NbNodes();
111
112   /* Combien de mailles, faces ou aretes ? */
113   Standard_Integer nb_of_nodes, nb_of_edges,nb_of_faces, nb_of_volumes;
114   nb_of_edges = myMesh->NbEdges();
115   nb_of_faces = myMesh->NbFaces();
116   nb_of_volumes = myMesh->NbVolumes();
117   nbCells = nb_of_edges + nb_of_faces + nb_of_volumes;
118
119   fprintf(stdout,"%d %d\n",nbNodes,nbCells);
120 //fprintf(myFileId,"%d %d\n",nbNodes,nbCells);
121
122   /****************************************************************************
123   *                       ECRITURE DES NOEUDS                                 *
124   ****************************************************************************/
125   fprintf(stdout,"\n(************************)\n");
126   fprintf(stdout,"(* NOEUDS DU MAILLAGE : *)\n");
127   fprintf(stdout,"(************************)\n");
128
129   SMDS_MeshNodesIterator itNodes(myMesh);
130
131   fprintf(myFileId,"%s\n", sUNV_SEPARATOR);
132   fprintf(myFileId,"%s\n", sNODE_UNV_ID  );
133
134   for (;itNodes.More();itNodes.Next()) {
135     const Handle(SMDS_MeshElement)& elem = itNodes.Value();
136     const Handle(SMDS_MeshNode   )& node = myMesh->GetNode(1, elem);
137
138     fprintf(myFileId, sNODE_UNV_DESCR, node->GetID());
139     fprintf(myFileId, "%25.16E%25.16E%25.16E\n", node->X(), node->Y(), node->Z());
140   }
141   fprintf(myFileId,"%s\n", sUNV_SEPARATOR);
142
143   /****************************************************************************
144   *                       ECRITURE DES ELEMENTS                                *
145   ****************************************************************************/
146   fprintf(stdout,"\n(**************************)\n");
147   fprintf(stdout,"(* ELEMENTS DU MAILLAGE : *)\n");
148   fprintf(stdout,"(**************************)");
149   /* Ecriture des connectivites, noms, numeros des mailles */
150
151   fprintf(myFileId,"%s\n", sUNV_SEPARATOR);
152   fprintf(myFileId,"%s\n", sELT_UNV_ID   );
153
154   SMDS_MeshEdgesIterator itEdges(myMesh);
155   for (;itEdges.More();itEdges.Next()) {
156     const Handle(SMDS_MeshElement)& elem = itEdges.Value();
157
158     switch (elem->NbNodes()) {
159       case 2 : {
160         fprintf(myFileId, sELT_BEAM_DESC1, elem->GetID(), 21, elem->NbNodes());
161         fprintf(myFileId, sELT_BEAM_DESC2);
162         fprintf(myFileId, "%10d%10d\n", elem->GetConnection(1), elem->GetConnection(2));
163         break;
164       }
165       case 3 : {
166         fprintf(myFileId, sELT_BEAM_DESC1, elem->GetID(), 24, elem->NbNodes());
167         fprintf(myFileId, sELT_BEAM_DESC2);
168         fprintf(myFileId, "%10d%10d%10d\n",elem->GetConnection(1), elem->GetConnection(2), elem->GetConnection(3));
169         break;
170       }
171     }
172   }
173
174   SMDS_MeshFacesIterator itFaces(myMesh);
175   for (;itFaces.More();itFaces.Next()) {
176     const Handle(SMDS_MeshElement)& elem = itFaces.Value();
177
178     switch (elem->NbNodes()) {
179       case 3 :
180         // linear triangle
181         fprintf(myFileId, sELT_SURF_DESC, elem->GetID(), 74, elem->NbNodes());
182         break;
183       case 4 :
184         // linear quadrilateral
185         fprintf(myFileId, sELT_SURF_DESC, elem->GetID(), 71, elem->NbNodes());
186         break;
187       case 6 :
188         // parabolic triangle
189         fprintf(myFileId, sELT_SURF_DESC, elem->GetID(), 72, elem->NbNodes());
190         break;
191       case 8 :
192         // parabolic quadrilateral
193         fprintf(myFileId, sELT_SURF_DESC, elem->GetID(), 75, elem->NbNodes());
194         break;
195       default:
196         fprintf(myFileId, "element not registered\n");
197     }
198
199     for (i=0;i<elem->NbNodes();i++)
200       fprintf(myFileId,"%10d",elem->GetConnection(i+1));
201
202     fprintf(myFileId,"\n");
203   }
204
205   SMDS_MeshVolumesIterator itVolumes(myMesh);
206   for (;itVolumes.More();itVolumes.Next()) {
207     const Handle(SMDS_MeshElement)& elem = itVolumes.Value();
208
209     switch (elem->NbNodes()) {
210       case 4 :
211         // linear tetrahedron
212         fprintf(myFileId, sELT_SURF_DESC, elem->GetID(), 111, elem->NbNodes());
213         break;
214       case 6 :
215         // linear tetrahedron
216         fprintf(myFileId, sELT_SURF_DESC, elem->GetID(), 112, elem->NbNodes());
217         break;
218       case 8 :
219         // linear brick
220         fprintf(myFileId, sELT_SURF_DESC, elem->GetID(), 115, elem->NbNodes());
221         break;
222     }
223
224     for (i=0;i<elem->NbNodes();i++)
225       fprintf(myFileId,"%10d",elem->GetConnection(i+1));
226
227     fprintf(myFileId,"\n");
228   }
229   fprintf(myFileId,"%s\n", sUNV_SEPARATOR);
230
231   fclose (myFileId);
232 }
233