Salome HOME
Copyright update 2020
[plugins/hexoticplugin.git] / src / HexoticPlugin / HexoticPlugin_Hexotic.cxx
index f56c997a5ba700943c2109957ff6b580215bd876..2449920bcb7f71edaf54a8a19324a8090bc81494 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D
+// Copyright (C) 2007-2020  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
@@ -47,6 +47,7 @@
 #include <SMESH_HypoFilter.hxx>
 #include <SMESH_MesherHelper.hxx>
 #include <SMESH_subMesh.hxx>
+#include <SMESH_MeshEditor.hxx>
 #include <SMESH_ControlPnt.hxx>
 
 #include <list>
@@ -108,13 +109,6 @@ HexoticPlugin_Hexotic::HexoticPlugin_Hexotic(int hypId, SMESH_Gen* gen)
 #endif
   _computeCanceled = false;
   
-  // Copy of what is done in BLSURFPLugin TODO : share the code
-  smeshGen_i = SMESH_Gen_i::GetSMESHGen();
-  CORBA::Object_var anObject = smeshGen_i->GetNS()->Resolve("/Study");
-  
-  myStudy = SALOMEDS::Study::_narrow(anObject);;
-  if ( !myStudy->_is_nil() )
-    MESSAGE("myStudy not empty");
 }
 
 //=============================================================================
