Salome HOME
c36dbdc6e7a6d513bd14376bad57524ae087566b
[modules/smesh.git] / src / DriverMED / DriverMED_R_SMDS_Mesh.cxx
1 //  SMESH DriverMED : driver to read and write 'med' 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   : DriverMED_R_SMDS_Mesh.cxx
25 //  Module : SMESH
26
27 using namespace std;
28 #include "DriverMED_R_SMDS_Mesh.h"
29 #include "utilities.h"
30
31 DriverMED_R_SMDS_Mesh::DriverMED_R_SMDS_Mesh() {
32 }
33
34 DriverMED_R_SMDS_Mesh::~DriverMED_R_SMDS_Mesh() {
35 ;
36 }
37
38 void DriverMED_R_SMDS_Mesh::SetMesh(Handle(SMDS_Mesh)& aMesh) {
39   myMesh = aMesh;
40 }
41
42 void DriverMED_R_SMDS_Mesh::SetFile(string aFile) {
43   myFileId = -1;
44   myFile = aFile;
45 }
46
47 void DriverMED_R_SMDS_Mesh::SetFileId(med_idt aFileId) {
48   myFileId = aFileId;
49 }
50
51 void DriverMED_R_SMDS_Mesh::SetMeshId(int aMeshId) {
52   myMeshId = aMeshId;
53 }
54
55 void DriverMED_R_SMDS_Mesh::Add() {
56   ;
57 }
58
59 void DriverMED_R_SMDS_Mesh::Read() {
60
61   med_err ret = 0;
62   int i,j,k,l;
63   int numero;
64   char message[200];
65   Standard_Boolean ok;
66   /* nombre d'objets MED */
67   char nom_universel[MED_TAILLE_LNOM+1];
68   med_int long_fichier_en_tete; 
69   char *fichier_en_tete;
70   char version_hdf[10];
71   char version_med[10];
72   med_int nmaa,mdim,nnoe;
73   med_int nmai[MED_NBR_GEOMETRIE_MAILLE],nfac[MED_NBR_GEOMETRIE_FACE];
74   med_int nare[MED_NBR_GEOMETRIE_ARETE];
75   /* nom du maillage */
76   char nommaa[MED_TAILLE_NOM+1];
77   /* noeuds */
78   med_float *coo;
79   char nomcoo[3*MED_TAILLE_PNOM+1];
80   char unicoo[3*MED_TAILLE_PNOM+1];
81   char *nomnoe;
82   med_int *numnoe;
83   med_int *nufano; 
84   med_repere rep;
85   med_booleen inonoe,inunoe;
86   med_mode_switch mode_coo;
87   char str[MED_TAILLE_PNOM+1];
88   /* elements */
89   med_int nsup;
90   med_int edim;
91   med_int taille;
92   med_int elem_id;
93   med_int cmpt = 0;
94   med_int *connectivite;
95   char *nomele;
96   med_int *numele;
97   med_int *nufael;
98   med_booleen inoele, inuele;
99   med_connectivite typ_con;
100   med_geometrie_element typgeo;
101   med_geometrie_element typmai[MED_NBR_GEOMETRIE_MAILLE] = {MED_POINT1,MED_SEG2, 
102                                                    MED_SEG3,MED_TRIA3,
103                                                    MED_TRIA6,MED_QUAD4,
104                                                    MED_QUAD8,MED_TETRA4,
105                                                    MED_TETRA10,MED_HEXA8,
106                                                    MED_HEXA20,MED_PENTA6,
107                                                    MED_PENTA15,MED_PYRA5,
108                                                    MED_PYRA13};
109   med_int desmai[MED_NBR_GEOMETRIE_MAILLE] = {0,2,3,3,3,4,4,4,4,6,6,5,5,5,5};
110   med_int nmailles[MED_NBR_GEOMETRIE_MAILLE];
111   char nommai[MED_NBR_GEOMETRIE_MAILLE] [MED_TAILLE_NOM+1] = {"MED_POINT1",
112                                                           "MED_SEG2", 
113                                                           "MED_SEG3",
114                                                           "MED_TRIA3",
115                                                           "MED_TRIA6",
116                                                           "MED_QUAD4",
117                                                           "MED_QUAD8",
118                                                           "MED_TETRA4",
119                                                           "MED_TETRA10",
120                                                           "MED_HEXA8",
121                                                           "MED_HEXA20",
122                                                           "MED_PENTA6",
123                                                           "MED_PENTA15",
124                                                           "MED_PYRA5",
125                                                           "MED_PYRA13"};
126   med_geometrie_element typfac[MED_NBR_GEOMETRIE_FACE] = {MED_TRIA3,MED_TRIA6,
127                                                  MED_QUAD4,MED_QUAD8};
128   med_int desfac[MED_NBR_GEOMETRIE_FACE] = {3,3,4,4};
129   med_int nfaces[MED_NBR_GEOMETRIE_FACE];
130   char nomfac[MED_NBR_GEOMETRIE_FACE][MED_TAILLE_NOM+1] = {"MED_TRIA3","MED_TRIA6",
131                                                        "MED_QUAD4","MED_QUAD8"};
132   med_geometrie_element typare[MED_NBR_GEOMETRIE_ARETE] = {MED_SEG2,MED_SEG3};
133   med_int desare[MED_NBR_GEOMETRIE_ARETE] = {2,3};
134   med_int naretes[MED_NBR_GEOMETRIE_ARETE];
135   char nomare[MED_NBR_GEOMETRIE_ARETE] [MED_TAILLE_NOM+1] = {"MED_SEG2","MED_SEG3"};
136   /* familles */
137   med_int nfam;
138   med_int natt,ngro;
139   char *attdes,*gro;
140   med_int *attval,*attide;
141   char nomfam[MED_TAILLE_NOM+1];
142   med_int numfam;
143   char str1[MED_TAILLE_DESC+1];
144   char str2[MED_TAILLE_LNOM+1];
145   
146   char* file2Read;
147   bool locally_managed;
148   SCRUTE(myFileId);
149   if (myFileId==-1) 
150     locally_managed = true;
151   else
152     locally_managed = false;
153
154   if (locally_managed)
155     { 
156     file2Read = (char*)myFile.c_str();
157     myFileId = MEDouvrir(file2Read,MED_LECT);
158     if (myFileId < 0)
159       {
160         fprintf(stderr,">> ERREUR : ouverture du fichier %s \n",file2Read);
161         exit(EXIT_FAILURE);
162       }
163     numero = 1;
164     }
165   else
166     numero = myMeshId;
167     
168   typ_con = MED_NOD;
169   mode_coo = MED_FULL_INTERLACE;
170   
171
172   /****************************************************************************
173   *                       NOMBRES D'OBJETS MED                                *
174   ****************************************************************************/
175   fprintf(stdout,"\n(****************************)\n");
176   fprintf(stdout,"(* INFORMATIONS GENERALES : *)\n");
177   fprintf(stdout,"(****************************)\n");
178
179   /* lecture du nom et de la dimension du maillage */
180   fprintf(stdout,"%d %d\n",myFileId,numero);
181   ret = MEDmaaInfo(myFileId,numero,nommaa,&mdim);
182   fprintf(stdout,"%d\n",ret);
183   if (ret < 0)
184     {
185       fprintf(stderr,">> ERREUR : lecture du nom du maillage \n");
186       exit(EXIT_FAILURE);
187     }
188   fprintf(stdout,"- Nom du maillage : <<%s>>\n",nommaa);
189   fprintf(stdout,"- Dimension du maillage : %d\n",mdim);
190
191   /* Combien de noeuds ? */
192   nnoe = MEDnEntMaa(myFileId,nommaa,MED_COOR,MED_NOEUD,MED_POINT1,typ_con);
193   if (nnoe < 0)
194     {
195       fprintf(stderr,">> ERREUR : lecture du nombre de noeuds \n");
196       exit(EXIT_FAILURE);
197     }
198   fprintf(stdout,"- Nombre de noeuds : %d \n",nnoe);
199
200   /* Combien de mailles, faces ou aretes ? */
201   for (i=0;i<MED_NBR_GEOMETRIE_MAILLE;i++)
202     {
203       nmailles[i] = MEDnEntMaa(myFileId,nommaa,MED_CONN,MED_MAILLE,typmai[i],
204                                typ_con);
205       if (nmailles[i] < 0)
206         {
207           fprintf(stderr,">> ERREUR : lecture du nombre de mailles \n");
208           exit(EXIT_FAILURE);
209         }
210       fprintf (stdout,"- Nombre de mailles de type %s : %d \n",nommai[i],nmailles[i]);
211     }
212
213   for (i=0;i<MED_NBR_GEOMETRIE_FACE;i++)
214     {
215       nfaces[i] = MEDnEntMaa(myFileId,nommaa,MED_CONN,MED_FACE,typfac[i],
216                              typ_con);
217       if (nfaces[i] < 0)
218         {
219           fprintf(stderr,">> ERREUR : lecture du nombre de faces \n");
220           exit(EXIT_FAILURE);
221         }
222       fprintf (stdout,"- Nombre de faces de type %s : %d \n",nomfac[i],nfaces[i]);
223     }    
224
225   for (i=0;i<MED_NBR_GEOMETRIE_ARETE;i++)
226     {
227       naretes[i] = MEDnEntMaa(myFileId,nommaa,MED_CONN,MED_ARETE,typare[i],
228                               typ_con); 
229       if (naretes[i] < 0)
230         {
231           fprintf(stderr,">> ERREUR : lecture du nombre d'aretes \n");
232           exit(EXIT_FAILURE);
233         }
234       fprintf (stdout,"- Nombre d'aretes de type %s : %d \n",nomare[i],naretes[i]);
235     }
236
237   /* nombre de familles */
238   nfam = MEDnFam(myFileId,nommaa,0,MED_FAMILLE);
239   if (nfam < 0)
240     {
241       fprintf(stderr,">> ERREUR : lecture du nombre de familles \n");
242       exit(EXIT_FAILURE);
243     }   
244   fprintf(stdout,"- Nombre de familles : %d \n",nfam);
245
246   /****************************************************************************
247   *                       LECTURE DES NOEUDS                                  *
248   ****************************************************************************/
249   fprintf(stdout,"\n(************************)\n");
250   fprintf(stdout,"(* NOEUDS DU MAILLAGE : *)\n");
251   fprintf(stdout,"(************************)\n");
252
253   /* Allocations memoires */
254   /* table des coordonnees 
255      profil : (dimension * nombre de noeuds ) */
256   coo = (med_float*) malloc(sizeof(med_float)*nnoe*mdim);
257   /* table  des numeros, des numeros de familles des noeuds
258      profil : (nombre de noeuds) */
259   numnoe = (med_int*) malloc(sizeof(med_int)*nnoe);
260   nufano = (med_int*) malloc(sizeof(med_int)*nnoe);
261   /* table des noms des noeuds 
262      profil : (nnoe*MED_TAILLE_PNOM+1) */
263   nomnoe = (char*) malloc(MED_TAILLE_PNOM*nnoe+1);
264
265   /* lecture des noeuds : 
266      - coordonnees
267      - noms (optionnel dans un fichier MED) 
268      - numeros (optionnel dans un fichier MED) 
269      - numeros des familles */
270   ret = MEDnoeudsLire(myFileId,nommaa,mdim,coo,mode_coo,&rep,
271                       nomcoo,unicoo,nomnoe,&inonoe,numnoe,&inunoe,
272                       nufano,nnoe);
273   if (ret < 0)
274     strcpy(message,">> ERREUR : lecture des noeuds \n");
275
276   if (inunoe) {
277     for (int i=0;i<nnoe;i++) {
278       ok = myMesh->AddNodeWithID(coo[i*3],coo[i*3+1],coo[i*3+2],numnoe[i]);
279       //fprintf(Out,"%d %f %f %f\n",numnoe[i],coo[i*3],coo[i*3+1],coo[i*3+2]);
280     }
281   }
282   else {
283     for (int i=0;i<nnoe;i++) {
284       ok = myMesh->AddNodeWithID(coo[i*3],coo[i*3+1],coo[i*3+2],i+1);
285       //fprintf(Out,"%d %f %f %f\n",numnoe[i],coo[i*3],coo[i*3+1],i);
286     }
287   }
288
289   //fprintf(stdout,"\n- Numeros des familles des noeuds : \n");
290   //for (i=0;i<nnoe;i++)
291   //fprintf(stdout," %d ",*(nufano+i));
292   //fprintf(stdout,"\n");
293
294   SCRUTE(myMesh->NbNodes());
295
296   /* liberation memoire */
297   free(coo);
298   free(nomnoe);
299   free(numnoe);
300   free(nufano);
301
302   /****************************************************************************
303   *                       LECTURE DES ELEMENTS                                *
304   ****************************************************************************/
305   fprintf(stdout,"\n(**************************)\n");
306   fprintf(stdout,"(* ELEMENTS DU MAILLAGE : *)\n");
307   fprintf(stdout,"(**************************)");
308   //fprintf(Out,"CELLS\n");
309   /* Lecture des connectivites, noms, numeros des mailles */
310   //printf("%d %d %d %d\n",nmailles[3],nmailles[4],nmailles[5],nmailles[9]);
311
312   if (ret == 0)
313     for (i=0;i<MED_NBR_GEOMETRIE_MAILLE;i++)
314       {
315         if (nmailles[i] > 0 && ret == 0)
316           {
317             /* dimension de la maille */
318             edim = typmai[i] / 100;
319             nsup = 0;
320             if (mdim  == 2 || mdim == 3)
321               if (edim == 1)
322                 nsup = 1;
323             if (mdim == 3)
324               if (edim == 2)
325                 nsup = 1;
326
327             taille = nsup+typmai[i]%100;
328             //taille = typmai[i]%100;
329             
330             /* allocation memoire */
331             connectivite = (med_int*)malloc(sizeof(med_int)*
332                                             taille*nmailles[i]);
333             nomele = (char*)malloc(sizeof(char)*MED_TAILLE_PNOM*
334                                    nmailles[i]+1);
335             numele = (med_int*)malloc(sizeof(med_int)*
336                                       nmailles[i]);
337             nufael = (med_int*)malloc(sizeof(med_int)*
338                                       nmailles[i]);
339             
340             /* lecture des données */
341             ret = MEDelementsLire(myFileId,nommaa,mdim,connectivite,mode_coo,
342                                   nomele,&inoele,numele,&inuele,nufael,
343                                   nmailles[i],MED_MAILLE,typmai[i],
344                                   typ_con);
345             SCRUTE(typmai[i]);
346             switch (typmai[i])
347               {
348               case MED_SEG2  : {
349                 if (inuele) {
350                   for (j=0;j<nmailles[i];j++) {
351                     elem_id=*(numele+j);
352                     ok = myMesh->AddEdgeWithID(*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),elem_id);
353                   }
354                 }
355                 else {
356                   for (j=0;j<nmailles[i];j++) {
357                     cmpt++;
358                     ok = myMesh->AddEdgeWithID(*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),cmpt);
359                   }
360                 }
361
362                 break;
363               }
364               case MED_TRIA3  : {
365                 if (inuele) {
366                   for (j=0;j<nmailles[i];j++) {
367                     elem_id=*(numele+j);
368                     ok = myMesh->AddFaceWithID(*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),elem_id);
369                     //fprintf(Out,"%d %d %d %d\n",elem_id,*(connectivite+j*(taille-nsup)),*(connectivite+j*(taille-nsup)+1),*(connectivite+j*(taille-nsup)+2));
370                   }
371                 }
372                 else {
373                   for (j=0;j<nmailles[i];j++) {
374                     cmpt++;
375                     ok = myMesh->AddFaceWithID(*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),cmpt);
376                     //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2));
377                   }
378                 }
379
380                 break;
381               }
382               case MED_QUAD4  : {
383                 if (inuele) {
384                   for (j=0;j<nmailles[i];j++) {
385                     elem_id=*(numele+j);
386                     ok = myMesh->AddFaceWithID(*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3),elem_id);
387                     //fprintf(Out,"%d %d %d %d\n",elem_id,*(connectivite+j*(taille-nsup)),*(connectivite+j*(taille-nsup)+1),*(connectivite+j*(taille-nsup)+2),*(connectivite+j*(taille-nsup)+3));
388                   }
389                 }
390                 else {
391                   for (j=0;j<nmailles[i];j++) {
392                     cmpt++;
393                     ok = myMesh->AddFaceWithID(*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3),cmpt);
394                     //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3));
395                   }
396                 }
397                 break;
398               }
399               case MED_HEXA8  : {
400                 if (inuele) {
401                   for (j=0;j<nmailles[i];j++) {
402                     elem_id=*(numele+j);
403                     ok = myMesh->AddVolumeWithID(*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3),*(connectivite+j*(taille)+4),*(connectivite+j*(taille)+5),*(connectivite+j*(taille)+6),*(connectivite+j*(taille)+7),elem_id);
404                     //fprintf(Out,"%d %d %d %d\n",elem_id,*(connectivite+j*(taille-nsup)),*(connectivite+j*(taille-nsup)+1),*(connectivite+j*(taille-nsup)+2),*(connectivite+j*(taille-nsup)+3),*(connectivite+j*(taille-nsup)+4),*(connectivite+j*(taille-nsup)+5),*(connectivite+j*(taille-nsup)+6),*(connectivite+j*(taille-nsup)+7));
405                   }
406                 }
407                 else {
408                   for (j=0;j<nmailles[i];j++) {
409                     cmpt++;
410                     ok = myMesh->AddVolumeWithID(*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3),*(connectivite+j*(taille)+4),*(connectivite+j*(taille)+5),*(connectivite+j*(taille)+6),*(connectivite+j*(taille)+7),cmpt);
411                     //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3),*(connectivite+j*(taille)+4),*(connectivite+j*(taille)+5),*(connectivite+j*(taille)+6),*(connectivite+j*(taille)+7));
412                   }
413                 }
414                 break;
415               }
416               default : {
417                 break ;
418               }
419               }
420
421             //fprintf(stdout,"\n  - Numéros de familles : \n");
422             //for (j=0;j<nmailles[i];j++)
423             //fprintf(stdout," %d ",*(nufael+j));
424             
425             /* liberation memoire */
426             free(connectivite);
427             free(nomele);
428             free(numele);
429             free(nufael);
430           }
431       }
432
433   SCRUTE(myMesh->NbEdges());
434   SCRUTE(myMesh->NbFaces());
435   /****************************************************************************
436    *                       LECTURE DES FAMILLES                                *
437    ****************************************************************************/
438   printf("\n(*************************)\n");
439   printf("(* FAMILLES DU MAILLAGE : *)\n");
440   printf("(*************************)\n");
441   if (ret == 0)
442     for (i=0;i<nfam;i++)
443       {
444         
445         /* nombre de groupes */
446         ngro = MEDnFam(myFileId,nommaa,i+1,MED_GROUPE);
447         if (ngro < 0)  
448           {
449             ret = -1;
450             strcpy(message,
451                    ">> ERREUR : lecture du nombre de groupes d'une famille \n");
452           }
453         
454         /* nombre d'attributs */
455         if (ret == 0)
456           {
457             natt = MEDnFam(myFileId,nommaa,i+1,MED_ATTR);
458             if (natt < 0)
459               {
460                 ret = -1;
461                 strcpy(message,
462                    ">> ERREUR : lecture du nombre d'attributs d'une famille\n");
463               }
464           }
465
466         if (ret == 0)
467           fprintf(stdout,"- Famille %d a %d attributs et %d groupes \n",i+1,natt,ngro); 
468
469         /* nom,numero,attributs,groupes */
470         if (ret == 0)
471           {
472             attide = (med_int*) malloc(sizeof(med_int)*natt);
473             attval = (med_int*) malloc(sizeof(med_int)*natt);       
474             attdes = (char *) malloc(MED_TAILLE_DESC*natt+1);
475             gro = (char*) malloc(MED_TAILLE_LNOM*ngro+1);
476             ret = MEDfamInfo(myFileId,nommaa,i+1,nomfam,&numfam,attide,attval,
477                              attdes,&natt,gro,&ngro);
478             fprintf(stdout,"  - Famille de nom %s et de numero %d : \n",nomfam,numfam);
479             fprintf(stdout,"  - Attributs : \n");
480             for (j=0;j<natt;j++)
481               {
482                 strncpy(str1,attdes+j*MED_TAILLE_DESC,MED_TAILLE_DESC);
483                 str1[MED_TAILLE_DESC] = '\0';
484                 fprintf(stdout,"   ide = %d - val = %d - des = %s\n",*(attide+j),
485                        *(attval+j),str1);
486               }
487             free(attide);
488             free(attval);
489             free(attdes);
490             fprintf(stdout,"  - Groupes :\n");
491             for (j=0;j<ngro;j++)
492               {
493                 strncpy(str2,gro+j*MED_TAILLE_LNOM,MED_TAILLE_LNOM);
494                 str2[MED_TAILLE_LNOM] = '\0';
495                 fprintf(stdout,"   gro = %s\n",str2);
496               }
497             free(gro);
498           }
499       }
500
501   if (locally_managed)
502     ret = MEDfermer(myFileId);
503
504 }