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"
34 DriverMED_R_SMESHDS_Mesh::DriverMED_R_SMESHDS_Mesh()
39 DriverMED_R_SMESHDS_Mesh::~DriverMED_R_SMESHDS_Mesh()
44 void DriverMED_R_SMESHDS_Mesh::SetMesh(SMDS_Mesh * aMesh)
46 //myMesh = SMESHDS_Mesh *::DownCast(aMesh);
50 void DriverMED_R_SMESHDS_Mesh::SetFile(string aFile)
55 void DriverMED_R_SMESHDS_Mesh::SetFileId(med_idt aFileId)
60 void DriverMED_R_SMESHDS_Mesh::SetMeshId(int aMeshId)
65 void DriverMED_R_SMESHDS_Mesh::Read()
68 string myClass = string("SMDS_Mesh");
69 string myExtension = string("MED");
71 DriverMED_R_SMDS_Mesh *myReader = new DriverMED_R_SMDS_Mesh;
73 myReader->SetMesh(myMesh);
74 myReader->SetMeshId(myMeshId);
75 myReader->SetFile(myFile);
76 myReader->SetFileId(-1);
82 void DriverMED_R_SMESHDS_Mesh::Add()
85 string myClass = string("SMDS_Mesh");
86 string myExtension = string("MED");
88 DriverMED_R_SMDS_Mesh *myReader = new DriverMED_R_SMDS_Mesh;
90 myReader->SetMesh(myMesh);
91 myReader->SetMeshId(myMeshId);
94 myReader->SetFileId(myFileId);
100 void DriverMED_R_SMESHDS_Mesh::ReadMySelf()
108 /* nombre d'objets MED */
109 char nom_universel[MED_TAILLE_LNOM + 1];
110 med_int long_fichier_en_tete;
111 char *fichier_en_tete;
112 char version_hdf[10];
113 char version_med[10];
114 med_int nmaa, mdim, nnoe;
115 med_int nmai[MED_NBR_GEOMETRIE_MAILLE], nfac[MED_NBR_GEOMETRIE_FACE];
116 med_int nare[MED_NBR_GEOMETRIE_ARETE];
117 /* nom du maillage */
118 char nommaa[MED_TAILLE_NOM + 1];
121 char nomcoo[3 * MED_TAILLE_PNOM + 1];
122 char unicoo[3 * MED_TAILLE_PNOM + 1];
127 med_booleen inonoe, inunoe;
128 med_mode_switch mode_coo;
129 char str[MED_TAILLE_PNOM + 1];
136 med_int *connectivite;
140 med_booleen inoele, inuele;
141 med_connectivite typ_con;
142 med_geometrie_element typgeo;
143 med_geometrie_element typmai[MED_NBR_GEOMETRIE_MAILLE] =
144 { MED_POINT1, MED_SEG2,
146 MED_TRIA6, MED_QUAD4,
147 MED_QUAD8, MED_TETRA4,
148 MED_TETRA10, MED_HEXA8,
149 MED_HEXA20, MED_PENTA6,
150 MED_PENTA15, MED_PYRA5,
153 med_int desmai[MED_NBR_GEOMETRIE_MAILLE] =
154 { 0, 2, 3, 3, 3, 4, 4, 4, 4, 6, 6, 5, 5, 5, 5 };
155 med_int nmailles[MED_NBR_GEOMETRIE_MAILLE];
156 char nommai[MED_NBR_GEOMETRIE_MAILLE][MED_TAILLE_NOM + 1] = { "MED_POINT1",
172 med_geometrie_element typfac[MED_NBR_GEOMETRIE_FACE] =
173 { MED_TRIA3, MED_TRIA6,
176 med_int desfac[MED_NBR_GEOMETRIE_FACE] = { 3, 3, 4, 4 };
177 med_int nfaces[MED_NBR_GEOMETRIE_FACE];
178 char nomfac[MED_NBR_GEOMETRIE_FACE][MED_TAILLE_NOM + 1] =
179 { "MED_TRIA3", "MED_TRIA6",
180 "MED_QUAD4", "MED_QUAD8"
182 med_geometrie_element typare[MED_NBR_GEOMETRIE_ARETE] =
183 { MED_SEG2, MED_SEG3 };
184 med_int desare[MED_NBR_GEOMETRIE_ARETE] = { 2, 3 };
185 med_int naretes[MED_NBR_GEOMETRIE_ARETE];
186 char nomare[MED_NBR_GEOMETRIE_ARETE][MED_TAILLE_NOM + 1] =
187 { "MED_SEG2", "MED_SEG3" };
192 med_int *attval, *attide;
193 char nomfam[MED_TAILLE_NOM + 1];
195 char str1[MED_TAILLE_DESC + 1];
196 char str2[MED_TAILLE_LNOM + 1];
202 bool locally_managed;
205 locally_managed = true;
207 locally_managed = false;
211 file2Read = (char *)myFile.c_str();
212 myFileId = MEDouvrir(file2Read, MED_LECT);
215 fprintf(stderr, ">> ERREUR : ouverture du fichier %s \n",
223 sprintf(nommaa, "Mesh %d", myMeshId); //pour load
227 mode_coo = MED_FULL_INTERLACE;
230 SMESHDS_Mesh * mySMESHDSMesh = dynamic_cast<SMESHDS_Mesh *>(myMesh);
232 //TopoDS_Shape myShape = mySMESHDSMesh->ShapeToMesh();
234 /****************************************************************************
235 * NOMBRES D'OBJETS MED *
236 ****************************************************************************/
237 fprintf(stdout, "\n(****************************)\n");
238 fprintf(stdout, "(* INFORMATIONS GENERALES : *)\n");
239 fprintf(stdout, "(****************************)\n");
241 /* lecture du nom et de la dimension du maillage */
242 /*! fprintf(stdout,"%d %d\n",myFileId,numero);
243 * ret = MEDmaaInfo(myFileId,numero,nommaa,&mdim);
244 * fprintf(stdout,"%d\n",ret);
247 * fprintf(stderr,">> ERREUR : lecture du nom du maillage \n");
248 * exit(EXIT_FAILURE);
250 * fprintf(stdout,"- Nom du maillage : <<%s>>\n",nommaa);
251 * fprintf(stdout,"- Dimension du maillage : %d\n",mdim);
253 /* Combien de noeuds ? */
255 MEDnEntMaa(myFileId, nommaa, MED_COOR, MED_NOEUD, MED_POINT1, typ_con);
258 fprintf(stderr, ">> ERREUR : lecture du nombre de noeuds \n");
261 fprintf(stdout, "- Nombre de noeuds : %d \n", nnoe);
263 /* Combien de mailles, faces ou aretes ? */
264 for (i = 0; i < MED_NBR_GEOMETRIE_MAILLE; i++)
267 MEDnEntMaa(myFileId, nommaa, MED_CONN, MED_MAILLE, typmai[i],
271 fprintf(stderr, ">> ERREUR : lecture du nombre de mailles \n");
274 fprintf(stdout, "- Nombre de mailles de type %s : %d \n", nommai[i],
278 for (i = 0; i < MED_NBR_GEOMETRIE_FACE; i++)
280 nfaces[i] = MEDnEntMaa(myFileId, nommaa, MED_CONN, MED_FACE, typfac[i],
284 fprintf(stderr, ">> ERREUR : lecture du nombre de faces \n");
287 fprintf(stdout, "- Nombre de faces de type %s : %d \n", nomfac[i],
291 for (i = 0; i < MED_NBR_GEOMETRIE_ARETE; i++)
294 MEDnEntMaa(myFileId, nommaa, MED_CONN, MED_ARETE, typare[i],
298 fprintf(stderr, ">> ERREUR : lecture du nombre d'aretes \n");
301 fprintf(stdout, "- Nombre d'aretes de type %s : %d \n", nomare[i],
305 /* nombre de familles */
306 nfam = MEDnFam(myFileId, nommaa, 0, MED_FAMILLE);
309 fprintf(stderr, ">> ERREUR : lecture du nombre de familles \n");
312 fprintf(stdout, "- Nombre de familles : %d \n", nfam);
314 vector < int >family[nfam];
316 /****************************************************************************
317 * LECTURE DES NOEUDS *
318 ****************************************************************************/
319 fprintf(stdout, "\n(************************)\n");
320 fprintf(stdout, "(* NOEUDS DU MAILLAGE : *)\n");
321 fprintf(stdout, "(************************)\n");
323 /* Allocations memoires */
324 /* table des coordonnees
325 * profil : (dimension * nombre de noeuds ) */
326 coo = (med_float *) malloc(sizeof(med_float) * nnoe * mdim);
327 /* table des numeros, des numeros de familles des noeuds
328 * profil : (nombre de noeuds) */
329 numnoe = (med_int *) malloc(sizeof(med_int) * nnoe);
330 nufano = (med_int *) malloc(sizeof(med_int) * nnoe);
331 /* table des noms des noeuds
332 * profil : (nnoe*MED_TAILLE_PNOM+1) */
333 nomnoe = (char *)malloc(MED_TAILLE_PNOM * nnoe + 1);
335 /* lecture des noeuds :
337 * - noms (optionnel dans un fichier MED)
338 * - numeros (optionnel dans un fichier MED)
339 * - numeros des familles */
340 ret = MEDnoeudsLire(myFileId, nommaa, mdim, coo, mode_coo, &rep,
341 nomcoo, unicoo, nomnoe, &inonoe, numnoe, &inunoe, nufano, nnoe);
343 strcpy(message, ">> ERREUR : lecture des noeuds \n");
347 for (int i = 0; i < nnoe; i++)
349 ok = mySMESHDSMesh->AddNodeWithID(coo[i * 3], coo[i * 3 + 1],
350 coo[i * 3 + 2], numnoe[i]);
351 //fprintf(Out,"%d %f %f %f\n",numnoe[i],coo[i*3],coo[i*3+1],coo[i*3+2]);
356 for (int i = 0; i < nnoe; i++)
358 ok = mySMESHDSMesh->AddNodeWithID(coo[i * 3], coo[i * 3 + 1],
359 coo[i * 3 + 2], i + 1);
360 //fprintf(Out,"%d %f %f %f\n",numnoe[i],coo[i*3],coo[i*3+1],i);
361 family[*(nufano + i)].push_back(numnoe[i]);
365 fprintf(stdout, "\n- Numeros des familles des noeuds : \n");
366 for (i = 0; i < nnoe; i++)
367 fprintf(stdout, " %d ", *(nufano + i));
368 fprintf(stdout, "\n");
370 /* liberation memoire */
376 /****************************************************************************
377 * LECTURE DES ELEMENTS *
378 ****************************************************************************/
379 fprintf(stdout, "\n(**************************)\n");
380 fprintf(stdout, "(* ELEMENTS DU MAILLAGE : *)\n");
381 fprintf(stdout, "(**************************)");
382 //fprintf(Out,"CELLS\n");
383 /* Lecture des connectivites, noms, numeros des mailles */
384 //printf("%d %d %d %d\n",nmailles[3],nmailles[4],nmailles[5],nmailles[9]);
387 for (i = 0; i < MED_NBR_GEOMETRIE_MAILLE; i++)
389 if (nmailles[i] > 0 && ret == 0)
391 /* dimension de la maille */
392 edim = typmai[i] / 100;
394 if (mdim == 2 || mdim == 3)
401 taille = nsup + typmai[i] % 100;
402 //taille = typmai[i]%100;
404 /* allocation memoire */
405 connectivite = (med_int *) malloc(sizeof(med_int) *
406 taille * nmailles[i]);
407 nomele = (char *)malloc(sizeof(char) * MED_TAILLE_PNOM *
409 numele = (med_int *) malloc(sizeof(med_int) * nmailles[i]);
410 nufael = (med_int *) malloc(sizeof(med_int) * nmailles[i]);
412 /* lecture des données */
414 MEDelementsLire(myFileId, nommaa, mdim, connectivite,
415 mode_coo, nomele, &inoele, numele, &inuele, nufael,
416 nmailles[i], MED_MAILLE, typmai[i], typ_con);
424 for (j = 0; j < nmailles[i]; j++)
426 elem_id = *(numele + j);
427 ok = mySMESHDSMesh->AddFaceWithID(*(connectivite +
428 j * (taille - nsup)),
429 *(connectivite + j * (taille - nsup) + 1),
430 *(connectivite + j * (taille - nsup) + 2),
432 //fprintf(Out,"%d %d %d %d\n",elem_id,*(connectivite+j*(taille-nsup)),*(connectivite+j*(taille-nsup)+1),*(connectivite+j*(taille-nsup)+2));
437 for (j = 0; j < nmailles[i]; j++)
440 ok = mySMESHDSMesh->AddFaceWithID(*(connectivite +
442 *(connectivite + j * (taille) + 1),
443 *(connectivite + j * (taille) + 2), cmpt);
444 //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2));
454 for (j = 0; j < nmailles[i]; j++)
456 elem_id = *(numele + j);
457 ok = mySMESHDSMesh->AddFaceWithID(*(connectivite +
458 j * (taille - nsup)),
459 *(connectivite + j * (taille - nsup) + 1),
460 *(connectivite + j * (taille - nsup) + 2),
461 *(connectivite + j * (taille - nsup) + 3),
463 //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));
468 for (j = 0; j < nmailles[i]; j++)
471 ok = myMesh->AddFaceWithID(*(connectivite +
473 *(connectivite + j * (taille) + 1),
474 *(connectivite + j * (taille) + 2),
475 *(connectivite + j * (taille) + 3), cmpt);
476 //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3));
485 for (j = 0; j < nmailles[i]; j++)
487 elem_id = *(numele + j);
488 ok = mySMESHDSMesh->AddVolumeWithID(*(connectivite +
489 j * (taille - nsup)),
490 *(connectivite + j * (taille - nsup) + 1),
491 *(connectivite + j * (taille - nsup) + 2),
492 *(connectivite + j * (taille - nsup) + 3),
494 //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));
499 for (j = 0; j < nmailles[i]; j++)
502 ok = mySMESHDSMesh->AddVolumeWithID(*(connectivite +
504 *(connectivite + j * (taille) + 1),
505 *(connectivite + j * (taille) + 2),
506 *(connectivite + j * (taille) + 3), cmpt);
507 //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3));
516 for (j = 0; j < nmailles[i]; j++)
518 elem_id = *(numele + j);
519 ok = mySMESHDSMesh->AddVolumeWithID(*(connectivite +
520 j * (taille - nsup)),
521 *(connectivite + j * (taille - nsup) + 1),
522 *(connectivite + j * (taille - nsup) + 2),
523 *(connectivite + j * (taille - nsup) + 3),
524 *(connectivite + j * (taille - nsup) + 4),
525 *(connectivite + j * (taille - nsup) + 5),
526 *(connectivite + j * (taille - nsup) + 6),
527 *(connectivite + j * (taille - nsup) + 7),
529 //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));
534 for (j = 0; j < nmailles[i]; j++)
537 ok = mySMESHDSMesh->AddVolumeWithID(*(connectivite +
539 *(connectivite + j * (taille) + 1),
540 *(connectivite + j * (taille) + 2),
541 *(connectivite + j * (taille) + 3),
542 *(connectivite + j * (taille) + 4),
543 *(connectivite + j * (taille) + 5),
544 *(connectivite + j * (taille) + 6),
545 *(connectivite + j * (taille) + 7), cmpt);
546 //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));
557 fprintf(stdout, "\n - Numéros de familles : \n");
558 for (j = 0; j < nmailles[i]; j++)
559 fprintf(stdout, " %d ", *(nufael + j));
561 /* liberation memoire */
569 /****************************************************************************
570 * LECTURE DES FAMILLES *
571 ****************************************************************************/
572 printf("\n(*************************)\n");
573 printf("(* FAMILLES DU MAILLAGE : *)\n");
574 printf("(*************************)\n");
576 for (i = 0; i < nfam; i++)
579 /* nombre de groupes */
580 ngro = MEDnFam(myFileId, nommaa, i + 1, MED_GROUPE);
585 ">> ERREUR : lecture du nombre de groupes d'une famille \n");
588 /* nombre d'attributs */
591 natt = MEDnFam(myFileId, nommaa, i + 1, MED_ATTR);
596 ">> ERREUR : lecture du nombre d'attributs d'une famille\n");
601 fprintf(stdout, "- Famille %d a %d attributs et %d groupes \n",
604 /* nom,numero,attributs,groupes */
607 attide = (med_int *) malloc(sizeof(med_int) * natt);
608 attval = (med_int *) malloc(sizeof(med_int) * natt);
609 attdes = (char *)malloc(MED_TAILLE_DESC * natt + 1);
610 gro = (char *)malloc(MED_TAILLE_LNOM * ngro + 1);
612 MEDfamInfo(myFileId, nommaa, i + 1, nomfam, &numfam, attide,
613 attval, attdes, &natt, gro, &ngro);
615 fam = string(nomfam);
616 fam_type = fam.substr(1, 1);
617 fam_id = fam.substr(2, 1);
618 if ((fam_type == string("V")) || (fam_type == string("A")) ||
619 (fam_type == string("F")))
620 LinkMeshToShape(fam_type, fam_id, family[i]);
622 fprintf(stdout, " - Famille de nom %s et de numero %d : \n",
624 fprintf(stdout, " - Attributs : \n");
625 for (j = 0; j < natt; j++)
627 strncpy(str1, attdes + j * MED_TAILLE_DESC,
629 str1[MED_TAILLE_DESC] = '\0';
630 fprintf(stdout, " ide = %d - val = %d - des = %s\n",
631 *(attide + j), *(attval + j), str1);
636 fprintf(stdout, " - Groupes :\n");
637 for (j = 0; j < ngro; j++)
639 strncpy(str2, gro + j * MED_TAILLE_LNOM, MED_TAILLE_LNOM);
640 str2[MED_TAILLE_LNOM] = '\0';
641 fprintf(stdout, " gro = %s\n", str2);
648 ret = MEDfermer(myFileId);
652 void DriverMED_R_SMESHDS_Mesh::LinkMeshToShape(string fam_type, string fam_id,
653 vector < int >myNodes)
656 SMESHDS_Mesh * mySMESHDSMesh = dynamic_cast<SMESHDS_Mesh *>(myMesh);
658 int id = atoi(fam_id.c_str());
659 if (fam_type == string("V"))
660 { //Linked to a vertex
661 for (int i = 0; i < myNodes.size(); i++)
663 const SMDS_MeshNode * node = mySMESHDSMesh->FindNode(myNodes[i]);
664 //const TopoDS_Vertex& S;//le recuperer !!!
665 //mySMESHDSMesh->SetNodeOnVertex (node,S);
668 else if (fam_type == string("E"))
669 { //Linked to an edge
670 for (int i = 0; i < myNodes.size(); i++)
672 const SMDS_MeshNode * node = mySMESHDSMesh->FindNode(myNodes[i]);
673 //const TopoDS_Edge& S;//le recuperer !!!
674 //mySMESHDSMesh->SetNodeOnEdge (node,S);
677 else if (fam_type == string("F"))
679 for (int i = 0; i < myNodes.size(); i++)
681 const SMDS_MeshNode * node = mySMESHDSMesh->FindNode(myNodes[i]);
682 //const TopoDS_Face& S;//le recuperer !!!
683 //mySMESHDSMesh->SetNodeOnFace (node,S);