Salome HOME
Merge branch 'master' into gni/adaptation
[modules/smesh.git] / src / SMESHGUI / MG_ADAPTGUI.cxx
index 8e7a593b1ad42c2fcfc7d9da55e25c7542af21a9..a449cdd0ba018bd22d5209fa4471f6b4961d7d2e 100644 (file)
@@ -23,6 +23,9 @@
 
 #include "MG_ADAPTGUI.hxx"
 
+#include "MEDFileData.hxx"
+#include "MEDLoader.hxx"
+
 #include "SUIT_Desktop.h"
 #include "SUIT_Application.h"
 #include "SUIT_Session.h"
@@ -484,6 +487,7 @@ SMESHGUI_MgAdaptArguments::SMESHGUI_MgAdaptArguments( QWidget* parent )
   }
 
   meshDim = 0;
+  meshDimBG = 0;
   // Mesh in
   aMeshIn = new QGroupBox( tr( "MeshIn" ), this );
   aMedfile       = new QRadioButton( tr( "MEDFile" ),    aMeshIn );
@@ -698,6 +702,10 @@ void SMESHGUI_MgAdaptArguments::onSelectMedFileBackgroundbutton()
         int typeStepInField = it->second > 2 ?  2 : it->second ;
         timeStepGroupChanged(typeStepInField, false);
       }
+      // Dimension du maillage de fonds
+      MEDCoupling::MCAuto<MEDCoupling::MEDFileData> mfd = MEDCoupling::MEDFileData::New(fileName.toStdString());
+      meshDimBG = mfd->getMeshes()->getMeshAtPos(0)->getMeshDimension() ;
+      valueAdaptation ();
     }
   }
   else
@@ -753,8 +761,9 @@ void SMESHGUI_MgAdaptArguments::onSelectMedFilebuttonClicked()
     else
     {
       meshNameLineEdit->setText(aMeshName);
-      ADAPTATION_MODE aMode = meshDim == 3 ? ADAPTATION_MODE::BOTH : ADAPTATION_MODE::SURFACE; // and when dimesh 3 without 2D mesh?
-      emit meshDimSignal(aMode);
+      valueAdaptation ();
+//       ADAPTATION_MODE aMode = meshDim == 3 ? ADAPTATION_MODE::BOTH : ADAPTATION_MODE::SURFACE; // and when dimesh 3 without 2D mesh?
+//       emit meshDimSignal(aMode);
     }
   }
   else
@@ -773,6 +782,20 @@ void SMESHGUI_MgAdaptArguments::onSelectMedFilebuttonClicked()
 
 }
 
+void SMESHGUI_MgAdaptArguments::valueAdaptation()
+{
+  ADAPTATION_MODE aMode ;
+  if ( meshDimBG < 3 )
+  {
+    aMode = meshDim == 3 ? ADAPTATION_MODE::BOTH : ADAPTATION_MODE::SURFACE;
+  }
+  else
+  {
+    aMode = ADAPTATION_MODE::BOTH;
+  }
+  emit meshDimSignal(aMode);
+}
+
 void SMESHGUI_MgAdaptArguments::onLocalSelected(QString filePath)
 {
   myFieldList = GetListeChamps(filePath, false);
@@ -883,7 +906,6 @@ void SMESHGUI_MgAdaptArguments::sizeMapDefChanged( int  theSizeMap )
     valueLabel->hide();
     dvalue->hide();
     sizeMapField->setEnabled(true);
-
   }
   else
   {
@@ -896,6 +918,8 @@ void SMESHGUI_MgAdaptArguments::sizeMapDefChanged( int  theSizeMap )
     dvalue->show();
     sizeMapField->setEnabled(false);
   }
+  meshDimBG = 0;
+  valueAdaptation();
 }
 void SMESHGUI_MgAdaptArguments::timeStepGroupChanged(int timeStepType, bool disableOther, int vmax)
 {
@@ -922,10 +946,10 @@ void SMESHGUI_MgAdaptArguments::clear()
   meshNameLineEdit->clear();
   selectOutMedFileLineEdit->clear();
 }
