From: dbv Date: Tue, 8 Sep 2015 15:28:48 +0000 (+0300) Subject: Translation and Rotation code clean up X-Git-Tag: V_1.4.0_beta4~71 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=98ab1b11edf389663aef6057f0bdd7ac2d82ec71;p=modules%2Fshaper.git Translation and Rotation code clean up --- diff --git a/src/FeaturesPlugin/FeaturesPlugin_Rotation.cpp b/src/FeaturesPlugin/FeaturesPlugin_Rotation.cpp index e71fabcfa..422a4d334 100755 --- a/src/FeaturesPlugin/FeaturesPlugin_Rotation.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Rotation.cpp @@ -78,32 +78,35 @@ void FeaturesPlugin_Rotation::execute() anObjectsIt++, aContext++) { std::shared_ptr aBaseShape = *anObjectsIt; bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group(); - GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle, isPart); - - // Checking that the algorithm worked properly. - if(!aRotationAlgo.isDone()) { - static const std::string aFeatureError = "Rotation algorithm failed"; - setError(aFeatureError); - break; - } - if(aRotationAlgo.shape()->isNull()) { - static const std::string aShapeError = "Resulting shape is Null"; - setError(aShapeError); - break; - } - if(!aRotationAlgo.isValid()) { - std::string aFeatureError = "Warning: resulting shape is not valid"; - setError(aFeatureError); - break; - } // Setting result. if (isPart) { + std::shared_ptr aTrsf(new GeomAPI_Trsf()); + aTrsf->setRotation(anAxis, anAngle); ResultPartPtr anOrigin = std::dynamic_pointer_cast(*aContext); ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex); - aResultPart->setTrsf(*aContext, aRotationAlgo.transformation()); + aResultPart->setTrsf(*aContext, aTrsf); setResult(aResultPart); } else { + GeomAlgoAPI_Rotation aRotationAlgo(aBaseShape, anAxis, anAngle); + + // Checking that the algorithm worked properly. + if(!aRotationAlgo.isDone()) { + static const std::string aFeatureError = "Rotation algorithm failed"; + setError(aFeatureError); + break; + } + if(aRotationAlgo.shape()->isNull()) { + static const std::string aShapeError = "Resulting shape is Null"; + setError(aShapeError); + break; + } + if(!aRotationAlgo.isValid()) { + std::string aFeatureError = "Warning: resulting shape is not valid"; + setError(aFeatureError); + break; + } + ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); LoadNamingDS(aRotationAlgo, aResultBody, aBaseShape); setResult(aResultBody, aResultIndex); diff --git a/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp b/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp index d1ff455a2..423ea9459 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp @@ -78,32 +78,35 @@ void FeaturesPlugin_Translation::execute() anObjectsIt++, aContext++) { std::shared_ptr aBaseShape = *anObjectsIt; bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group(); - GeomAlgoAPI_Translation aMovementAlgo(aBaseShape, anAxis, aDistance, isPart); - - // Checking that the algorithm worked properly. - if(!aMovementAlgo.isDone()) { - static const std::string aFeatureError = "Movement algorithm failed"; - setError(aFeatureError); - break; - } - if(aMovementAlgo.shape()->isNull()) { - static const std::string aShapeError = "Resulting shape is Null"; - setError(aShapeError); - break; - } - if(!aMovementAlgo.isValid()) { - std::string aFeatureError = "Warning: resulting shape is not valid"; - setError(aFeatureError); - break; - } // Setting result. if (isPart) { + std::shared_ptr aTrsf(new GeomAPI_Trsf()); + aTrsf->setTranslation(anAxis, aDistance); ResultPartPtr anOrigin = std::dynamic_pointer_cast(*aContext); ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex); - aResultPart->setTrsf(*aContext, aMovementAlgo.transformation()); + aResultPart->setTrsf(*aContext, aTrsf); setResult(aResultPart); } else { + GeomAlgoAPI_Translation aMovementAlgo(aBaseShape, anAxis, aDistance); + + // Checking that the algorithm worked properly. + if(!aMovementAlgo.isDone()) { + static const std::string aFeatureError = "Movement algorithm failed"; + setError(aFeatureError); + break; + } + if(aMovementAlgo.shape()->isNull()) { + static const std::string aShapeError = "Resulting shape is Null"; + setError(aShapeError); + break; + } + if(!aMovementAlgo.isValid()) { + std::string aFeatureError = "Warning: resulting shape is not valid"; + setError(aFeatureError); + break; + } + ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); LoadNamingDS(aMovementAlgo, aResultBody, aBaseShape); setResult(aResultBody, aResultIndex); diff --git a/src/GeomAPI/GeomAPI_Trsf.cpp b/src/GeomAPI/GeomAPI_Trsf.cpp index 7a3c8c178..f0775141c 100644 --- a/src/GeomAPI/GeomAPI_Trsf.cpp +++ b/src/GeomAPI/GeomAPI_Trsf.cpp @@ -6,14 +6,35 @@ #include +#include + +#include #include +#define MY_TRSF implPtr() + +//================================================================================================= GeomAPI_Trsf::GeomAPI_Trsf() - : GeomAPI_Interface() +: GeomAPI_Interface(new gp_Trsf()) { } +//================================================================================================= GeomAPI_Trsf::GeomAPI_Trsf(void* theTrsf) - : GeomAPI_Interface(theTrsf) +: GeomAPI_Interface(theTrsf) +{ +} + +//================================================================================================= +void GeomAPI_Trsf::setTranslation(const std::shared_ptr theAxis, + const double theDistance) +{ + MY_TRSF->SetTranslation(gp_Vec(theAxis->impl().Direction()) * theDistance); +} + +//================================================================================================= +void GeomAPI_Trsf::setRotation(const std::shared_ptr theAxis, + const double theAngle) { + MY_TRSF->SetRotation(theAxis->impl(), theAngle / 180.0 * M_PI); } diff --git a/src/GeomAPI/GeomAPI_Trsf.h b/src/GeomAPI/GeomAPI_Trsf.h index ebb7871f9..d5f3ffd5b 100644 --- a/src/GeomAPI/GeomAPI_Trsf.h +++ b/src/GeomAPI/GeomAPI_Trsf.h @@ -10,6 +10,8 @@ #include #include +class GeomAPI_Ax1; + /**\class GeomAPI_Trsf * \ingroup DataModel * \brief Keep the transformation matrix coefficients @@ -22,6 +24,20 @@ class GeomAPI_Trsf : public GeomAPI_Interface GEOMAPI_EXPORT GeomAPI_Trsf(); /// Takes the pointer to existing transformation GEOMAPI_EXPORT GeomAPI_Trsf(void* theTrsf); + + /** \brief Sets a translation transformation. + * \param[in] theAxis translation axis. + * \param[in] theDistance translation distance. + */ + GEOMAPI_EXPORT void setTranslation(const std::shared_ptr theAxis, + const double theDistance); + + /** \brief Sets a rotation transformation. + * \param[in] theAxis rotation axis. + * \param[in] theAngle rotation angle(in degree). + */ + GEOMAPI_EXPORT void setRotation(const std::shared_ptr theAxis, + const double theAngle); }; #endif diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_Rotation.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_Rotation.cpp index 0ecd76ecc..62e687d98 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_Rotation.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_Rotation.cpp @@ -16,18 +16,16 @@ //================================================================================================= GeomAlgoAPI_Rotation::GeomAlgoAPI_Rotation(std::shared_ptr theSourceShape, std::shared_ptr theAxis, - double theAngle, - bool theSimpleTransform) + double theAngle) : myDone(false) { - build(theSourceShape, theAxis, theAngle, theSimpleTransform); + build(theSourceShape, theAxis, theAngle); } //================================================================================================= void GeomAlgoAPI_Rotation::build(std::shared_ptr theSourceShape, std::shared_ptr theAxis, - double theAngle, - bool theSimpleTransform) + double theAngle) { if(!theSourceShape || !theAxis) { return; @@ -40,38 +38,30 @@ void GeomAlgoAPI_Rotation::build(std::shared_ptr theSourceShape, return; } - gp_Trsf aTrsf; - aTrsf.SetRotation(anAxis, theAngle / 180.0 * M_PI); + gp_Trsf* aTrsf = new gp_Trsf(); + aTrsf->SetRotation(anAxis, theAngle / 180.0 * M_PI); + myTrsf.reset(new GeomAPI_Trsf(aTrsf)); - TopoDS_Shape aResult; // Transform the shape with copying it. - if (theSimpleTransform) { - TopLoc_Location aDelta(aTrsf); - aResult = aSourceShape.Moved(aDelta); - //myTrsf = std::shared_ptr(new GeomAPI_Trsf(new gp_Trsf(aTrsf * aSourceShape.Location().Transformation()))); - myTrsf = std::shared_ptr(new GeomAPI_Trsf(new gp_Trsf(aTrsf))); - myDone = true; // is OK for sure - } else { - BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true); - if(!aBuilder) { - return; - } - myMkShape.reset(new GeomAlgoAPI_MakeShape(aBuilder)); - - myDone = aBuilder->IsDone() == Standard_True; - - if(!myDone) { - return; - } - - aResult = aBuilder->Shape(); - // Fill data map to keep correct orientation of sub-shapes. - myMap.reset(new GeomAPI_DataMapOfShapeShape()); - for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More(); anExp.Next()) { - std::shared_ptr aCurrentShape(new GeomAPI_Shape()); - aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current())); - myMap->bind(aCurrentShape, aCurrentShape); - } + BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true); + if(!aBuilder) { + return; + } + myMkShape.reset(new GeomAlgoAPI_MakeShape(aBuilder)); + + myDone = aBuilder->IsDone() == Standard_True; + + if(!myDone) { + return; + } + + TopoDS_Shape aResult = aBuilder->Shape(); + // Fill data map to keep correct orientation of sub-shapes. + myMap.reset(new GeomAPI_DataMapOfShapeShape()); + for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More(); anExp.Next()) { + std::shared_ptr aCurrentShape(new GeomAPI_Shape()); + aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current())); + myMap->bind(aCurrentShape, aCurrentShape); } myShape.reset(new GeomAPI_Shape()); diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_Rotation.h b/src/GeomAlgoAPI/GeomAlgoAPI_Rotation.h index bf8e03579..ef56c33ea 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_Rotation.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_Rotation.h @@ -25,12 +25,10 @@ public: * \param[in] theSourceShape a shape to be rotated. * \param[in] theAxis rotation axis. * \param[in] theAngle rotation angle(in degree). - * \param[in] theSimpleTransform makes just transformation of shape without changing of topology or geometry */ GEOMALGOAPI_EXPORT GeomAlgoAPI_Rotation(std::shared_ptr theSourceShape, std::shared_ptr theAxis, - double theAngle, - bool theSimpleTransform = false); + double theAngle); /// \return true if algorithm succeed. GEOMALGOAPI_EXPORT const bool isDone() const @@ -58,8 +56,7 @@ private: /// Builds resulting shape. void build(std::shared_ptr theSourceShape, std::shared_ptr theAxis, - double theAngle, - bool theSimpleTransform); + double theAngle); private: /// Fields. diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_Translation.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_Translation.cpp index b9cc3b690..8f505072f 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_Translation.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_Translation.cpp @@ -16,19 +16,17 @@ //================================================================================================= GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr theSourceShape, - std::shared_ptr theAxis, - double theDistance, - bool theSimpleTransform) + std::shared_ptr theAxis, + double theDistance) : myDone(false) { - build(theSourceShape, theAxis, theDistance, theSimpleTransform); + build(theSourceShape, theAxis, theDistance); } //================================================================================================= void GeomAlgoAPI_Translation::build(std::shared_ptr theSourceShape, - std::shared_ptr theAxis, - double theDistance, - bool theSimpleTransform) + std::shared_ptr theAxis, + double theDistance) { if(!theSourceShape || !theAxis) { return; @@ -41,39 +39,30 @@ void GeomAlgoAPI_Translation::build(std::shared_ptr theSourceShap return; } - gp_Trsf aTrsf; - aTrsf.SetTranslation(gp_Vec(anAxis.Direction()) * theDistance); + gp_Trsf* aTrsf = new gp_Trsf(); + aTrsf->SetTranslation(gp_Vec(anAxis.Direction()) * theDistance); + myTrsf.reset(new GeomAPI_Trsf(aTrsf)); - TopoDS_Shape aResult; // Transform the shape with copying it. - if (theSimpleTransform) { - TopLoc_Location aDelta(aTrsf); - aResult = aSourceShape.Moved(aDelta); - // store the accumulated information about the result and this delta - //myTrsf = std::shared_ptr(new GeomAPI_Trsf(new gp_Trsf(aTrsf * aSourceShape.Location().Transformation()))); - myTrsf = std::shared_ptr(new GeomAPI_Trsf(new gp_Trsf(aTrsf))); - myDone = true; // is OK for sure - } else { - BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, aTrsf, true); - if(!aBuilder) { - return; - } - myMkShape.reset(new GeomAlgoAPI_MakeShape(aBuilder)); - - myDone = aBuilder->IsDone() == Standard_True; - - if(!myDone) { - return; - } - - aResult = aBuilder->Shape(); - // Fill data map to keep correct orientation of sub-shapes. - myMap.reset(new GeomAPI_DataMapOfShapeShape()); - for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More(); anExp.Next()) { - std::shared_ptr aCurrentShape(new GeomAPI_Shape()); - aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current())); - myMap->bind(aCurrentShape, aCurrentShape); - } + BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true); + if(!aBuilder) { + return; + } + myMkShape.reset(new GeomAlgoAPI_MakeShape(aBuilder)); + + myDone = aBuilder->IsDone() == Standard_True; + + if(!myDone) { + return; + } + + TopoDS_Shape aResult = aBuilder->Shape(); + // Fill data map to keep correct orientation of sub-shapes. + myMap.reset(new GeomAPI_DataMapOfShapeShape()); + for(TopExp_Explorer anExp(aResult, TopAbs_FACE); anExp.More(); anExp.Next()) { + std::shared_ptr aCurrentShape(new GeomAPI_Shape()); + aCurrentShape->setImpl(new TopoDS_Shape(anExp.Current())); + myMap->bind(aCurrentShape, aCurrentShape); } myShape.reset(new GeomAPI_Shape()); diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_Translation.h b/src/GeomAlgoAPI/GeomAlgoAPI_Translation.h index 95f9c55f7..e8f3d8147 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_Translation.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_Translation.h @@ -25,12 +25,10 @@ public: * \param[in] theSourceShape a shape to be moved. * \param[in] theAxis movement axis. * \param[in] theDistance movement distance. - * \param[in] theSimpleTransform makes just transformation of shape without changing of topology or geometry */ GEOMALGOAPI_EXPORT GeomAlgoAPI_Translation(std::shared_ptr theSourceShape, - std::shared_ptr theAxis, - double theDistance, - bool theSimpleTransform = false); + std::shared_ptr theAxis, + double theDistance); /// \return true if algorithm succeed. GEOMALGOAPI_EXPORT const bool isDone() const @@ -58,8 +56,7 @@ private: /// Builds resulting shape. void build(std::shared_ptr theSourceShape, std::shared_ptr theAxis, - double theDistance, - bool theSimpleTransform); + double theDistance); private: /// Fields. @@ -67,7 +64,7 @@ private: std::shared_ptr myShape; std::shared_ptr myMap; std::shared_ptr myMkShape; - std::shared_ptr myTrsf; ///< transformation of the shape in case theSimpleTransform + std::shared_ptr myTrsf; }; #endif