1 // SMESH DriverMED : driver to read and write 'med' files
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : DriverMED_R_SMDS_Mesh.cxx
28 #include "DriverMED_R_SMDS_Mesh.h"
29 #include "utilities.h"
31 DriverMED_R_SMDS_Mesh::DriverMED_R_SMDS_Mesh()
35 DriverMED_R_SMDS_Mesh::~DriverMED_R_SMDS_Mesh()
40 void DriverMED_R_SMDS_Mesh::SetMesh(SMDS_Mesh * aMesh)
45 void DriverMED_R_SMDS_Mesh::SetFile(string aFile)
51 void DriverMED_R_SMDS_Mesh::SetFileId(med_idt aFileId)
56 void DriverMED_R_SMDS_Mesh::SetMeshId(int aMeshId)
61 void DriverMED_R_SMDS_Mesh::Add()
66 void DriverMED_R_SMDS_Mesh::Read()
74 /* nombre d'objets MED */
75 char nom_universel[MED_TAILLE_LNOM + 1];
76 med_int long_fichier_en_tete;
77 char *fichier_en_tete;
80 med_int nmaa, mdim, nnoe;
81 med_int nmai[MED_NBR_GEOMETRIE_MAILLE], nfac[MED_NBR_GEOMETRIE_FACE];
82 med_int nare[MED_NBR_GEOMETRIE_ARETE];
84 char nommaa[MED_TAILLE_NOM + 1];
87 char nomcoo[3 * MED_TAILLE_PNOM + 1];
88 char unicoo[3 * MED_TAILLE_PNOM + 1];
93 med_booleen inonoe, inunoe;
94 med_mode_switch mode_coo;
95 char str[MED_TAILLE_PNOM + 1];
102 med_int *connectivite;
106 med_booleen inoele, inuele;
107 med_connectivite typ_con;
108 med_geometrie_element typgeo;
109 med_geometrie_element typmai[MED_NBR_GEOMETRIE_MAILLE] =
110 { MED_POINT1, MED_SEG2,
112 MED_TRIA6, MED_QUAD4,
113 MED_QUAD8, MED_TETRA4,
114 MED_TETRA10, MED_HEXA8,
115 MED_HEXA20, MED_PENTA6,
116 MED_PENTA15, MED_PYRA5,
119 med_int desmai[MED_NBR_GEOMETRIE_MAILLE] =
120 { 0, 2, 3, 3, 3, 4, 4, 4, 4, 6, 6, 5, 5, 5, 5 };
121 med_int nmailles[MED_NBR_GEOMETRIE_MAILLE];
122 char nommai[MED_NBR_GEOMETRIE_MAILLE][MED_TAILLE_NOM + 1] = { "MED_POINT1",
138 med_geometrie_element typfac[MED_NBR_GEOMETRIE_FACE] =
139 { MED_TRIA3, MED_TRIA6,
142 med_int desfac[MED_NBR_GEOMETRIE_FACE] = { 3, 3, 4, 4 };
143 med_int nfaces[MED_NBR_GEOMETRIE_FACE];
144 char nomfac[MED_NBR_GEOMETRIE_FACE][MED_TAILLE_NOM + 1] =
145 { "MED_TRIA3", "MED_TRIA6",
146 "MED_QUAD4", "MED_QUAD8"
148 med_geometrie_element typare[MED_NBR_GEOMETRIE_ARETE] =
149 { MED_SEG2, MED_SEG3 };
150 med_int desare[MED_NBR_GEOMETRIE_ARETE] = { 2, 3 };
151 med_int naretes[MED_NBR_GEOMETRIE_ARETE];
152 char nomare[MED_NBR_GEOMETRIE_ARETE][MED_TAILLE_NOM + 1] =
153 { "MED_SEG2", "MED_SEG3" };
158 med_int *attval, *attide;
159 char nomfam[MED_TAILLE_NOM + 1];
161 char str1[MED_TAILLE_DESC + 1];
162 char str2[MED_TAILLE_LNOM + 1];
165 bool locally_managed;
168 locally_managed = true;
170 locally_managed = false;
174 file2Read = (char *)myFile.c_str();
175 myFileId = MEDouvrir(file2Read, MED_LECT);
178 fprintf(stderr, ">> ERREUR : ouverture du fichier %s \n",
188 mode_coo = MED_FULL_INTERLACE;
190 /****************************************************************************
191 * NOMBRES D'OBJETS MED *
192 ****************************************************************************/
193 fprintf(stdout, "\n(****************************)\n");
194 fprintf(stdout, "(* INFORMATIONS GENERALES : *)\n");
195 fprintf(stdout, "(****************************)\n");
197 /* lecture du nom et de la dimension du maillage */
198 fprintf(stdout, "%d %d\n", myFileId, numero);
199 ret = MEDmaaInfo(myFileId, numero, nommaa, &mdim);
200 fprintf(stdout, "%d\n", ret);
203 fprintf(stderr, ">> ERREUR : lecture du nom du maillage \n");
206 fprintf(stdout, "- Nom du maillage : <<%s>>\n", nommaa);
207 fprintf(stdout, "- Dimension du maillage : %d\n", mdim);
209 /* Combien de noeuds ? */
211 MEDnEntMaa(myFileId, nommaa, MED_COOR, MED_NOEUD, MED_POINT1, typ_con);
214 fprintf(stderr, ">> ERREUR : lecture du nombre de noeuds \n");
217 fprintf(stdout, "- Nombre de noeuds : %d \n", nnoe);
219 /* Combien de mailles, faces ou aretes ? */
220 for (i = 0; i < MED_NBR_GEOMETRIE_MAILLE; i++)
223 MEDnEntMaa(myFileId, nommaa, MED_CONN, MED_MAILLE, typmai[i],
227 fprintf(stderr, ">> ERREUR : lecture du nombre de mailles \n");
230 fprintf(stdout, "- Nombre de mailles de type %s : %d \n", nommai[i],
234 for (i = 0; i < MED_NBR_GEOMETRIE_FACE; i++)
236 nfaces[i] = MEDnEntMaa(myFileId, nommaa, MED_CONN, MED_FACE, typfac[i],
240 fprintf(stderr, ">> ERREUR : lecture du nombre de faces \n");
243 fprintf(stdout, "- Nombre de faces de type %s : %d \n", nomfac[i],
247 for (i = 0; i < MED_NBR_GEOMETRIE_ARETE; i++)
250 MEDnEntMaa(myFileId, nommaa, MED_CONN, MED_ARETE, typare[i],
254 fprintf(stderr, ">> ERREUR : lecture du nombre d'aretes \n");
257 fprintf(stdout, "- Nombre d'aretes de type %s : %d \n", nomare[i],
261 /* nombre de familles */
262 nfam = MEDnFam(myFileId, nommaa, 0, MED_FAMILLE);
265 fprintf(stderr, ">> ERREUR : lecture du nombre de familles \n");
268 fprintf(stdout, "- Nombre de familles : %d \n", nfam);
270 /****************************************************************************
271 * LECTURE DES NOEUDS *
272 ****************************************************************************/
273 fprintf(stdout, "\n(************************)\n");
274 fprintf(stdout, "(* NOEUDS DU MAILLAGE : *)\n");
275 fprintf(stdout, "(************************)\n");
277 /* Allocations memoires */
278 /* table des coordonnees
279 * profil : (dimension * nombre de noeuds ) */
280 coo = (med_float *) malloc(sizeof(med_float) * nnoe * mdim);
281 /* table des numeros, des numeros de familles des noeuds
282 * profil : (nombre de noeuds) */
283 numnoe = (med_int *) malloc(sizeof(med_int) * nnoe);
284 nufano = (med_int *) malloc(sizeof(med_int) * nnoe);
285 /* table des noms des noeuds
286 * profil : (nnoe*MED_TAILLE_PNOM+1) */
287 nomnoe = (char *)malloc(MED_TAILLE_PNOM * nnoe + 1);
289 /* lecture des noeuds :
291 * - noms (optionnel dans un fichier MED)
292 * - numeros (optionnel dans un fichier MED)
293 * - numeros des familles */
294 ret = MEDnoeudsLire(myFileId, nommaa, mdim, coo, mode_coo, &rep,
295 nomcoo, unicoo, nomnoe, &inonoe, numnoe, &inunoe, nufano, nnoe);
297 strcpy(message, ">> ERREUR : lecture des noeuds \n");
301 for (int i = 0; i < nnoe; i++)
303 ok = myMesh->AddNodeWithID(coo[i * 3], coo[i * 3 + 1],
304 coo[i * 3 + 2], numnoe[i]);
305 //fprintf(Out,"%d %f %f %f\n",numnoe[i],coo[i*3],coo[i*3+1],coo[i*3+2]);
310 for (int i = 0; i < nnoe; i++)
312 ok = myMesh->AddNodeWithID(coo[i * 3], coo[i * 3 + 1],
313 coo[i * 3 + 2], i + 1);
314 //fprintf(Out,"%d %f %f %f\n",numnoe[i],coo[i*3],coo[i*3+1],i);
318 //fprintf(stdout,"\n- Numeros des familles des noeuds : \n");
319 //for (i=0;i<nnoe;i++)
320 //fprintf(stdout," %d ",*(nufano+i));
321 //fprintf(stdout,"\n");
323 SCRUTE(myMesh->NbNodes());
325 /* liberation memoire */
331 /****************************************************************************
332 * LECTURE DES ELEMENTS *
333 ****************************************************************************/
334 fprintf(stdout, "\n(**************************)\n");
335 fprintf(stdout, "(* ELEMENTS DU MAILLAGE : *)\n");
336 fprintf(stdout, "(**************************)");
337 //fprintf(Out,"CELLS\n");
338 /* Lecture des connectivites, noms, numeros des mailles */
339 //printf("%d %d %d %d\n",nmailles[3],nmailles[4],nmailles[5],nmailles[9]);
342 for (i = 0; i < MED_NBR_GEOMETRIE_MAILLE; i++)
344 if (nmailles[i] > 0 && ret == 0)
346 /* dimension de la maille */
347 edim = typmai[i] / 100;
349 if (mdim == 2 || mdim == 3)
356 taille = nsup + typmai[i] % 100;
357 //taille = typmai[i]%100;
359 /* allocation memoire */
360 connectivite = (med_int *) malloc(sizeof(med_int) *
361 taille * nmailles[i]);
362 nomele = (char *)malloc(sizeof(char) * MED_TAILLE_PNOM *
364 numele = (med_int *) malloc(sizeof(med_int) * nmailles[i]);
365 nufael = (med_int *) malloc(sizeof(med_int) * nmailles[i]);
367 /* lecture des données */
369 MEDelementsLire(myFileId, nommaa, mdim, connectivite,
370 mode_coo, nomele, &inoele, numele, &inuele, nufael,
371 nmailles[i], MED_MAILLE, typmai[i], typ_con);
379 for (j = 0; j < nmailles[i]; j++)
381 elem_id = *(numele + j);
382 ok = myMesh->AddEdgeWithID(*(connectivite +
384 *(connectivite + j * (taille) + 1), elem_id);
389 for (j = 0; j < nmailles[i]; j++)
392 ok = myMesh->AddEdgeWithID(*(connectivite +
394 *(connectivite + j * (taille) + 1), cmpt);
404 for (j = 0; j < nmailles[i]; j++)
406 elem_id = *(numele + j);
407 ok = myMesh->AddFaceWithID(*(connectivite +
409 *(connectivite + j * (taille) + 1),
410 *(connectivite + j * (taille) + 2), elem_id);
411 //fprintf(Out,"%d %d %d %d\n",elem_id,*(connectivite+j*(taille-nsup)),*(connectivite+j*(taille-nsup)+1),*(connectivite+j*(taille-nsup)+2));
416 for (j = 0; j < nmailles[i]; j++)
419 ok = myMesh->AddFaceWithID(*(connectivite +
421 *(connectivite + j * (taille) + 1),
422 *(connectivite + j * (taille) + 2), cmpt);
423 //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2));
433 for (j = 0; j < nmailles[i]; j++)
435 elem_id = *(numele + j);
436 ok = myMesh->AddFaceWithID(*(connectivite +
438 *(connectivite + j * (taille) + 1),
439 *(connectivite + j * (taille) + 2),
440 *(connectivite + j * (taille) + 3), elem_id);
441 //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));
446 for (j = 0; j < nmailles[i]; j++)
449 ok = myMesh->AddFaceWithID(*(connectivite +
451 *(connectivite + j * (taille) + 1),
452 *(connectivite + j * (taille) + 2),
453 *(connectivite + j * (taille) + 3), cmpt);
454 //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3));
463 for (j = 0; j < nmailles[i]; j++)
465 elem_id = *(numele + j);
466 ok = myMesh->AddVolumeWithID(*(connectivite +
468 *(connectivite + j * (taille) + 1),
469 *(connectivite + j * (taille) + 2),
470 *(connectivite + j * (taille) + 3), elem_id);
471 //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));
476 for (j = 0; j < nmailles[i]; j++)
479 ok = myMesh->AddVolumeWithID(*(connectivite +
481 *(connectivite + j * (taille) + 1),
482 *(connectivite + j * (taille) + 2),
483 *(connectivite + j * (taille) + 3), cmpt);
484 //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3));
493 for (j = 0; j < nmailles[i]; j++)
495 elem_id = *(numele + j);
496 ok = myMesh->AddVolumeWithID(*(connectivite +
498 *(connectivite + j * (taille) + 1),
499 *(connectivite + j * (taille) + 2),
500 *(connectivite + j * (taille) + 3),
501 *(connectivite + j * (taille) + 4),
502 *(connectivite + j * (taille) + 5),
503 *(connectivite + j * (taille) + 6),
504 *(connectivite + j * (taille) + 7), elem_id);
505 //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));
510 for (j = 0; j < nmailles[i]; j++)
513 ok = myMesh->AddVolumeWithID(*(connectivite +
515 *(connectivite + j * (taille) + 1),
516 *(connectivite + j * (taille) + 2),
517 *(connectivite + j * (taille) + 3),
518 *(connectivite + j * (taille) + 4),
519 *(connectivite + j * (taille) + 5),
520 *(connectivite + j * (taille) + 6),
521 *(connectivite + j * (taille) + 7), cmpt);
522 //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));
533 //fprintf(stdout,"\n - Numéros de familles : \n");
534 //for (j=0;j<nmailles[i];j++)
535 //fprintf(stdout," %d ",*(nufael+j));
537 /* liberation memoire */
545 SCRUTE(myMesh->NbEdges());
546 SCRUTE(myMesh->NbFaces());
547 /****************************************************************************
548 * LECTURE DES FAMILLES *
549 ****************************************************************************/
550 printf("\n(*************************)\n");
551 printf("(* FAMILLES DU MAILLAGE : *)\n");
552 printf("(*************************)\n");
554 for (i = 0; i < nfam; i++)
557 /* nombre de groupes */
558 ngro = MEDnFam(myFileId, nommaa, i + 1, MED_GROUPE);
563 ">> ERREUR : lecture du nombre de groupes d'une famille \n");
566 /* nombre d'attributs */
569 natt = MEDnFam(myFileId, nommaa, i + 1, MED_ATTR);
574 ">> ERREUR : lecture du nombre d'attributs d'une famille\n");
579 fprintf(stdout, "- Famille %d a %d attributs et %d groupes \n",
582 /* nom,numero,attributs,groupes */
585 attide = (med_int *) malloc(sizeof(med_int) * natt);
586 attval = (med_int *) malloc(sizeof(med_int) * natt);
587 attdes = (char *)malloc(MED_TAILLE_DESC * natt + 1);
588 gro = (char *)malloc(MED_TAILLE_LNOM * ngro + 1);
590 MEDfamInfo(myFileId, nommaa, i + 1, nomfam, &numfam, attide,
591 attval, attdes, &natt, gro, &ngro);
592 fprintf(stdout, " - Famille de nom %s et de numero %d : \n",
594 fprintf(stdout, " - Attributs : \n");
595 for (j = 0; j < natt; j++)
597 strncpy(str1, attdes + j * MED_TAILLE_DESC,
599 str1[MED_TAILLE_DESC] = '\0';
600 fprintf(stdout, " ide = %d - val = %d - des = %s\n",
601 *(attide + j), *(attval + j), str1);
606 fprintf(stdout, " - Groupes :\n");
607 for (j = 0; j < ngro; j++)
609 strncpy(str2, gro + j * MED_TAILLE_LNOM, MED_TAILLE_LNOM);
610 str2[MED_TAILLE_LNOM] = '\0';
611 fprintf(stdout, " gro = %s\n", str2);
618 ret = MEDfermer(myFileId);