Salome HOME
#24596 [CEA] New MeshGems license:: regressions in tests
[plugins/ghs3dplugin.git] / src / GHS3DPlugin / MG_Tetra_API.cxx
index 5709912dbd9aefb7a95379d2b58da497525dbaa9..93eb485448b783e8d7be47558c0134f1fdd74ee9 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2004-2016  CEA/DEN, EDF R&D
+// Copyright (C) 2004-2021  CEA/DEN, EDF R&D
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 #ifdef WIN32
 #define NOMINMAX
 #endif
+
 #include <SMESH_Comment.hxx>
 #include <SMESH_File.hxx>
+#include <SMESH_MGLicenseKeyGen.hxx>
 #include <Utils_SALOME_Exception.hxx>
 
-#include <vector>
-#include <iterator>
 #include <cstring>
+#include <iostream>
+#include <iterator>
+#include <vector>
 
 #ifdef USE_MG_LIBS
 
@@ -351,7 +354,7 @@ struct MG_Tetra_API::LibData
     _nbRequiredTria = nb;
   }
 
-  void AddNode( double x, double y, double z, int domain )
+  void AddNode( double x, double y, double z, int /*domain*/ )
   {
     _xyz.push_back( x );
     _xyz.push_back( y );
@@ -363,20 +366,20 @@ struct MG_Tetra_API::LibData
     _nodeSize.push_back( size );
   }
   
-  void AddEdgeNodes( int node1, int node2, int domain )
+  void AddEdgeNodes( int node1, int node2, int /*domain*/ )
   {
     _edgeNodes.push_back( node1 );
     _edgeNodes.push_back( node2 );
   }
   
-  void AddTriaNodes( int node1, int node2, int node3, int domain )
+  void AddTriaNodes( int node1, int node2, int node3, int /*domain*/ )
   {
     _triaNodes.push_back( node1 );
     _triaNodes.push_back( node2 );
     _triaNodes.push_back( node3 );
   }
 
-  void AddTetraNodes( int node1, int node2, int node3, int node4, int domain )
+  void AddTetraNodes( int node1, int node2, int node3, int node4, int /*domain*/ )
   {
     _tetraNodes.push_back( node1 );
     _tetraNodes.push_back( node2 );
@@ -563,10 +566,10 @@ namespace // functions called by MG library to exchange with the application
       // progress message (10%): "MGMESSAGE  1009001  0 1 1.000000e+01"
       data->_progress = atof( desc + 24 );
 
-      _progressInCallBack = true;
+      data->_progressInCallBack = true;
     }
 
-    if ( !_progressInCallBack )
+    if ( !data->_progressInCallBack )
     {
       // Compute progress
       // corresponding messages are:
@@ -645,15 +648,26 @@ void MG_Tetra_API::LibData::Init()
 bool MG_Tetra_API::LibData::Compute()
 {
   status_t ret;
+  std::string errorTxt;
 
   if ( _tetraNodes.empty() )
   {
+    if ( !SMESHUtils_MGLicenseKeyGen::SignMesh( _tria_mesh, errorTxt ))
+    {
+      AddError( SMESH_Comment( "Problem with library SalomeMeshGemsKeyGenerator: ") << errorTxt );
+      return false;
+    }
     // Set surface mesh
     ret = tetra_set_surface_mesh( _session, _tria_mesh );
     if ( ret != STATUS_OK ) MG_Error( "unable to set surface mesh");
   }
   else
   {
+    if ( !SMESHUtils_MGLicenseKeyGen::SignMesh( _tria_mesh, errorTxt ))
+    {
+      AddError( SMESH_Comment( "Problem with library SalomeMeshGemsKeyGenerator: ") << errorTxt );
+      return false;
+    }
     ret = tetra_set_volume_mesh( _session, _tria_mesh );
     if ( ret != STATUS_OK ) MG_Error( "unable to set volume mesh");
   }
@@ -689,6 +703,16 @@ bool MG_Tetra_API::LibData::Compute()
   return true;
 }
 
+#else // ifdef USE_MG_LIBS
+
+struct MG_Tetra_API::LibData // to avoid compiler warnings
+{
+  volatile bool& _cancelled_flag;
+  double& _progress;
+  LibData(volatile bool& cancelled_flag, double& progress):
+    _cancelled_flag{cancelled_flag}, _progress{progress}
+  {}
+};
 
 #endif // ifdef USE_MG_LIBS
 
@@ -699,12 +723,13 @@ bool MG_Tetra_API::LibData::Compute()
  */
 //================================================================================
 
-MG_Tetra_API::MG_Tetra_API(volatile bool& cancelled_flag, double& progress)
+MG_Tetra_API::MG_Tetra_API(volatile bool& cancelled_flag, double& progress):
+  _nbNodes(0), _nbEdges(0), _nbFaces(0), _nbVolumes(0)
 {
   _useLib = false;
+  _libData = new LibData( cancelled_flag, progress );
 #ifdef USE_MG_LIBS
   _useLib = true;
-  _libData = new LibData( cancelled_flag, progress );
   _libData->Init();
   if ( getenv("MG_TETRA_USE_EXE"))
     _useLib = false;
@@ -793,7 +818,7 @@ bool MG_Tetra_API::Compute( const std::string& cmdLine, std::string& errStr )
     }
 
     // compute
-    bool ok =  _libData->Compute();
+    bool ok = _libData->Compute();
 
     GetLog(); // write a log file
     _logFile = ""; // not to write it again
@@ -802,6 +827,21 @@ bool MG_Tetra_API::Compute( const std::string& cmdLine, std::string& errStr )
 #endif
   }
 
+  // add MG license key
+  {
+    std::string errorTxt, meshIn;
+    std::string key = SMESHUtils_MGLicenseKeyGen::GetKey( meshIn,
+                                                          _nbNodes, _nbEdges, _nbFaces, _nbVolumes,
+                                                          errorTxt );
+    if ( key.empty() )
+    {
+      errStr = "Problem with library SalomeMeshGemsKeyGenerator: " + errorTxt;
+      return false;
+    }
+
+    const_cast< std::string& >( cmdLine ) += " --key " + key;
+  }
+
   int err = system( cmdLine.c_str() ); // run
 
   if ( err )
@@ -888,7 +928,7 @@ void MG_Tetra_API::GmfGotoKwd( int iMesh, GmfKwdCod what )
  */
 //================================================================================
 
-void MG_Tetra_API::GmfGetLin( int iMesh, GmfKwdCod what, int* nbNodes, int* faceInd, int* ori, int* domain, int dummy )
+void MG_Tetra_API::GmfGetLin( int iMesh, GmfKwdCod what, int* nbNodes, int* faceInd, int* ori, int* domain, int /*dummy*/ )
 {
   if ( _useLib ) {
 #ifdef USE_MG_LIBS
@@ -1059,6 +1099,17 @@ int  MG_Tetra_API::GmfOpenMesh(const char* theFile, int rdOrWr, int ver, int dim
 
 void MG_Tetra_API::GmfSetKwd(int iMesh, GmfKwdCod what, int nb )
 {
+  //if ( iMesh == 1 )
+  {
+    switch ( what ) {
+    case GmfVertices:   _nbNodes   += nb; break;
+    case GmfEdges:      _nbEdges   += nb; break;
+    case GmfTriangles:  _nbFaces   += nb; break;
+    case GmfTetrahedra: _nbVolumes += nb; break;
+    default:;
+    }
+  }
+
   if ( _useLib ) {
 #ifdef USE_MG_LIBS
     switch ( what ) {