[bos #40653][CEA] New mesh import export formats with meshio.

Added using MESHIO_VERSION env variable to detect meshio version.
Exodus format removed from a file filter on Windows.
This commit is contained in:
Konstantin Leontev 2024-05-14 20:50:43 +01:00
parent 35c26749e5
commit e85c9d5b6b
4 changed files with 140 additions and 15 deletions

View 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(

View 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.

View 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:

View 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):
"""