@@ -133,7 +127,7 @@ HexoticPlugin_Hexotic::~HexoticPlugin_Hexotic()
 bool HexoticPlugin_Hexotic::CheckBLSURFHypothesis( SMESH_Mesh&         aMesh,
                                                    const TopoDS_Shape& aShape )
 {
-  // MESSAGE("HexoticPlugin_Hexotic::CheckBLSURFHypothesis");
+  MESSAGE("HexoticPlugin_Hexotic::CheckBLSURFHypothesis");
   _blsurfHypo = NULL;
 
   std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
@@ -141,7 +135,8 @@ bool HexoticPlugin_Hexotic::CheckBLSURFHypothesis( SMESH_Mesh&         aMesh,
 
   // If a BLSURF hypothesis is applied, get it
   SMESH_HypoFilter blsurfFilter;
-  blsurfFilter.Init( blsurfFilter.HasName( BLSURFPlugin_Hypothesis::GetHypType() ));
+  blsurfFilter.Init( blsurfFilter.HasName( BLSURFPlugin_Hypothesis::GetHypType(true) ));
+  blsurfFilter.Or  ( blsurfFilter.HasName( BLSURFPlugin_Hypothesis::GetHypType(false)));
   std::list<const SMESHDS_Hypothesis *> appliedHyps;
   aMesh.GetHypotheses( aShape, blsurfFilter, appliedHyps, false );
 
@@ -149,7 +144,9 @@ bool HexoticPlugin_Hexotic::CheckBLSURFHypothesis( SMESH_Mesh&         aMesh,
     itl = appliedHyps.begin();
     theHyp = (*itl); // use only the first hypothesis
     std::string hypName = theHyp->GetName();
-    if (hypName == BLSURFPlugin_Hypothesis::GetHypType()) {
+    if (hypName == BLSURFPlugin_Hypothesis::GetHypType(true) ||
+        hypName == BLSURFPlugin_Hypothesis::GetHypType(false) )
+    {
       _blsurfHypo = static_cast<const BLSURFPlugin_Hypothesis*> (theHyp);
       ASSERT(_blsurfHypo);
       return true;
@@ -169,7 +166,7 @@ bool HexoticPlugin_Hexotic::CheckHypothesis( SMESH_Mesh&
                                              const TopoDS_Shape&                  aShape,
                                              SMESH_Hypothesis::Hypothesis_Status& aStatus )
 {
-  // MESSAGE("HexoticPlugin_Hexotic::CheckHypothesis");
+  MESSAGE("HexoticPlugin_Hexotic::CheckHypothesis");
   _hypothesis = NULL;
 
   std::list<const SMESHDS_Hypothesis*>::const_iterator itl;
@@ -178,6 +175,10 @@ bool HexoticPlugin_Hexotic::CheckHypothesis( SMESH_Mesh&
   const std::list<const SMESHDS_Hypothesis*>& hyps = GetUsedHypothesis(aMesh, aShape, false);
   int nbHyp = hyps.size();
   if (!nbHyp) {
+    // retrieve BLSURF hypothesis if no hexotic hypothesis has been set
+#ifdef WITH_BLSURFPLUGIN
+    CheckBLSURFHypothesis(aMesh, aShape);
+#endif
     aStatus = SMESH_Hypothesis::HYP_OK;
     return true;  // can work with no hypothesis
   }
@@ -654,6 +655,7 @@ void HexoticPlugin_Hexotic::SetParameters(const HexoticPlugin_Hypothesis* hyp) {
     _hexesMaxLevel = hyp->GetHexesMaxLevel();
     _hexesMinSize = hyp->GetMinSize();
     _hexesMaxSize = hyp->GetMaxSize();
+    _approxAngle = hyp->GetGeomApproxAngle();
     _hexoticIgnoreRidges = hyp->GetHexoticIgnoreRidges();
     _hexoticInvalidElements = hyp->GetHexoticInvalidElements();
     _hexoticSharpAngleThreshold = hyp->GetHexoticSharpAngleThreshold();
@@ -670,6 +672,9 @@ void HexoticPlugin_Hexotic::SetParameters(const HexoticPlugin_Hypothesis* hyp) {
     _growth = hyp->GetGrowth();
     _facesWithLayers = hyp->GetFacesWithLayers();
     _imprintedFaces = hyp->GetImprintedFaces();
+    _keepFiles = hyp->GetKeepFiles();
+    _removeLogOnSuccess = hyp->GetRemoveLogOnSuccess();
+    _logInStandardOutput = hyp->GetStandardOutputLog();
   }
   else {
     cout << std::endl;
@@ -679,6 +684,7 @@ void HexoticPlugin_Hexotic::SetParameters(const HexoticPlugin_Hypothesis* hyp) {
     _hexesMaxLevel = hyp->GetDefaultHexesMaxLevel();
     _hexesMinSize = hyp->GetDefaultMinSize();
     _hexesMaxSize = hyp->GetDefaultMaxSize();
+    _approxAngle = hyp->GetDefaultGeomApproxAngle();
     _hexoticIgnoreRidges = hyp->GetDefaultHexoticIgnoreRidges();
     _hexoticInvalidElements = hyp->GetDefaultHexoticInvalidElements();
     _hexoticSharpAngleThreshold = hyp->GetDefaultHexoticSharpAngleThreshold();
@@ -695,6 +701,9 @@ void HexoticPlugin_Hexotic::SetParameters(const HexoticPlugin_Hypothesis* hyp) {
     _growth = hyp->GetDefaultGrowth();
     _facesWithLayers = hyp->GetDefaultFacesWithLayers();
     _imprintedFaces = hyp->GetDefaultImprintedFaces();
+    _keepFiles = hyp->GetDefaultKeepFiles();
+    _removeLogOnSuccess = hyp->GetDefaultRemoveLogOnSuccess();
+    _logInStandardOutput = hyp->GetDefaultStandardOutputLog();
   }
 }
 
@@ -900,18 +909,17 @@ std::string HexoticPlugin_Hexotic::getHexoticCommand(const TCollection_AsciiStri
 TopoDS_Shape HexoticPlugin_Hexotic::entryToShape(std::string entry)
 {
   MESSAGE("HexoticPlugin_Hexotic::entryToShape "<<entry );
-  if ( myStudy->_is_nil() )
-    throw SALOME_Exception("MG-Hexa plugin can't work w/o publishing in the study");
+
   GEOM::GEOM_Object_var aGeomObj;
   TopoDS_Shape S = TopoDS_Shape();
-  SALOMEDS::SObject_var aSObj = myStudy->FindObjectID( entry.c_str() );
+  SALOMEDS::SObject_var aSObj = SMESH_Gen_i::getStudyServant()->FindObjectID( entry.c_str() );
   if (!aSObj->_is_nil()) {
     CORBA::Object_var obj = aSObj->GetObject();
     aGeomObj = GEOM::GEOM_Object::_narrow(obj);
     aSObj->UnRegister();
   }
   if ( !aGeomObj->_is_nil() )
-    S = smeshGen_i->GeomObjectToShape( aGeomObj.in() );
+    S = SMESH_Gen_i::GetSMESHGen()->GeomObjectToShape( aGeomObj.in() );
   return S;
 }
 
@@ -1042,11 +1050,12 @@ bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh&          aMesh,
     Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
 #ifdef WITH_BLSURFPLUGIN
     bool defaultInputFile = true;
-    if (_blsurfHypo && !_blsurfHypo->GetQuadAllowed()) {
+    if (_blsurfHypo && _blsurfHypo->GetElementType() == BLSURFPlugin_Hypothesis::Triangles ) {
       Hexotic_In = _blsurfHypo->GetGMFFile().c_str();
       if ( !Hexotic_In.IsEmpty() &&
            SMESH_File( _blsurfHypo->GetGMFFile() ).exists() )
       {
+        MESSAGE("Use output file from blsurf as input file from hexotic: " << Hexotic_In);
         mgHexa.SetUseExecutable();
         defaultInputFile = false;
       }
@@ -1101,7 +1110,7 @@ bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh&          aMesh,
     // read a result
     // --------------
 
-    if ( mgHexa.IsExecutable() )
+    if ( mgHexa.IsExecutable() && SMESH_File( Hexotic_Out.ToCString() ).exists() )
     {
 #ifndef WIN32
       modeFile_Out += Hexotic_Out;
@@ -1115,35 +1124,45 @@ bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh&          aMesh,
                      &aHelper, _nbShape, tabShape, tabBox );
 
     std::string log = mgHexa.GetLog();
+    hexahedraMessage = "failed";
     if ( Ok )
     {
       hexahedraMessage = "success";
-#ifndef _DEBUG_
-      removeFile(Hexotic_Out);
-      removeFile(Hexotic_In);
-      //removeFile(aLogFileName);
-      for( size_t i=0; i<sizeMapFiles.size(); i++)
-      {
-        removeFile( TCollection_AsciiString( sizeMapFiles[i].c_str() ) );
-      }
-#endif
+      if ( _removeLogOnSuccess )
+        removeFile( aLogFileName );
     }
-    else
+    else if ( !log.empty() )
     {
-      hexahedraMessage = "failed";
-      if ( mgHexa.IsExecutable() )
-        cout << "Problem with MG-Hexa output file " << Hexotic_Out.ToCString() << std::endl;
-      // analyse log file
-      if ( !log.empty() )
+      if( _computeCanceled )
+        error( "interruption initiated by user" );
+      else
       {
+        // get problem description from the log file
         char msgLic[] = " Dlim ";
-        std::string log = mgHexa.GetLog();
         const char* fileBeg = &log[0], *fileEnd = fileBeg + log.size();
         if ( std::search( fileBeg, fileEnd, msgLic, msgLic+strlen(msgLic)) != fileEnd )
           error("Licence problems.");
       }
-      if ( !errStr.empty() )
-        error(errStr);
+    }
+    else if ( !errStr.empty() )
+    {
+      // the log file is empty
+      removeFile( aLogFileName );
+      INFOS( "MG-Hexa Error, " << errStr);
+      error(COMPERR_ALGO_FAILED, errStr);
+    }
+
+    if ( !Ok && mgHexa.IsExecutable() )
+      cout << "Problem with MG-Hexa output file " << Hexotic_Out.ToCString() << std::endl;
+
+    if ( !_keepFiles )
+    {
+      if (! Ok && _computeCanceled )
+        removeFile( aLogFileName );
+      removeFile(Hexotic_Out);
+      removeFile(Hexotic_In);
+      for ( size_t i = 0; i < sizeMapFiles.size(); i++ )
+        removeFile( sizeMapFiles[i].c_str() );
     }
     cout << "Hexahedra meshing " << hexahedraMessage << std::endl;
     cout << std::endl;
@@ -1156,9 +1175,6 @@ bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh&          aMesh,
     _iShape  = 0;
   }
 
-  if(_computeCanceled)
-    return error(SMESH_Comment("interruption initiated by user"));
-
   return Ok;
 }
 
@@ -1176,14 +1192,14 @@ bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh&          aMesh,
 bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHelper)
 {
   _computeCanceled = false;
-/*
-  SMESH_ComputeErrorPtr myError = SMESH_ComputeError::New();
-*/
+  /*
+    SMESH_ComputeErrorPtr myError = SMESH_ComputeError::New();
+  */
   bool Ok = true;
   TCollection_AsciiString hexahedraMessage;
   TCollection_AsciiString aQuote("");
 #ifdef WIN32
-    aQuote = "\"";
+  aQuote = "\"";
 #endif
   SetParameters(_hypothesis);
 
@@ -1198,7 +1214,7 @@ bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHel
   Hexotic_In  = aTmpDir + "Hexotic"+getSuffix()+"_In.mesh";
   Hexotic_Out = aTmpDir + "Hexotic"+getSuffix()+"_Out.mesh";
   Hexotic_SizeMap_Prefix = aTmpDir + "Hexotic_SizeMap";
+
   MG_Hexotic_API mgHexa( _computeCanceled, _progress );
 
   std::vector<std::string> sizeMapFiles = writeSizeMapFile( &mgHexa, Hexotic_SizeMap_Prefix.ToCString() );
@@ -1237,7 +1253,7 @@ bool HexoticPlugin_Hexotic::Compute(SMESH_Mesh & aMesh, SMESH_MesherHelper* aHel
   // read a result
   // --------------
 
-  if ( mgHexa.IsExecutable() )
+  if ( mgHexa.IsExecutable() && SMESH_File( Hexotic_Out.ToCString() ).exists() )
   {
     modeFile_Out += Hexotic_Out;
     system( modeFile_Out.ToCString() );