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