void _MEDmeshnEntity236(int dummy, ...) { med_int _ret=-1,_n=0,_tmpn=0; med_size _nn =0; med_data_type _meddatatype; med_int _nfaces=0; char _meshpath[MED_TAILLE_MAA+MED_TAILLE_NOM+1]=MED_MAA; med_idt _meshid=0,_datagroup1=0; char _datagroupname1[MED_TAILLE_NOM_ENTITE+1]=""; med_err _err =0; med_bool _datagroupexist=MED_FALSE; med_bool _isasoftlink =MED_FALSE; int _cmodeit=0, _ncmode=1; med_connectivite _cmode[2] = { MED_NO_CMODE, MED_NO_CMODE }; MED_VARGS_DECL(const, med_idt , , fid ); MED_VARGS_DECL(const, char * , const , meshname ); MED_VARGS_DECL(const, med_int , , numdt ); MED_VARGS_DECL(const, med_int , , numit ); MED_VARGS_DECL(const, med_entity_type , , entitytype ); MED_VARGS_DECL(const, med_geometry_type , , geotype ); MED_VARGS_DECL(const, med_data_type , , meddatatype ); MED_VARGS_DECL(const, med_connectivity_mode , , cmode ); MED_VARGS_DECL(const, med_storage_mode , , storagemode ); MED_VARGS_DECL(, char *, const , profilename ); MED_VARGS_DECL(, med_int *, const , profilesize ); MED_VARGS_DECL(, med_bool *, const , changement ); MED_VARGS_DECL(, med_bool *, const , transformation ); MED_VARGS_DECL(, med_int * ,, fret ); va_list params; va_start(params,dummy); MED_VARGS_DEF(const, med_idt , , fid ); MED_VARGS_DEF(const, char * , const , meshname ); MED_VARGS_DEF(const, med_int , , numdt ); MED_VARGS_DEF(const, med_int , , numit ); MED_VARGS_DEF(const, med_entity_type , , entitytype ); MED_VARGS_DEF(const, med_geometry_type , , geotype ); MED_VARGS_DEF(const, med_data_type , , meddatatype ); MED_VARGS_DEF(const, med_connectivity_mode , , cmode ); MED_VARGS_DEF(const, med_storage_mode , , storagemode ); MED_VARGS_DEF(, char *, const , profilename ); MED_VARGS_DEF(, med_int *, const , profilesize ); MED_VARGS_DEF(, med_bool *, const , changement ); MED_VARGS_DEF(, med_bool *, const , transformation ); MED_VARGS_DEF(, med_int * ,, fret ); _meddatatype=meddatatype; if ( (numdt != MED_NO_DT) || (numit != MED_NO_IT) ) { MED_ERR_(_ret,MED_ERR_RANGE,MED_ERR_COMPUTINGSTEP,MED_ERR_MESH_MSG); SSCRUTE(meshname);ISCRUTE(numdt);ISCRUTE(numit); goto ERROR; } _cmode[0]= (med_connectivite) cmode; if ( entitytype == MED_UNDEF_ENTITY_TYPE ) goto SORTIE; /* * Gestion entitytype == MED_ALL_ENTITY_TYPE */ if ( entitytype == MED_ALL_ENTITY_TYPE ) { strcat(_meshpath,meshname); if ((_meshid = _MEDdatagroupOuvrir(fid,_meshpath)) < 0) { MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_DATAGROUP,_meshpath); ISCRUTE_id(_meshid);goto ERROR; } if( _MEDdatagroupExist(_meshid,MED_NOM_NOE,&_datagroupexist,&_isasoftlink) < 0 ) { MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"_MEDdatagroupExist"); SSCRUTE(MED_NOM_NOE);goto ERROR; } if ( _datagroupexist ) _nn++; if( _MEDdatagroupExist(_meshid,MED_NOM_MAI,&_datagroupexist,&_isasoftlink) < 0 ) { MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"_MEDdatagroupExist"); SSCRUTE(MED_NOM_MAI);goto ERROR; } if ( _datagroupexist ) _nn++; if( _MEDdatagroupExist(_meshid,MED_NOM_FAC,&_datagroupexist,&_isasoftlink) < 0 ) { MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"_MEDdatagroupExist"); SSCRUTE(MED_NOM_FAC);goto ERROR; } if ( _datagroupexist ) _nn++; if( _MEDdatagroupExist(_meshid,MED_NOM_ARE,&_datagroupexist,&_isasoftlink) < 0 ) { MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"_MEDdatagroupExist"); SSCRUTE(MED_NOM_ARE);goto ERROR; } if ( _datagroupexist ) _nn++; /* En 2.3.6 les familles, les équivalences ... sont stockées dans le groupe <meshname>*/ /* _err=_MEDnObjects(_meshid,".",&_nn); */ /* if ( _err == (MED_ERR_COUNT + MED_ERR_DATAGROUP) ) { */ /* MED_ERR_(_ret,MED_ERR_COUNT,MED_ERR_DATAGROUP,_meshid); */ /* goto ERROR; */ /* } */ _n = _nn; goto SORTIE; } if ( geotype == MED_GEO_ALL ) { strcat(_meshpath,meshname); if ((_meshid = _MEDdatagroupOuvrir(fid,_meshpath)) < 0) { MED_ERR_(_ret,MED_ERR_OPEN,MED_ERR_DATAGROUP,_meshpath); ISCRUTE_id(_meshid);goto ERROR; } if (_MEDgetEntityTypeName(_datagroupname1,entitytype) < 0) { MED_ERR_(_ret,MED_ERR_INVALID,MED_ERR_ENTITY,MED_ERR_VALUE_MSG); ISCRUTE_int(entitytype);SSCRUTE(meshname);goto ERROR; } if ((_datagroup1 = _MEDdatagroupOuvrir(_meshid,_datagroupname1)) < 0) { _n=0;_datagroup1=0; goto SORTIE; } _err=_MEDnObjects(_datagroup1,".",&_nn); if ( _err == (MED_ERR_COUNT + MED_ERR_DATAGROUP) ) { MED_ERR_(_ret,MED_ERR_COUNT,MED_ERR_DATAGROUP,_datagroupname1); goto ERROR; } /* SSCRUTE(_datagroupname1);ISCRUTE_long(_nn); */ if ( ( entitytype == MED_NODE ) && (_nn > 0) ) _nn=1; /* SSCRUTE(_datagroupname1);ISCRUTE_long(_nn); */ _n = _nn; goto SORTIE; } switch( meddatatype ) { case MED_GLOBAL_NUMBER: case MED_NAME: case MED_NUMBER: case MED_FAMILY_NUMBER: switch (entitytype) { case MED_NODE: /*Pour connaître le nombre d'entités des attributs optionnels, on se base sur le tableau de coordonnées */ /* _meddatatype = MED_COORDINATE; */ break; case MED_CELL: case MED_DESCENDING_FACE: case MED_DESCENDING_EDGE: /*Pour connaître le nombre d'entités des attributs optionnels, on se base sur le tableau de connectivité */ /* _meddatatype = MED_CONNECTIVITY; */ /* * Positionne un mode de connectivité _cmode si le meddatatype demandé * est autre chose que des coordonnées ou des connectivités et le cmode non * positionné. * Cette Information est necessaire pour construire le nom du datatset. */ if ( _cmode[0] == MED_NO_CMODE ) { _ncmode =2;_cmode[0]=MED_NOD;_cmode[1]=MED_DESC; } break; default: MED_ERR_(_ret,MED_ERR_RANGE,MED_ERR_ENTITY,MED_ERR_VALUE_MSG); ISCRUTE_int(entitytype);goto ERROR; } goto CALL_NENT; case MED_COORDINATE: /*Par extension au CONNECTIVITY mais ne devrait pas être utilisé.*/ case MED_CONNECTIVITY: if ( geotype == MED_POLYGON2 ) {_n=0; break;} if ( geotype == MED_POLYGON ) { if ( MEDpolygoneInfo( fid,(char *)meshname, (med_entite_maillage) entitytype, (med_connectivite) _cmode[0], &_n) < 0) { /* MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"MEDpolygoneInfo");SSCRUTE(meshname); */ /* ISCRUTE(meddatatype);ISCRUTE(entitytype);ISCRUTE(geotype);goto ERROR; */ _n=0; } break; } if ( geotype == MED_POLYHEDRON ) { if ( MEDpolyedreInfo(fid,(char *) meshname, (med_connectivite) _cmode[0] ,&_nfaces,&_n) < 0) { /* MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"MEDpolyedreInfo");SSCRUTE(meshname); */ /* ISCRUTE(meddatatype);ISCRUTE(entitytype);ISCRUTE(geotype);goto ERROR; */ _n=0; } break; } goto CALL_NENT; case MED_INDEX_FACE: if ( geotype == MED_POLYHEDRON ) { _meddatatype = MED_CONNECTIVITY; _n=1 ; goto CALL_NENT; /* if ( (_n = MEDnEntMaa(fid,meshname,(med_table) MED_CONNECTIVITY, (med_entite_maillage) MED_CELL, */ /* (med_geometrie_element) MED_POLYHEDRON, (med_connectivite) MED_NOD)) < 0) { */ /* MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"MEDnEntMaa");SSCRUTE(meshname); */ /* ISCRUTE(meddatatype);ISCRUTE(entitytype);ISCRUTE(geotype);goto ERROR; */ /* } */ _n+=1; break; } else { MED_ERR_(_ret,MED_ERR_RANGE,MED_ERR_GEOMETRIC,MED_ERR_VALUE_MSG); ISCRUTE_int(geotype); goto ERROR; } case MED_INDEX_NODE: if ( geotype == MED_POLYHEDRON ) { if ( MEDpolyedreInfo(fid,(char *) meshname, (med_connectivite) _cmode[0],&_nfaces, &_n) < 0) { /* MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"MEDpolyedreInfo");SSCRUTE(meshname); */ /* ISCRUTE(meddatatype);ISCRUTE(entitytype);ISCRUTE(geotype);goto ERROR; */ _nfaces=0; } _n=_nfaces; break; } else if ( (geotype == MED_POLYGON) || (geotype == MED_POLYGON2) ) { _meddatatype = MED_CONNECTIVITY; _n = 1; } else { MED_ERR_(_ret,MED_ERR_RANGE,MED_ERR_GEOMETRIC,MED_ERR_VALUE_MSG); ISCRUTE_int(geotype);goto ERROR; } case MED_COORDINATE_AXIS1: case MED_COORDINATE_AXIS2: case MED_COORDINATE_AXIS3: CALL_NENT: for (_cmodeit=0; _cmodeit < _ncmode; ++_cmodeit) if ( (_tmpn = MEDnEntMaa( fid, (char *) meshname, (med_table) _meddatatype, (med_entite_maillage) entitytype, (med_geometrie_element) geotype, (med_connectivite) _cmode[_cmodeit]) ) > 0 ) break; if ( _tmpn < 0 ) { MED_ERR_(_ret,MED_ERR_CALL,MED_ERR_API,"MEDnEntMaa");SSCRUTE(meshname); ISCRUTE_int(_meddatatype);ISCRUTE_int(entitytype);ISCRUTE_int(geotype);goto ERROR; } _n+=_tmpn; break; default: MED_ERR_(_ret,MED_ERR_RANGE,MED_ERR_MEDDATATYPE,MED_ERR_VALUE_MSG); ISCRUTE_int(meddatatype);goto ERROR; } SORTIE: *transformation = MED_FALSE; /*TODO : 3.0.1 : Tester la présence d'autres datasets que les coord/conn/index pour positionner chgt TRUE uniquement dans ce cas de figure.*/ *changement = MED_TRUE; *profilesize = 0; profilename[0] = '\0'; _ret = _n; ERROR: if (_datagroup1>0) if (_MEDdatagroupFermer(_datagroup1) < 0) { MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATAGROUP,_datagroupname1); ISCRUTE_id(_datagroup1); } if (_meshid>0) if (_MEDdatagroupFermer(_meshid) < 0) { MED_ERR_(_ret,MED_ERR_CLOSE,MED_ERR_DATAGROUP,_meshpath); ISCRUTE_id(_meshid); } *fret = _ret; va_end(params); return; }
int main (int argc, char **argv) { med_err ret = 0; med_idt fid; char maa[MED_TAILLE_NOM+1]; med_int nmaa,i,mdim,npoly,j; char desc[MED_TAILLE_DESC+1]; med_int taille,nf,np; med_int taille2,nf2,np2; med_int *conn, *conn2, *indexf, *indexf2, *num, *fam; med_int *indexp, *indexp2; char *nom; char tmp[MED_TAILLE_PNOM+1]; int ind1, ind2,k,nfaces,nnoeuds,l; med_maillage type; /* Ouverture du fichier test25.med en lecture seule */ fid = MEDouvrir("test25.med",MED_LECTURE); if (fid < 0) { MESSAGE("Erreur a l'ouverture du fichier test25.med"); return -1; } printf("Ouverture du fichier test25.med \n"); /* Lecture du nombre de maillages */ nmaa = MEDnMaa(fid); if (nmaa < 0) { MESSAGE("Erreur a lecture du nombre de maillage"); return -1; } printf("Nombre de maillages = "IFORMAT"\n",nmaa); for (i=0;i<nmaa;i++) if (ret == 0) { /* Lecture des infos sur le maillage */ if (MEDmaaInfo(fid,i+1,maa,&mdim,&type,desc) < 0) { MESSAGE("Erreur a lecture des infos sur le maillage"); return -1; } printf("maillage "IFORMAT" de nom [%s] et de dimension : "IFORMAT" \n",i+1,maa,mdim); /* Combien de mailles polyedres en mode nodal */ if ((npoly = MEDnEntMaa(fid,maa,MED_CONN,MED_MAILLE,MED_POLYEDRE,MED_NOD)) < 0) { MESSAGE("Erreur a lecture du nombre de maille MED_POLYEDRE en mode nodal"); return -1; } printf("Nombre de mailles polyedres : "IFORMAT" \n",npoly); /* Quelle taille pour le tableau des connectivites et d'indexation en mode MED_NOD */ if (MEDpolyedreInfo(fid,maa,MED_NOD,&nf,&taille) < 0) { MESSAGE("Erreur a lecture des infos sur les polyedres"); return -1; } printf("Taille a allouer pour la connectivite nodale des polyedres : "IFORMAT" \n",taille); printf("Taille a allouer pour le tableau d'indexation des faces : "IFORMAT" \n",nf); /* Quelle taille pour le tableau des connectivites et d'indexation en mode MED_DESC */ if (MEDpolyedreInfo(fid,maa,MED_DESC,&nf2,&taille2) < 0) { MESSAGE("Erreur a la lecture des infos sur les polyedres"); return -1; } printf("Taille a allouer pour la connectivite descendante des polyedres : "IFORMAT" \n",taille2); printf("Taille a allouer pour le tableau d'indexation des types de faces : "IFORMAT" \n",nf2); /* Allocation memoire : * - tableau indexp et indexp2 : npoly + 1 * - tableau indexf et indexf2 : nf et nf2 * - tableau des connectivites : consize * - tableaux numeros et numeros de familles : npoly * - tableau des noms : MED_TAILLE_PNOM*npoly + 1 */ indexp = (med_int *) malloc(sizeof(med_int)*(npoly+1)); indexp2 = (med_int *) malloc(sizeof(med_int)*(npoly+1)); indexf = (med_int *) malloc(sizeof(med_int)*nf); indexf2 = (med_int *) malloc(sizeof(med_int)*nf2); conn = (med_int *) malloc(sizeof(med_int)*taille); conn2 = (med_int *) malloc(sizeof(med_int)*taille2); num = (med_int *) malloc(sizeof(med_int)*npoly); fam = (med_int *) malloc(sizeof(med_int)*npoly); nom = (char *) malloc(sizeof(char)*MED_TAILLE_PNOM*npoly+1); /* Lecture de la connectivite des mailles polyedres en mode nodal */ if (MEDpolyedreConnLire(fid,maa,indexp,npoly+1,indexf,nf,conn,MED_NOD) < 0) { MESSAGE("Erreur a lecture de la connectivite nodale des polyedres"); ret = -1; } printf("Lecture de la connectivite des mailles MED_POLYEDRE en mode nodal \n"); /* Lecture de la connectivite des mailles polyedres en mode descendant */ if (ret == 0) { if (MEDpolyedreConnLire(fid,maa,indexp2,npoly+1,indexf2,nf2,conn2,MED_DESC) < 0) { MESSAGE("Erreur a lecture de la connectivite descendante des polyedres"); ret = -1; } printf("Lecture de la connectivite des mailles MED_POLYEDRE en mode descendant \n"); } /* Lecture noms */ if (ret == 0) { if (MEDnomLire(fid,maa,nom,npoly,MED_MAILLE,MED_POLYEDRE) < 0) { MESSAGE("Erreur a lecture des noms des polyedres"); ret = -1; } printf("Lecture des noms des mailles MED_POLYEDRE \n"); } /* Lecture des numeros */ if (ret == 0) { if (MEDnumLire(fid,maa,num,npoly,MED_MAILLE,MED_POLYEDRE) < 0) { MESSAGE("Erreur a lecture des numeros des polyedres"); ret = -1; } printf("Lecture des numeros des mailles MED_POLYEDRE \n"); } /* Lecture des numeros de familles */ if (ret == 0) { if (MEDfamLire(fid,maa,fam,npoly,MED_MAILLE,MED_POLYEDRE) < 0) { MESSAGE("Erreur a lecture des numeros de famille des polyedres"); ret = -1; } printf("Lecture des numeros de familles des mailles MED_POLYEDRE \n"); } if (ret == 0) { printf("Affichage des resultats \n"); for (j=0;j<npoly;j++) { printf(">> Maille MED_POLYEDRE "IFORMAT" : \n",j+1); printf("---- Connectivite nodale ----- : \n"); nfaces = *(indexp+j+1) - *(indexp+j); /* ind1 = indice dans "indexf" pour acceder aux numeros des faces */ ind1 = *(indexp+j) - 1; for (k=0;k<nfaces;k++) { /* ind2 = indice dans "conn" pour acceder au premier noeud de la face */ ind2 = *(indexf+ind1+k) - 1; nnoeuds = *(indexf+ind1+k+1) - *(indexf+ind1+k); printf(" - Face %d : [ ", k+1); for (l=0;l<nnoeuds;l++) printf(" "IFORMAT" ",*(conn+ind2+l)); printf(" ] \n"); } printf("---- Connectivite descendante ----- : \n"); nfaces = *(indexp2+j+1) - *(indexp2+j); /* ind1 = indice dans "conn2" pour acceder aux numeros des faces */ ind1 = *(indexp2+j) - 1; for (k=0;k<nfaces;k++) printf(" - Face %d de numero : "IFORMAT" et de type "IFORMAT" \n", k+1,*(conn2+ind1+k),*(indexf2+ind1+k)); strncpy(tmp,nom+j*MED_TAILLE_PNOM,MED_TAILLE_PNOM); tmp[MED_TAILLE_PNOM] = '\0'; printf("---- Nom ----- : %s \n",tmp); printf("---- Numero ----- : "IFORMAT" \n",*(num+j)); printf("---- Numero de famille ----- : "IFORMAT" \n",*(fam+j)); } } /* liberation de la memoire */ free(indexp); free(indexp2); free(indexf); free(indexf2); free(conn); free(conn2); free(num); free(fam); free(nom); } /* Fermeture du fichier */ if (MEDfermer(fid) < 0) { MESSAGE("Erreur a fermeture du fichier"); return -1; } printf("Fermeture du fichier \n"); return ret; }
int GModel::readMED(const std::string &name) { med_idt fid = MEDouvrir((char*)name.c_str(), MED_LECTURE); if(fid < 0) { Msg::Error("Unable to open file '%s'", name.c_str()); return 0; } med_int v[3], vf[3]; MEDversionDonner(&v[0], &v[1], &v[2]); MEDversionLire(fid, &vf[0], &vf[1], &vf[2]); Msg::Info("Reading MED file V%d.%d.%d using MED library V%d.%d.%d", vf[0], vf[1], vf[2], v[0], v[1], v[2]); if(vf[0] < 2 || (vf[0] == 2 && vf[1] < 2)){ Msg::Error("Cannot read MED file older than V2.2"); return 0; } std::vector<std::string> meshNames; for(int i = 0; i < MEDnMaa(fid); i++){ char meshName[MED_TAILLE_NOM + 1], meshDesc[MED_TAILLE_DESC + 1]; med_int spaceDim; med_maillage meshType; #if (MED_MAJOR_NUM == 3) med_int meshDim, nStep; char dtUnit[MED_SNAME_SIZE + 1]; char axisName[3 * MED_SNAME_SIZE + 1], axisUnit[3 * MED_SNAME_SIZE + 1]; med_sorting_type sortingType; med_axis_type axisType; if(MEDmeshInfo(fid, i + 1, meshName, &spaceDim, &meshDim, &meshType, meshDesc, dtUnit, &sortingType, &nStep, &axisType, axisName, axisUnit) < 0){ #else if(MEDmaaInfo(fid, i + 1, meshName, &spaceDim, &meshType, meshDesc) < 0){ #endif Msg::Error("Unable to read mesh information"); return 0; } meshNames.push_back(meshName); } if(MEDfermer(fid) < 0){ Msg::Error("Unable to close file '%s'", (char*)name.c_str()); return 0; } int ret = 1; for(unsigned int i = 0; i < meshNames.size(); i++){ // we use the filename as a kind of "partition" indicator, allowing to // complete a model part by part (used e.g. in DDM, since MED does not store // a partition index) GModel *m = findByName(meshNames[i], name); if(!m) m = new GModel(meshNames[i]); ret = m->readMED(name, i); if(!ret) return 0; } return ret; } int GModel::readMED(const std::string &name, int meshIndex) { med_idt fid = MEDouvrir((char*)name.c_str(), MED_LECTURE); if(fid < 0){ Msg::Error("Unable to open file '%s'", name.c_str()); return 0; } int numMeshes = MEDnMaa(fid); if(meshIndex >= numMeshes){ Msg::Info("Could not find mesh %d in MED file", meshIndex); return 0; } checkPointMaxNumbers(); GModel::setCurrent(this); // make sure we increment max nums in this model // read mesh info char meshName[MED_TAILLE_NOM + 1], meshDesc[MED_TAILLE_DESC + 1]; med_int spaceDim, nStep = 1; med_maillage meshType; #if (MED_MAJOR_NUM == 3) med_int meshDim; char dtUnit[MED_SNAME_SIZE + 1]; char axisName[3 * MED_SNAME_SIZE + 1], axisUnit[3 * MED_SNAME_SIZE + 1]; med_sorting_type sortingType; med_axis_type axisType; if(MEDmeshInfo(fid, meshIndex + 1, meshName, &spaceDim, &meshDim, &meshType, meshDesc, dtUnit, &sortingType, &nStep, &axisType, axisName, axisUnit) < 0){ #else if(MEDmaaInfo(fid, meshIndex + 1, meshName, &spaceDim, &meshType, meshDesc) < 0){ #endif Msg::Error("Unable to read mesh information"); return 0; } // FIXME: we should support multi-step MED3 meshes (probably by // storing each mesh as a separate model, with a naming convention // e.g. meshName_step%d). This way we could also handle multi-mesh // time sequences in MED3. if(nStep > 1) Msg::Warning("Discarding %d last meshes in multi-step MED mesh", nStep - 1); setName(meshName); setFileName(name); if(meshType == MED_NON_STRUCTURE){ Msg::Info("Reading %d-D unstructured mesh <<%s>>", spaceDim, meshName); } else{ Msg::Error("Reading structured MED meshes is not supported"); return 0; } med_int vf[3]; MEDversionLire(fid, &vf[0], &vf[1], &vf[2]); // read nodes #if (MED_MAJOR_NUM == 3) med_bool changeOfCoord, geoTransform; med_int numNodes = MEDmeshnEntity(fid, meshName, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE, MED_COORDINATE, MED_NO_CMODE, &changeOfCoord, &geoTransform); #else med_int numNodes = MEDnEntMaa(fid, meshName, MED_COOR, MED_NOEUD, MED_NONE, MED_NOD); #endif if(numNodes < 0){ Msg::Error("Could not read number of MED nodes"); return 0; } if(numNodes == 0){ Msg::Error("No nodes in MED mesh"); return 0; } std::vector<MVertex*> verts(numNodes); std::vector<med_float> coord(spaceDim * numNodes); #if (MED_MAJOR_NUM == 3) if(MEDmeshNodeCoordinateRd(fid, meshName, MED_NO_DT, MED_NO_IT, MED_FULL_INTERLACE, &coord[0]) < 0){ #else std::vector<char> coordName(spaceDim * MED_TAILLE_PNOM + 1); std::vector<char> coordUnit(spaceDim * MED_TAILLE_PNOM + 1); med_repere rep; if(MEDcoordLire(fid, meshName, spaceDim, &coord[0], MED_FULL_INTERLACE, MED_ALL, 0, 0, &rep, &coordName[0], &coordUnit[0]) < 0){ #endif Msg::Error("Could not read MED node coordinates"); return 0; } std::vector<med_int> nodeTags(numNodes); #if (MED_MAJOR_NUM == 3) if(MEDmeshEntityNumberRd(fid, meshName, MED_NO_DT, MED_NO_IT, MED_NODE, MED_NO_GEOTYPE, &nodeTags[0]) < 0) #else if(MEDnumLire(fid, meshName, &nodeTags[0], numNodes, MED_NOEUD, MED_NONE) < 0) #endif nodeTags.clear(); for(int i = 0; i < numNodes; i++) verts[i] = new MVertex(coord[spaceDim * i], (spaceDim > 1) ? coord[spaceDim * i + 1] : 0., (spaceDim > 2) ? coord[spaceDim * i + 2] : 0., 0, nodeTags.empty() ? 0 : nodeTags[i]); // read elements (loop over all possible MSH element types) for(int mshType = 0; mshType < MSH_NUM_TYPE; mshType++){ med_geometrie_element type = msh2medElementType(mshType); if(type == MED_NONE) continue; #if (MED_MAJOR_NUM == 3) med_bool changeOfCoord; med_bool geoTransform; med_int numEle = MEDmeshnEntity(fid, meshName, MED_NO_DT, MED_NO_IT, MED_CELL, type, MED_CONNECTIVITY, MED_NODAL, &changeOfCoord, &geoTransform); #else med_int numEle = MEDnEntMaa(fid, meshName, MED_CONN, MED_MAILLE, type, MED_NOD); #endif if(numEle <= 0) continue; int numNodPerEle = type % 100; std::vector<med_int> conn(numEle * numNodPerEle); #if (MED_MAJOR_NUM == 3) if(MEDmeshElementConnectivityRd(fid, meshName, MED_NO_DT, MED_NO_IT, MED_CELL, type, MED_NODAL, MED_FULL_INTERLACE, &conn[0]) < 0){ #else if(MEDconnLire(fid, meshName, spaceDim, &conn[0], MED_FULL_INTERLACE, 0, MED_ALL, MED_MAILLE, type, MED_NOD) < 0){ #endif Msg::Error("Could not read MED elements"); return 0; } std::vector<med_int> fam(numEle, 0); #if (MED_MAJOR_NUM == 3) if(MEDmeshEntityFamilyNumberRd(fid, meshName, MED_NO_DT, MED_NO_IT, MED_CELL, type, &fam[0]) < 0){ #else if(MEDfamLire(fid, meshName, &fam[0], numEle, MED_MAILLE, type) < 0){ #endif Msg::Info("No family number for elements: using 0 as default family number"); } std::vector<med_int> eleTags(numEle); #if (MED_MAJOR_NUM == 3) if(MEDmeshEntityNumberRd(fid, meshName, MED_NO_DT, MED_NO_IT, MED_CELL, type, &eleTags[0]) < 0) #else if(MEDnumLire(fid, meshName, &eleTags[0], numEle, MED_MAILLE, type) < 0) #endif eleTags.clear(); std::map<int, std::vector<MElement*> > elements; MElementFactory factory; for(int j = 0; j < numEle; j++){ std::vector<MVertex*> v(numNodPerEle); for(int k = 0; k < numNodPerEle; k++) v[k] = verts[conn[numNodPerEle * j + med2mshNodeIndex(type, k)] - 1]; MElement *e = factory.create(mshType, v, eleTags.empty() ? 0 : eleTags[j]); if(e) elements[-fam[j]].push_back(e); } _storeElementsInEntities(elements); } _associateEntityWithMeshVertices(); _storeVerticesInEntities(verts); // read family info med_int numFamilies = MEDnFam(fid, meshName); if(numFamilies < 0){ Msg::Error("Could not read MED families"); return 0; } for(int i = 0; i < numFamilies; i++){ #if (MED_MAJOR_NUM == 3) med_int numAttrib = (vf[0] == 2) ? MEDnFamily23Attribute(fid, meshName, i + 1) : 0; med_int numGroups = MEDnFamilyGroup(fid, meshName, i + 1); #else med_int numAttrib = MEDnAttribut(fid, meshName, i + 1); med_int numGroups = MEDnGroupe(fid, meshName, i + 1); #endif if(numAttrib < 0 || numGroups < 0){ Msg::Error("Could not read MED groups or attributes"); return 0; } std::vector<med_int> attribId(numAttrib + 1); std::vector<med_int> attribVal(numAttrib + 1); std::vector<char> attribDes(MED_TAILLE_DESC * numAttrib + 1); std::vector<char> groupNames(MED_TAILLE_LNOM * numGroups + 1); char familyName[MED_TAILLE_NOM + 1]; med_int familyNum; #if (MED_MAJOR_NUM == 3) if(vf[0] == 2){ // MED2 file if(MEDfamily23Info(fid, meshName, i + 1, familyName, &attribId[0], &attribVal[0], &attribDes[0], &familyNum, &groupNames[0]) < 0){ Msg::Error("Could not read info for MED2 family %d", i + 1); continue; } } else{ if(MEDfamilyInfo(fid, meshName, i + 1, familyName, &familyNum, &groupNames[0]) < 0){ Msg::Error("Could not read info for MED3 family %d", i + 1); continue; } } #else if(MEDfamInfo(fid, meshName, i + 1, familyName, &familyNum, &attribId[0], &attribVal[0], &attribDes[0], &numAttrib, &groupNames[0], &numGroups) < 0){ Msg::Error("Could not read info for MED family %d", i + 1); continue; } #endif // family tags are unique (for all dimensions) GEntity *ge; if((ge = getRegionByTag(-familyNum))){} else if((ge = getFaceByTag(-familyNum))){} else if((ge = getEdgeByTag(-familyNum))){} else ge = getVertexByTag(-familyNum); if(ge){ elementaryNames[std::pair<int, int>(ge->dim(), -familyNum)] = familyName; if(numGroups > 0){ for(int j = 0; j < numGroups; j++){ char tmp[MED_TAILLE_LNOM + 1]; strncpy(tmp, &groupNames[j * MED_TAILLE_LNOM], MED_TAILLE_LNOM); tmp[MED_TAILLE_LNOM] = '\0'; // don't use same physical number across dimensions, as e.g. getdp // does not support this int pnum = setPhysicalName(tmp, ge->dim(), getMaxPhysicalNumber(-1) + 1); if(std::find(ge->physicals.begin(), ge->physicals.end(), pnum) == ge->physicals.end()) ge->physicals.push_back(pnum); } } } } // check if we need to read some post-processing data later #if (MED_MAJOR_NUM == 3) bool postpro = (MEDnField(fid) > 0) ? true : false; #else bool postpro = (MEDnChamp(fid, 0) > 0) ? true : false; #endif if(MEDfermer(fid) < 0){ Msg::Error("Unable to close file '%s'", (char*)name.c_str()); return 0; } return postpro ? 2 : 1; } template<class T> static void fillElementsMED(med_int family, std::vector<T*> &elements, std::vector<med_int> &conn, std::vector<med_int> &fam, med_geometrie_element &type) { if(elements.empty()) return; type = msh2medElementType(elements[0]->getTypeForMSH()); if(type == MED_NONE){ Msg::Warning("Unsupported element type in MED format"); return; } for(unsigned int i = 0; i < elements.size(); i++){ elements[i]->setVolumePositive(); for(int j = 0; j < elements[i]->getNumVertices(); j++) conn.push_back(elements[i]->getVertex(med2mshNodeIndex(type, j))->getIndex()); fam.push_back(family); } } static void writeElementsMED(med_idt &fid, char *meshName, std::vector<med_int> &conn, std::vector<med_int> &fam, med_geometrie_element type) { if(fam.empty()) return; #if (MED_MAJOR_NUM == 3) if(MEDmeshElementWr(fid, meshName, MED_NO_DT, MED_NO_IT, 0., MED_CELL, type, MED_NODAL, MED_FULL_INTERLACE, (med_int)fam.size(), &conn[0], MED_FALSE, 0, MED_FALSE, 0, MED_TRUE, &fam[0]) < 0) #else if(MEDelementsEcr(fid, meshName, (med_int)3, &conn[0], MED_FULL_INTERLACE, 0, MED_FAUX, 0, MED_FAUX, &fam[0], (med_int)fam.size(), MED_MAILLE, type, MED_NOD) < 0) #endif Msg::Error("Could not write MED elements"); } int GModel::writeMED(const std::string &name, bool saveAll, double scalingFactor) { med_idt fid = MEDouvrir((char*)name.c_str(), MED_CREATION); if(fid < 0){ Msg::Error("Unable to open file '%s'", name.c_str()); return 0; } // write header if(MEDfichDesEcr(fid, (char*)"MED file generated by Gmsh") < 0){ Msg::Error("Unable to write MED descriptor"); return 0; } char *meshName = (char*)getName().c_str(); // Gmsh always writes 3D unstructured meshes #if (MED_MAJOR_NUM == 3) char dtUnit[MED_SNAME_SIZE + 1] = ""; char axisName[3 * MED_SNAME_SIZE + 1] = ""; char axisUnit[3 * MED_SNAME_SIZE + 1] = ""; if(MEDmeshCr(fid, meshName, 3, 3, MED_UNSTRUCTURED_MESH, "Mesh created with Gmsh", dtUnit, MED_SORT_DTIT, MED_CARTESIAN, axisName, axisUnit) < 0){ #else if(MEDmaaCr(fid, meshName, 3, MED_NON_STRUCTURE, (char*)"Mesh created with Gmsh") < 0){ #endif Msg::Error("Could not create MED mesh"); return 0; } // if there are no physicals we save all the elements if(noPhysicalGroups()) saveAll = true; // index the vertices we save in a continuous sequence (MED // connectivity is given in terms of vertex indices) indexMeshVertices(saveAll); // get a vector containing all the geometrical entities in the // model (the ordering of the entities must be the same as the one // used during the indexing of the vertices) std::vector<GEntity*> entities; getEntities(entities); std::map<GEntity*, int> families; // write the families { // always create a "0" family, with no groups or attributes #if (MED_MAJOR_NUM == 3) if(MEDfamilyCr(fid, meshName, "F_0", 0, 0, "") < 0) #else if(MEDfamCr(fid, meshName, (char*)"F_0", 0, 0, 0, 0, 0, 0, 0) < 0) #endif Msg::Error("Could not create MED family 0"); // create one family per elementary entity, with one group per // physical entity and no attributes for(unsigned int i = 0; i < entities.size(); i++){ if(saveAll || entities[i]->physicals.size()){ int num = - ((int)families.size() + 1); families[entities[i]] = num; std::ostringstream fs; fs << entities[i]->dim() << "D_" << entities[i]->tag(); std::string familyName = "F_" + fs.str(); std::string groupName; for(unsigned j = 0; j < entities[i]->physicals.size(); j++){ std::string tmp = getPhysicalName (entities[i]->dim(), entities[i]->physicals[j]); if(tmp.empty()){ // create unique name std::ostringstream gs; gs << entities[i]->dim() << "D_" << entities[i]->physicals[j]; groupName += "G_" + gs.str(); } else groupName += tmp; groupName.resize((j + 1) * MED_TAILLE_LNOM, ' '); } #if (MED_MAJOR_NUM == 3) if(MEDfamilyCr(fid, meshName, familyName.c_str(), (med_int)num, (med_int)entities[i]->physicals.size(), groupName.c_str()) < 0) #else if(MEDfamCr(fid, meshName, (char*)familyName.c_str(), (med_int)num, 0, 0, 0, 0, (char*)groupName.c_str(), (med_int)entities[i]->physicals.size()) < 0) #endif Msg::Error("Could not create MED family %d", num); } } } // write the nodes { std::vector<med_float> coord; std::vector<med_int> fam; for(unsigned int i = 0; i < entities.size(); i++){ for(unsigned int j = 0; j < entities[i]->mesh_vertices.size(); j++){ MVertex *v = entities[i]->mesh_vertices[j]; if(v->getIndex() >= 0){ coord.push_back(v->x() * scalingFactor); coord.push_back(v->y() * scalingFactor); coord.push_back(v->z() * scalingFactor); fam.push_back(0); // we never create node families } } } if(fam.empty()){ Msg::Error("No nodes to write in MED mesh"); return 0; } #if (MED_MAJOR_NUM == 3) if(MEDmeshNodeWr(fid, meshName, MED_NO_DT, MED_NO_IT, 0., MED_FULL_INTERLACE, (med_int)fam.size(), &coord[0], MED_FALSE, "", MED_FALSE, 0, MED_TRUE, &fam[0]) < 0) #else char coordName[3 * MED_TAILLE_PNOM + 1] = "x y z "; char coordUnit[3 * MED_TAILLE_PNOM + 1] = "unknown unknown unknown "; if(MEDnoeudsEcr(fid, meshName, (med_int)3, &coord[0], MED_FULL_INTERLACE, MED_CART, coordName, coordUnit, 0, MED_FAUX, 0, MED_FAUX, &fam[0], (med_int)fam.size()) < 0) #endif Msg::Error("Could not write nodes"); } // write the elements { { // points med_geometrie_element typ = MED_NONE; std::vector<med_int> conn, fam; for(viter it = firstVertex(); it != lastVertex(); it++) if(saveAll || (*it)->physicals.size()) fillElementsMED(families[*it], (*it)->points, conn, fam, typ); writeElementsMED(fid, meshName, conn, fam, typ); } { // lines med_geometrie_element typ = MED_NONE; std::vector<med_int> conn, fam; for(eiter it = firstEdge(); it != lastEdge(); it++) if(saveAll || (*it)->physicals.size()) fillElementsMED(families[*it], (*it)->lines, conn, fam, typ); writeElementsMED(fid, meshName, conn, fam, typ); } { // triangles med_geometrie_element typ = MED_NONE; std::vector<med_int> conn, fam; for(fiter it = firstFace(); it != lastFace(); it++) if(saveAll || (*it)->physicals.size()) fillElementsMED(families[*it], (*it)->triangles, conn, fam, typ); writeElementsMED(fid, meshName, conn, fam, typ); } { // quads med_geometrie_element typ = MED_NONE; std::vector<med_int> conn, fam; for(fiter it = firstFace(); it != lastFace(); it++) if(saveAll || (*it)->physicals.size()) fillElementsMED(families[*it], (*it)->quadrangles, conn, fam, typ); writeElementsMED(fid, meshName, conn, fam, typ); } { // tets med_geometrie_element typ = MED_NONE; std::vector<med_int> conn, fam; for(riter it = firstRegion(); it != lastRegion(); it++) if(saveAll || (*it)->physicals.size()) fillElementsMED(families[*it], (*it)->tetrahedra, conn, fam, typ); writeElementsMED(fid, meshName, conn, fam, typ); } { // hexas med_geometrie_element typ = MED_NONE; std::vector<med_int> conn, fam; for(riter it = firstRegion(); it != lastRegion(); it++) if(saveAll || (*it)->physicals.size()) fillElementsMED(families[*it], (*it)->hexahedra, conn, fam, typ); writeElementsMED(fid, meshName, conn, fam, typ); } { // prisms med_geometrie_element typ = MED_NONE; std::vector<med_int> conn, fam; for(riter it = firstRegion(); it != lastRegion(); it++) if(saveAll || (*it)->physicals.size()) fillElementsMED(families[*it], (*it)->prisms, conn, fam, typ); writeElementsMED(fid, meshName, conn, fam, typ); } { // pyramids med_geometrie_element typ = MED_NONE; std::vector<med_int> conn, fam; for(riter it = firstRegion(); it != lastRegion(); it++) if(saveAll || (*it)->physicals.size()) fillElementsMED(families[*it], (*it)->pyramids, conn, fam, typ); writeElementsMED(fid, meshName, conn, fam, typ); } } if(MEDfermer(fid) < 0){ Msg::Error("Unable to close file '%s'", (char*)name.c_str()); return 0; } return 1; } #else int GModel::readMED(const std::string &name) { Msg::Error("Gmsh must be compiled with MED support to read '%s'", name.c_str()); return 0; }
int main (int argc, char **argv) { med_err ret = 0; med_idt fid; med_int nse2; med_int *se2_1; med_int *se2_2; char *nomse2; med_int *numse2; med_int *nufase2; med_int ntr3; med_int *tr3; char *nomtr3; med_int *numtr3; med_int *nufatr3; char maa[MED_TAILLE_NOM+1] ="maa1"; med_int mdim = 2; med_booleen inoele,inuele; med_int tse2,ttr3; med_int i; char str[MED_TAILLE_PNOM+1]; med_int profil[2] = { 2, 3 }; char desc[MED_TAILLE_DESC+1]; med_maillage type; /* Ouverture du fichier en mode lecture seule */ if ((fid = MEDouvrir("test6.med",MED_LECTURE)) < 0) { MESSAGE("Erreur a l'ouverture du fichier test6.med"); return -1; } /* Lecture des informations sur le premier maillage */ if (MEDmaaInfo(fid,1,maa,&mdim,&type,desc) < 0) { MESSAGE("Erreur a la lecture des information sur le 1er maillage"); return -1; } else printf("Maillage de nom : %s et de dimension %d \n",maa,mdim); /* Combien de triangles et de segments */ if ((nse2 = MEDnEntMaa(fid,maa,MED_CONN,MED_ARETE,MED_SEG2,MED_DESC)) < 0) { MESSAGE("Erreur a la lecture du nombre de faces MED_SEG2"); return -1; } if ((ntr3 = MEDnEntMaa(fid,maa,MED_CONN,MED_MAILLE,MED_TRIA3,MED_DESC))<0) { MESSAGE("Erreur a la lecture du nombre de mailles MED_TRIA3"); return -1; } printf("Nombre de MED_SEG2 : "IFORMAT" - nombre de MED_TRIA3 : "IFORMAT"\n",nse2,ntr3); /* Allocations memoire */ tse2 = 2; se2_1 = (med_int*) calloc(tse2*nse2,sizeof(med_int)); se2_2 = (med_int*) malloc(sizeof(med_int)*tse2*nse2); nomse2 = (char*) malloc(MED_TAILLE_PNOM*nse2+1); numse2 = (med_int*) malloc(sizeof(med_int)*nse2); nufase2 = (med_int*) malloc(sizeof(med_int)*nse2); ttr3 = 3; tr3 = (med_int*) malloc(sizeof(med_int)*ntr3*ttr3); nomtr3 = (char*) malloc(MED_TAILLE_PNOM*ntr3+1); numtr3 = (med_int*) malloc(sizeof(med_int)*ntr3); nufatr3 = (med_int*) malloc(sizeof(med_int)*ntr3); /* Lecture des connectivites des segments avec profil */ if (MEDconnLire(fid,maa,mdim,se2_1,MED_FULL_INTERLACE,profil,2, MED_ARETE,MED_SEG2,MED_DESC) < 0) { MESSAGE("Erreur a la lecture de la connectivite des segments"); return -1; } /* Lecture de la connectivite des segments */ if (MEDconnLire(fid,maa,mdim,se2_2,MED_FULL_INTERLACE,NULL,0, MED_ARETE ,MED_SEG2,MED_DESC) < 0) { MESSAGE("Erreur a la lecture de la connectivite des segments"); return -1; } /* Lecture (optionnelle) des noms des segments */ if (MEDnomLire(fid,maa,nomse2,nse2,MED_ARETE,MED_SEG2) < 0) inoele = MED_FAUX; else inoele = MED_VRAI; /* Lecture (optionnelle) des numeros des segments */ if (MEDnumLire(fid,maa,numse2,nse2,MED_ARETE,MED_SEG2) < 0) inuele = MED_FAUX; else inuele = MED_VRAI; /* Lecture des numeros des familles des segments */ if (MEDfamLire(fid,maa,nufase2,nse2,MED_ARETE,MED_SEG2) < 0) { MESSAGE("Erreur a la lecture des numéros de famille des segments"); return -1; } /* Lecture de la connectivite des triangles */ if (MEDconnLire(fid,maa,mdim,tr3,MED_NO_INTERLACE,NULL,0,MED_MAILLE,MED_TRIA3, MED_DESC) < 0) { MESSAGE("Erreur a la lecture de la connectivite des triangles"); return -1; } /* Lecture (optionnelle) des noms des triangles */ if (MEDnomLire(fid,maa,nomtr3,ntr3,MED_MAILLE,MED_TRIA3) < 0) inoele = MED_FAUX; else inoele = MED_VRAI; /* Lecture (optionnelle) des numeros des triangles */ if (MEDnumLire(fid,maa,numtr3,ntr3,MED_MAILLE,MED_TRIA3) < 0) inuele = MED_FAUX; else inuele = MED_VRAI; /* Lecture des numeros des familles des triangles */ if (ret = MEDfamLire(fid,maa,nufatr3,ntr3,MED_MAILLE,MED_TRIA3) < 0) { MESSAGE("Erreur a la lecture des numeros de famille des segments"); return -1; } /* Fermeture du fichier */ if (MEDfermer(fid) < 0) { MESSAGE("Erreur a la fermeture du fichier"); return -1; } /* Affichage */ if (ret == 0) { printf("Connectivite des segments (1): \n"); for (i=0;i<nse2*tse2;i++) printf(IFORMAT" ",*(se2_1+i)); printf("\n"); printf("Connectivite des segments (2): \n"); for (i=0;i<nse2*tse2;i++) printf(IFORMAT" ",*(se2_2+i)); if (inoele) { printf("\nNoms des segments :\n"); for (i=0;i<nse2;i++) { strncpy(str,nomse2+i*MED_TAILLE_PNOM,MED_TAILLE_PNOM); str[MED_TAILLE_PNOM] = '\0'; printf("%s ",str); } } if (inuele) { printf("\nNumeros des segments :\n"); for (i=0;i<nse2;i++) printf(IFORMAT" ",*(numse2+i)); } printf("\nNumeros des familles des segments :\n"); for (i=0;i<nse2;i++) printf(IFORMAT" ",*(nufase2+i)); printf("\nConnectivite des triangles : \n"); for (i=0;i<ntr3*ttr3;i++) printf(IFORMAT" ",*(tr3+i)); if (inoele) { printf("\nNoms des triangles :\n"); for (i=0;i<ntr3;i++) { strncpy(str,nomtr3+i*MED_TAILLE_PNOM,MED_TAILLE_PNOM); str[MED_TAILLE_PNOM] = '\0'; printf("%s ",str); } } if (inuele) { printf("\nNumeros des triangles :\n"); for (i=0;i<ntr3;i++) printf(IFORMAT" ",*(numtr3+i)); } printf("\nNumeros des familles des triangles :\n"); for (i=0;i<ntr3;i++) printf(IFORMAT" ",*(nufatr3+i)); printf("\n"); } /* Nettoyage memoire */ free(se2_1); free(se2_2); free(nomse2); free(numse2); free(nufase2); free(tr3); free(nomtr3); free(numtr3); free(nufatr3); return ret; }