Salome HOME
updated copyright message
[plugins/netgenplugin.git] / src / NETGENPlugin / NETGENPlugin_NETGEN_3D_Remote.cxx
index dc70cf4af5c66501f8011fb02149fc6b10b1cbd8..ba04e35f8be88caaec6a9af004c34cb5d95ed104 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2021  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
 #include "NETGENPlugin_DriverParam.hxx"
 #include "NETGENPlugin_Hypothesis.hxx"
 
+#include "Utils_SALOME_Exception.hxx"
+
 #include <SMESH_Gen.hxx>
 #include <SMESH_Mesh.hxx>
 #include <SMESH_MesherHelper.hxx>
 #include <SMESH_DriverShape.hxx>
 #include <SMESH_DriverMesh.hxx>
 #include <SMESHDS_Mesh.hxx>
+#include <SMESH_MeshLocker.hxx>
 
 #include <QString>
 #include <QProcess>
 
+#ifdef WIN32
+#include <filesystem>
+namespace fs = std::filesystem;
+#else
 #include <boost/filesystem.hpp>
 namespace fs = boost::filesystem;
+#endif
 
 /*
   Netgen include files
@@ -79,7 +87,7 @@ using namespace nglib;
 
 //=============================================================================
 /*!
- *
+ * Constructor
  */
 //=============================================================================
 
