From c3ca179bcbb814aae3ab1ee9c2ec5b6b29c84c10 Mon Sep 17 00:00:00 2001 From: Yoann Audouin Date: Thu, 13 Oct 2022 14:03:31 +0200 Subject: [PATCH] Handling error in PyRun + better import of MED file --- src/SMESH/SMESH_Mesh.cxx | 30 +++++++------- src/SMESH_I/SMESH_Gen_i.cxx | 61 +++++++++++++++++++++------- src/SMESH_SWIG/smesh_tools.py | 6 +++ test/SMESH_create_dual_mesh_adapt.py | 2 +- 4 files changed, 69 insertions(+), 30 deletions(-) diff --git a/src/SMESH/SMESH_Mesh.cxx b/src/SMESH/SMESH_Mesh.cxx index 8d0d9392b..5ff4c1830 100644 --- a/src/SMESH/SMESH_Mesh.cxx +++ b/src/SMESH/SMESH_Mesh.cxx @@ -76,7 +76,7 @@ #ifndef WIN32 #include #include -#else +#else #include #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::const_iterator smIt = ancestors.begin(); + std::vector::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 & 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 SMESH_Mesh::GetGroupIds() const std::map::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 -} +} //============================================================================= /*! diff --git a/src/SMESH_I/SMESH_Gen_i.cxx b/src/SMESH_I/SMESH_Gen_i.cxx index f6abcbdce..6c0face11 100644 --- a/src/SMESH_I/SMESH_Gen_i.cxx +++ b/src/SMESH_I/SMESH_Gen_i.cxx @@ -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( 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(); } diff --git a/src/SMESH_SWIG/smesh_tools.py b/src/SMESH_SWIG/smesh_tools.py index 3d95f730c..e475ab24e 100644 --- a/src/SMESH_SWIG/smesh_tools.py +++ b/src/SMESH_SWIG/smesh_tools.py @@ -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() diff --git a/test/SMESH_create_dual_mesh_adapt.py b/test/SMESH_create_dual_mesh_adapt.py index 9f79618da..ccabb5aa5 100644 --- a/test/SMESH_create_dual_mesh_adapt.py +++ b/test/SMESH_create_dual_mesh_adapt.py @@ -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() -- 2.30.2