Salome HOME
[bos #40653][CEA] New mesh import export formats with meshio. kleontev/40653_mesh_import_export_formats_meshio 39/head
authorKonstantin Leontev <Konstantin.LEONTEV@opencascade.com>
Tue, 14 May 2024 19:50:43 +0000 (20:50 +0100)
committerKonstantin Leontev <Konstantin.LEONTEV@opencascade.com>
Wed, 22 May 2024 10:58:12 +0000 (11:58 +0100)
Added using MESHIO_VERSION env variable to detect meshio version.
Exodus format removed from a file filter on Windows.

src/SMESHGUI/SMESHGUI_Meshio.cxx
src/SMESH_I/SMESH_Meshio.cxx
src/SMESH_I/SMESH_Meshio.h
test/SMESH_MeshioShapes.py

index 048dd4ac36b88bf591b409dd439fa55814589878..10fb582c2d578676e56c0e71bfd2c2fe3d24d50e 100644 (file)
@@ -137,7 +137,9 @@ const QStringList& SMESHGUI_Meshio::GetExportFileFilter()
     "AVS-UCD (*.avs)",
     "CGNS (*.cgns)",
     "DOLFIN XML (*.xml)",
+#if !defined(WIN32)
     "Exodus (*.e *.exo)",
+#endif
     "FLAC3D (*.f3grid)",
     "Gmsh 2.2 (*.msh)",
     "Gmsh 4.0, and 4.1 (*.msh)",
@@ -241,19 +243,7 @@ QString SMESHGUI_Meshio::GetFileName(QString& selectedFilter, const bool isOpen/
 */
 bool SMESHGUI_Meshio::IsMeshioInstalled()
 {
-  auto IsAbleToCallMeshio = []() -> bool
-  {
-    // Try to call meshio to check if it's present
-    const std::string cmd =
-      SMESH_Meshio::IsModernPythonVersion() ? "meshio --version" : "meshio-info --version";
-
-    const int status = system(cmd.c_str());
-    MESSAGE("status: " << status);
-
-    return status == 0;
-  };
-
-  static const bool isInstalled = IsAbleToCallMeshio();
+  const bool isInstalled = SMESH_Meshio::IsMeshioInstalled();
   if (!isInstalled)
   {
     SUIT_MessageBox::warning(
index d23d52c6c123b35efd288bea4146bbb55a03d7a2..6e744669f0573de403ec48b5af05658fb753fb2d 100644 (file)
@@ -71,7 +71,7 @@ SMESH_Meshio::~SMESH_Meshio()
 void SMESH_Meshio::Convert(const QString& sourceFileName, const QString& targetFileName) const
 {
   // Execute meshio convert command
-  const QString convert = IsModernPythonVersion() ? "meshio convert " : "meshio-convert ";
+  const QString convert = IsModernMeshioVersion() ? "meshio convert " : "meshio-convert ";
   const QString optArgs = GetConvertOptArgs();
 
   const std::string cmd = 
@@ -128,6 +128,133 @@ void SMESH_Meshio::RemoveTempFile()
   }
 }
 
+/*!
+  Returns meshio version string that has valid integer at least in the first position.
+*/
+QString SMESH_Meshio::GetMeshioVersion()
+{
+  auto IsVersionStringValid = [](const QString& version) -> bool
+  {
+    if (version.isEmpty())
+    {
+      return false;
+    }
+
+    // Check if we have an integer at least at the first position
+    const QStringList curVersionNums = version.split('.');
+
+    bool ok;
+    const int firstNum = curVersionNums[0].toInt(&ok);
+    if (!ok)
+    {
+      ERROR_MESSAGE("meshio version value is not valid!");
+      return false;
+    }
+
+    MESSAGE("meshio version first number: " << firstNum);
+    return true;
+  };
+
+  auto GetMeshioVersionFromEnv = []() -> QString
+  {
+    const char *envVar = std::getenv("MESHIO_VERSION");
+    if (envVar && (envVar[0] != '\0'))
+    {
+      MESSAGE("MESHIO_VERSION: " << envVar);
+      return envVar;
+    }
+
+    MESSAGE("MESHIO_VERSION is not set!");
+    return {};
+  };
+
+  auto GetMeshioVersionHelper = [&]() -> QString
+  {
+    // Check if we can get a version from environment
+    const QString meshioVersionEnv = GetMeshioVersionFromEnv();
+    if (IsVersionStringValid(meshioVersionEnv))
+    {
+      return meshioVersionEnv;
+    }
+
+    // Try to gess a version by installed Python version
+    const QString meshioVersionByPython = IsModernPythonVersion() ? "5" : "4";
+    MESSAGE("meshio version was defined by Python version: " << meshioVersionByPython.toStdString());
+
+    return meshioVersionByPython;
+  };
+
+  static const QString meshioVersion = GetMeshioVersionHelper();
+  return meshioVersion;
+};
+
+/*!
+  Returns true if we're going to use meshio version of 5.0 or greater.
+*/
+bool SMESH_Meshio::IsModernMeshioVersion()
+{
+  // It's a version when meshio commands were changed from using
+  // many executables for each operation to one for everything (meshio 5.0).
+  // For example, from
+  // meshio-convert input.msh output.vtk
+  // to
+  // meshio convert input.msh output.vtk
+
+  auto IsModernVersion = [&]() -> bool
+  {
+    const QString curVersion = GetMeshioVersion();
+
+    const int minReqVersion = 5;
+    const QStringList curVersionNums = curVersion.split('.');
+    if (minReqVersion > curVersionNums[0].toInt())
+    {
+      return false;
+    }
+
+    return true;
+  };
+
+  static const bool isModern = IsModernVersion();
+  return isModern;
+}
+
+/*!
+  Returns true if meshio is installed
+*/
+bool SMESH_Meshio::IsMeshioInstalled()
+{
+  auto IsAllowedFromEnvironment = []() -> bool
+  {
+    const QString curVersion = GetMeshioVersion();
+
+    // Check if we explicitly set off using of meshio from environment
+    const QStringList curVersionNums = curVersion.split('.');
+    const int firstNum = curVersionNums[0].toInt();
+    if (firstNum <= 0)
+    {
+      MESSAGE("meshio was set as not installed from an environment");
+      return false;
+    }
+
+    return true;
+  };
+
+  auto IsAbleToCallMeshio = []() -> bool
+  {
+    // Try to call meshio to check if it's present
+    const std::string cmd =
+      SMESH_Meshio::IsModernMeshioVersion() ? "meshio --version" : "meshio-info --version";
+
+    const int status = system(cmd.c_str());
+    MESSAGE("status: " << status);
+
+    return status == 0;
+  };
+
+  static const bool isInstalled = IsAllowedFromEnvironment() && IsAbleToCallMeshio();
+  return isInstalled;
+}
+
 /*!
   Returns true if current Python equal or newer than required version for
   meshio release from 5.0 and greater.
index efb41b0bf49017c7d20ba94276b77e4f42b6fc6b..2263f5c78f377a7ddf9701236f1e13809366ca09 100644 (file)
@@ -42,6 +42,9 @@ public:
   QString CreateTempFileName(const QString& targetFileName);
   void Convert(const QString& sourceFileName, const QString& targetFileName) const;
 
+  static QString GetMeshioVersion();
+  static bool IsModernMeshioVersion();
+  static bool IsMeshioInstalled();
   static bool IsModernPythonVersion();
 
 private:
index f77d7a9995784b550e2fcf831208ca9a797f7d4b..914c8b9b914fc6a94e3b7c817b091852677651fa 100644 (file)
@@ -37,6 +37,8 @@ from salome.smesh import smeshBuilder
 
 import SALOME
 
+import platform
+
 # Constants
 EXPORT_TITLE = 'Export'
 IMPORT_TITLE = 'Import'
@@ -92,7 +94,7 @@ def file_extensions():
     Commented formats should be checked on next meshio release to see if the problem was fixed.
     """
 
-    return [
+    extensions = [
         '.avs',
         '.bdf',
         # '.cgns', # meshio IndexError: index 2 is out of bounds for axis 1 with size 2
@@ -131,7 +133,10 @@ def file_extensions():
         '.xmf',
         '.xml'
     ]
+    if platform.system() == 'Windows':
+        extensions = [ext for ext in extensions if not ext in ['.e', '.exo']] # needs to be digged out - presumably an issue about encoding.
 
+    return extensions
 
 def exception_handle(file_name, errors, operation_type, ex_text):
     """