Salome HOME
Handling error in PyRun + better import of MED file yan/dual_mesh_mc
authorYoann Audouin <yoann.audouin@edf.fr>
Thu, 13 Oct 2022 12:03:31 +0000 (14:03 +0200)
committerYoann Audouin <yoann.audouin@edf.fr>
Thu, 13 Oct 2022 12:33:43 +0000 (14:33 +0200)
src/SMESH/SMESH_Mesh.cxx
src/SMESH_I/SMESH_Gen_i.cxx
src/SMESH_SWIG/smesh_tools.py
test/SMESH_create_dual_mesh_adapt.py

index 8d0d9392b73c54719ecd75664413afb09946e74f..5ff4c1830298e7bea5ee208a347ab3082f1bd83e 100644 (file)
@@ -76,7 +76,7 @@
 #ifndef WIN32
 #include <boost/thread/thread.hpp>
 #include <boost/bind.hpp>
-#else 
+#else
 #include <pthread.h>
 #endif
 
@@ -99,7 +99,7 @@ class SMESH_Mesh::SubMeshHolder : public SMESHDS_TSubMeshHolder< SMESH_subMesh >
 
 //=============================================================================
 /*!
- * 
+ *
  */
 //=============================================================================
 
@@ -629,7 +629,7 @@ SMESH_ComputeErrorPtr SMESH_Mesh::GMFToMesh(const char* theFileName,
 
 //=============================================================================
 /*!
- * 
+ *
  */
 //=============================================================================
 
@@ -724,7 +724,7 @@ SMESH_Mesh::AddHypothesis(const TopoDS_Shape & aSubShape,
 
 //=============================================================================
 /*!
- * 
+ *
  */
 //=============================================================================
 
@@ -741,7 +741,7 @@ SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
   SMESH_Hypothesis *anHyp = sc->mapHypothesis[anHypId];
   if(MYDEBUG) { SCRUTE(anHyp->GetType()); }
 
-  // shape 
+  // shape
 
   bool                     isAlgo = ( !anHyp->GetType() == SMESHDS_Hypothesis::PARAM_ALGO );
   SMESH_subMesh::algo_event event = isAlgo ? SMESH_subMesh::REMOVE_ALGO : SMESH_subMesh::REMOVE_HYP;
@@ -794,7 +794,7 @@ SMESH_Mesh::RemoveHypothesis(const TopoDS_Shape & aSubShape,
 
 //=============================================================================
 /*!
- * 
+ *
  */
 //=============================================================================
 
@@ -861,7 +861,7 @@ const SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const SMESH_subMesh *   aSubM
       const_cast< std::vector< SMESH_subMesh * > & > ( aSubMesh->GetAncestors() );
     SortByMeshOrder( ancestors );
 
-    std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin(); 
+    std::vector<SMESH_subMesh*>::const_iterator smIt = ancestors.begin();
     for ( ; smIt != ancestors.end(); smIt++ )
     {
       const TopoDS_Shape& curSh = (*smIt)->GetSubShape();
@@ -1004,7 +1004,7 @@ SMESH_Hypothesis * SMESH_Mesh::GetHypothesis(const int anHypId) const
 
 //=============================================================================
 /*!
- * 
+ *
  */
 //=============================================================================
 
@@ -1015,7 +1015,7 @@ const std::list<SMESHDS_Command*> & SMESH_Mesh::GetLog()
 
 //=============================================================================
 /*!
- * 
+ *
  */
 //=============================================================================
 void SMESH_Mesh::ClearLog()
@@ -1727,7 +1727,7 @@ double SMESH_Mesh::GetComputeProgress() const
   const SMESH_subMesh* curSM = _gen->GetCurrentSubMesh();
 
   // get progress of a current algo
-  TColStd_MapOfInteger currentSubIds; 
+  TColStd_MapOfInteger currentSubIds;
   if ( curSM )
     if ( SMESH_Algo* algo = curSM->GetAlgo() )
     {
@@ -2035,7 +2035,7 @@ bool SMESH_Mesh::IsNotConformAllowed() const
 
 //=======================================================================
 //function : IsMainShape
-//purpose  : 
+//purpose  :
 //=======================================================================
 
 bool SMESH_Mesh::IsMainShape(const TopoDS_Shape& theShape) const
@@ -2055,7 +2055,7 @@ TopoDS_Shape SMESH_Mesh::GetShapeByEntry(const std::string& entry) const
 
 //=============================================================================
 /*!
- *  
+ *
  */
 //=============================================================================
 
@@ -2169,7 +2169,7 @@ std::list<int> SMESH_Mesh::GetGroupIds() const
   std::map<int, SMESH_Group*>::const_iterator it = _mapGroup.begin();
   for ( ; it != _mapGroup.end(); it++ )
     anIds.push_back( it->first );
-  
+
   return anIds;
 }
 
@@ -2188,7 +2188,7 @@ void SMESH_Mesh::SetCallUp( TCallUp* upCaller )
 
 //=============================================================================
 /*!
- *  
+ *
  */
 //=============================================================================
 
@@ -2546,7 +2546,7 @@ bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore,
     }
   }
   return true; // no order imposed to given sub-meshes
-} 
+}
 
 //=============================================================================
 /*!
index f6abcbdceb5767cac2dcc9937152b2f81a7deaf3..6c0face11b2c79c6d883426c12db6c4f10eea34a 100644 (file)
@@ -2833,7 +2833,6 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateDualMesh(SMESH::SMESH_IDSource_ptr mesh
   if ( CORBA::is_nil( mesh ))
     THROW_SALOME_CORBA_EXCEPTION( "bad IDSource", SALOME::BAD_PARAM );
 
-  std::cout << mesh << std::endl;
   SMESH::SMESH_Mesh_var srcMesh = mesh->GetMesh();
   SMESH_Mesh_i*       srcMesh_i = SMESH::DownCast<SMESH_Mesh_i*>( srcMesh );
   if ( !srcMesh_i )
@@ -2846,7 +2845,7 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateDualMesh(SMESH::SMESH_IDSource_ptr mesh
   fs::path tmp_folder = fs::temp_directory_path() / fs::unique_path(fs::path("dual_mesh-%%%%"));
   fs::create_directories(tmp_folder);
   fs::path dual_mesh_file = tmp_folder / fs::path("tmp_dual_mesh.med");
-  std::string mesh_name = meshName;
+  std::string mesh_name(meshName);
   MESSAGE("Working in folder" + tmp_folder.string());
 
   // Running Python script
@@ -2854,21 +2853,47 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateDualMesh(SMESH::SMESH_IDSource_ptr mesh
   PyGILState_STATE gstate;
   gstate = PyGILState_Ensure();
 
-  std::string cmd="import salome.smesh.smesh_tools as smt";
-  PyRun_SimpleString(cmd.c_str());
+
   std::string ats;
   if(adapt_to_shape)
     ats = "True";
   else
     ats = "False";
 
-  cmd = "smt.smesh_create_dual_mesh(\"" + mesh_ior + "\", \"" +
+  std::string cmd="import salome.smesh.smesh_tools as smt\n";
+  cmd +="smt.smesh_create_dual_mesh(\"" + mesh_ior + "\", \"" +
         dual_mesh_file.string() + "\", mesh_name=\"" + mesh_name + "\", adapt_to_shape=" + ats + ")";
   MESSAGE(cmd);
-  PyRun_SimpleString(cmd.c_str());
+
+  PyObject *py_main = PyImport_AddModule("__main__");
+  PyObject *py_dict = PyModule_GetDict(py_main);
+
+  PyRun_String(cmd.c_str(), Py_file_input, py_dict, py_dict);
+
+  if (PyErr_Occurred()) {
+    // Restrieving python error
+    MESSAGE("Catching error")
+    PyObject *errtype, *errvalue, *traceback;
+    PyErr_Fetch(&errtype, &errvalue, &traceback);
+    if(errvalue != NULL) {
+      MESSAGE("Error has a value")
+      PyObject *s = PyObject_Str(errvalue);
+      Py_ssize_t size;
+      std::string msg = PyUnicode_AsUTF8AndSize(s, &size);
+      msg = "Issue with the execution of create_dual_mesh:\n"+msg;
+      MESSAGE("throwing exception")
+      // We need to deactivate the GIL before throwing the exception
+      PyGILState_Release(gstate);
+      THROW_SALOME_CORBA_EXCEPTION(msg.c_str(), SALOME::INTERNAL_ERROR );
+      Py_DECREF(s);
+    }
+    Py_XDECREF(errvalue);
+    Py_XDECREF(errtype);
+    Py_XDECREF(traceback);
+  }
 
   PyGILState_Release(gstate);
-  MESSAGE("Executed python script");
+
   MESSAGE("Mesh created in " + dual_mesh_file.string());
 
   // Import created MED
@@ -2882,24 +2907,32 @@ SMESH::SMESH_Mesh_ptr SMESH_Gen_i::CreateDualMesh(SMESH::SMESH_IDSource_ptr mesh
     SetName( meshSO, meshName, meshName );
     SetPixMap( meshSO, "ICON_SMESH_TREE_MESH_IMPORTED");
   }
+  int ret = newMesh_i->ImportMEDFile(dual_mesh_file.c_str(), meshName);
+  if(ret)
+    THROW_SALOME_CORBA_EXCEPTION( "Issue when importing mesh", SALOME::INTERNAL_ERROR );
 
+  /*
   SMESH_Mesh& newMesh2 = newMesh_i->GetImpl();
 
-  MESSAGE("Loading file: " << dual_mesh_file.string() << " with mesh " << mesh_name);
-  newMesh2.MEDToMesh(dual_mesh_file.c_str(), meshName);
 
-  MESSAGE("Imported created MED")
+  MESSAGE("Loading file: " << dual_mesh_file.string() << " with mesh " << meshName);
+  int ret = newMesh2.MEDToMesh(dual_mesh_file.c_str(), meshName);
+    */
 
