]> SALOME platform Git repositories - plugins/hexoticplugin.git/blobdiff - src/HexoticPlugin/MG_Hexotic_API.cxx
Salome HOME
Deal with old MG suite
[plugins/hexoticplugin.git] / src / HexoticPlugin / MG_Hexotic_API.cxx
index 1946f4fd0d9dc70ad3becf46998fbfe242d5be26..36f7a7d5973a28dfd7f5dff9a21ae704e96e3487 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
 
 #include "MG_Hexotic_API.hxx"
 
+#ifdef WIN32
+#define NOMINMAX
+#endif
+
+#include <DriverGMF_Read.hxx>
 #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
 
@@ -73,12 +80,12 @@ struct MG_Hexotic_API::LibData
   {
     if ( _hexa_mesh )
       hexa_regain_mesh( _session, _hexa_mesh );
-    if ( _session )
-      hexa_session_delete( _session );
     if ( _tria_mesh )
       mesh_delete( _tria_mesh );
     if ( _sizemap )
       sizemap_delete( _sizemap );
+    if ( _session )
+      hexa_session_delete( _session );
     if ( _context )
       context_delete( _context );
 
@@ -369,7 +376,7 @@ struct MG_Hexotic_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 );
@@ -438,12 +445,12 @@ struct MG_Hexotic_API::LibData
 
   int IsVertexRequired( int iNode )
   {
-    return ! ( iNode < int( _xyz.size() - _nodeSize.size() ));
+    return ! ( iNode < int( NbNodes() - _nodeSize.size() ));
   }
 
   double GetSizeAtVertex( int iNode )
   {
-    return IsVertexRequired( iNode ) ? _nodeSize[ iNode - _xyz.size() + _nodeSize.size() ] : 0.;
+    return IsVertexRequired( iNode ) ? _nodeSize[ iNode - NbNodes() + _nodeSize.size() ] : 0.;
   }
 };
 
@@ -583,6 +590,10 @@ namespace // functions called by MG library to exchange with the application
     MG_Hexotic_API::LibData* data = (MG_Hexotic_API::LibData *) user_data;
     data->AddError( desc );
 
+#ifdef _DEBUG_
+    //std::cout << desc << std::endl;
+#endif
+
     // Compute progress
     // corresponding messages are:
     // " | Analyzing the surface mesh |"         => 1 %
@@ -659,6 +670,14 @@ void MG_Hexotic_API::LibData::Init()
 
 bool MG_Hexotic_API::LibData::Compute()
 {
+  // MG license
+  std::string errorTxt;
+  if ( !SMESHUtils_MGLicenseKeyGen::SignMesh( _tria_mesh, errorTxt ))
+  {
+    AddError( SMESH_Comment( "Problem with library SalomeMeshGemsKeyGenerator: ") << errorTxt );
+    return false;
+  }
+
   // Set surface mesh
   status_t ret = hexa_set_surface_mesh( _session, _tria_mesh );
   if ( ret != STATUS_OK ) MG_Error( "unable to set surface mesh");
@@ -693,6 +712,14 @@ bool MG_Hexotic_API::LibData::Compute()
   return true;
 }
 
+#else // ifdef USE_MG_LIBS
+
+struct MG_Hexotic_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
 
@@ -703,15 +730,17 @@ bool MG_Hexotic_API::LibData::Compute()
  */
 //================================================================================
 
-MG_Hexotic_API::MG_Hexotic_API(volatile bool& cancelled_flag, double& progress)
+MG_Hexotic_API::MG_Hexotic_API(volatile bool& cancelled_flag, double& progress):
+  _isMesh(true), _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();
-#endif
   if ( getenv("MG_HEXA_USE_EXE"))
     _useLib = false;
+#endif
 }
 
 //================================================================================
@@ -786,6 +815,8 @@ bool MG_Hexotic_API::Compute( const std::string& cmdLine, std::string& errStr )
       value = "";
       while ( i+1 < args.size() && args[i+1][0] != '-' )
       {
+        if ( strncmp( "1>", args[i+1].c_str(), 2 ) == 0 )
+          break;
         if ( !value.empty() ) value += " ";
         value += args[++i];
       }
@@ -794,9 +825,29 @@ bool MG_Hexotic_API::Compute( const std::string& cmdLine, std::string& errStr )
     }
 
     // compute
-    return _libData->Compute();
+    bool ok = _libData->Compute();
+
+    GetLog(); // write a log file
+    _logFile = ""; // not to write it again
+
+    return ok;
 #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;
+    }
+    
+    if ( key != "0")
+      const_cast< std::string& >( cmdLine ) += " --key " + key;
+  }
 
   int err = system( cmdLine.c_str() ); // run
 
