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() {
38 DriverMED_R_SMESHDS_Mesh::~DriverMED_R_SMESHDS_Mesh() {
42 void DriverMED_R_SMESHDS_Mesh::SetMesh(Handle(SMDS_Mesh)& aMesh) {
43 //myMesh = Handle(SMESHDS_Mesh)::DownCast(aMesh);
47 void DriverMED_R_SMESHDS_Mesh::SetFile(string aFile) {
51 void DriverMED_R_SMESHDS_Mesh::SetFileId(med_idt aFileId) {
55 void DriverMED_R_SMESHDS_Mesh::SetMeshId(int aMeshId) {
59 void DriverMED_R_SMESHDS_Mesh::Read() {
61 string myClass = string("SMDS_Mesh");
62 string myExtension = string("MED");
64 DriverMED_R_SMDS_Mesh* myReader = new DriverMED_R_SMDS_Mesh;
66 myReader->SetMesh(myMesh);
67 myReader->SetMeshId(myMeshId);
68 myReader->SetFile(myFile);
69 myReader->SetFileId(-1);
75 void DriverMED_R_SMESHDS_Mesh::Add() {
77 string myClass = string("SMDS_Mesh");
78 string myExtension = string("MED");
80 DriverMED_R_SMDS_Mesh* myReader = new DriverMED_R_SMDS_Mesh;
82 myReader->SetMesh(myMesh);
83 myReader->SetMeshId(myMeshId);
86 myReader->SetFileId(myFileId);
92 void DriverMED_R_SMESHDS_Mesh::ReadMySelf() {
99 /* nombre d'objets MED */
100 char nom_universel[MED_TAILLE_LNOM+1];
101 med_int long_fichier_en_tete;
102 char *fichier_en_tete;
103 char version_hdf[10];
104 char version_med[10];
105 med_int nmaa,mdim,nnoe;
106 med_int nmai[MED_NBR_GEOMETRIE_MAILLE],nfac[MED_NBR_GEOMETRIE_FACE];
107 med_int nare[MED_NBR_GEOMETRIE_ARETE];
108 /* nom du maillage */
109 char nommaa[MED_TAILLE_NOM+1];
112 char nomcoo[3*MED_TAILLE_PNOM+1];
113 char unicoo[3*MED_TAILLE_PNOM+1];
118 med_booleen inonoe,inunoe;
119 med_mode_switch mode_coo;
120 char str[MED_TAILLE_PNOM+1];
127 med_int *connectivite;
131 med_booleen inoele, inuele;
132 med_connectivite typ_con;
133 med_geometrie_element typgeo;
134 med_geometrie_element typmai[MED_NBR_GEOMETRIE_MAILLE] = {MED_POINT1,MED_SEG2,
137 MED_QUAD8,MED_TETRA4,
138 MED_TETRA10,MED_HEXA8,
139 MED_HEXA20,MED_PENTA6,
140 MED_PENTA15,MED_PYRA5,
142 med_int desmai[MED_NBR_GEOMETRIE_MAILLE] = {0,2,3,3,3,4,4,4,4,6,6,5,5,5,5};
143 med_int nmailles[MED_NBR_GEOMETRIE_MAILLE];
144 char nommai[MED_NBR_GEOMETRIE_MAILLE] [MED_TAILLE_NOM+1] = {"MED_POINT1",
159 med_geometrie_element typfac[MED_NBR_GEOMETRIE_FACE] = {MED_TRIA3,MED_TRIA6,
160 MED_QUAD4,MED_QUAD8};
161 med_int desfac[MED_NBR_GEOMETRIE_FACE] = {3,3,4,4};
162 med_int nfaces[MED_NBR_GEOMETRIE_FACE];
163 char nomfac[MED_NBR_GEOMETRIE_FACE][MED_TAILLE_NOM+1] = {"MED_TRIA3","MED_TRIA6",
164 "MED_QUAD4","MED_QUAD8"};
165 med_geometrie_element typare[MED_NBR_GEOMETRIE_ARETE] = {MED_SEG2,MED_SEG3};
166 med_int desare[MED_NBR_GEOMETRIE_ARETE] = {2,3};
167 med_int naretes[MED_NBR_GEOMETRIE_ARETE];
168 char nomare[MED_NBR_GEOMETRIE_ARETE] [MED_TAILLE_NOM+1] = {"MED_SEG2","MED_SEG3"};
173 med_int *attval,*attide;
174 char nomfam[MED_TAILLE_NOM+1];
176 char str1[MED_TAILLE_DESC+1];
177 char str2[MED_TAILLE_LNOM+1];
183 bool locally_managed;
186 locally_managed = true;
188 locally_managed = false;
192 file2Read = (char*)myFile.c_str();
193 myFileId = MEDouvrir(file2Read,MED_LECT);
196 fprintf(stderr,">> ERREUR : ouverture du fichier %s \n",file2Read);
203 sprintf(nommaa,"Mesh %d",myMeshId);//pour load
207 mode_coo = MED_FULL_INTERLACE;
210 Handle(SMESHDS_Mesh) mySMESHDSMesh = Handle(SMESHDS_Mesh)::DownCast(myMesh);
212 //TopoDS_Shape myShape = mySMESHDSMesh->ShapeToMesh();
214 /****************************************************************************
215 * NOMBRES D'OBJETS MED *
216 ****************************************************************************/
217 fprintf(stdout,"\n(****************************)\n");
218 fprintf(stdout,"(* INFORMATIONS GENERALES : *)\n");
219 fprintf(stdout,"(****************************)\n");
221 /* lecture du nom et de la dimension du maillage */
222 /*! fprintf(stdout,"%d %d\n",myFileId,numero);
223 ret = MEDmaaInfo(myFileId,numero,nommaa,&mdim);
224 fprintf(stdout,"%d\n",ret);
227 fprintf(stderr,">> ERREUR : lecture du nom du maillage \n");
230 fprintf(stdout,"- Nom du maillage : <<%s>>\n",nommaa);
231 fprintf(stdout,"- Dimension du maillage : %d\n",mdim);
233 /* Combien de noeuds ? */
234 nnoe = MEDnEntMaa(myFileId,nommaa,MED_COOR,MED_NOEUD,MED_POINT1,typ_con);
237 fprintf(stderr,">> ERREUR : lecture du nombre de noeuds \n");
240 fprintf(stdout,"- Nombre de noeuds : %d \n",nnoe);
242 /* Combien de mailles, faces ou aretes ? */
243 for (i=0;i<MED_NBR_GEOMETRIE_MAILLE;i++)
245 nmailles[i] = MEDnEntMaa(myFileId,nommaa,MED_CONN,MED_MAILLE,typmai[i],
249 fprintf(stderr,">> ERREUR : lecture du nombre de mailles \n");
252 fprintf (stdout,"- Nombre de mailles de type %s : %d \n",nommai[i],nmailles[i]);
255 for (i=0;i<MED_NBR_GEOMETRIE_FACE;i++)
257 nfaces[i] = MEDnEntMaa(myFileId,nommaa,MED_CONN,MED_FACE,typfac[i],
261 fprintf(stderr,">> ERREUR : lecture du nombre de faces \n");
264 fprintf (stdout,"- Nombre de faces de type %s : %d \n",nomfac[i],nfaces[i]);
267 for (i=0;i<MED_NBR_GEOMETRIE_ARETE;i++)
269 naretes[i] = MEDnEntMaa(myFileId,nommaa,MED_CONN,MED_ARETE,typare[i],
273 fprintf(stderr,">> ERREUR : lecture du nombre d'aretes \n");
276 fprintf (stdout,"- Nombre d'aretes de type %s : %d \n",nomare[i],naretes[i]);
279 /* nombre de familles */
280 nfam = MEDnFam(myFileId,nommaa,0,MED_FAMILLE);
283 fprintf(stderr,">> ERREUR : lecture du nombre de familles \n");
286 fprintf(stdout,"- Nombre de familles : %d \n",nfam);
288 vector<int> family[nfam];
290 /****************************************************************************
291 * LECTURE DES NOEUDS *
292 ****************************************************************************/
293 fprintf(stdout,"\n(************************)\n");
294 fprintf(stdout,"(* NOEUDS DU MAILLAGE : *)\n");
295 fprintf(stdout,"(************************)\n");
297 /* Allocations memoires */
298 /* table des coordonnees
299 profil : (dimension * nombre de noeuds ) */
300 coo = (med_float*) malloc(sizeof(med_float)*nnoe*mdim);
301 /* table des numeros, des numeros de familles des noeuds
302 profil : (nombre de noeuds) */
303 numnoe = (med_int*) malloc(sizeof(med_int)*nnoe);
304 nufano = (med_int*) malloc(sizeof(med_int)*nnoe);
305 /* table des noms des noeuds
306 profil : (nnoe*MED_TAILLE_PNOM+1) */
307 nomnoe = (char*) malloc(MED_TAILLE_PNOM*nnoe+1);
309 /* lecture des noeuds :
311 - noms (optionnel dans un fichier MED)
312 - numeros (optionnel dans un fichier MED)
313 - numeros des familles */
314 ret = MEDnoeudsLire(myFileId,nommaa,mdim,coo,mode_coo,&rep,
315 nomcoo,unicoo,nomnoe,&inonoe,numnoe,&inunoe,
318 strcpy(message,">> ERREUR : lecture des noeuds \n");
321 for (int i=0;i<nnoe;i++) {
322 ok = mySMESHDSMesh->AddNodeWithID(coo[i*3],coo[i*3+1],coo[i*3+2],numnoe[i]);
323 //fprintf(Out,"%d %f %f %f\n",numnoe[i],coo[i*3],coo[i*3+1],coo[i*3+2]);
327 for (int i=0;i<nnoe;i++) {
328 ok = mySMESHDSMesh->AddNodeWithID(coo[i*3],coo[i*3+1],coo[i*3+2],i+1);
329 //fprintf(Out,"%d %f %f %f\n",numnoe[i],coo[i*3],coo[i*3+1],i);
330 family[*(nufano+i)].push_back(numnoe[i]);
334 fprintf(stdout,"\n- Numeros des familles des noeuds : \n");
336 fprintf(stdout," %d ",*(nufano+i));
337 fprintf(stdout,"\n");
339 /* liberation memoire */
345 /****************************************************************************
346 * LECTURE DES ELEMENTS *
347 ****************************************************************************/
348 fprintf(stdout,"\n(**************************)\n");
349 fprintf(stdout,"(* ELEMENTS DU MAILLAGE : *)\n");
350 fprintf(stdout,"(**************************)");
351 //fprintf(Out,"CELLS\n");
352 /* Lecture des connectivites, noms, numeros des mailles */
353 //printf("%d %d %d %d\n",nmailles[3],nmailles[4],nmailles[5],nmailles[9]);
356 for (i=0;i<MED_NBR_GEOMETRIE_MAILLE;i++)
358 if (nmailles[i] > 0 && ret == 0)
360 /* dimension de la maille */
361 edim = typmai[i] / 100;
363 if (mdim == 2 || mdim == 3)
370 taille = nsup+typmai[i]%100;
371 //taille = typmai[i]%100;
373 /* allocation memoire */
374 connectivite = (med_int*)malloc(sizeof(med_int)*
376 nomele = (char*)malloc(sizeof(char)*MED_TAILLE_PNOM*
378 numele = (med_int*)malloc(sizeof(med_int)*
380 nufael = (med_int*)malloc(sizeof(med_int)*
383 /* lecture des données */
384 ret = MEDelementsLire(myFileId,nommaa,mdim,connectivite,mode_coo,
385 nomele,&inoele,numele,&inuele,nufael,
386 nmailles[i],MED_MAILLE,typmai[i],
393 for (j=0;j<nmailles[i];j++) {
395 ok = mySMESHDSMesh->AddFaceWithID(*(connectivite+j*(taille-nsup)),*(connectivite+j*(taille-nsup)+1),*(connectivite+j*(taille-nsup)+2),elem_id);
396 //fprintf(Out,"%d %d %d %d\n",elem_id,*(connectivite+j*(taille-nsup)),*(connectivite+j*(taille-nsup)+1),*(connectivite+j*(taille-nsup)+2));
400 for (j=0;j<nmailles[i];j++) {
402 ok = mySMESHDSMesh->AddFaceWithID(*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),cmpt);
403 //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2));
411 for (j=0;j<nmailles[i];j++) {
413 ok = mySMESHDSMesh->AddFaceWithID(*(connectivite+j*(taille-nsup)),*(connectivite+j*(taille-nsup)+1),*(connectivite+j*(taille-nsup)+2),*(connectivite+j*(taille-nsup)+3),elem_id);
414 //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));
418 for (j=0;j<nmailles[i];j++) {
420 ok = myMesh->AddFaceWithID(*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3),cmpt);
421 //fprintf(Out,"%d %d %d %d\n",j,*(connectivite+j*(taille)),*(connectivite+j*(taille)+1),*(connectivite+j*(taille)+2),*(connectivite+j*(taille)+3));
428 for (j=0;j<nmailles[i];j++) {
430 ok = mySMESHDSMesh->AddVolumeWithID(*(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),elem_id);
431 //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));
435 for (j=0;j<nmailles[i];j++) {
437 ok = mySMESHDSMesh->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);
438 //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));
448 fprintf(stdout,"\n - Numéros de familles : \n");
449 for (j=0;j<nmailles[i];j++)
450 fprintf(stdout," %d ",*(nufael+j));
452 /* liberation memoire */
460 /****************************************************************************
461 * LECTURE DES FAMILLES *
462 ****************************************************************************/
463 printf("\n(*************************)\n");
464 printf("(* FAMILLES DU MAILLAGE : *)\n");
465 printf("(*************************)\n");
470 /* nombre de groupes */
471 ngro = MEDnFam(myFileId,nommaa,i+1,MED_GROUPE);
476 ">> ERREUR : lecture du nombre de groupes d'une famille \n");
479 /* nombre d'attributs */
482 natt = MEDnFam(myFileId,nommaa,i+1,MED_ATTR);
487 ">> ERREUR : lecture du nombre d'attributs d'une famille\n");
492 fprintf(stdout,"- Famille %d a %d attributs et %d groupes \n",i+1,natt,ngro);
494 /* nom,numero,attributs,groupes */
497 attide = (med_int*) malloc(sizeof(med_int)*natt);
498 attval = (med_int*) malloc(sizeof(med_int)*natt);
499 attdes = (char *) malloc(MED_TAILLE_DESC*natt+1);
500 gro = (char*) malloc(MED_TAILLE_LNOM*ngro+1);
501 ret = MEDfamInfo(myFileId,nommaa,i+1,nomfam,&numfam,attide,attval,
502 attdes,&natt,gro,&ngro);
504 fam = string(nomfam);
505 fam_type = fam.substr(1,1);
506 fam_id = fam.substr(2,1);
507 if ((fam_type==string("V"))||(fam_type==string("A"))||(fam_type==string("F")))
508 LinkMeshToShape(fam_type, fam_id, family[i]);
510 fprintf(stdout," - Famille de nom %s et de numero %d : \n",nomfam,numfam);
511 fprintf(stdout," - Attributs : \n");
514 strncpy(str1,attdes+j*MED_TAILLE_DESC,MED_TAILLE_DESC);
515 str1[MED_TAILLE_DESC] = '\0';
516 fprintf(stdout," ide = %d - val = %d - des = %s\n",*(attide+j),
522 fprintf(stdout," - Groupes :\n");
525 strncpy(str2,gro+j*MED_TAILLE_LNOM,MED_TAILLE_LNOM);
526 str2[MED_TAILLE_LNOM] = '\0';
527 fprintf(stdout," gro = %s\n",str2);
534 ret = MEDfermer(myFileId);
538 void DriverMED_R_SMESHDS_Mesh::LinkMeshToShape(string fam_type, string fam_id, vector<int> myNodes) {
540 Handle(SMESHDS_Mesh) mySMESHDSMesh = Handle(SMESHDS_Mesh)::DownCast(myMesh);
542 int id = atoi(fam_id.c_str());
543 if (fam_type==string("V")) { //Linked to a vertex
544 for (int i=0;i<myNodes.size();i++) {
545 Handle(SMDS_MeshElement) elem = mySMESHDSMesh->FindNode(myNodes[i]);
546 const Handle(SMDS_MeshNode)& node = mySMESHDSMesh->GetNode(1,elem);
547 //const TopoDS_Vertex& S;//le recuperer !!!
548 //mySMESHDSMesh->SetNodeOnVertex (node,S);
552 if (fam_type==string("E")) { //Linked to an edge
553 for (int i=0;i<myNodes.size();i++) {
554 Handle(SMDS_MeshElement) elem = mySMESHDSMesh->FindNode(myNodes[i]);
555 const Handle(SMDS_MeshNode)& node = mySMESHDSMesh->GetNode(1,elem);
556 //const TopoDS_Edge& S;//le recuperer !!!
557 //mySMESHDSMesh->SetNodeOnEdge (node,S);
561 if (fam_type==string("F")) { //Linked to a face
562 for (int i=0;i<myNodes.size();i++) {
563 Handle(SMDS_MeshElement) elem = mySMESHDSMesh->FindNode(myNodes[i]);
564 const Handle(SMDS_MeshNode)& node = mySMESHDSMesh->GetNode(1,elem);
565 //const TopoDS_Face& S;//le recuperer !!!
566 //mySMESHDSMesh->SetNodeOnFace (node,S);