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