Salome HOME
Update of CheckDone
[modules/smesh.git] / src / SMESH / MG_ADAPT.cxx
index 8f2a36a44f25b9be1fbb7a83dd88ed959cf6ce5f..b7af53ab04a152f4750c8f36c8c4b7685d195413 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2020-2021  CEA/DEN, EDF R&D
+// Copyright (C) 2020-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
 
 #include "MG_ADAPT.hxx"
 
-#include "SMESH_File.hxx"
-#include "SMESH_Comment.hxx"
+#include <DriverGMF_Read.hxx>
+#include <SMESH_Comment.hxx>
+#include <SMESH_File.hxx>
+#include <SMESH_MGLicenseKeyGen.hxx>
+#include <SMESH_TypeDefs.hxx>
 
 #include <MEDFileData.hxx>
 #include <MEDFileField.hxx>
@@ -40,6 +43,7 @@
 #include <fcntl.h>
 #include <array>
 #include <memory>   // unique_ptr
+#include <chrono>
 
 typedef SMESH_Comment ToComment;
 
@@ -320,7 +324,7 @@ MgAdaptHypothesisData* MgAdapt::getData() const
 }
 void MgAdapt::setMedFileIn(std::string fileName)
 {
-  if ( isFileExist(fileName) )
+  if ( isFileExist( fileName ))
   {
     medFileIn = fileName;
 
@@ -953,6 +957,7 @@ std::string MgAdapt::getCommandToRun()
   cmd+= " --in "+ meshIn;
   meshFormatOutputMesh = getFileName()+".mesh";
   tmpFilesToBeDeleted.push_back(meshFormatOutputMesh);
+
   cmd+= " --out "+ meshFormatOutputMesh;
   if (useLocalMap || useConstantValue) cmd+= " --sizemap "+ solFileIn;
   else //  (useBackgroundMap)
@@ -1006,7 +1011,7 @@ std::string MgAdapt::getCommandToRun()
         cmd += " --";
       else
         cmd += " ";
-//       std::cout << "--- option: '" << option << ", value: '" << value <<"'"<< std::endl;
+      //       std::cout << "--- option: '" << option << ", value: '" << value <<"'"<< std::endl;
       cmd += option + " " + value;
     }
   }
@@ -1016,13 +1021,36 @@ std::string MgAdapt::getCommandToRun()
   {
     cmd+= " --verbose "+ ToComment(verbosityLevel);
   }
-    //~}
-//~cmd+= " >"
+  // get license key
+  {
+    smIdType nbVertex, nbEdge, nbFace, nbVol;
+    DriverGMF_Read gmfReader;
+    gmfReader.SetFile( meshIn );
+    gmfReader.GetMeshInfo( nbVertex, nbEdge, nbFace, nbVol );
+
+    std::string errorTxt;
+    std::string key = SMESHUtils_MGLicenseKeyGen::GetKey( meshIn,
+                                                          FromSmIdType<int>( nbVertex ),
+                                                          FromSmIdType<int>( nbEdge ),
+                                                          FromSmIdType<int>( nbFace ),
+                                                          FromSmIdType<int>( nbVol ),
+                                                          errorTxt );
+    if ( key.empty() )
+      return ToComment( "Problem with library SalomeMeshGemsKeyGenerator: " + errorTxt );
+
+    if ( key!="0" )
+      cmd += " --key " + key;
+  }
+
 #ifdef WIN32
-    cmd += " < NUL";
+  cmd += " < NUL";
 #endif
-//   std::cout << "--- cmd :"<< std::endl;
-//   std::cout << cmd << std::endl;
+
+  if (SALOME::VerbosityActivated())
+    {
+      std::cout << "--- cmd :"<< std::endl;
+      std::cout << cmd << std::endl;
+    }
 
   return cmd;
 }
@@ -1074,7 +1102,9 @@ std::string MgAdapt::defaultWorkingDirectory()
   {
     aTmpDir = Tmp_dir;
   }
-  else {
+
+  if ( ! isFileExist( aTmpDir ))
+  {
 #ifdef WIN32
     aTmpDir = "C:\\";
 #else
@@ -1107,7 +1137,7 @@ std::string MgAdapt::getFileName() const
 aGenericName << _getpid();
 #endif
   aGenericName << "_";
-  aGenericName << std::abs((int)(long) aGenericName.data());
+  aGenericName << std::chrono::system_clock::now().time_since_epoch().count();
 
   return aGenericName;
 }
@@ -1381,8 +1411,17 @@ void MgAdapt::convertMedFile(std::string& meshFormatMeshFileName, std::string& s
     checkTimeStepRank(medFileIn) ;
     MEDCoupling::MCAuto<MEDCoupling::MEDFileAnyTypeFieldMultiTS> fts( mfd->getFields()->getFieldWithName(fieldName) );
     MEDCoupling::MCAuto<MEDCoupling::MEDFileAnyTypeField1TS> f = fts->getTimeStep(timeStep, rank);
-    MEDCoupling::MCAuto<MEDCoupling::MEDFileFieldMultiTS> tmFts = MEDCoupling::MEDFileFieldMultiTS::New();
-    tmFts->pushBackTimeStep(f);
+    MEDCoupling::MCAuto<MEDCoupling::MEDFileFieldMultiTS> tmFts = MEDCoupling::DynamicCast<MEDCoupling::MEDFileAnyTypeFieldMultiTS,MEDCoupling::MEDFileFieldMultiTS>(fts);
+
+    // if not able to cast to double field, try float field
+    if (!tmFts)
+    {
+      MEDCoupling::MCAuto<MEDCoupling::MEDFileFloatFieldMultiTS>  tmFtsFloat = MEDCoupling::DynamicCast<MEDCoupling::MEDFileAnyTypeFieldMultiTS,MEDCoupling::MEDFileFloatFieldMultiTS>(fts);
+      if (!tmFtsFloat)
+        THROW_SALOME_EXCEPTION("\nUnexpected field type.\n");
+      // convert float field to double
+      tmFts = tmFtsFloat->convertToDouble();
+    }
 
     fields->pushField(tmFts);
 
@@ -1543,9 +1582,9 @@ MgAdapt::Status MgAdapt::addMessage(const std::string& msg,
   _errorMessages.push_back( msg );
 
   //~MESSAGE(msg);
-#ifdef _DEBUG_
-  std::cout << msg << std::endl;
-#endif
+  if (SALOME::VerbosityActivated())
+    std::cout << msg << std::endl;
+
   return ( _status = isFatal ? MgAdapt::DRS_FAIL : MgAdapt::DRS_WARN_SKIP_ELEM );
 }