From a1ece5b5697121ca193130af46d33e018040fee3 Mon Sep 17 00:00:00 2001 From: mzn Date: Tue, 17 Dec 2013 09:52:02 +0000 Subject: [PATCH] Feature #233: Export of groups. Move export to GEOM code from GUI to the data model. --- src/HYDROData/CMakeLists.txt | 4 +- src/HYDROData/HYDROData_CalculationCase.cxx | 174 ++++++++++++++++++ src/HYDROData/HYDROData_CalculationCase.h | 48 ++++- src/HYDROGUI/HYDROGUI_ExportCalculationOp.cxx | 65 ++----- src/HYDROPy/CMakeLists.txt | 1 + 5 files changed, 236 insertions(+), 56 deletions(-) diff --git a/src/HYDROData/CMakeLists.txt b/src/HYDROData/CMakeLists.txt index 3aa79d56..e8840cd2 100644 --- a/src/HYDROData/CMakeLists.txt +++ b/src/HYDROData/CMakeLists.txt @@ -82,9 +82,11 @@ set(PROJECT_SOURCES add_definitions( -DHYDRODATA_EXPORTS ${CAS_DEFINITIONS} + ${OMNIORB_DEFINITIONS} ${QT_DEFINITIONS} # ${GUI_CXXFLAGS} ${GUI_DEFINITIONS} + ${GEOM_DEFINITIONS} ) include_directories( @@ -98,7 +100,7 @@ include_directories( add_library(HYDROData SHARED ${PROJECT_SOURCES} ${PROJECT_HEADERS}) target_link_libraries(HYDROData ${GEOM_GEOMUtils} ${CAS_OCAF} ${CAS_OCAFVIS} ${CAS_TKG3d} ${CAS_TKGeomBase} ${CAS_TKGeomAlgo} ${CAS_TKBrep} ${CAS_TKIGES} ${CAS_TKSTEP} ${CAS_TKTopAlgo} ${CAS_TKBO} ${CAS_TKBool} ${CAS_TKOffset} - ${QT_LIBRARIES} ${GUI_ImageComposer} ${CAS_TKHLR} ) + ${QT_LIBRARIES} ${GUI_ImageComposer} ${CAS_TKHLR} ${GEOM_GEOM} ${GEOM_GEOMBase} ) INSTALL(TARGETS HYDROData EXPORT ${PROJECT_NAME}TargetGroup DESTINATION ${SALOME_INSTALL_LIBS}) set(PROJECT_LIBRARIES HYDROData) diff --git a/src/HYDROData/HYDROData_CalculationCase.cxx b/src/HYDROData/HYDROData_CalculationCase.cxx index 29c0b314..40a98695 100644 --- a/src/HYDROData/HYDROData_CalculationCase.cxx +++ b/src/HYDROData/HYDROData_CalculationCase.cxx @@ -14,6 +14,8 @@ #include "HYDROData_Tool.h" #include "HYDROData_Zone.h" +#include + #include #include #include @@ -22,6 +24,8 @@ #include #include +#include + #include #include #include @@ -34,6 +38,8 @@ #define PYTHON_CALCULATION_ID "KIND_CALCULATION" +#define EXPORT_NAME "HYDRO_" + GetName() + IMPLEMENT_STANDARD_HANDLE(HYDROData_CalculationCase, HYDROData_Entity) IMPLEMENT_STANDARD_RTTIEXT(HYDROData_CalculationCase, HYDROData_Entity) @@ -753,4 +759,172 @@ Handle(HYDROData_SplittedEdgesGroup) HYDROData_CalculationCase::addNewSplittedGr return aNewGroup; } +bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine, + SALOMEDS::Study_ptr theStudy ) +{ + // Get faces + // TODO + TopTools_ListOfShape aFaces; + + // Get groups + // TODO + HYDROData_SequenceOfObjects aSplittedGroups; + + return Export( theGeomEngine, theStudy, aFaces, aSplittedGroups ); +} + +bool HYDROData_CalculationCase::Export( GEOM::GEOM_Gen_var theGeomEngine, + SALOMEDS::Study_ptr theStudy, + const TopTools_ListOfShape& theFaces, + const HYDROData_SequenceOfObjects& theSplittedGroups ) +{ + // Make shell from the faces + TopoDS_Shell aShell; + TopTools_ListOfShape aFacesList; + + BRepBuilderAPI_Sewing aSewing( Precision::Confusion()*10.0 ); + + TopTools_ListIteratorOfListOfShape aFaceIter( theFaces ); + for ( ; aFaceIter.More(); aFaceIter.Next() ) { + TopoDS_Shape aShape = aFaceIter.Value(); + if ( !aShape.IsNull() && (aShape.ShapeType() == TopAbs_FACE) ) { + TopoDS_Face aFace = TopoDS::Face( aShape ); + if ( !aFace.IsNull() ) { + aFacesList.Append( aFace ); + aSewing.Add( aFace ); + } + } else { + TopExp_Explorer anExp( aShape, TopAbs_FACE ); + for ( ; anExp.More(); anExp.Next() ) { + TopoDS_Face aFace = TopoDS::Face( anExp.Current() ); + if ( !aFace.IsNull() ) { + aFacesList.Append( aFace ); + aSewing.Add( aFace ); + } + } + } + } // faces iterator + + aSewing.Perform(); + TopoDS_Shape aSewedShape = aSewing.SewedShape(); + + if ( !aSewedShape.IsNull() ) { + if ( aSewedShape.ShapeType() == TopAbs_FACE && theFaces.Extent() ==1 ) { + // create shell from one face + BRep_Builder aBuilder; + aBuilder.MakeShell( aShell ); + aBuilder.Add( aShell, aSewedShape); + } else { + TopExp_Explorer anExpShells( aSewedShape, TopAbs_SHELL ); + Standard_Integer aNbOfShells = 0; + for ( ; anExpShells.More(); anExpShells.Next() ) { + aShell = TopoDS::Shell( anExpShells.Current() ); + aNbOfShells++; + } + + if ( aNbOfShells != 1 ) { + aShell.Nullify(); + BRep_Builder aBuilder; + aBuilder.MakeShell( aShell ); + + TopExp_Explorer anExpFaces( aSewedShape, TopAbs_FACE ); + for ( ; anExpFaces.More(); anExpFaces.Next() ) { + TopoDS_Face aFace = TopoDS::Face( anExpFaces.Current() ); + if ( !aFace.IsNull() ) { + aBuilder.Add( aShell, aFace ); + } + } + } + } + } + + if ( !aShell.IsNull() ) { + TopTools_IndexedMapOfShape aMapOfFaces; + TopExp::MapShapes( aShell, TopAbs_FACE, aMapOfFaces ); + if ( aMapOfFaces.Extent() != aFacesList.Extent() ) { + aShell.Nullify(); + BRep_Builder aBuilder; + aBuilder.MakeShell( aShell ); + + TopTools_ListIteratorOfListOfShape anIter( aFacesList ); + for ( ; anIter.More(); anIter.Next() ) { + TopoDS_Face aFace = TopoDS::Face( anIter.Value() ); + aBuilder.Add( aShell, aFace ); + } + } + } + + // If the shell is empty - return false + if ( aShell.IsNull() || !TopoDS_Iterator(aShell).More() ) { + return false; + } + + // Publish the shell + QString aName = EXPORT_NAME; + GEOM::GEOM_Object_ptr aPublishedObj = + publishShapeInGEOM( theGeomEngine, theStudy, aShell, aName ); + + if ( aPublishedObj->_is_nil() ) { + return false; + } + + // Create groups + GEOM::GEOM_IGroupOperations_var aGroupOp = + theGeomEngine->GetIGroupOperations( theStudy->StudyId() ); + + GEOM::GEOM_Object_var aGroup = aGroupOp->CreateGroup( aPublishedObj, TopAbs_EDGE ); + if ( !CORBA::is_nil(aGroup) && aGroupOp->IsDone() ) { + GEOM::ListOfLong_var anIndexes = new GEOM::ListOfLong; + aGroupOp->UnionIDs( aGroup, anIndexes ); + if ( aGroupOp->IsDone() ) { + SALOMEDS::SObject_var aGroupSO = + theGeomEngine->AddInStudy( theStudy, aGroup, qPrintable( aName ), aPublishedObj ); + } + } + + return true; +} + +GEOM::GEOM_Object_ptr HYDROData_CalculationCase::publishShapeInGEOM( + GEOM::GEOM_Gen_var theGeomEngine, SALOMEDS::Study_ptr theStudy, + const TopoDS_Shape& theShape, const QString& theName ) +{ + GEOM::GEOM_Object_var aGeomObj; + + bool isNil = aGeomObj->_is_nil(); ///@MZN + + if ( theGeomEngine->_is_nil() || theStudy->_is_nil() || + theShape.IsNull() ) { + return aGeomObj._retn(); + } + + std::ostringstream aStreamShape; + // Write TopoDS_Shape in ASCII format to the stream + BRepTools::Write( theShape, aStreamShape ); + // Returns the number of bytes that have been stored in the stream's buffer. + int aSize = aStreamShape.str().size(); + // Allocate octect buffer of required size + CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize ); + // Copy ostrstream content to the octect buffer + memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize ); + // Create TMPFile + SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 ); + + // Restore shape from the stream and get the GEOM object + GEOM::GEOM_IInsertOperations_var anInsOp = theGeomEngine->GetIInsertOperations( theStudy->StudyId() ); + aGeomObj = anInsOp->RestoreShape( aSeqFile ); + + // Puplish the GEOM object + if ( !aGeomObj->_is_nil() ) { + QString aName = GEOMBase::GetDefaultName( theName ); + + SALOMEDS::SObject_var aResultSO = + theGeomEngine->PublishInStudy( theStudy, SALOMEDS::SObject::_nil(), + aGeomObj, qPrintable( theName ) ); + if ( aResultSO->_is_nil() ) { + aGeomObj = GEOM::GEOM_Object::_nil(); + } + } + return aGeomObj._retn(); + } \ No newline at end of file diff --git a/src/HYDROData/HYDROData_CalculationCase.h b/src/HYDROData/HYDROData_CalculationCase.h index eef8ea0b..30d61d53 100644 --- a/src/HYDROData/HYDROData_CalculationCase.h +++ b/src/HYDROData/HYDROData_CalculationCase.h @@ -5,9 +5,15 @@ #include +// IDL includes +#include +#include CORBA_SERVER_HEADER(GEOM_Gen) + class gp_XY; +class TopoDS_Shape; class TopoDS_Shell; +class TopTools_ListOfShape; class Handle(HYDROData_Object); class Handle(HYDROData_Region); @@ -190,11 +196,13 @@ public: /** - * Returns shell containing faces which correspond to regions. - * \return shell as TopoDS_Shell + * Exports the calculation case data (shell and groups) to GEOM module. + * \param theGeomEngine GEOM module engine + * \param theStudy SALOMEDS study, is used for publishing of GEOM objects + * \return true in case of success */ - HYDRODATA_EXPORT virtual TopoDS_Shell GetShell(); - + HYDRODATA_EXPORT virtual bool Export( GEOM::GEOM_Gen_var theGeomEngine, + SALOMEDS::Study_ptr theStudy ); public: // Public methods to work with Calculation services @@ -236,6 +244,38 @@ private: */ HYDRODATA_EXPORT virtual Handle(HYDROData_SplittedEdgesGroup) addNewSplittedGroup(); + /** + * Returns shell containing faces which correspond to regions. + * \return shell as TopoDS_Shell + */ + HYDRODATA_EXPORT virtual TopoDS_Shell GetShell(); + + /** + * Exports the given faces as shell and the given groups to GEOM module. + * \param theGeomEngine GEOM module engine + * \param theStudy SALOMEDS study, is used for publishing of GEOM objects + * \param theFaces the list of faces to make shell + * \param theSplittedGroups the list of groups + * \return true in case of success + */ + HYDRODATA_EXPORT bool Export( GEOM::GEOM_Gen_var theGeomEngine, + SALOMEDS::Study_ptr theStudy, + const TopTools_ListOfShape& theFaces, + const HYDROData_SequenceOfObjects& theSplittedGroups ); + + /** + * Publish the given shape in GEOM as a GEOM object. + * \param theGeomEngine GEOM module engine + * \param theStudy SALOMEDS study, used for publishing of the shape + * \param theShape the shape to publish as a GEOM object + * \param theName the name of the published object + * \return the published GEOM object + */ + GEOM::GEOM_Object_ptr publishShapeInGEOM( GEOM::GEOM_Gen_var theGeomEngine, + SALOMEDS::Study_ptr theStudy, + const TopoDS_Shape& theShape, + const QString& theName ); + protected: friend class HYDROData_Iterator; diff --git a/src/HYDROGUI/HYDROGUI_ExportCalculationOp.cxx b/src/HYDROGUI/HYDROGUI_ExportCalculationOp.cxx index 77722af3..8e3f8522 100644 --- a/src/HYDROGUI/HYDROGUI_ExportCalculationOp.cxx +++ b/src/HYDROGUI/HYDROGUI_ExportCalculationOp.cxx @@ -29,13 +29,9 @@ #include #include -#include #include -#include -#include - #include #include @@ -84,56 +80,23 @@ bool HYDROGUI_ExportCalculationOp::processApply( int& theUpdateFlags, return false; } - QString anErrorMsg; - bool anIsOk = false; - - TopoDS_Shell aShell = aCalculation->GetShell(); - - if ( !aShell.IsNull() ) { - // TODO move this code to anothe place? - std::ostringstream aStreamShape; - // Write TopoDS_Shape in ASCII format to the stream - BRepTools::Write( aShell, aStreamShape ); - // Returns the number of bytes that have been stored in the stream's buffer. - int aSize = aStreamShape.str().size(); - // Allocate octect buffer of required size - CORBA::Octet* anOctetBuf = SALOMEDS::TMPFile::allocbuf( aSize ); - // Copy ostrstream content to the octect buffer - memcpy( anOctetBuf, aStreamShape.str().c_str(), aSize ); - // Create TMPFile - SALOMEDS::TMPFile_var aSeqFile = new SALOMEDS::TMPFile( aSize, aSize, anOctetBuf, 1 ); - - // Get active study - SalomeApp_Study* aStudy = - dynamic_cast( module()->getApp()->activeStudy() ); - - // Restore shape from the stream and get the GEOM object - GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen(); // TODO: get GEOM engine in another way? - GEOM::GEOM_IInsertOperations_var anInsOp = aGeomEngine->GetIInsertOperations( module()->getStudyId() ); - GEOM::GEOM_Object_var aGeomObj = anInsOp->RestoreShape( aSeqFile ); - - // Puplish the GEOM object - if ( !aGeomObj->_is_nil() ) { - QString aName = GEOMBase::GetDefaultName( tr( "OBJ_PREFIX" ) + aCalculation->GetName() ); - - SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() ); - SALOMEDS::SObject_var aResultSO = - aGeomEngine->PublishInStudy( aDSStudy, SALOMEDS::SObject::_nil(), - aGeomObj, qPrintable( aName ) ); - if ( !aResultSO->_is_nil() ) { - theUpdateFlags = UF_ObjBrowser; - anIsOk = true; - } - } + bool isOk = false; - if ( !anIsOk ) - theErrorMsg = tr( "IMPOSSIBLE_TO_CREATE_GEOM_SHAPE" ); + // Get active study + SalomeApp_Study* aStudy = + dynamic_cast( module()->getApp()->activeStudy() ); - } else { - theErrorMsg = tr( "RESULT_SHAPE_NULL" ); + // Export + if ( aStudy ) { + SALOMEDS::Study_var aDSStudy = GeometryGUI::ClientStudyToStudy( aStudy->studyDS() ); + GEOM::GEOM_Gen_var aGeomEngine = GeometryGUI::GetGeomGen(); + if ( aCalculation->Export( aGeomEngine, aDSStudy ) ) { + theUpdateFlags = UF_ObjBrowser; + isOk = true; + } } - return anIsOk; + return isOk; } void HYDROGUI_ExportCalculationOp::onApply() @@ -182,4 +145,4 @@ void HYDROGUI_ExportCalculationOp::onApply() tr( "EXPORT_STATUS" ), anErrorMsg ); } -} \ No newline at end of file +} diff --git a/src/HYDROPy/CMakeLists.txt b/src/HYDROPy/CMakeLists.txt index 73dc1425..09d94dd3 100644 --- a/src/HYDROPy/CMakeLists.txt +++ b/src/HYDROPy/CMakeLists.txt @@ -18,6 +18,7 @@ ADD_DEFINITIONS( ${QT_DEFINITIONS} ${CAS_DEFINITIONS} ${PYTHON_DEFINITIONS} + ${OMNIORB_DEFINITIONS} ) # libraries to link to -- 2.39.2