-med_int SMESHGUI_MgAdaptArguments::getMeshDim() const
-{
-  return meshDim;
-}
+// med_int SMESHGUI_MgAdaptArguments::getMeshDim() const
+// {
+//   return meshDim;
+// }
 QWidget* ItemDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &o, const QModelIndex &index) const
 {
   bool editable = index.data( EDITABLE_ROLE ).toInt();
@@ -1146,7 +1170,7 @@ void MgAdaptAdvWidget::_onWorkingDirectoryPushButton()
 void MgAdaptAdvWidget::onMeshDimChanged(ADAPTATION_MODE aMode)
 {
 /* default adaptation mode
-  * assume that if meshDim == 2 -->adaptation surface
+  * assume that if meshDim == 2 and no 3D backgrounmesh-->adaptation surface
   * if meshDim == 3 and  if there is not 2D mesh -->VOLUME
   * else BOTH
   */
@@ -1256,101 +1280,43 @@ void MgAdaptAdvWidgetTreeWidget::keyPressEvent( QKeyEvent* e )
   QTreeWidget::keyPressEvent( e );
 }
 
-
-// =======================================================================
-// renvoie le medId associe au fichier Med apres ouverture
-// =======================================================================
-med_idt OuvrirFichier(QString aFile)
-{
-  med_idt medIdt = MEDfileOpen(aFile.toStdString().c_str(),MED_ACC_RDONLY);
-  if (medIdt <0)
-  {
-    QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
-                              QObject::tr("MG_ADAPT_MED_FILE_1") );
-  }
-  return medIdt;
-}
-
 // ======================================================
 // ========================================================
 QString lireNomMaillage(QString aFile, med_int& meshdim)
 {
   QString nomMaillage = QString::null ;
-  int erreur = 0 ;
-  med_idt medIdt ;
 
-  //  Ouverture du fichier
-  medIdt = OuvrirFichier(aFile);
-  if ( medIdt < 0 )
-  {
-    erreur = 1 ;
-    return nomMaillage;
-  }
-  med_int numberOfMeshes = MEDnMesh(medIdt) ;
-  if (numberOfMeshes == 0 )
+  while ( true )
   {
-    QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
-                              QObject::tr("MG_ADAPT_MED_FILE_2") );
-    erreur = 2 ;
-    return nomMaillage;
-  }
-  if (numberOfMeshes > 1 )
-  {
-    QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
-                              QObject::tr("MG_ADAPT_MED_FILE_3") );
-    erreur = 3 ;
-    return nomMaillage;
-  }
+    std::vector<std::string> listMeshesNames = MEDCoupling::GetMeshNames(aFile.toStdString());
 
-  nomMaillage = lireNomMaillage2(medIdt,1, meshdim);
-  // Fermeture du fichier
-  if ( medIdt > 0 ) MEDfileClose(medIdt);
+    std::size_t numberOfMeshes(listMeshesNames.size());
+  //   std::cout << "numberOfMeshes:" << numberOfMeshes << std::endl;
+    if (numberOfMeshes == 0 )
+    {
+      QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
+                                QObject::tr("MG_ADAPT_MED_FILE_2") );
+      break ;
+    }
+    if (numberOfMeshes > 1 )
+    {
+      QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
+                                QObject::tr("MG_ADAPT_MED_FILE_3") );
+      break ;
+    }
 
-  return nomMaillage;
-}
+//     std::cout << "nomMaillage:" << listMeshesNames[0] << std::endl;
+    nomMaillage = QString(listMeshesNames[0].c_str());
 
-// =======================================================================
-// =======================================================================
-QString lireNomMaillage2(med_idt medIdt,int meshId, med_int& meshdim )
-{
-  QString NomMaillage=QString::null;
-  char meshname[MED_NAME_SIZE+1];
-  med_int spacedim;
-  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];
-  med_err aRet = MEDmeshInfo(medIdt,
-                              meshId,
-                              meshname,
-                              &spacedim,
-                              &meshdim,
-                              &meshtype,
-                              descriptionription,
-                              dtunit,
-                              &sortingtype,
-                              &nstep,
-                              &axistype,
-                              axisname,
-                              axisunit);
-
-  if ( aRet < 0 ) {
-    QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"), \
-                              QObject::tr("MG_ADAPT_MED_FILE_4") );
-  }
-  else            {
-    NomMaillage=QString(meshname);
-  }
+    // Dimension du maillage
+    MEDCoupling::MCAuto<MEDCoupling::MEDFileData> mfd = MEDCoupling::MEDFileData::New(aFile.toStdString());
+    meshdim = mfd->getMeshes()->getMeshAtPos(0)->getMeshDimension() ;
+//     std::cout << "meshdim:" << meshdim << std::endl;
 