@@ -91,7 +99,7 @@ NETGENPlugin_NETGEN_3D_Remote::NETGENPlugin_NETGEN_3D_Remote(int hypId, SMESH_Ge
 
 //=============================================================================
 /*!
- *
+ * Destructor
  */
 //=============================================================================
 
@@ -99,7 +107,12 @@ NETGENPlugin_NETGEN_3D_Remote::~NETGENPlugin_NETGEN_3D_Remote()
 {
 }
 
-
+/**
+ * @brief Fill the structure netgen_param with the information from the hypothesis
+ *
+ * @param hyp the hypothesis
+ * @param aParams the netgen_param structure
+ */
 void NETGENPlugin_NETGEN_3D_Remote::fillParameters(const NETGENPlugin_Hypothesis* hyp, netgen_params &aParams)
 {
   aParams.maxh               = hyp->GetMaxSize();
@@ -124,21 +137,27 @@ void NETGENPlugin_NETGEN_3D_Remote::fillParameters(const NETGENPlugin_Hypothesis
 #ifdef NETGEN_V6
   // std::string
   aParams.meshsizefilename = hyp->GetMeshSizeFile();
+  aParams.closeedgefac = 2;
+  aParams.nbThreads = hyp->GetNbThreads();
 #else
   // const char*
   aParams.meshsizefilename = hyp->GetMeshSizeFile();
-#endif
-#ifdef NETGEN_V6
-  aParams.closeedgefac = 2;
-#else
   aParams.closeedgefac = 0;
+  aParams.nbThreads = 0;
 #endif
 }
 
-// write in a binary file the orientation for each 2D element of the mesh
+//
+
+/**
+ * @brief write in a binary file the orientation for each surface element of the mesh
+ *
+ * @param aMesh The mesh
+ * @param aShape the shape associated to the mesh
+ * @param output_file name of the binary file
+ */
 void NETGENPlugin_NETGEN_3D_Remote::exportElementOrientation(SMESH_Mesh& aMesh,
                                                       const TopoDS_Shape& aShape,
-                                                      netgen_params& aParams,
                                                       const std::string output_file)
 {
   SMESH_MesherHelper helper(aMesh);
@@ -162,7 +181,7 @@ void NETGENPlugin_NETGEN_3D_Remote::exportElementOrientation(SMESH_Mesh& aMesh,
     if ( !aSubMeshDSFace ) continue;
 
     SMDS_ElemIteratorPtr iteratorElem = aSubMeshDSFace->GetElements();
-    if ( aParams._quadraticMesh &&
+    if ( _quadraticMesh &&
           dynamic_cast< const SMESH_ProxyMesh::SubMesh*>( aSubMeshDSFace ))
     {
       // add medium nodes of proxy triangles to helper (#16843)
@@ -195,20 +214,37 @@ void NETGENPlugin_NETGEN_3D_Remote::exportElementOrientation(SMESH_Mesh& aMesh,
   }
 }
 
-
+/**
+ * @brief Compute mesh associate to shape
+ *
+ * @param aMesh The mesh
+ * @param aShape The shape
+ * @return true fi there are some error
+ */
 bool NETGENPlugin_NETGEN_3D_Remote::Compute(SMESH_Mesh&         aMesh,
                                            const TopoDS_Shape& aShape)
 {
-  aMesh.Lock();
-  SMESH_Hypothesis::Hypothesis_Status hypStatus;
-  NETGENPlugin_NETGEN_3D::CheckHypothesis(aMesh, aShape, hypStatus);
+  {
+    SMESH_MeshLocker myLocker(&aMesh);
+    SMESH_Hypothesis::Hypothesis_Status hypStatus;
+    NETGENPlugin_NETGEN_3D::CheckHypothesis(aMesh, aShape, hypStatus);
+  }
 
 
   // Temporary folder for run
-  fs::path tmp_folder = aMesh.tmp_folder / fs::unique_path(fs::path("Volume-%%%%-%%%%"));
+#ifdef WIN32
+  // On windows mesh does not have GetTmpFolder
+  fs::path tmp_folder = fs::path("Volume-%%%%-%%%%");
+#else
+  fs::path tmp_folder = aMesh.GetTmpFolder() / fs::unique_path(fs::path("Volume-%%%%-%%%%"));
+#endif
   fs::create_directories(tmp_folder);
   // Using MESH2D generated after all triangles where created.
-  fs::path mesh_file=aMesh.tmp_folder / fs::path("Mesh2D.med");
+#ifdef WIN32
+  fs::path mesh_file=fs::path("Mesh2D.med");
+#else
+  fs::path mesh_file=aMesh.GetTmpFolder() / fs::path("Mesh2D.med");
+#endif
   fs::path element_orientation_file=tmp_folder / fs::path("element_orientation.dat");
   fs::path new_element_file=tmp_folder / fs::path("new_elements.dat");
   fs::path tmp_mesh_file=tmp_folder / fs::path("tmp_mesh.med");
@@ -218,37 +254,40 @@ bool NETGENPlugin_NETGEN_3D_Remote::Compute(SMESH_Mesh&         aMesh,
   fs::path param_file=tmp_folder / fs::path("netgen3d_param.txt");
   fs::path log_file=tmp_folder / fs::path("run.log");
   fs::path cmd_file=tmp_folder / fs::path("cmd.log");
-  //TODO: Handle variable mesh_name
   std::string mesh_name = "MESH";
 
-  //Writing Shape
-  exportShape(shape_file.string(), aShape);
+  {
+    SMESH_MeshLocker myLocker(&aMesh);
+    //Writing Shape
+    SMESH_DriverShape::exportShape(shape_file.string(), aShape);
 
-  //Writing hypo
-  netgen_params aParams;
-  fillParameters(_hypParameters, aParams);
+    //Writing hypo
+    netgen_params aParams;
+    fillParameters(_hypParameters, aParams);
 
-  exportNetgenParams(param_file.string(), aParams);
+    exportNetgenParams(param_file.string(), aParams);
 
-  // Exporting element orientation
-  exportElementOrientation(aMesh, aShape, aParams, element_orientation_file.string());
+    // Exporting element orientation
+    exportElementOrientation(aMesh, aShape, element_orientation_file.string());
+  }
 
-  aMesh.Unlock();
   // Calling run_mesher
-  // TODO: check if we need to handle the .exe for windows
   std::string cmd;
   fs::path run_mesher_exe =
     fs::path(std::getenv("NETGENPLUGIN_ROOT_DIR"))/
     fs::path("bin")/
     fs::path("salome")/
+#ifdef WIN32
+    fs::path("NETGENPlugin_Runner.exe");
+#else
     fs::path("NETGENPlugin_Runner");
+#endif
 
   cmd = run_mesher_exe.string() +
                   " NETGEN3D " + mesh_file.string() + " "
                                + shape_file.string() + " "
                                + param_file.string() + " "
                                + element_orientation_file.string() + " "
-                               + std::to_string(aMesh.GetMesherNbThreads()) + " "
                                + new_element_file.string() + " "
                                + "NONE";
   // Writing command in log
@@ -262,34 +301,35 @@ bool NETGENPlugin_NETGEN_3D_Remote::Compute(SMESH_Mesh&         aMesh,
 
 
   // Building arguments for QProcess
-  QString program = run_mesher_exe.c_str();
+  QString program = run_mesher_exe.string().c_str();
   QStringList arguments;
   arguments << "NETGEN3D";
-  arguments << mesh_file.c_str();
-  arguments << shape_file.c_str();
-  arguments << param_file.c_str();
-  arguments << element_orientation_file.c_str();
-  arguments << std::to_string(aMesh.GetMesherNbThreads()).c_str();
-  arguments << new_element_file.c_str();
+  arguments << mesh_file.string().c_str();
+  arguments << shape_file.string().c_str();
+  arguments << param_file.string().c_str();
+  arguments << element_orientation_file.string().c_str();
+  arguments << new_element_file.string().c_str();
   arguments << "NONE";
-  QString out_file = log_file.c_str();
+  QString out_file = log_file.string().c_str();
   QProcess myProcess;
   myProcess.setStandardOutputFile(out_file);
 
   myProcess.start(program, arguments);
-  myProcess.waitForFinished();
+  // Waiting for process to finish (argument -1 make it wait until the end of
+  // the process otherwise it just waits 30 seconds)
+  myProcess.waitForFinished(-1);
   int ret = myProcess.exitStatus();
 
   if(ret != 0){
     // Run crahed
-    std::cerr << "Issue with command: " << std::endl;
-    std::cerr << "See log for more details: " << log_file.string() << std::endl;
-    std::cerr << cmd << std::endl;
-    return false;
+    std::string msg = "Issue with command: \n";
+    msg += "See log for more details: " + log_file.string() + "\n";
+    msg += cmd + "\n";
+    throw SALOME_Exception(msg);
   }
 
-  aMesh.Lock();
   {
+    SMESH_MeshLocker myLocker(&aMesh);
     std::ifstream df(new_element_file.string(), ios::binary);
 
     int Netgen_NbOfNodes;
@@ -344,7 +384,16 @@ bool NETGENPlugin_NETGEN_3D_Remote::Compute(SMESH_Mesh&         aMesh,
     }
   }
 
-  aMesh.Unlock();
-
   return true;
-}
\ No newline at end of file
+}
+
+/**
+ * @brief Assign submeshes to compute
+ *
+ * @param aSubMesh submesh to add
+ */
+void NETGENPlugin_NETGEN_3D_Remote::setSubMeshesToCompute(SMESH_subMesh * aSubMesh)
+{
+  SMESH_MeshLocker myLocker(aSubMesh->GetFather());
+  SMESH_Algo::setSubMeshesToCompute(aSubMesh);
+}