From e88352f46f7dc2f379cd0a7c9b7258e39b52e97d Mon Sep 17 00:00:00 2001 From: Mathis Chevallier Date: Wed, 11 Dec 2024 15:01:07 +0100 Subject: [PATCH] First implementation of iterator --- idl/SMESH_Gen.idl | 4 +++ idl/SMESH_Mesh.idl | 3 +++ src/SMESH/CMakeLists.txt | 8 ++++++ src/SMESH/SMESH_Mesh.cxx | 45 ++++++++++++++++++++++++++++++-- src/SMESH/SMESH_Mesh.hxx | 8 +++++- src/SMESH/SMESH_ParallelMesh.cxx | 6 ++++- src/SMESH/smesh_algoIterator.py | 19 ++++++++++++++ src/SMESH_I/SMESH_Gen_i.cxx | 5 ++++ src/SMESH_I/SMESH_Gen_i.hxx | 2 ++ src/SMESH_I/SMESH_Mesh_i.cxx | 18 +++++++++++++ src/SMESH_I/SMESH_Mesh_i.hxx | 2 ++ src/SMESH_SWIG/smeshBuilder.py | 17 ++++++++++++ 12 files changed, 133 insertions(+), 4 deletions(-) create mode 100644 src/SMESH/smesh_algoIterator.py diff --git a/idl/SMESH_Gen.idl b/idl/SMESH_Gen.idl index f3bf7f454..96829ff56 100644 --- a/idl/SMESH_Gen.idl +++ b/idl/SMESH_Gen.idl @@ -170,6 +170,10 @@ module SMESH */ boolean IsEnablePublish(); + /*! + * void SetAlgoIterator(in long long ptAlgoPy); + */ + /*! * Create a hypothesis that can be shared by different parts of the mesh. * An hypothesis is either: diff --git a/idl/SMESH_Mesh.idl b/idl/SMESH_Mesh.idl index 6535888a1..45a52b4e3 100644 --- a/idl/SMESH_Mesh.idl +++ b/idl/SMESH_Mesh.idl @@ -776,6 +776,9 @@ module SMESH */ double GetComputeProgress(); + void SetAlgoIterator(in long long ptAlgoPy) + raises (SALOME::SALOME_Exception); + /*! * Get information about mesh contents */ diff --git a/src/SMESH/CMakeLists.txt b/src/SMESH/CMakeLists.txt index a9d9e8ad8..ab0910f9b 100644 --- a/src/SMESH/CMakeLists.txt +++ b/src/SMESH/CMakeLists.txt @@ -61,6 +61,7 @@ SET(_link_LIBRARIES ${GEOM_NMTTools} ${GEOM_GEOMUtils} ${Boost_LIBRARIES} + ${PYTHON_LIBRARIES} SMESHDS SMESHControls MeshDriverDAT @@ -124,6 +125,11 @@ SET(SMESHimpl_SOURCES SMESH_MeshLocker.cxx ) +SET(SMESH_ENGINE_PY +smesh_algoIterator.py +) + + # --- rules --- ADD_LIBRARY(SMESHimpl ${SMESHimpl_SOURCES}) @@ -136,3 +142,5 @@ TARGET_LINK_LIBRARIES(SMESHimpl ${_link_LIBRARIES} ) INSTALL(TARGETS SMESHimpl EXPORT ${PROJECT_NAME}TargetGroup DESTINATION ${SALOME_INSTALL_LIBS}) INSTALL(FILES ${SMESHimpl_HEADERS} DESTINATION ${SALOME_INSTALL_HEADERS}) + +SALOME_INSTALL_SCRIPTS("${SMESH_ENGINE_PY}" ${SALOME_INSTALL_PYTHON}/salome/smesh DEF_PERMS) diff --git a/src/SMESH/SMESH_Mesh.cxx b/src/SMESH/SMESH_Mesh.cxx index 8d545cc58..970947d2e 100644 --- a/src/SMESH/SMESH_Mesh.cxx +++ b/src/SMESH/SMESH_Mesh.cxx @@ -39,6 +39,7 @@ #include "SMESH_Hypothesis.hxx" #include "SMESH_subMesh.hxx" +#include "PythonCppUtils.hxx" #include "utilities.h" #include "DriverDAT_W_SMDS_Mesh.h" @@ -97,6 +98,12 @@ class SMESH_Mesh::SubMeshHolder : public SMESHDS_TSubMeshHolder< SMESH_subMesh > { }; +struct SMESH_Mesh::PyImpl +{ + AutoPyRef pyref; + operator PyObject *() { return pyref; } +}; + //============================================================================= /*! * @@ -107,7 +114,7 @@ SMESH_Mesh::SMESH_Mesh(int theLocalId, SMESH_Gen* theGen, bool theIsEmbeddedMode, SMESHDS_Document* theDocument): - _groupId( 0 ), _nbSubShapes( 0 ) + _groupId( 0 ), _nbSubShapes( 0 ), _pAlgoIt(new SMESH_Mesh::PyImpl) { MESSAGE("SMESH_Mesh::SMESH_Mesh(int localId)"); _id = theLocalId; @@ -158,7 +165,8 @@ SMESH_Mesh::SMESH_Mesh(): _isAutoColor( false ), _isModified( false ), _shapeDiagonal( 0.0 ), - _callUp( 0 ) + _callUp( 0 ), + _pAlgoIt(new SMESH_Mesh::PyImpl) { _subMeshHolder = new SubMeshHolder; } @@ -233,6 +241,7 @@ SMESH_Mesh::~SMESH_Mesh() int result=pthread_create(&thread, NULL, deleteMeshDS, (void*)_meshDS); #endif } + delete _pAlgoIt; } //================================================================================ @@ -2588,6 +2597,38 @@ bool SMESH_Mesh::IsOrderOK( const SMESH_subMesh* smBefore, return true; // no order imposed to given sub-meshes } +void *SMESH_Mesh::getAlgoIt() +{ + return (PyObject *)this->_pAlgoIt->pyref; +} + +void SMESH_Mesh::SetAlgoIterator(void *pAlgoItVoid) +{ + PyObject *pAlgoIt = reinterpret_cast(pAlgoItVoid); + AutoGIL gstate; + + if(pAlgoIt){ + + _pAlgoIt->pyref = pAlgoIt; + + std::cout << "pAlgoIt OK " << std::endl; + + //seg fault + //PyObject *pGetPath = PyObject_GetAttrString(_pAlgoIt, "getItPath"); + + AutoPyRef name_attr = PyObject_GetAttrString(*_pAlgoIt, "path"); + + if (!!name_attr && PyUnicode_Check(name_attr)) { + const char* name = PyUnicode_AsUTF8(name_attr); + std::cout << "name: " << name << std::endl; + } else { + std::cerr << "Error: Attribute 'name' not found" << std::endl; + } + }else{ + std::cerr << "pAlgoIt KO" << std::endl; + } +} + //============================================================================= /*! * \brief sort submeshes according to stored mesh order diff --git a/src/SMESH/SMESH_Mesh.hxx b/src/SMESH/SMESH_Mesh.hxx index f9cc2e913..5719d6d12 100644 --- a/src/SMESH/SMESH_Mesh.hxx +++ b/src/SMESH/SMESH_Mesh.hxx @@ -392,6 +392,10 @@ class SMESH_EXPORT SMESH_Mesh std::ostream& Dump(std::ostream & save); + void* getAlgoIt(); + + void SetAlgoIterator(void* pAlgoIt); + // Parallel computation functions virtual void Lock(){}; @@ -459,9 +463,11 @@ protected: // when group removal is invoked by hyp modification (issue 0020918) // 2) to forget not loaded mesh data at hyp modification TCallUp* _callUp; + struct PyImpl; + PyImpl *_pAlgoIt = nullptr; // python iterator object of each mesh to generate a json protected: SMESH_Mesh(); - SMESH_Mesh(const SMESH_Mesh&) {}; + SMESH_Mesh(const SMESH_Mesh&) { _pAlgoIt = nullptr; } }; #endif diff --git a/src/SMESH/SMESH_ParallelMesh.cxx b/src/SMESH/SMESH_ParallelMesh.cxx index 9d61f6bd4..fc935a710 100644 --- a/src/SMESH/SMESH_ParallelMesh.cxx +++ b/src/SMESH/SMESH_ParallelMesh.cxx @@ -76,8 +76,12 @@ void SMESH_ParallelMesh::cleanup() //============================================================================= bool SMESH_ParallelMesh::keepingTmpFolfer() { + const char* envSALOME_SMESH_DEBUG = std::getenv("SALOME_SMESH_DEBUG"); + if(!!envSALOME_SMESH_DEBUG){ + setenv("SMESH_KEEP_TMP", "1", 1); //1 to overwrite + } const char* envVar = std::getenv("SMESH_KEEP_TMP"); - + if (envVar && (envVar[0] != '\0')) { try diff --git a/src/SMESH/smesh_algoIterator.py b/src/SMESH/smesh_algoIterator.py new file mode 100644 index 000000000..e70680dfd --- /dev/null +++ b/src/SMESH/smesh_algoIterator.py @@ -0,0 +1,19 @@ +#testing import +import salome +import SMESH +import json +import os + +class AlgoIterator: + + def __init__(self, path): + self.path = path + self.jsonData = [] + + def getNumberOfAlgoCalls(self): + return len(self.jsonData) + + def getItPath(self): + return self.path + + diff --git a/src/SMESH_I/SMESH_Gen_i.cxx b/src/SMESH_I/SMESH_Gen_i.cxx index 1c78a2526..d1ca3c5fe 100644 --- a/src/SMESH_I/SMESH_Gen_i.cxx +++ b/src/SMESH_I/SMESH_Gen_i.cxx @@ -724,6 +724,11 @@ void SMESH_Gen_i::SetEnablePublish( CORBA::Boolean theIsEnablePublish ) myIsEnablePublish = theIsEnablePublish; } +// void SMESH_Gen_i::SetAlgoIterator( CORBA::LongLong ptAlgoPy ) +// { +// std::cout << "Mathoissssssssssssssssssssssss " << ptAlgoPy << std::endl; +// } + //============================================================================= /*! * SMESH_Gen_i::IsEnablePublish diff --git a/src/SMESH_I/SMESH_Gen_i.hxx b/src/SMESH_I/SMESH_Gen_i.hxx index 1b3af8ad5..c94d55c8b 100644 --- a/src/SMESH_I/SMESH_Gen_i.hxx +++ b/src/SMESH_I/SMESH_Gen_i.hxx @@ -165,6 +165,8 @@ public: // Set enable publishing in the study void SetEnablePublish( CORBA::Boolean theIsEnablePublish ); + //void SetAlgoIterator( CORBA::LongLong ptAlgoPy); + // Check enable publishing CORBA::Boolean IsEnablePublish(); diff --git a/src/SMESH_I/SMESH_Mesh_i.cxx b/src/SMESH_I/SMESH_Mesh_i.cxx index 7f732ae9c..e2fd98123 100644 --- a/src/SMESH_I/SMESH_Mesh_i.cxx +++ b/src/SMESH_I/SMESH_Mesh_i.cxx @@ -23,6 +23,8 @@ // Author : Paul RASCLE, EDF // Module : SMESH +#include + #include "SMESH_Mesh_i.hxx" #include "DriverMED_R_SMESHDS_Mesh.h" @@ -4638,6 +4640,22 @@ CORBA::Double SMESH_Mesh_i::GetComputeProgress() return 0.; } +void SMESH_Mesh_i::SetAlgoIterator( CORBA::LongLong ptAlgoPy ) +{ + SMESH_TRY; + std::cout << "SMESH_Mesh_i.cxx::SetAlgoIterator -> " << ptAlgoPy << std::endl; + + PyObject* pAlgoIt = reinterpret_cast(ptAlgoPy); + std::cout << "SMESH_Mesh_i.cxx::SetAlgoIterator 2 -> " << pAlgoIt << std::endl; + + + _impl->SetAlgoIterator(pAlgoIt); + + + SMESH_CATCH( SMESH::throwCorbaException ); + +} + //================================================================================ /*! * \brief Return nb of nodes diff --git a/src/SMESH_I/SMESH_Mesh_i.hxx b/src/SMESH_I/SMESH_Mesh_i.hxx index a7cb38fab..c464caa97 100644 --- a/src/SMESH_I/SMESH_Mesh_i.hxx +++ b/src/SMESH_I/SMESH_Mesh_i.hxx @@ -277,6 +277,8 @@ public: CORBA::Double GetComputeProgress(); + void SetAlgoIterator( CORBA::LongLong ptAlgoPy); + SMESH::smIdType NbNodes(); SMESH::smIdType NbElements(); diff --git a/src/SMESH_SWIG/smeshBuilder.py b/src/SMESH_SWIG/smeshBuilder.py index 607314411..70fe29451 100644 --- a/src/SMESH_SWIG/smeshBuilder.py +++ b/src/SMESH_SWIG/smeshBuilder.py @@ -20,6 +20,8 @@ # Author : Francis KLOSS, OCC # Module : SMESH +import ctypes + import salome from salome.geom import geomBuilder @@ -8082,6 +8084,21 @@ class ParallelMesh(Mesh): param2d = algo2d.Parameters() copy_param(2, param2d, hyp) + def setAlgoIterator(self, it): + """ + doc : to do + """ + + print(id(it)) + print(hex(id(it))) + ctypes.pythonapi.Py_IncRef(ctypes.py_object(it)) + + self.mesh.SetAlgoIterator(id(it)) + + #to be continued + parallelMeshIt = it.jsonData + print(parallelMeshIt) + pass # End of ParallelMesh class meshProxy(SMESH._objref_SMESH_Mesh):