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_SMESHDS_Mesh.cxx
28 #include "DriverMED_R_SMESHDS_Mesh.h"
29 #include "DriverMED_R_SMDS_Mesh.h"
30 #include "utilities.h"
37 * Factory function which will be called by SMESHDriver
39 void * SMESH_createMEDMeshReader()
41 return new DriverMED_R_SMESHDS_Mesh();
46 DriverMED_R_SMESHDS_Mesh::DriverMED_R_SMESHDS_Mesh()
51 DriverMED_R_SMESHDS_Mesh::~DriverMED_R_SMESHDS_Mesh()
56 void DriverMED_R_SMESHDS_Mesh::SetMesh(SMDS_Mesh * aMesh)
58 //myMesh = SMESHDS_Mesh *::DownCast(aMesh);
62 void DriverMED_R_SMESHDS_Mesh::SetFile(string aFile)
67 void DriverMED_R_SMESHDS_Mesh::SetFileId(med_idt aFileId)
72 void DriverMED_R_SMESHDS_Mesh::SetMeshId(int aMeshId)
77 void DriverMED_R_SMESHDS_Mesh::Read()
80 string myClass = string("SMDS_Mesh");
81 string myExtension = string("MED");
83 DriverMED_R_SMDS_Mesh *myReader = new DriverMED_R_SMDS_Mesh;
85 myReader->SetMesh(myMesh);
86 myReader->SetMeshId(myMeshId);
87 myReader->SetFile(myFile);
88 myReader->SetFileId(-1);
94 void DriverMED_R_SMESHDS_Mesh::Add()
97 string myClass = string("SMDS_Mesh");
98 string myExtension = string("MED");
100 DriverMED_R_SMDS_Mesh *myReader = new DriverMED_R_SMDS_Mesh;
102 myReader->SetMesh(myMesh);
103 myReader->SetMeshId(myMeshId);
106 myReader->SetFileId(myFileId);
112 void DriverMED_R_SMESHDS_Mesh::ReadMySelf()
120 /* nombre d'objets MED */
121 char nom_universel[MED_TAILLE_LNOM + 1];
122 med_int long_fichier_en_tete;
123 char *fichier_en_tete;
124 char version_hdf[10];
125 char version_med[10];
126 med_int nmaa, mdim, nnoe;
127 med_int nmai[MED_NBR_GEOMETRIE_MAILLE], nfac[MED_NBR_GEOMETRIE_FACE];
128 med_int nare[MED_NBR_GEOMETRIE_ARETE];
129 /* nom du maillage */
130 char nommaa[MED_TAILLE_NOM + 1];
133 char nomcoo[3 * MED_TAILLE_PNOM + 1];
134 char unicoo[3 * MED_TAILLE_PNOM + 1];
139 med_booleen inonoe, inunoe;
140 med_mode_switch mode_coo;
141 char str[MED_TAILLE_PNOM + 1];
148 med_int *connectivite;
152 med_booleen inoele, inuele;
153 med_connectivite typ_con;
154 med_geometrie_element typgeo;
155 med_geometrie_element typmai[MED_NBR_GEOMETRIE_MAILLE] =
156 { MED_POINT1, MED_SEG2,
158 MED_TRIA6, MED_QUAD4,
159 MED_QUAD8, MED_TETRA4,
160 MED_TETRA10, MED_HEXA8,
161 MED_HEXA20, MED_PENTA6,
162 MED_PENTA15, MED_PYRA5,
165 med_int desmai[MED_NBR_GEOMETRIE_MAILLE] =
166 { 0, 2, 3, 3, 3, 4, 4, 4, 4, 6, 6, 5, 5, 5, 5 };
167 med_int nmailles[MED_NBR_GEOMETRIE_MAILLE];
168 char nommai[MED_NBR_GEOMETRIE_MAILLE][MED_TAILLE_NOM + 1] = { "MED_POINT1",
184 med_geometrie_element typfac[MED_NBR_GEOMETRIE_FACE] =
185 { MED_TRIA3, MED_TRIA6,
188 med_int desfac[MED_NBR_GEOMETRIE_FACE] = { 3, 3, 4, 4 };
189 med_int nfaces[MED_NBR_GEOMETRIE_FACE];
190 char nomfac[MED_NBR_GEOMETRIE_FACE][MED_TAILLE_NOM + 1] =
191 { "MED_TRIA3", "MED_TRIA6",
192 "MED_QUAD4", "MED_QUAD8"
194 med_geometrie_element typare[MED_NBR_GEOMETRIE_ARETE] =
195 { MED_SEG2, MED_SEG3 };
196 med_int desare[MED_NBR_GEOMETRIE_ARETE] = { 2, 3 };
197 med_int naretes[MED_NBR_GEOMETRIE_ARETE];
198 char nomare[MED_NBR_GEOMETRIE_ARETE][MED_TAILLE_NOM + 1] =
199 { "MED_SEG2", "MED_SEG3" };
204 med_int *attval, *attide;
205 char nomfam[MED_TAILLE_NOM + 1];
207 char str1[MED_TAILLE_DESC + 1];
208 char str2[MED_TAILLE_LNOM + 1];
214 bool locally_managed;
217 locally_managed = true;
219 locally_managed = false;
223 file2Read = (char *)myFile.c_str();
224 myFileId = MEDouvrir(file2Read, MED_LECT);
227 fprintf(stderr, ">> ERREUR : ouverture du fichier %s \n",
235 sprintf(nommaa, "Mesh %d", myMeshId); //pour load
239 mode_coo = MED_FULL_INTERLACE;
242 SMESHDS_Mesh * mySMESHDSMesh = dynamic_cast<SMESHDS_Mesh *>(myMesh);
244 //TopoDS_Shape myShape = mySMESHDSMesh->ShapeToMesh();
246 /****************************************************************************
247 * NOMBRES D'OBJETS MED *
248 ****************************************************************************/
249 fprintf(stdout, "\n(****************************)\n");
250 fprintf(stdout, "(* INFORMATIONS GENERALES : *)\n");
251 fprintf(stdout, "(****************************)\n");
253 /* lecture du nom et de la dimension du maillage */
254 /*! fprintf(stdout,"%d %d\n",myFileId,numero);
255 * ret = MEDmaaInfo(myFileId,numero,nommaa,&mdim);
256 * fprintf(stdout,"%d\n",ret);
259 * fprintf(stderr,">> ERREUR : lecture du nom du maillage \n");
260 * exit(EXIT_FAILURE);
262 * fprintf(stdout,"- Nom du maillage : <<%s>>\n",nommaa);
263 * fprintf(stdout,"- Dimension du maillage : %d\n",mdim);
265 /* Combien de noeuds ? */
267 MEDnEntMaa(myFileId, nommaa, MED_COOR, MED_NOEUD, MED_POINT1, typ_con);
270 fprintf(stderr, ">> ERREUR : lecture du nombre de noeuds \n");
273 fprintf(stdout, "- Nombre de noeuds : %d \n", nnoe);
275 /* Combien de mailles, faces ou aretes ? */
276 for (i = 0; i < MED_NBR_GEOMETRIE_MAILLE; i++)
279 MEDnEntMaa(myFileId, nommaa, MED_CONN, MED_MAILLE, typmai[i],
283 fprintf(stderr, ">> ERREUR : lecture du nombre de mailles \n");
286 fprintf(stdout, "- Nombre de mailles de type %s : %d \n", nommai[i],
290 for (i = 0; i < MED_NBR_GEOMETRIE_FACE; i++)
292 nfaces[i] = MEDnEntMaa(myFileId, nommaa, MED_CONN, MED_FACE, typfac[i],
296 fprintf(stderr, ">> ERREUR : lecture du nombre de faces \n");
299 fprintf(stdout, "- Nombre de faces de type %s : %d \n", nomfac[i],
303 for (i = 0; i < MED_NBR_GEOMETRIE_ARETE; i++)
306 MEDnEntMaa(myFileId, nommaa, MED_CONN, MED_ARETE, typare[i],
310 fprintf(stderr, ">> ERREUR : lecture du nombre d'aretes \n");
313 fprintf(stdout, "- Nombre d'aretes de type %s : %d \n", nomare[i],
317 /* nombre de familles */
318 nfam = MEDnFam(myFileId, nommaa, 0, MED_FAMILLE);
321 fprintf(stderr, ">> ERREUR : lecture du nombre de familles \n");
324 fprintf(stdout, "- Nombre de familles : %d \n", nfam);
326 vector < int >family[nfam];
328 /****************************************************************************
329 * LECTURE DES NOEUDS *
330 ****************************************************************************/
331 fprintf(stdout, "\n(************************)\n");
332 fprintf(stdout, "(* NOEUDS DU MAILLAGE : *)\n");
333 fprintf(stdout, "(************************)\n");
335 /* Allocations memoires */
336 /* table des coordonnees
337 * profil : (dimension * nombre de noeuds ) */
338 coo = (med_float *) malloc(sizeof(med_float) * nnoe * mdim);
339 /* table des numeros, des numeros de familles des noeuds
340 * profil : (nombre de noeuds) */
341 numnoe = (med_int *) malloc(sizeof(med_int) * nnoe);
342 nufano = (med_int *) malloc(sizeof(med_int) * nnoe);
343 /* table des noms des noeuds
344 * profil : (nnoe*MED_TAILLE_PNOM+1) */
345 nomnoe = (char *)malloc(MED_TAILLE_PNOM * nnoe + 1);
347 /* lecture des noeuds :
349 * - noms (optionnel dans un fichier MED)
350 * - numeros (optionnel dans un fichier MED)
351 * - numeros des familles */
352 ret = MEDnoeudsLire(myFileId, nommaa, mdim, coo, mode_coo, &rep,
353 nomcoo, unicoo, nomnoe, &inonoe, numnoe, &inunoe, nufano, nnoe);
355 strcpy(message, ">> ERREUR : lecture des noeuds \n");
359 for (int i = 0; i < nnoe; i++)
361 ok = mySMESHDSMesh->AddNodeWithID(coo[i * 3], coo[i * 3 + 1],
362 coo[i * 3 + 2], numnoe[i]);
363 //fprintf(Out,"%d %f %f %f\n",numnoe[i],coo[i*3],coo[i*3+1],coo[i*3+2]);
368 for (int i = 0; i < nnoe; i++)
370 ok = mySMESHDSMesh->AddNodeWithID(coo[i * 3], coo[i * 3 + 1],
371 coo[i * 3 + 2], i + 1);
372 //fprintf(Out,"%d %f %f %f\n",numnoe[i],coo[i*3],coo[i*3+1],i);
373 family[*(nufano + i)].push_back(numnoe[i]);
377 fprintf(stdout, "\n- Numeros des familles des noeuds : \n");
378 for (i = 0; i < nnoe; i++)
379 fprintf(stdout, " %d ", *(nufano + i));
380 fprintf(stdout, "\n");
382 /* liberation memoire */
388 /****************************************************************************
389 * LECTURE DES ELEMENTS *
390 ****************************************************************************/
391 fprintf(stdout, "\n(**************************)\n");
392 fprintf(stdout, "(* ELEMENTS DU MAILLAGE : *)\n");
393 fprintf(stdout, "(**************************)");
394 //fprintf(Out,"CELLS\n");
395 /* Lecture des connectivites, noms, numeros des mailles */
396 //printf("%d %d %d %d\n",nmailles[3],nmailles[4],nmailles[5],nmailles[9]);
399 for (i = 0; i < MED_NBR_GEOMETRIE_MAILLE; i++)
401 if (nmailles[i] > 0 && ret == 0)
403 /* dimension de la maille */
404 edim = typmai[i] / 100;
406 if (mdim == 2 || mdim == 3)
413 taille = nsup + typmai[i] % 100;
414 //taille = typmai[i]%100;
416 /* allocation memoire */
417 connectivite = (med_int *) malloc(sizeof(med_int) *
418 taille * nmailles[i]);
419 nomele = (char *)malloc(sizeof(char) * MED_TAILLE_PNOM *
421 numele = (med_int *) malloc(sizeof(med_int) * nmailles[i]);
422 nufael = (med_int *) malloc(sizeof(med_int) * nmailles[i]);
424 /* lecture des données */
426 MEDelementsLire(myFileId, nommaa, mdim, connectivite,
427 mode_coo, nomele, &inoele, numele, &inuele, nufael,
428 nmailles[i], MED_MAILLE, typmai[i], typ_con);
436 for (j = 0; j < nmailles[i]; j++)
438 elem_id = *(numele + j);
439 ok = mySMESHDSMesh->AddFaceWithID(*(connectivite +
440 j * (taille - nsup)),
441 *(connectivite + j * (taille - nsup) + 1),
442 *(connectivite + j * (taille - nsup) + 2),
444 //fprintf(Out,"%d %d %d %d\n",elem_id,*(connectivite+j*(taille-nsup)),*(connectivite+j*(taille-nsup)+1),*(connectivite+j*(taille-nsup)+2));
449 for (j = 0; j < nmailles[i]; j++)
452 ok = mySMESHDSMesh->AddFaceWithID(*(connectivite +
454 *(connectivite + j * (taille) + 1),
455 *(connectivite + j * (taille) + 2), cmpt);
456 //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2));
466 for (j = 0; j < nmailles[i]; j++)
468 elem_id = *(numele + j);
469 ok = mySMESHDSMesh->AddFaceWithID(*(connectivite +
470 j * (taille - nsup)),
471 *(connectivite + j * (taille - nsup) + 1),
472 *(connectivite + j * (taille - nsup) + 2),
473 *(connectivite + j * (taille - nsup) + 3),
475 //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));
480 for (j = 0; j < nmailles[i]; j++)
483 ok = myMesh->AddFaceWithID(*(connectivite +
485 *(connectivite + j * (taille) + 1),
486 *(connectivite + j * (taille) + 2),
487 *(connectivite + j * (taille) + 3), cmpt);
488 //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3));
497 for (j = 0; j < nmailles[i]; j++)
499 elem_id = *(numele + j);
500 ok = mySMESHDSMesh->AddVolumeWithID(*(connectivite +
501 j * (taille - nsup)),
502 *(connectivite + j * (taille - nsup) + 1),
503 *(connectivite + j * (taille - nsup) + 2),
504 *(connectivite + j * (taille - nsup) + 3),
506 //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));
511 for (j = 0; j < nmailles[i]; j++)
514 ok = mySMESHDSMesh->AddVolumeWithID(*(connectivite +
516 *(connectivite + j * (taille) + 1),
517 *(connectivite + j * (taille) + 2),
518 *(connectivite + j * (taille) + 3), cmpt);
519 //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3));
528 for (j = 0; j < nmailles[i]; j++)
530 elem_id = *(numele + j);
531 ok = mySMESHDSMesh->AddVolumeWithID(*(connectivite +
532 j * (taille - nsup)),
533 *(connectivite + j * (taille - nsup) + 1),
534 *(connectivite + j * (taille - nsup) + 2),
535 *(connectivite + j * (taille - nsup) + 3),
536 *(connectivite + j * (taille - nsup) + 4),
537 *(connectivite + j * (taille - nsup) + 5),
538 *(connectivite + j * (taille - nsup) + 6),
539 *(connectivite + j * (taille - nsup) + 7),
541 //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));
546 for (j = 0; j < nmailles[i]; j++)
549 ok = mySMESHDSMesh->AddVolumeWithID(*(connectivite +
551 *(connectivite + j * (taille) + 1),
552 *(connectivite + j * (taille) + 2),
553 *(connectivite + j * (taille) + 3),
554 *(connectivite + j * (taille) + 4),
555 *(connectivite + j * (taille) + 5),
556 *(connectivite + j * (taille) + 6),
557 *(connectivite + j * (taille) + 7), cmpt);
558 //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));
569 fprintf(stdout, "\n - Numéros de familles : \n");
570 for (j = 0; j < nmailles[i]; j++)
571 fprintf(stdout, " %d ", *(nufael + j));
573 /* liberation memoire */
581 /****************************************************************************
582 * LECTURE DES FAMILLES *
583 ****************************************************************************/
584 printf("\n(*************************)\n");
585 printf("(* FAMILLES DU MAILLAGE : *)\n");
586 printf("(*************************)\n");
588 for (i = 0; i < nfam; i++)
591 /* nombre de groupes */
592 ngro = MEDnFam(myFileId, nommaa, i + 1, MED_GROUPE);
597 ">> ERREUR : lecture du nombre de groupes d'une famille \n");
600 /* nombre d'attributs */
603 natt = MEDnFam(myFileId, nommaa, i + 1, MED_ATTR);
608 ">> ERREUR : lecture du nombre d'attributs d'une famille\n");
613 fprintf(stdout, "- Famille %d a %d attributs et %d groupes \n",
616 /* nom,numero,attributs,groupes */
619 attide = (med_int *) malloc(sizeof(med_int) * natt);
620 attval = (med_int *) malloc(sizeof(med_int) * natt);
621 attdes = (char *)malloc(MED_TAILLE_DESC * natt + 1);
622 gro = (char *)malloc(MED_TAILLE_LNOM * ngro + 1);
624 MEDfamInfo(myFileId, nommaa, i + 1, nomfam, &numfam, attide,
625 attval, attdes, &natt, gro, &ngro);
627 fam = string(nomfam);
628 fam_type = fam.substr(1, 1);
629 fam_id = fam.substr(2, 1);
630 if ((fam_type == string("V")) || (fam_type == string("A")) ||
631 (fam_type == string("F")))
632 LinkMeshToShape(fam_type, fam_id, family[i]);
634 fprintf(stdout, " - Famille de nom %s et de numero %d : \n",
636 fprintf(stdout, " - Attributs : \n");
637 for (j = 0; j < natt; j++)
639 strncpy(str1, attdes + j * MED_TAILLE_DESC,
641 str1[MED_TAILLE_DESC] = '\0';
642 fprintf(stdout, " ide = %d - val = %d - des = %s\n",
643 *(attide + j), *(attval + j), str1);
648 fprintf(stdout, " - Groupes :\n");
649 for (j = 0; j < ngro; j++)
651 strncpy(str2, gro + j * MED_TAILLE_LNOM, MED_TAILLE_LNOM);
652 str2[MED_TAILLE_LNOM] = '\0';
653 fprintf(stdout, " gro = %s\n", str2);
660 ret = MEDfermer(myFileId);
664 void DriverMED_R_SMESHDS_Mesh::LinkMeshToShape(string fam_type, string fam_id,
665 vector < int >myNodes)
668 SMESHDS_Mesh * mySMESHDSMesh = dynamic_cast<SMESHDS_Mesh *>(myMesh);
670 int id = atoi(fam_id.c_str());
671 if (fam_type == string("V"))
672 { //Linked to a vertex
673 for (int i = 0; i < myNodes.size(); i++)
675 const SMDS_MeshNode * node = mySMESHDSMesh->FindNode(myNodes[i]);
676 //const TopoDS_Vertex& S;//le recuperer !!!
677 //mySMESHDSMesh->SetNodeOnVertex (node,S);
680 else if (fam_type == string("E"))
681 { //Linked to an edge
682 for (int i = 0; i < myNodes.size(); i++)
684 const SMDS_MeshNode * node = mySMESHDSMesh->FindNode(myNodes[i]);
685 //const TopoDS_Edge& S;//le recuperer !!!
686 //mySMESHDSMesh->SetNodeOnEdge (node,S);
689 else if (fam_type == string("F"))
691 for (int i = 0; i < myNodes.size(); i++)
693 const SMDS_MeshNode * node = mySMESHDSMesh->FindNode(myNodes[i]);
694 //const TopoDS_Face& S;//le recuperer !!!
695 //mySMESHDSMesh->SetNodeOnFace (node,S);