+// Copyright (C) 2011-2024 CEA, EDF
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+
#include "HomardMedCommun.h"
-using namespace std;
+
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstring>
+#include <algorithm>
-extern "C"
-{
#include <med.h>
-}
#include "utilities.h"
// =======================================================================
+int MEDFileExist( const char * aFile )
+// Retourne 1 si le fichier existe, 0 sinon
+// =======================================================================
+{
+ int existe ;
+ med_idt medIdt = MEDfileOpen(aFile,MED_ACC_RDONLY);
+ if ( medIdt < 0 ) { existe = 0 ; }
+ else { MEDfileClose(medIdt);
+ existe = 1 ; }
+ return existe ;
+}
+// =======================================================================
std::set<std::string> GetListeGroupesInMedFile(const char * aFile)
// =======================================================================
{
- std::set<std::string> ListeGroupes;
- int MedIdt = MEDouvrir(const_cast<char *>(aFile),MED_LECTURE);
- if ( MedIdt < 0 ) { return ListeGroupes; };
-
- char maa[MED_TAILLE_NOM+1];
- char desc[MED_TAILLE_DESC+1];
- char nomfam[MED_TAILLE_NOM+1];
- med_int numfam;
-
- med_int mdim;
- med_maillage type;
- med_int numMaillage=1;
- if ( MEDmaaInfo(MedIdt,numMaillage,maa,&mdim,&type,desc) < 0 ) { return ListeGroupes; };
- med_int nfam, ngro, natt;
- if ((nfam = MEDnFam(MedIdt,maa)) < 0) { return ListeGroupes; };
- for (int i=0;i<nfam;i++)
- {
- if ((ngro = MEDnGroupe(MedIdt,maa,i+1)) < 0)
+ std::set<std::string> ListeGroupes;
+ med_err erreur = 0 ;
+ med_idt medIdt ;
+ while ( erreur == 0 )
+ {
+ // Ouverture du fichier
+ medIdt = MEDfileOpen(aFile,MED_ACC_RDONLY);
+ if ( medIdt < 0 )
+ {
+ erreur = 1 ;
+ break ;
+ }
+ // Caracteristiques du maillage
+ char meshname[MED_NAME_SIZE+1];
+ med_int spacedim,meshdim;
+ med_mesh_type meshtype;
+ char descriptionription[MED_COMMENT_SIZE+1];
+ char dtunit[MED_SNAME_SIZE+1];
+ med_sorting_type sortingtype;
+ med_int nstep;
+ med_axis_type axistype;
+ int naxis = MEDmeshnAxis(medIdt,1);
+ char *axisname=new char[naxis*MED_SNAME_SIZE+1];
+ char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
+ erreur = MEDmeshInfo(medIdt,
+ 1,
+ meshname,
+ &spacedim,
+ &meshdim,
+ &meshtype,
+ descriptionription,
+ dtunit,
+ &sortingtype,
+ &nstep,
+ &axistype,
+ axisname,
+ axisunit);
+ delete[] axisname ;
+ delete[] axisunit ;
+ if ( erreur < 0 ) { break ; }
+ // Nombre de familles
+ med_int nfam ;
+ nfam = MEDnFamily(medIdt,meshname) ;
+ if ( nfam < 0 )
+ {
+ erreur = 2 ;
+ break ;
+ }
+ // Lecture des caracteristiques des familles
+ for (int i=0;i<nfam;i++)
+ {
+// Lecture du nombre de groupes
+ med_int ngro = MEDnFamilyGroup(medIdt,meshname,i+1);
+ if ( ngro < 0 )
{
- // GERALD -- QMESSAGE BOX
- std::cerr << " Error : Families are unreadable" << std::endl;
- std::cerr << "Pb avec la famille : " << i << std::endl;
- break;
+ erreur = 3 ;
+ break ;
}
- if (ngro == 0) continue;
-
- if ((natt = MEDnAttribut(MedIdt,maa,i+1)) < 0)
+// Lecture de la famille
+ else if ( ngro > 0 )
{
- // GERALD -- QMESSAGE BOX
- std::cerr << " Error : Families are unreadable" << std::endl;
- std::cerr << "Pb avec la famille : " << i << std::endl;
- break;
+ char familyname[MED_NAME_SIZE+1];
+ med_int numfam;
+ char* gro = (char*) malloc(MED_LNAME_SIZE*ngro+1);
+ erreur = MEDfamilyInfo(medIdt,
+ meshname,
+ i+1,
+ familyname,
+ &numfam,
+ gro);
+ if ( erreur < 0 )
+ {
+ free(gro);
+ break ;
+ }
+ // Lecture des groupes pour une famille de mailles
+ if ( numfam < 0)
+ {
+ for (int j=0;j<ngro;j++)
+ {
+ char str2[MED_LNAME_SIZE+1];
+ strncpy(str2,gro+j*MED_LNAME_SIZE,MED_LNAME_SIZE);
+ str2[MED_LNAME_SIZE] = '\0';
+ ListeGroupes.insert(std::string(str2));
+ }
+ }
+ free(gro);
}
+ }
+ break ;
+ }
+ // Fermeture du fichier
+ if ( medIdt > 0 ) MEDfileClose(medIdt);
- med_int* attide = (med_int*) malloc(sizeof(med_int)*natt);
- med_int* attval = (med_int*) malloc(sizeof(med_int)*natt);
- char* attdes = (char *) malloc(MED_TAILLE_DESC*natt+1);
- char* gro = (char*) malloc(MED_TAILLE_LNOM*ngro+1);
- if (MEDfamInfo(MedIdt,maa,i+1,nomfam,&numfam,attide,attval,attdes,
- &natt,gro,&ngro) < 0)
- {
- // GERALD -- QMESSAGE BOX
- std::cerr << " Error : Families are unreadable" << std::endl;
- std::cerr << "Pb avec la famille : " << i << std::endl;
- break;
- }
- free(attide);
- free(attval);
- free(attdes);
- if ((numfam )> 0) { continue;} // On ne garde que les familles d elts
-
- for (int j=0;j<ngro;j++)
- {
- char str2[MED_TAILLE_LNOM+1];
- strncpy(str2,gro+j*MED_TAILLE_LNOM,MED_TAILLE_LNOM);
- str2[MED_TAILLE_LNOM] = '\0';
- ListeGroupes.insert(std::string(str2));
-
- }
- free(gro);
- }
- MEDfermer(MedIdt);
- return ListeGroupes;
+ return ListeGroupes;
}
// =======================================================================
// en position 6 et 7 Zmin, Zmax et en position 8 Dz si < 0 2D
// 9 distance max dans le maillage
- std::vector<double> LesExtremes;
-
- // Ouverture du Fichier Med
- int MedIdt = MEDouvrir(const_cast<char *>(aFile),MED_LECTURE);
- if (MedIdt <0)
- {
- // GERALD -- QMESSAGE BOX
- std::cerr << "Error : mesh is unreadable" << std::endl;
- return LesExtremes;
- }
-
- // Le fichier Med est lisible
- // Boucle sur les noms de maillage
- int numberOfMeshes = MEDnMaa(MedIdt) ;
- if (numberOfMeshes != 1 )
- {
- // GERALD -- QMESSAGE BOX
- std::cerr << "Error : file contains more than one mesh" << std::endl;
- return LesExtremes;
- }
-
- char maa[MED_TAILLE_NOM+1];
- char desc[MED_TAILLE_DESC+1];
- char nomcoo[3*MED_TAILLE_PNOM+1];
- char unicoo[3*MED_TAILLE_PNOM+1];
- med_maillage type;
- med_repere rep;
- med_booleen inonoe,inunoe;
+ std::vector<double> LesExtremes;
+ med_err erreur = 0 ;
+ med_idt medIdt ;
+ while ( erreur == 0 )
+ {
+ // Ouverture du fichier
+ medIdt = MEDfileOpen(aFile,MED_ACC_RDONLY);
+ if ( medIdt < 0 )
+ {
+ erreur = 1 ;
+ break ;
+ }
+ //Nombre de maillage : on ne peut en lire qu'un seul
+ med_int numberOfMeshes = MEDnMesh(medIdt) ;
+ if (numberOfMeshes != 1 )
+ {
+ erreur = 2 ;
+ break ;
+ }
+ // Caracteristiques du maillage
+ char meshname[MED_NAME_SIZE+1];
+ med_int spacedim,meshdim;
+ med_mesh_type meshtype;
+ char descriptionription[MED_COMMENT_SIZE+1];
+ char dtunit[MED_SNAME_SIZE+1];
+ med_sorting_type sortingtype;
+ med_int nstep;
+ med_axis_type axistype;
+ int naxis = MEDmeshnAxis(medIdt,1);
+ char *axisname=new char[naxis*MED_SNAME_SIZE+1];
+ char *axisunit=new char[naxis*MED_SNAME_SIZE+1];
+ erreur = MEDmeshInfo(medIdt,
+ 1,
+ meshname,
+ &spacedim,
+ &meshdim,
+ &meshtype,
+ descriptionription,
+ dtunit,
+ &sortingtype,
+ &nstep,
+ &axistype,
+ axisname,
+ axisunit);
+ delete[] axisname ;
+ delete[] axisunit ;
+ if ( erreur < 0 ) { break ; }
- med_int dim;
- med_int numMaillage=1;
+ // Nombre de noeuds
+ med_bool chgt,trsf;
+ med_int nnoe = MEDmeshnEntity(medIdt,
+ meshname,
+ MED_NO_DT,
+ MED_NO_IT,
+ MED_NODE,
+ MED_NO_GEOTYPE,
+ MED_COORDINATE,
+ MED_NO_CMODE,
+ &chgt,
+ &trsf);
+ if ( nnoe < 0 )
+ {
+ erreur = 4;
+ break ;
+ }
- if (MEDmaaInfo(MedIdt,numMaillage,maa,&dim,&type,desc) < 0)
- {
- // GERALD -- QMESSAGE BOX
- std::cerr << "Error : mesh " << numMaillage << " is unreadable" << std::endl;
- return LesExtremes;
- }
+ // Les coordonnees
+ med_float* coo = (med_float*) malloc(sizeof(med_float)*nnoe*spacedim);
- med_int nnoe = MEDnEntMaa(MedIdt,maa,MED_COOR,MED_NOEUD,(med_geometrie_element)0,(med_connectivite)0);
- if ( nnoe < 0)
- {
- // GERALD -- QMESSAGE BOX
- std::cerr << "Error : mesh " << numMaillage << " is unreadable" << std::endl;
- return LesExtremes;
- }
+ erreur = MEDmeshNodeCoordinateRd(medIdt,
+ meshname,
+ MED_NO_DT,
+ MED_NO_IT,
+ MED_NO_INTERLACE,
+ coo);
+ if ( erreur < 0 )
+ {
+ free(coo) ;
+ break ;
+ }
- med_float* coo = (med_float*) malloc(sizeof(med_float)*nnoe*dim);
- med_int* numnoe = (med_int*) malloc(sizeof(med_int)*nnoe);
- med_int* nufano = (med_int*) malloc(sizeof(med_int)*nnoe);
- char * nomnoe = (char*) malloc(MED_TAILLE_PNOM*nnoe+1);
+ // Calcul des extremes
+ med_float xmin,xmax,ymin,ymax,zmin,zmax;
- MEDnoeudsLire(MedIdt,maa,dim,coo,MED_NO_INTERLACE,&rep,
- nomcoo,unicoo,nomnoe,&inonoe,numnoe,&inunoe,
- nufano,nnoe);
-
- med_float xmin,xmax,ymin,ymax,zmin,zmax;
-
- xmin=coo[0];
- xmax=coo[0];
- for (int i=1;i<nnoe;i++)
- {
- xmin = min(xmin,coo[i]);
- xmax = max(xmax,coo[i]);
- }
-//
- if (dim > 1)
- {
- ymin=coo[nnoe]; ymax=coo[nnoe];
- for (int i=nnoe+1;i<2*nnoe;i++)
- {
- ymin = min(ymin,coo[i]);
- ymax = max(ymax,coo[i]);
- }
- }
- else
- {
- ymin=0;
- ymax=0;
- zmin=0;
- zmax=0;
- }
+ xmin=coo[0];
+ xmax=coo[0];
+ for (int i=1;i<nnoe;i++)
+ {
+ xmin = std::min(xmin,coo[i]);
+ xmax = std::max(xmax,coo[i]);
+ }
+ //
+ if (spacedim > 1)
+ {
+ ymin=coo[nnoe]; ymax=coo[nnoe];
+ for (int i=nnoe+1;i<2*nnoe;i++)
+ {
+ ymin = std::min(ymin,coo[i]);
+ ymax = std::max(ymax,coo[i]);
+ }
+ }
+ else
+ {
+ ymin=0;
+ ymax=0;
+ zmin=0;
+ zmax=0;
+ }
//
- if (dim > 2)
- {
- zmin=coo[2*nnoe]; zmax=coo[2*nnoe];
- for (int i=2*nnoe+1;i<3*nnoe;i++)
- {
- zmin = min(zmin,coo[i]);
- zmax = max(zmax,coo[i]);
- }
- }
- else
- {
- zmin=0;
- zmax=0;
- }
- MEDfermer(MedIdt);
+ if (spacedim > 2)
+ {
+ zmin=coo[2*nnoe]; zmax=coo[2*nnoe];
+ for (int i=2*nnoe+1;i<3*nnoe;i++)
+ {
+ zmin = std::min(zmin,coo[i]);
+ zmax = std::max(zmax,coo[i]);
+ }
+ }
+ else
+ {
+ zmin=0;
+ zmax=0;
+ }
- MESSAGE( "_______________________________________");
- MESSAGE( "xmin : " << xmin << " xmax : " << xmax );
- MESSAGE( "ymin : " << ymin << " ymax : " << ymax );
- MESSAGE( "zmin : " << zmin << " zmax : " << zmax );
- MESSAGE( "_______________________________________" );
- double epsilon = 1.e-6 ;
- LesExtremes.push_back(xmin);
- LesExtremes.push_back(xmax);
- LesExtremes.push_back(0);
- LesExtremes.push_back(ymin);
- LesExtremes.push_back(ymax);
- LesExtremes.push_back(0);
- LesExtremes.push_back(zmin);
- LesExtremes.push_back(zmax);
- LesExtremes.push_back(0);
+ MESSAGE( "_______________________________________");
+ MESSAGE( "xmin : " << xmin << " xmax : " << xmax );
+ MESSAGE( "ymin : " << ymin << " ymax : " << ymax );
+ MESSAGE( "zmin : " << zmin << " zmax : " << zmax );
+ MESSAGE( "_______________________________________" );
+ double epsilon = 1.e-6 ;
+ LesExtremes.push_back(xmin);
+ LesExtremes.push_back(xmax);
+ LesExtremes.push_back(0);
+ LesExtremes.push_back(ymin);
+ LesExtremes.push_back(ymax);
+ LesExtremes.push_back(0);
+ LesExtremes.push_back(zmin);
+ LesExtremes.push_back(zmax);
+ LesExtremes.push_back(0);
- double max1=max ( LesExtremes[1] - LesExtremes[0] , LesExtremes[4] - LesExtremes[3] ) ;
- double max2=max ( max1 , LesExtremes[7] - LesExtremes[6] ) ;
+ double max1=std::max ( LesExtremes[1] - LesExtremes[0] , LesExtremes[4] - LesExtremes[3] ) ;
+ double max2=std::max ( max1 , LesExtremes[7] - LesExtremes[6] ) ;
LesExtremes.push_back(max2);
// LesExtremes[0] = Xmini du maillage
// On fait un traitement pour dans le cas d'une coordonnee constante
// inhiber ce cas en mettant un increment negatif
//
- double diff = LesExtremes[1] - LesExtremes[0];
- if (fabs(diff) > epsilon*max2)
- {
- LesExtremes[2] = diff/100.;
- }
- else
- {
- LesExtremes[2] = -1. ;
- }
+ double diff = LesExtremes[1] - LesExtremes[0];
+ if ( fabs(diff) > epsilon*max2 ) { LesExtremes[2] = diff/100.; }
+ else { LesExtremes[2] = -1. ; }
+
+ diff = LesExtremes[4] - LesExtremes[3];
+ if ( fabs(diff) > epsilon*max2 ) { LesExtremes[5]=diff/100.; }
+ else { LesExtremes[5] = -1. ; }
- diff = LesExtremes[4] - LesExtremes[3];
- if (fabs(diff) > epsilon*max2)
- {
- LesExtremes[5]=diff/100.;
- }
- else
- {
- LesExtremes[5] = -1. ;
- }
+ diff = LesExtremes[7] - LesExtremes[6];
+ if ( fabs(diff) > epsilon*max2 ) { LesExtremes[8]=diff/100.; }
+ else { LesExtremes[8] = -1. ; }
- diff = LesExtremes[7] - LesExtremes[6];
- if (fabs(diff) > epsilon*max2)
- {
- LesExtremes[8]=diff/100.;
- }
- else
- {
- LesExtremes[8] = -1. ;
- }
+ MESSAGE ( "_______________________________________" );
+ MESSAGE ( "xmin : " << LesExtremes[0] << " xmax : " << LesExtremes[1] << " xincr : " << LesExtremes[2] );
+ MESSAGE ( "ymin : " << LesExtremes[3] << " ymax : " << LesExtremes[4] << " yincr : " << LesExtremes[5] );
+ MESSAGE ( "zmin : " << LesExtremes[6] << " zmax : " << LesExtremes[7] << " zincr : " << LesExtremes[8] );
+ MESSAGE ( "dmax : " << LesExtremes[9] );
+ MESSAGE ( "_______________________________________" );
- MESSAGE ( "_______________________________________" );
- MESSAGE ( "xmin : " << LesExtremes[0] << " xmax : " << LesExtremes[1] << " xincr : " << LesExtremes[2] );
- MESSAGE ( "ymin : " << LesExtremes[3] << " ymax : " << LesExtremes[4] << " yincr : " << LesExtremes[5] );
- MESSAGE ( "zmin : " << LesExtremes[6] << " zmax : " << LesExtremes[7] << " zincr : " << LesExtremes[8] );
- MESSAGE ( "dmax : " << LesExtremes[9] );
- MESSAGE ( "_______________________________________" );
+ free(coo) ;
+ break ;
+ }
+ // Fermeture du fichier
+ if ( medIdt > 0 ) MEDfileClose(medIdt);
return LesExtremes;
}