From 772ad1d5f4f1be95a6dd232d7c4af387df7cb88d Mon Sep 17 00:00:00 2001 From: eap Date: Tue, 10 Aug 2021 20:46:27 +0300 Subject: [PATCH 01/10] bos #24596 [CEA] New MeshGems license --- src/DriverGMF/DriverGMF_Read.cxx | 34 ++ src/DriverGMF/DriverGMF_Read.hxx | 5 + src/SMESH/MG_ADAPT.cxx | 37 +- src/SMESHUtils/CMakeLists.txt | 2 + src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx | 492 +++++++++++++++++++++++ src/SMESHUtils/SMESH_MGLicenseKeyGen.hxx | 52 +++ 6 files changed, 614 insertions(+), 8 deletions(-) create mode 100644 src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx create mode 100644 src/SMESHUtils/SMESH_MGLicenseKeyGen.hxx diff --git a/src/DriverGMF/DriverGMF_Read.cxx b/src/DriverGMF/DriverGMF_Read.cxx index 175d8100d..c85abbf3c 100644 --- a/src/DriverGMF/DriverGMF_Read.cxx +++ b/src/DriverGMF/DriverGMF_Read.cxx @@ -537,3 +537,37 @@ Driver_Mesh::Status DriverGMF_Read::storeBadNodeIds(const char* gmfKwd, int elem } return DRS_OK; } + +//================================================================================ +/*! + * \brief Return number of mesh entities in a file + */ +//================================================================================ + +bool DriverGMF_Read::GetMeshInfo(smIdType & nbNodes, + smIdType & nbEdges, + smIdType & nbFaces, + smIdType & nbVols) +{ + nbNodes = nbEdges = nbFaces = nbVols = 0; + + Kernel_Utils::Localizer loc; + + int dim, version; + int meshID = GmfOpenMesh( myFile.c_str(), GmfRead, &version, &dim ); + if ( !meshID ) + return false; + + DriverGMF::MeshCloser aMeshCloser( meshID ); // An object closing GMF mesh at destruction + + nbNodes = GmfStatKwd(meshID, GmfVertices ); + nbEdges = GmfStatKwd(meshID, GmfEdges ); + nbFaces = ( GmfStatKwd(meshID, GmfTriangles ) + + GmfStatKwd(meshID, GmfQuadrilaterals )); + nbVols = ( GmfStatKwd(meshID, GmfTetrahedra ) + + GmfStatKwd(meshID, GmfPyramids ) + + GmfStatKwd(meshID, GmfHexahedra ) + + GmfStatKwd(meshID, GmfPrisms )); + + return true; +} diff --git a/src/DriverGMF/DriverGMF_Read.hxx b/src/DriverGMF/DriverGMF_Read.hxx index f055f54b7..8464a5345 100644 --- a/src/DriverGMF/DriverGMF_Read.hxx +++ b/src/DriverGMF/DriverGMF_Read.hxx @@ -57,6 +57,11 @@ public: virtual Status Perform(); + bool GetMeshInfo(smIdType & nbVertex, + smIdType & nbEdge, + smIdType & nbFace, + smIdType & nbVol); + private: Status storeBadNodeIds(const char* gmfKwd, int elemNb, int nb, ...); diff --git a/src/SMESH/MG_ADAPT.cxx b/src/SMESH/MG_ADAPT.cxx index 3530fe477..1e748c534 100644 --- a/src/SMESH/MG_ADAPT.cxx +++ b/src/SMESH/MG_ADAPT.cxx @@ -19,8 +19,11 @@ #include "MG_ADAPT.hxx" -#include "SMESH_File.hxx" -#include "SMESH_Comment.hxx" +#include +#include +#include +#include +#include #include #include @@ -1005,7 +1008,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; } } @@ -1015,13 +1018,31 @@ 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( nbVertex ), + FromSmIdType( nbEdge ), + FromSmIdType( nbFace ), + FromSmIdType( nbVol ), + errorTxt ); + if ( key.empty() ) + return ToComment( "Problem with library SalomeMeshGemsKeyGenerator: " + errorTxt ); + + cmd += " --key " + key; + } + #ifdef WIN32 - cmd += " < NUL"; + cmd += " < NUL"; #endif -// std::cout << "--- cmd :"<< std::endl; -// std::cout << cmd << std::endl; + // std::cout << "--- cmd :"<< std::endl; + // std::cout << cmd << std::endl; return cmd; } diff --git a/src/SMESHUtils/CMakeLists.txt b/src/SMESHUtils/CMakeLists.txt index 8eb4e3b68..e35033177 100644 --- a/src/SMESHUtils/CMakeLists.txt +++ b/src/SMESHUtils/CMakeLists.txt @@ -62,6 +62,7 @@ SET(SMESHUtils_HEADERS SMESH_Delaunay.hxx SMESH_Indexer.hxx SMESH_BoostTxtArchive.hxx + SMESH_MGLicenseKeyGen.hxx ) # --- sources --- @@ -86,6 +87,7 @@ SET(SMESHUtils_SOURCES SMESH_Slot.cxx SMESH_PolyLine.cxx SMESH_BoostTxtArchive.cxx + SMESH_MGLicenseKeyGen.cxx ) # --- rules --- diff --git a/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx b/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx new file mode 100644 index 000000000..e1d2db8d2 --- /dev/null +++ b/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx @@ -0,0 +1,492 @@ +// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE +// +// 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 +// +// File : SMESHUtils_MGLicenseKeyGen.cxx +// Created : Sat Jul 31 18:54:16 2021 +// Author : Edward AGAPOV (OCC) + +#include "SMESH_MGLicenseKeyGen.hxx" + +#include "SMESH_Comment.hxx" +#include "SMESH_File.hxx" +#include "SMESH_TryCatch.hxx" + +#include +#include + +#include +#include // getenv, system + +#include +namespace boofs = boost::filesystem; + +#ifdef WIN32 + +# include +# include + +# define LibHandle HMODULE +# define LoadLib( name ) LoadLibrary( name ) +# define GetProc GetProcAddress +# define UnLoadLib( handle ) FreeLibrary( handle ); + +#else // WIN32 + +# include + +# define LibHandle void* +# define LoadLib( name ) dlopen( name, RTLD_LAZY | RTLD_LOCAL ) +# define GetProc dlsym +# define UnLoadLib( handle ) dlclose( handle ); + +#endif // WIN32 + +// to retrieve description of exception caught by SMESH_TRY +#undef SMESH_CAUGHT +#define SMESH_CAUGHT error = + + +namespace +{ + static LibHandle theLibraryHandle = nullptr; //!< handle of a loaded library + + const char* theEnvVar = "SALOME_MG_KEYGEN_LIB_PATH"; /* var specifies either full file name + of libSalomeMeshGemsKeyGenerator or + URL to download the library from */ + + const char* theTmpEnvVar = "SALOME_TMP_DIR"; // directory to download the library to + + //----------------------------------------------------------------------------------- + /*! + * \brief Remove library file at destruction in case if it was downloaded from server + */ + //----------------------------------------------------------------------------------- + + struct LibraryFile + { + std::string _name; // full file name + bool _isURL; + + LibraryFile(): _isURL( false ) {} + + ~LibraryFile() + { + if ( _isURL ) + { + if ( theLibraryHandle ) + { + UnLoadLib( theLibraryHandle ); + theLibraryHandle = nullptr; + } + + std::string tmpDir; // tmp dir that should not be removed + if ( const char* libPath = getenv( theTmpEnvVar )) + { + tmpDir = libPath; + while (( !tmpDir.empty() ) && + ( tmpDir.back() == '/' || tmpDir.back() == '\\' )) + tmpDir.pop_back(); + } + + while ( SMESH_File( _name ).remove() ) + { + size_t length = _name.size(); + _name = boofs::path( _name ).parent_path().string(); // goto parent directory + if ( _name.size() == length ) + break; // no more parents + + if ( _name == tmpDir ) + break; // don't remove tmp dir + + if ( !Kernel_Utils::IsEmptyDir( _name )) + break; + } + } + } + }; + + + //================================================================================ + /*! + * \brief Retrieve description of the last error + * \param [out] error - return the description + * \return bool - true if the description found + */ + //================================================================================ + + bool getLastError( std::string& error ) + { +#ifndef WIN32 + + if ( const char* text = dlerror() ) + { + error = text; + return true; + } + return false; + +#else + + DWORD dw = GetLastError(); + void* cstr; + DWORD msgLen = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, + dw, + MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language + (LPTSTR) &cstr, + 0, + NULL + ); + if ( msgLen > 0 ) + error = (char*) cstr; + + LocalFree(cstr); + + return msgLen; + +#endif + } + + //================================================================================ + /*! + * \brief Adjust file extension according to the platform + */ + //================================================================================ + + bool setExtension( std::string& fileName, std::string& error ) + { + if ( fileName.empty() ) + { + error = "Library file name is empty"; + return false; + } +#if defined(WIN32) + std::string ext = ".dll"; +#elif defined(__APPLE__) + std::string ext = ".dylib"; +#else + std::string ext = ".so"; +#endif + + fileName = fileName.substr( 0, fileName.find_last_of('.')) + ext; + return true; + } + + //================================================================================ + /*! + * \brief Check if library file name looks like an URL + * \param [in,out] libraryFile - holds file name and returns result in _isURL member field + * \return bool - true if the file name looks like an URL + */ + //================================================================================ + + bool isURL( LibraryFile & libraryFile ) + { + enum { SCHEME = 2, AUTHORITY = 4, PATH = 5 }; // sub-strings + std::regex urlRegex ( R"(^(([^:\/?#]+):)?(//([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)", + std::regex::extended ); + std::smatch matchResult; + + libraryFile._isURL = false; + if ( std::regex_match( libraryFile._name, matchResult, urlRegex )) + libraryFile._isURL = ( !matchResult.str( SCHEME ).empty() && + !matchResult.str( AUTHORITY ).empty() && + !matchResult.str( PATH ).empty() ); + + return libraryFile._isURL; + } + + //================================================================================ + /*! + * \brief Download libraryFile._name URL to SALOME_TMP_DIR + * \param [in,out] libraryFile - holds the URL and returns name of a downloaded file + * \param [out] error - return error description + * \return bool - is a success + */ + //================================================================================ + + bool downloadLib( LibraryFile& libraryFile, std::string & error ) + { + // check if can write into SALOME_TMP_DIR + + std::string tmpDir = Kernel_Utils::GetTmpDirByEnv( theTmpEnvVar ); + if ( tmpDir.empty() || + !Kernel_Utils::IsExists( tmpDir )) + { + error = "Can't download " + libraryFile._name + " as SALOME_TMP_DIR is not correctly set"; + return false; + } + if ( !Kernel_Utils::IsWritable( tmpDir )) + { + error = "Can't download " + libraryFile._name + " as '" + tmpDir + "' is not writable. " + "Check SALOME_TMP_DIR environment variable"; + return false; + } + + // Download + + std::string url = libraryFile._name; + +#ifdef WIN32 + + std::string outFile = tmpDir + "libMeshGemsKeyGenerator.dll"; + + // use wget (== Invoke-WebRequest) PowerShell command available since Windows 7 + std::string psCmd = "wget -Uri " + url + " -OutFile " + outFile; + std::string cmd = "start powershell.exe " + psCmd; + +#else + + std::string outFile = tmpDir + "libMeshGemsKeyGenerator.so"; + + std::string cmd = "wget " + url + " -O " + outFile; + +#endif + + if ( Kernel_Utils::IsExists( outFile )) // remove existing file + { + SMESH_File lib( outFile, /*open=*/false ); + if ( !lib.remove() ) + { + error = lib.error(); + return false; + } + } + + system( cmd.c_str() ); // download + + SMESH_File resultFile( outFile, /*open=*/false ); + bool ok = ( resultFile.exists() && resultFile.size() > 0 ); + + if ( ok ) + libraryFile._name = outFile; + + return ok; + } + + //================================================================================ + /*! + * \brief Load libMeshGemsKeyGenerator.so + * \param [out] error - return error description + * \param [out] libraryFile - return library file name and _isURL flag + * \return bool - is a success + */ + //================================================================================ + + bool loadLibrary( std::string& error, LibraryFile& libraryFile ) + { + if ( theLibraryHandle ) + return true; + + const char* libPath = getenv( theEnvVar ); + if ( !libPath ) + { + error = SMESH_Comment( "Environment variable ") << theEnvVar << " is not set"; + return false; + } + + libraryFile._name = libPath; + // if ( !setExtension( libraryFile._name, error )) // is it necessary? + // return false; + + if ( isURL( libraryFile )) + { + if ( !downloadLib( libraryFile, error )) + { + // try to fix extension + std::string url = libraryFile._name; + if ( !setExtension( libraryFile._name, error )) + return false; + if ( url == libraryFile._name ) + return false; // extension not changed + + if ( !downloadLib( libraryFile, error )) + return false; + } + } + +#if defined( WIN32 ) && defined( UNICODE ) + std::wstring encodePath = Kernel_Utils::utf8_decode_s( libraryFile._name ); + const wchar_t* path = encodePath.c_str(); +#else + const char* path = libraryFile._name.c_str(); +#endif + + theLibraryHandle = LoadLib( path ); + if ( !theLibraryHandle ) + { + if ( ! getLastError( error )) + error = "Can't load library '" + libraryFile._name + "'"; + } + + return theLibraryHandle; + } + +} // anonymous namespace + + +namespace SMESHUtils_MGLicenseKeyGen // API implementation +{ + //================================================================================ + /*! + * \brief Sign a CAD + * \param [in] meshgems_cad - pointer to a MG CAD object (meshgems_cad_t) + * \param [out] error - return error description + * \return bool - is a success + */ + //================================================================================ + + bool SignCAD( void* meshgems_cad, std::string& error ) + { + LibraryFile libraryFile; + if ( !loadLibrary( error, libraryFile )) + return false; + + typedef bool (*SignFun)(void* ); + SignFun signFun = (SignFun) GetProc( theLibraryHandle, "SignCAD" ); + if ( !signFun ) + { + if ( ! getLastError( error )) + error = SMESH_Comment( "Can't find symbol 'SignCAD' in '") << getenv( theEnvVar ) << "'"; + } + + bool ok; + + SMESH_TRY; + + ok = signFun( meshgems_cad ); + + SMESH_CATCH( SMESH::returnError ); + + if ( !error.empty() ) + ok = false; + else if ( !ok ) + error = "SignCAD() failed (located in '" + libraryFile._name + "')"; + + return ok; + } + + //================================================================================ + /*! + * \brief Sign a mesh + * \param [in] meshgems_mesh - pointer to a MG mesh (meshgems_mesh_t) + * \param [out] error - return error description + * \return bool - is a success + */ + //================================================================================ + + bool SignMesh( void* meshgems_mesh, std::string& error ) + { + LibraryFile libraryFile; + if ( !loadLibrary( error, libraryFile )) + return false; + + typedef bool (*SignFun)(void* ); + SignFun signFun = (SignFun) GetProc( theLibraryHandle, "SignMesh" ); + if ( !signFun ) + { + if ( ! getLastError( error )) + error = SMESH_Comment( "Can't find symbol 'SignMesh' in '") << getenv( theEnvVar ) << "'"; + } + bool ok; + + SMESH_TRY; + + ok = signFun( meshgems_mesh ); + + SMESH_CATCH( SMESH::returnError ); + + if ( !error.empty() ) + ok = false; + else if ( !ok ) + error = "SignMesh() failed (located in '" + libraryFile._name + "')"; + + return ok; + } + + //================================================================================ + /*! + * \brief Return a license key to pass as argument to a MG mesher executable + * \param [in] gmfFile - path to an input mesh file + * \param [in] nb* - nb of entities in the input mesh + * \param [out] error - return error description + * \return std::string - the key + */ + //================================================================================ + + std::string GetKey(const std::string& gmfFile, + int nbVertex, + int nbEdge, + int nbFace, + int nbVol, + std::string& error) + { + std::string key; + LibraryFile libraryFile; + if ( !loadLibrary( error, libraryFile )) + return key; + + typedef std::string (*GetKeyFun)(std::string const &, int, int, int, int ); + GetKeyFun keyFun = (GetKeyFun) GetProc( theLibraryHandle, "GetKey" ); + if ( !keyFun ) + { + if ( ! getLastError( error )) + error = SMESH_Comment( "Can't find symbol 'GetKey' in '") << getenv( theEnvVar ) << "'"; + } + key = keyFun( gmfFile, nbVertex, nbEdge, nbFace, nbVol ); + + if ( key.empty() ) + error = "GetKey() failed (located in '" + libraryFile._name + "')"; + + return key; + } + + //================================================================================ + /*! + * \brief Return false if libMeshGemsKeyGenerator.so is not functional + * \param [out] error - return error description + * \return bool - is a success + */ + //================================================================================ + + bool CheckKeyGenLibrary( std::string& error ) + { + return !GetKey("",4,0,2,0,error ).empty(); + } + + //================================================================================ + /*! + * \brief Return KeyGenerator library name + */ + //================================================================================ + + std::string GetLibraryName() + { + std::string libName, error; + if ( const char* libPath = getenv( theEnvVar )) + { + libName = Kernel_Utils::GetBaseName( libPath ); + } + else + { + libName = "libSalomeMeshGemsKeyGenerator"; + } + setExtension( libName, error ); + return libName; + } +} diff --git a/src/SMESHUtils/SMESH_MGLicenseKeyGen.hxx b/src/SMESHUtils/SMESH_MGLicenseKeyGen.hxx new file mode 100644 index 000000000..c81fa3090 --- /dev/null +++ b/src/SMESHUtils/SMESH_MGLicenseKeyGen.hxx @@ -0,0 +1,52 @@ +// Copyright (C) 2007-2021 CEA/DEN, EDF R&D, OPEN CASCADE +// +// 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 +// +// File : SMESH_MGLicenseKeyGen.hxx +// Created : Sat Jul 31 18:41:04 2021 +// Author : Edward AGAPOV (OCC) + +#ifndef __SMESHUtils_MGLicenseKeyGen_HXX__ +#define __SMESHUtils_MGLicenseKeyGen_HXX__ + +#include "SMESH_Utils.hxx" + +#include + +/*! + * \brief Manage loading libSalomeMeshGemsKeyGenerator.[so|dll] and sing MeshGems CAD or mesh + */ + +namespace SMESHUtils_MGLicenseKeyGen +{ + SMESHUtils_EXPORT bool SignCAD( void* meshgems_cad, std::string& error ); + + SMESHUtils_EXPORT bool SignMesh( void* meshgems_mesh, std::string& error ); + + SMESHUtils_EXPORT std::string GetKey(const std::string& gmfFile, + int nbVertex, + int nbEdge, + int nbFace, + int nbVol, + std::string& error); + + SMESHUtils_EXPORT bool CheckKeyGeLibrary( std::string& error ); + + SMESHUtils_EXPORT std::string GetLibraryName(); +} + +#endif From 8c5da1b9d0745c6eeaaec89636380d4580ba7c66 Mon Sep 17 00:00:00 2001 From: eap Date: Wed, 11 Aug 2021 17:35:58 +0300 Subject: [PATCH 02/10] Prevent exception in MgAdapt::MgAdapt() in case if SALOME_TMP_DIR specifies unexciting directory --- src/SMESH/MG_ADAPT.cxx | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/SMESH/MG_ADAPT.cxx b/src/SMESH/MG_ADAPT.cxx index 1e748c534..d490092d1 100644 --- a/src/SMESH/MG_ADAPT.cxx +++ b/src/SMESH/MG_ADAPT.cxx @@ -322,7 +322,7 @@ MgAdaptHypothesisData* MgAdapt::getData() const } void MgAdapt::setMedFileIn(std::string fileName) { - if ( isFileExist(fileName) ) + if ( isFileExist( fileName )) { medFileIn = fileName; @@ -1094,7 +1094,9 @@ std::string MgAdapt::defaultWorkingDirectory() { aTmpDir = Tmp_dir; } - else { + + if ( ! isFileExist( aTmpDir )) + { #ifdef WIN32 aTmpDir = "C:\\"; #else From db3a0bbde458f89a367905853256671b42dcc906 Mon Sep 17 00:00:00 2001 From: eap Date: Thu, 12 Aug 2021 17:26:25 +0300 Subject: [PATCH 03/10] BoostTxtArchive minor changes --- src/SMESHUtils/SMESH_BoostTxtArchive.hxx | 2 +- src/SMESH_I/SMESH_Gen_i.cxx | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/SMESHUtils/SMESH_BoostTxtArchive.hxx b/src/SMESHUtils/SMESH_BoostTxtArchive.hxx index cb5071f69..5806ffba4 100644 --- a/src/SMESHUtils/SMESH_BoostTxtArchive.hxx +++ b/src/SMESHUtils/SMESH_BoostTxtArchive.hxx @@ -39,7 +39,7 @@ namespace SMESHUtils * Try to workaround the issue that loading fails if the archive string * is created by a newer version of boost::archive library. * - * Usage: ObjType obj; BoostTxtArchive( arcString ) >> obj; + * Usage: ObjType obj; SMESHUtils::BoostTxtArchive( arcString ) >> obj; */ class SMESHUtils_EXPORT BoostTxtArchive { diff --git a/src/SMESH_I/SMESH_Gen_i.cxx b/src/SMESH_I/SMESH_Gen_i.cxx index 677e7fb99..b34058a2c 100644 --- a/src/SMESH_I/SMESH_Gen_i.cxx +++ b/src/SMESH_I/SMESH_Gen_i.cxx @@ -145,7 +145,6 @@ #include #include -#include #include #include From 096a1397692403d428fa618a3a09b202ab500393 Mon Sep 17 00:00:00 2001 From: rnv Date: Thu, 19 Aug 2021 15:24:41 +0300 Subject: [PATCH 04/10] Debug under Windows + some minor corrections. --- src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx | 43 ++++++++++++++---------- 1 file changed, 25 insertions(+), 18 deletions(-) diff --git a/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx b/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx index e1d2db8d2..18289801d 100644 --- a/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx +++ b/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx @@ -152,12 +152,16 @@ namespace 0, NULL ); - if ( msgLen > 0 ) - error = (char*) cstr; + if ( msgLen > 0 ) { +#if defined( WIN32 ) && defined( UNICODE ) + error = Kernel_Utils::encode((wchar_t*)cstr); +#else + error = (char*)cstr; +#endif + LocalFree(cstr); + } - LocalFree(cstr); - - return msgLen; + return (bool)msgLen; #endif } @@ -244,11 +248,11 @@ namespace #ifdef WIN32 - std::string outFile = tmpDir + "libMeshGemsKeyGenerator.dll"; + std::string outFile = tmpDir + "MeshGemsKeyGenerator.dll"; // use wget (== Invoke-WebRequest) PowerShell command available since Windows 7 std::string psCmd = "wget -Uri " + url + " -OutFile " + outFile; - std::string cmd = "start powershell.exe " + psCmd; + std::string cmd = "powershell.exe " + psCmd; #else @@ -273,8 +277,10 @@ namespace SMESH_File resultFile( outFile, /*open=*/false ); bool ok = ( resultFile.exists() && resultFile.size() > 0 ); - if ( ok ) + if (ok) libraryFile._name = outFile; + else + error = "Can't download file " + url; return ok; } @@ -357,6 +363,7 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation if ( !loadLibrary( error, libraryFile )) return false; + bool ok = false; typedef bool (*SignFun)(void* ); SignFun signFun = (SignFun) GetProc( theLibraryHandle, "SignCAD" ); if ( !signFun ) @@ -364,20 +371,20 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation if ( ! getLastError( error )) error = SMESH_Comment( "Can't find symbol 'SignCAD' in '") << getenv( theEnvVar ) << "'"; } + else + { - bool ok; + SMESH_TRY; - SMESH_TRY; + ok = signFun( meshgems_cad ); - ok = signFun( meshgems_cad ); - - SMESH_CATCH( SMESH::returnError ); - - if ( !error.empty() ) - ok = false; - else if ( !ok ) - error = "SignCAD() failed (located in '" + libraryFile._name + "')"; + SMESH_CATCH( SMESH::returnError ); + if ( !error.empty() ) + ok = false; + else if ( !ok ) + error = "SignCAD() failed (located in '" + libraryFile._name + "')"; + } return ok; } From 4a8f4b4c1c2545fe6a0bf9cfa771bd38779738e7 Mon Sep 17 00:00:00 2001 From: eap Date: Thu, 19 Aug 2021 16:23:54 +0300 Subject: [PATCH 05/10] memory leaks --- src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx b/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx index 18289801d..a49e74bf8 100644 --- a/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx +++ b/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx @@ -153,11 +153,11 @@ namespace NULL ); if ( msgLen > 0 ) { -#if defined( WIN32 ) && defined( UNICODE ) - error = Kernel_Utils::encode((wchar_t*)cstr); -#else +# if defined( UNICODE ) + error = Kernel_Utils::encode_s((wchar_t*)cstr); +# else error = (char*)cstr; -#endif +# endif LocalFree(cstr); } @@ -277,7 +277,7 @@ namespace SMESH_File resultFile( outFile, /*open=*/false ); bool ok = ( resultFile.exists() && resultFile.size() > 0 ); - if (ok) + if ( ok ) libraryFile._name = outFile; else error = "Can't download file " + url; @@ -365,13 +365,13 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation bool ok = false; typedef bool (*SignFun)(void* ); - SignFun signFun = (SignFun) GetProc( theLibraryHandle, "SignCAD" ); + SignFun signFun = (SignFun) GetProc( theLibraryHandle, "SignCAD" ); if ( !signFun ) { if ( ! getLastError( error )) error = SMESH_Comment( "Can't find symbol 'SignCAD' in '") << getenv( theEnvVar ) << "'"; } - else + else { SMESH_TRY; From 499331b2078ac4207194e8a416c06a5c81cb6501 Mon Sep 17 00:00:00 2001 From: eap Date: Thu, 19 Aug 2021 17:09:28 +0300 Subject: [PATCH 06/10] same fix as in SignCAD() --- src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx | 28 +++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx b/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx index a49e74bf8..fa5b78858 100644 --- a/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx +++ b/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx @@ -373,7 +373,6 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation } else { - SMESH_TRY; ok = signFun( meshgems_cad ); @@ -403,6 +402,7 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation if ( !loadLibrary( error, libraryFile )) return false; + bool ok = false; typedef bool (*SignFun)(void* ); SignFun signFun = (SignFun) GetProc( theLibraryHandle, "SignMesh" ); if ( !signFun ) @@ -410,19 +410,19 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation if ( ! getLastError( error )) error = SMESH_Comment( "Can't find symbol 'SignMesh' in '") << getenv( theEnvVar ) << "'"; } - bool ok; + else + { + SMESH_TRY; - SMESH_TRY; + ok = signFun( meshgems_mesh ); - ok = signFun( meshgems_mesh ); - - SMESH_CATCH( SMESH::returnError ); - - if ( !error.empty() ) - ok = false; - else if ( !ok ) - error = "SignMesh() failed (located in '" + libraryFile._name + "')"; + SMESH_CATCH( SMESH::returnError ); + if ( !error.empty() ) + ok = false; + else if ( !ok ) + error = "SignMesh() failed (located in '" + libraryFile._name + "')"; + } return ok; } @@ -455,8 +455,10 @@ namespace SMESHUtils_MGLicenseKeyGen // API implementation if ( ! getLastError( error )) error = SMESH_Comment( "Can't find symbol 'GetKey' in '") << getenv( theEnvVar ) << "'"; } - key = keyFun( gmfFile, nbVertex, nbEdge, nbFace, nbVol ); - + else + { + key = keyFun( gmfFile, nbVertex, nbEdge, nbFace, nbVol ); + } if ( key.empty() ) error = "GetKey() failed (located in '" + libraryFile._name + "')"; From 1ffd1c1245a946aa45a08b5b7e84e89cb88975fe Mon Sep 17 00:00:00 2001 From: eap Date: Thu, 26 Aug 2021 18:23:22 +0300 Subject: [PATCH 07/10] bos #24596 [CEA] New MeshGems license Care of python plugins --- src/SMESHUtils/SMESH_MGLicenseKeyGen.hxx | 2 +- src/SMESH_SWIG/CMakeLists.txt | 4 ++- src/SMESH_SWIG/SMeshHelper.cxx | 26 +++++++++++++++++++ src/SMESH_SWIG/SMeshHelper.h | 2 ++ src/SMESH_SWIG/SMeshHelper.i | 5 ++++ .../MGCleanerPlug/MGCleanerMonPlugDialog.py | 5 ++++ src/Tools/YamsPlug/monYamsPlugDialog.py | 4 +++ 7 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/SMESHUtils/SMESH_MGLicenseKeyGen.hxx b/src/SMESHUtils/SMESH_MGLicenseKeyGen.hxx index c81fa3090..796cde4b5 100644 --- a/src/SMESHUtils/SMESH_MGLicenseKeyGen.hxx +++ b/src/SMESHUtils/SMESH_MGLicenseKeyGen.hxx @@ -44,7 +44,7 @@ namespace SMESHUtils_MGLicenseKeyGen int nbVol, std::string& error); - SMESHUtils_EXPORT bool CheckKeyGeLibrary( std::string& error ); + SMESHUtils_EXPORT bool CheckKeyGenLibrary( std::string& error ); SMESHUtils_EXPORT std::string GetLibraryName(); } diff --git a/src/SMESH_SWIG/CMakeLists.txt b/src/SMESH_SWIG/CMakeLists.txt index 53b87e321..bd3a578d4 100644 --- a/src/SMESH_SWIG/CMakeLists.txt +++ b/src/SMESH_SWIG/CMakeLists.txt @@ -25,6 +25,8 @@ include_directories( ${PROJECT_SOURCE_DIR}/src/SMDS ${PROJECT_SOURCE_DIR}/src/SMESH ${PROJECT_SOURCE_DIR}/src/SMESH_I + ${PROJECT_SOURCE_DIR}/src/Driver + ${PROJECT_SOURCE_DIR}/src/DriverGMF ${CMAKE_CURRENT_SOURCE_DIR} ${PROJECT_BINARY_DIR}/idl ) @@ -123,7 +125,7 @@ IF(${CMAKE_VERSION} VERSION_LESS "3.8.0") ELSE() SWIG_ADD_LIBRARY(SMeshHelper LANGUAGE python SOURCES ${SMeshHelper_SOURCES}) ENDIF() -SWIG_LINK_LIBRARIES(SMeshHelper ${PYTHON_LIBRARIES} ${PLATFORM_LIBS} SMESHEngine ) +SWIG_LINK_LIBRARIES(SMeshHelper ${PYTHON_LIBRARIES} ${PLATFORM_LIBS} SMESHEngine MeshDriverGMF ) SWIG_CHECK_GENERATION(SMeshHelper) IF(WIN32) SET_TARGET_PROPERTIES(_SMeshHelper PROPERTIES DEBUG_OUTPUT_NAME _SMeshHelper_d) diff --git a/src/SMESH_SWIG/SMeshHelper.cxx b/src/SMESH_SWIG/SMeshHelper.cxx index ee2d54b05..cfa7d7ef8 100644 --- a/src/SMESH_SWIG/SMeshHelper.cxx +++ b/src/SMESH_SWIG/SMeshHelper.cxx @@ -25,9 +25,35 @@ #include +#include +#include + + std::string BuildSMESHInstanceInternal() { Engines::EngineComponent_var zeRef = RetrieveSMESHInstance(); CORBA::String_var ior = KERNEL::getORB()->object_to_string(zeRef); return std::string(ior.in()); } + +std::string GetMGLicenseKeyImpl(const char* gmfFile) +{ + smIdType nbVertex, nbEdge, nbFace, nbVol; + DriverGMF_Read gmfReader; + gmfReader.SetFile( gmfFile ); + gmfReader.GetMeshInfo( nbVertex, nbEdge, nbFace, nbVol ); + + std::string errorTxt; + std::string key = SMESHUtils_MGLicenseKeyGen::GetKey( gmfFile, + FromSmIdType( nbVertex ), + FromSmIdType( nbEdge ), + FromSmIdType( nbFace ), + FromSmIdType( nbVol ), + errorTxt ); + if ( !errorTxt.empty() ) + { + std::cerr << "Error: Pb with MeshGens license: " << errorTxt << std::endl; + key = "<" + errorTxt + ">"; + } + return key; +} diff --git a/src/SMESH_SWIG/SMeshHelper.h b/src/SMESH_SWIG/SMeshHelper.h index 58b2c5321..9c912869e 100644 --- a/src/SMESH_SWIG/SMeshHelper.h +++ b/src/SMESH_SWIG/SMeshHelper.h @@ -22,3 +22,5 @@ #include std::string BuildSMESHInstanceInternal(); + +std::string GetMGLicenseKeyImpl(const char* gmfFile); diff --git a/src/SMESH_SWIG/SMeshHelper.i b/src/SMESH_SWIG/SMeshHelper.i index b73a87db9..d1220cb90 100644 --- a/src/SMESH_SWIG/SMeshHelper.i +++ b/src/SMESH_SWIG/SMeshHelper.i @@ -31,4 +31,9 @@ { return BuildSMESHInstanceInternal(); } + + std::string GetMGLicenseKey(const char* gmfFile) + { + return GetMGLicenseKeyImpl( gmfFile ); + } } diff --git a/src/Tools/MGCleanerPlug/MGCleanerMonPlugDialog.py b/src/Tools/MGCleanerPlug/MGCleanerMonPlugDialog.py index c701b3449..976b9bc7a 100644 --- a/src/Tools/MGCleanerPlug/MGCleanerMonPlugDialog.py +++ b/src/Tools/MGCleanerPlug/MGCleanerMonPlugDialog.py @@ -577,6 +577,11 @@ class MGCleanerMonPlugDialog(Ui_MGCleanerPlugDialog,QWidget): if not self.CB_ComputedOverlapDistance.isChecked(): #computed default self.commande+=" --overlap_distance " + self.SP_toStr(self.SP_OverlapDistance) self.commande+=" --overlap_angle " + str(self.SP_OverlapAngle.value()) + + import SMeshHelper + key = SMeshHelper.GetMGLicenseKey( self.self.fichierIn ) + self.commande+=' --key ' + key + if verbose: print(("INFO: MGCCleaner command:\n %s" % self.commande)) return True diff --git a/src/Tools/YamsPlug/monYamsPlugDialog.py b/src/Tools/YamsPlug/monYamsPlugDialog.py index 63efd6c2d..477a2b4e4 100644 --- a/src/Tools/YamsPlug/monYamsPlugDialog.py +++ b/src/Tools/YamsPlug/monYamsPlugDialog.py @@ -545,6 +545,10 @@ class MonYamsPlugDialog(Ui_YamsPlugDialog,QWidget): self.commande+=' --in "' + self.fichierIn +'"' self.commande+=' --out "' + self.fichierOut +'"' + + import SMeshHelper + key = SMeshHelper.GetMGLicenseKey( self.fichierIn ) + self.commande+=' --key ' + key print(self.commande) return True From 86b5d4b79ba8817cb377552c81e812753e29fcf9 Mon Sep 17 00:00:00 2001 From: eap Date: Fri, 27 Aug 2021 19:19:08 +0300 Subject: [PATCH 08/10] typo --- src/SMESH_SWIG/SMeshHelper.cxx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SMESH_SWIG/SMeshHelper.cxx b/src/SMESH_SWIG/SMeshHelper.cxx index cfa7d7ef8..92991e20c 100644 --- a/src/SMESH_SWIG/SMeshHelper.cxx +++ b/src/SMESH_SWIG/SMeshHelper.cxx @@ -52,7 +52,7 @@ std::string GetMGLicenseKeyImpl(const char* gmfFile) errorTxt ); if ( !errorTxt.empty() ) { - std::cerr << "Error: Pb with MeshGens license: " << errorTxt << std::endl; + std::cerr << "Error: Pb with MeshGems license: " << errorTxt << std::endl; key = "<" + errorTxt + ">"; } return key; From b1e37287d48c9d7eddae4bc4cad09857bde5a47b Mon Sep 17 00:00:00 2001 From: eap Date: Mon, 30 Aug 2021 13:33:06 +0300 Subject: [PATCH 09/10] fix new license in MGCleaner --- src/Tools/MGCleanerPlug/MGCleanerMonPlugDialog.py | 14 +------------- src/Tools/MGCleanerPlug/MGCleanerMonViewText.py | 5 ----- src/Tools/MGCleanerPlug/MGCleanerplug_plugin.py | 9 --------- src/Tools/YamsPlug/monViewText.py | 5 ----- src/Tools/YamsPlug/yamsplug_plugin.py | 9 --------- 5 files changed, 1 insertion(+), 41 deletions(-) diff --git a/src/Tools/MGCleanerPlug/MGCleanerMonPlugDialog.py b/src/Tools/MGCleanerPlug/MGCleanerMonPlugDialog.py index 976b9bc7a..f5c226b87 100644 --- a/src/Tools/MGCleanerPlug/MGCleanerMonPlugDialog.py +++ b/src/Tools/MGCleanerPlug/MGCleanerMonPlugDialog.py @@ -525,18 +525,6 @@ class MGCleanerMonPlugDialog(Ui_MGCleanerPlugDialog,QWidget): def PrepareLigneCommande(self): """ - #use doc examples of mg-cleaner: - ls -al /data/tmplgls/salome/prerequis/install/COMMON_64/MeshGems-1.0/bin - source /data/tmplgls/salome/prerequis/install/LICENSE/dlim8.var.sh - export PATH=/data/tmplgls/salome/prerequis/install/COMMON_64/MeshGems-1.0/bin/Linux_64:$PATH - cp -r /data/tmplgls/salome/prerequis/install/COMMON_64/MeshGems-1.0/examples . - cd examples - mg-cleaner.exe --help - mg-cleaner.exe --in case7.mesh --out case7-test.mesh --check - mg-cleaner.exe case7.mesh case7-fix.mesh --fix - mg-cleaner.exe --in Porsche.mesh --out Porsche-test.mesh --check - mg-cleaner.exe --in Porsche.mesh --out Porschefix.mesh --fix - mg-cleaner.exe --in Porsche.mesh --out PorscheNewfix.mesh --fix --resolution_length 0.03 """ #self.commande="mg-cleaner.exe --in " + self.fichierIn + " --out " + self.fichierOut + " --fix2pass" @@ -579,7 +567,7 @@ class MGCleanerMonPlugDialog(Ui_MGCleanerPlugDialog,QWidget): self.commande+=" --overlap_angle " + str(self.SP_OverlapAngle.value()) import SMeshHelper - key = SMeshHelper.GetMGLicenseKey( self.self.fichierIn ) + key = SMeshHelper.GetMGLicenseKey( self.fichierIn ) self.commande+=' --key ' + key if verbose: print(("INFO: MGCCleaner command:\n %s" % self.commande)) diff --git a/src/Tools/MGCleanerPlug/MGCleanerMonViewText.py b/src/Tools/MGCleanerPlug/MGCleanerMonViewText.py index 5df87ad6a..a117ed2e9 100644 --- a/src/Tools/MGCleanerPlug/MGCleanerMonViewText.py +++ b/src/Tools/MGCleanerPlug/MGCleanerMonViewText.py @@ -35,11 +35,6 @@ from MGCleanerViewText_ui import Ui_ViewExe verbose = True -force = os.getenv("FORCE_DISTENE_LICENSE_FILE") -if force != None: - os.environ["DISTENE_LICENSE_FILE"] = force - os.environ["DLIM8VAR"] = "NOTHING" - class MGCleanerMonViewText(Ui_ViewExe, QDialog): """ Classe permettant la visualisation de texte diff --git a/src/Tools/MGCleanerPlug/MGCleanerplug_plugin.py b/src/Tools/MGCleanerPlug/MGCleanerplug_plugin.py index 1058e658c..7bc19e021 100644 --- a/src/Tools/MGCleanerPlug/MGCleanerplug_plugin.py +++ b/src/Tools/MGCleanerPlug/MGCleanerplug_plugin.py @@ -31,15 +31,6 @@ def MGCleanerLct(context): import tempfile from qtsalome import QFileDialog, QMessageBox - #prior test to avoid unnecessary user GUI work with ending crash - try : - os.environ['DISTENE_LICENSE_FILE'] - except: - QMessageBox.warning(None, "Products", """\ -Distene's product MeshGem Cleaner is not installed. -required environment variable: -DISTENE_LICENSE_FILE='.../dlim8.var.sh'""") - return import MGCleanerMonPlugDialog window = MGCleanerMonPlugDialog.getDialog() window.show() diff --git a/src/Tools/YamsPlug/monViewText.py b/src/Tools/YamsPlug/monViewText.py index eb371b0f1..082c9fa01 100644 --- a/src/Tools/YamsPlug/monViewText.py +++ b/src/Tools/YamsPlug/monViewText.py @@ -34,11 +34,6 @@ from ViewText_ui import Ui_ViewExe verbose = True -force = os.getenv("FORCE_DISTENE_LICENSE_FILE") -if force != None: - os.environ["DISTENE_LICENSE_FILE"] = force - os.environ["DLIM8VAR"] = "NOTHING" - class MonViewText(Ui_ViewExe, QDialog): """ Classe permettant la visualisation de texte diff --git a/src/Tools/YamsPlug/yamsplug_plugin.py b/src/Tools/YamsPlug/yamsplug_plugin.py index 278406503..4a0b005fb 100644 --- a/src/Tools/YamsPlug/yamsplug_plugin.py +++ b/src/Tools/YamsPlug/yamsplug_plugin.py @@ -31,15 +31,6 @@ def YamsLct(context): import tempfile from qtsalome import QFileDialog, QMessageBox - #prior test to avoid unnecessary user GUI work with ending crash - try : - os.environ['DISTENE_LICENSE_FILE'] - except: - QMessageBox.warning(None,"Products","""\ -Distene's product MeshGem SurfOpt is not installed. -required environment variable: -DISTENE_LICENSE_FILE='.../dlim8.var.sh'""") - return import monYamsPlugDialog window = monYamsPlugDialog.getDialog() window.show() From bd19f3d3154d885b365ecad903bb9f6b31cb0a4a Mon Sep 17 00:00:00 2001 From: Anthony Geay Date: Fri, 10 Sep 2021 15:23:36 +0200 Subject: [PATCH 10/10] Allow EDF URL style --- src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx | 33 +++++++++++++++++------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx b/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx index e1d2db8d2..99336c6a3 100644 --- a/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx +++ b/src/SMESHUtils/SMESH_MGLicenseKeyGen.cxx @@ -197,17 +197,32 @@ namespace bool isURL( LibraryFile & libraryFile ) { - enum { SCHEME = 2, AUTHORITY = 4, PATH = 5 }; // sub-strings - std::regex urlRegex ( R"(^(([^:\/?#]+):)?(//([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)", - std::regex::extended ); - std::smatch matchResult; + {// round1 + enum { SCHEME = 2, AUTHORITY = 4, PATH = 5 }; // sub-strings + std::regex urlRegex ( R"(^(([^:\/?#]+):)?(//([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?)", + std::regex::extended ); + std::smatch matchResult; - libraryFile._isURL = false; - if ( std::regex_match( libraryFile._name, matchResult, urlRegex )) - libraryFile._isURL = ( !matchResult.str( SCHEME ).empty() && - !matchResult.str( AUTHORITY ).empty() && - !matchResult.str( PATH ).empty() ); + libraryFile._isURL = false; + if ( std::regex_match( libraryFile._name, matchResult, urlRegex )) + libraryFile._isURL = ( !matchResult.str( SCHEME ).empty() && + !matchResult.str( AUTHORITY ).empty() && + !matchResult.str( PATH ).empty() ); + } + if(libraryFile._isURL) + return true; + {// round2 + enum { HOST = 2, PORT = 3, PATH = 4 }; // sub-strings + std::regex urlRegex ( R"(^(([^:\/?#]+):)?([^/]+)?(/[^#]*))", + std::regex::extended ); + std::smatch matchResult; + libraryFile._isURL = false; + if ( std::regex_match( libraryFile._name, matchResult, urlRegex )) + libraryFile._isURL = ( !matchResult.str( HOST ).empty() && + !matchResult.str( PORT ).empty() && + !matchResult.str( PATH ).empty() ); + } return libraryFile._isURL; }