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