@@ -804,9 +855,6 @@ bool MG_Hexotic_API::Compute( const std::string& cmdLine, std::string& errStr )
     errStr = SMESH_Comment("system(mg-hexa.exe ...) command failed with error: ")
       << strerror( errno );
 
-  GetLog(); // write a log file
-  _logFile = ""; // not to write it again
-
   return !err;
 
 }
@@ -888,7 +936,7 @@ void MG_Hexotic_API::GmfGotoKwd( int iMesh, GmfKwdCod what )
  */
 //================================================================================
 
-void MG_Hexotic_API::GmfGetLin( int iMesh, GmfKwdCod what, int* nbNodes, int* faceInd, int* ori, int* domain, int dummy )
+void MG_Hexotic_API::GmfGetLin( int iMesh, GmfKwdCod what, int* nbNodes, int* faceInd, int* ori, int* domain, int /*dummy*/ )
 {
   if ( _useLib ) {
 #ifdef USE_MG_LIBS
@@ -906,7 +954,7 @@ void MG_Hexotic_API::GmfGetLin( int iMesh, GmfKwdCod what, int* nbNodes, int* fa
 //================================================================================
 
 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
-                             double* x, double* y, double *z, int* domain )
+                               double* x, double* y, double *z, int* domain )
 {
   if ( _useLib ) {
 #ifdef USE_MG_LIBS
@@ -924,7 +972,7 @@ void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
 //================================================================================
 
 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
-                             float* x, float* y, float *z, int* domain )
+                               float* x, float* y, float *z, int* domain )
 {
   if ( _useLib ) {
 #ifdef USE_MG_LIBS
@@ -980,7 +1028,7 @@ void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what, int* node1, int* node2
 //================================================================================
 
 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
-                             int* node1, int* node2, int* node3, int* domain )
+                               int* node1, int* node2, int* node3, int* domain )
 {
   if ( _useLib ) {
 #ifdef USE_MG_LIBS
@@ -998,7 +1046,7 @@ void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
 //================================================================================
 
 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
-                             int* node1, int* node2, int* node3, int* node4, int* domain )
+                               int* node1, int* node2, int* node3, int* node4, int* domain )
 {
   if ( _useLib ) {
 #ifdef USE_MG_LIBS
@@ -1019,9 +1067,9 @@ void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
 //================================================================================
 
 void MG_Hexotic_API::GmfGetLin(int iMesh, GmfKwdCod what,
-                             int* node1, int* node2, int* node3, int* node4,
-                             int* node5, int* node6, int* node7, int* node8,
-                             int* domain )
+                               int* node1, int* node2, int* node3, int* node4,
+                               int* node5, int* node6, int* node7, int* node8,
+                               int* domain )
 {
   if ( _useLib ) {
 #ifdef USE_MG_LIBS
@@ -1059,6 +1107,16 @@ int  MG_Hexotic_API::GmfOpenMesh(const char* theFile, int rdOrWr, int ver, int d
 
 void MG_Hexotic_API::GmfSetKwd(int iMesh, GmfKwdCod what, int nb )
 {
+  if ( iMesh == 1 && _isMesh )
+  {
+    switch ( what ) {
+    case GmfVertices:  _nbNodes += nb; break;
+    case GmfEdges:     _nbEdges += nb; break;
+    case GmfTriangles: _nbFaces += nb; break;
+    default:;
+    }
+  }
+
   if ( _useLib ) {
 #ifdef USE_MG_LIBS
     switch ( what ) {
@@ -1075,6 +1133,26 @@ void MG_Hexotic_API::GmfSetKwd(int iMesh, GmfKwdCod what, int nb )
   ::GmfSetKwd(iMesh, what, nb );
 }
 
+//================================================================================
+/*!
+ * \brief Set GMF file made by MG-CADSurf to get nb of mesh entities from it
+ */
+//================================================================================
+
+void MG_Hexotic_API::SetInputFile( const std::string mesh2DFile )
+{
+  DriverGMF_Read fileReader;
+  fileReader.SetFile( mesh2DFile );
+
+  smIdType nbVertex, nbEdge, nbFace, nbVol;
+  if ( fileReader.GetMeshInfo(nbVertex, nbEdge, nbFace, nbVol))
+  {
+    _nbNodes += nbVertex;
+    _nbEdges += nbEdge;
+    _nbFaces += nbFace;
+  }
+}
+
 //================================================================================
 /*!
  * \brief Add coordinates of a node
@@ -1238,5 +1316,5 @@ std::string MG_Hexotic_API::GetLog()
 #endif
   }
   SMESH_File file( _logFile );
-  return file.getPos();
+  return file.exists() ? file.getPos() : "";
 }