-  delete[] axisname ;
-  delete[] axisunit ;
+    break ;
+  }
 
-  return NomMaillage;
+  return nomMaillage;
 }
 
 // =======================================================================
@@ -1359,73 +1325,38 @@ std::map<QString, int> GetListeChamps(QString aFile, bool errorMessage)
 {
 // Il faut voir si plusieurs maillages
 
-  MESSAGE("GetListeChamps");
   std::map<QString, int> ListeChamp ;
 
-  med_err erreur = 0 ;
-  med_idt medIdt ;
-
-  while ( erreur == 0 )
+  while ( true )
   {
-    // Ouverture du fichier
-    SCRUTE(aFile.toStdString());
-    medIdt = OuvrirFichier(aFile);
-    if ( medIdt < 0 )
-    {
-      erreur = 1 ;
-      break ;
-    }
-    // Lecture du nombre de champs
-    med_int ncha = MEDnField(medIdt) ;
-    if (ncha < 1 )
+    MEDCoupling::MCAuto<MEDCoupling::MEDFileData> mfd = MEDCoupling::MEDFileData::New(aFile.toStdString());
+    std::vector<std::string> listFieldsNames(mfd->getFields()->getFieldsNames());
+    std::size_t jaux(listFieldsNames.size());
+    if (jaux < 1 )
     {
       if(errorMessage)
       {
         QMessageBox::critical( 0, QObject::tr("_ERROR"),
                                   QObject::tr("HOM_MED_FILE_5") );
       }
-      erreur = 2 ;
       break ;
     }
-    // Lecture des caracteristiques des champs
-    for (int i=0; i< ncha; i++)
+    // nbofcstp inutile pour le moment
+    med_int nbofcstp = 1;
+    for(std::size_t j=0;j<jaux;j++)
     {
-//       Lecture du nombre de composantes
-      med_int ncomp = MEDfieldnComponent(medIdt,i+1);
-//       Lecture du type du champ, des noms des composantes et du nom de l'unite
-      char nomcha  [MED_NAME_SIZE+1];
-      char meshname[MED_NAME_SIZE+1];
-      char * comp = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
-      char * unit = (char*) malloc(ncomp*MED_SNAME_SIZE+1);
-      char dtunit[MED_SNAME_SIZE+1];
-      med_bool local;
-      med_field_type typcha;
-      med_int nbofcstp;
-      erreur = MEDfieldInfo(medIdt,i+1,nomcha,meshname,&local,&typcha,comp,unit,dtunit,&nbofcstp) ;
-      free(comp);
-      free(unit);
-      if ( erreur < 0 )
-      {
-        if(errorMessage)
-        {
-          QMessageBox::critical( 0, QObject::tr("MG_ADAPT_ERROR"),
-                                    QObject::tr("MG_ADAPT_MED_FILE_6") );
-        }
-        break ;
-      }
-
-      ListeChamp.insert(std::pair<QString, int> (QString(nomcha), nbofcstp));
-
+//       std::cout << listFieldsNames[j] << std::endl;
+      ListeChamp.insert(std::pair<QString, int> (QString(listFieldsNames[j].c_str()), nbofcstp));
     }
     break ;
   }
-    // Fermeture du fichier
-  if ( medIdt > 0 ) MEDfileClose(medIdt);
 
   return ListeChamp;
 }
 
+// =======================================================================
 std::string remove_extension(const std::string& filename)
+// =======================================================================
 {
   size_t lastdot = filename.find_last_of(".");
   if (lastdot == std::string::npos) return filename;