-  SMESHDS_Mesh* newMeshDS = newMesh_i->GetImpl().GetMeshDS();
-
-  newMeshDS->Modified();
+  newMesh_i->GetImpl().GetMeshDS()->Modified();
 
   *pyDump << newMesh << " = " << this
           << ".CreateDualMesh("
           << mesh << ", "
-          << "'" << meshName << "', "
+          << "'" << mesh_name << "', "
           << ats << ") ";
 
+  pyDumpDeleter.reset(); // allow dump in GetGroups()
+
+  if ( srcMesh_i->GetImpl().GetGroupIds().size() > 0 ) // dump created groups
+    MESSAGE("Dump of groups");
+    SMESH::ListOfGroups_var groups = newMesh->GetGroups();
+
   return newMesh._retn();
 }
 
index 3d95f730c07f78144de4acbbdcffb1b8aa95aa15..e475ab24e7cc47840d93d724af8955d78821ded7 100644 (file)
@@ -37,6 +37,7 @@ def smesh_create_dual_mesh(mesh_ior, output_file, adapt_to_shape=True, mesh_name
     shape = mesh.GetShapeToMesh()
 
     # Creating output file
+    logger.debug("Creating file with mesh: "+mesh_name)
     myfile = mc.MEDFileUMesh()
     myfile.setName(mesh_name)
 
@@ -58,7 +59,12 @@ def smesh_create_dual_mesh(mesh_ior, output_file, adapt_to_shape=True, mesh_name
 
 
     for grp_name in mc_mesh_file.getGroupsOnSpecifiedLev(-1):
+        # This group is created by the export
+        if grp_name == "Group_Of_All_Faces":
+            logger.debug("Skipping group: "+ grp_name)
+            continue
         logger.debug("Transferring group: "+ grp_name)
+
         grp_tria = mc_mesh_file.getGroup(-1, grp_name)
         # Retrieve the nodes in group
         grp_nodes = grp_tria.computeFetchedNodeIds()
index 9f79618da72dc582c84f0c02fa9d6c5d3a23cc3d..ccabb5aa52469791a9e11f98fb5d885bc8239d71 100644 (file)
@@ -77,8 +77,8 @@ isDone = Mesh_1.Compute()
 [ top_1, middle_1, bottom_1 ] = Mesh_1.GetGroups()
 
 
+dual_Mesh_raw_1 = smesh.CreateDualMesh(Mesh_1, 'dual_Mesh_raw_1', False)
 dual_Mesh_1 = smesh.CreateDualMesh(Mesh_1, 'dual_Mesh_1', True)
-dual_Mesh_raw_1 = smesh.CreateDualMesh(Mesh_1, 'dual_Mesh_1', False)
 
 [ top_2, middle_2, bottom_2 ] = dual_Mesh_1.GetGroups()