From: Clarisse Genrault Date: Mon, 21 Nov 2016 09:48:03 +0000 (+0100) Subject: Add a new method for translation feature : translation by DX, DY, DZ vector. X-Git-Tag: V_2.6.0~79 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=2c892f5ecdcdbdce82da2b0bbd8f0c878971a076;p=modules%2Fshaper.git Add a new method for translation feature : translation by DX, DY, DZ vector. --- diff --git a/src/FeaturesAPI/FeaturesAPI_Translation.cpp b/src/FeaturesAPI/FeaturesAPI_Translation.cpp index 6b0995482..a57e812af 100644 --- a/src/FeaturesAPI/FeaturesAPI_Translation.cpp +++ b/src/FeaturesAPI/FeaturesAPI_Translation.cpp @@ -27,8 +27,22 @@ FeaturesAPI_Translation::FeaturesAPI_Translation( { if(initialize()) { fillAttribute(theMainObjects, mymainObjects); - fillAttribute(theAxisObject, myaxisObject); - setDistance(theDistance); + setAxisAndDistance(theAxisObject, theDistance); + } +} + +//================================================================================================== +FeaturesAPI_Translation::FeaturesAPI_Translation( + const std::shared_ptr& theFeature, + const std::list& theMainObjects, + const ModelHighAPI_Double& theDx, + const ModelHighAPI_Double& theDy, + const ModelHighAPI_Double& theDz) +: ModelHighAPI_Interface(theFeature) +{ + if(initialize()) { + fillAttribute(theMainObjects, mymainObjects); + setDimensions(theDx, theDy, theDz); } } @@ -48,17 +62,25 @@ void FeaturesAPI_Translation::setMainObjects( } //================================================================================================== -void FeaturesAPI_Translation::setAxisObject(const ModelHighAPI_Selection& theAxisObject) +void FeaturesAPI_Translation::setAxisAndDistance(const ModelHighAPI_Selection& theAxisObject, + const ModelHighAPI_Double& theDistance) { + fillAttribute(FeaturesPlugin_Translation::CREATION_METHOD_BY_DISTANCE(), mycreationMethod); fillAttribute(theAxisObject, myaxisObject); + fillAttribute(theDistance, mydistance); execute(); } //================================================================================================== -void FeaturesAPI_Translation::setDistance(const ModelHighAPI_Double& theDistance) +void FeaturesAPI_Translation::setDimensions(const ModelHighAPI_Double& theDx, + const ModelHighAPI_Double& theDy, + const ModelHighAPI_Double& theDz) { - fillAttribute(theDistance, mydistance); + fillAttribute(FeaturesPlugin_Translation::CREATION_METHOD_BY_DIMENSIONS(), mycreationMethod); + fillAttribute(theDx, mydx); + fillAttribute(theDy, mydy); + fillAttribute(theDz, mydz); execute(); } @@ -68,15 +90,28 @@ void FeaturesAPI_Translation::dump(ModelHighAPI_Dumper& theDumper) const { FeaturePtr aBase = feature(); const std::string& aDocName = theDumper.name(aBase->document()); - + AttributeSelectionListPtr anAttrObjects = aBase->selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID()); - AttributeSelectionPtr anAttrAxis = - aBase->selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID()); - AttributeDoublePtr anAttrDistance = aBase->real(FeaturesPlugin_Translation::DISTANCE_ID()); + theDumper << aBase << " = model.addTranslation(" << aDocName << ", " << anAttrObjects; + + std::string aCreationMethod = + aBase->string(FeaturesPlugin_Translation::CREATION_METHOD())->value(); + + if(aCreationMethod == FeaturesPlugin_Translation::CREATION_METHOD_BY_DISTANCE()) { + AttributeSelectionPtr anAttrAxis = + aBase->selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID()); + AttributeDoublePtr anAttrDistance = + aBase->real(FeaturesPlugin_Translation::DISTANCE_ID()); + theDumper << ", " << anAttrAxis << ", " << anAttrDistance; + } else if (aCreationMethod == FeaturesPlugin_Translation::CREATION_METHOD_BY_DIMENSIONS()) { + AttributeDoublePtr anAttrDx = aBase->real(FeaturesPlugin_Translation::DX_ID()); + AttributeDoublePtr anAttrDy = aBase->real(FeaturesPlugin_Translation::DY_ID()); + AttributeDoublePtr anAttrDz = aBase->real(FeaturesPlugin_Translation::DZ_ID()); + theDumper << ", " << anAttrDx << ", " << anAttrDy << ", " << anAttrDz; + } - theDumper << aBase << " = model.addTranslation(" << aDocName << ", " - << anAttrObjects << ", " << anAttrAxis << ", " << anAttrDistance << ")" << std::endl; + theDumper << ")" << std::endl; } //================================================================================================== @@ -89,3 +124,14 @@ TranslationPtr addTranslation(const std::shared_ptr& thePart, return TranslationPtr(new FeaturesAPI_Translation(aFeature, theMainObjects, theAxisObject, theDistance)); } + +//================================================================================================== +TranslationPtr addTranslation(const std::shared_ptr& thePart, + const std::list& theMainObjects, + const ModelHighAPI_Double& theDx, + const ModelHighAPI_Double& theDy, + const ModelHighAPI_Double& theDz) +{ + std::shared_ptr aFeature = thePart->addFeature(FeaturesAPI_Translation::ID()); + return TranslationPtr(new FeaturesAPI_Translation(aFeature, theMainObjects, theDx, theDy, theDz)); +} diff --git a/src/FeaturesAPI/FeaturesAPI_Translation.h b/src/FeaturesAPI/FeaturesAPI_Translation.h index 8d44d8578..3fd650e52 100644 --- a/src/FeaturesAPI/FeaturesAPI_Translation.h +++ b/src/FeaturesAPI/FeaturesAPI_Translation.h @@ -35,29 +35,49 @@ public: const ModelHighAPI_Selection& theAxisObject, const ModelHighAPI_Double& theDistance); + /// Constructor with values. + FEATURESAPI_EXPORT + FeaturesAPI_Translation(const std::shared_ptr& theFeature, + const std::list& theMainObjects, + const ModelHighAPI_Double& theDx, + const ModelHighAPI_Double& theDy, + const ModelHighAPI_Double& theDz); + /// Destructor. FEATURESAPI_EXPORT virtual ~FeaturesAPI_Translation(); - INTERFACE_3(FeaturesPlugin_Translation::ID(), + INTERFACE_7(FeaturesPlugin_Translation::ID(), + creationMethod, FeaturesPlugin_Translation::CREATION_METHOD(), + ModelAPI_AttributeString, /** Creation method */, mainObjects, FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList, /** Main objects */, axisObject, FeaturesPlugin_Translation::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection, /** Axis object */, distance, FeaturesPlugin_Translation::DISTANCE_ID(), - ModelAPI_AttributeDouble, /** Distance */) + ModelAPI_AttributeDouble, /** Distance */, + dx, FeaturesPlugin_Translation::DX_ID(), + ModelAPI_AttributeDouble, /** Dimension in X */, + dy, FeaturesPlugin_Translation::DY_ID(), + ModelAPI_AttributeDouble, /** Dimension in Y */, + dz, FeaturesPlugin_Translation::DZ_ID(), + ModelAPI_AttributeDouble, /** Dimension in Z */ + ) /// Set main objects. FEATURESAPI_EXPORT void setMainObjects(const std::list& theMainObjects); - /// Set axis object. + /// Modify CreationMethod, axis_object, distance attributes of the feature. FEATURESAPI_EXPORT - void setAxisObject(const ModelHighAPI_Selection& theAxisObject); - - /// Set distance. + void setAxisAndDistance(const ModelHighAPI_Selection& theAxisObject, + const ModelHighAPI_Double& theDistance); + + /// Modify CreationMethod, dx, dy, dz attributes of the feature. FEATURESAPI_EXPORT - void setDistance(const ModelHighAPI_Double& theDistance); + void setDimensions(const ModelHighAPI_Double& theDx, + const ModelHighAPI_Double& theDy, + const ModelHighAPI_Double& theDz); /// Dump wrapped feature FEATURESAPI_EXPORT @@ -75,4 +95,13 @@ TranslationPtr addTranslation(const std::shared_ptr& thePart, const ModelHighAPI_Selection& theAxisObject, const ModelHighAPI_Double& theDistance); +/// \ingroup CPPHighAPI +/// \brief Create Translation feature. +FEATURESAPI_EXPORT +TranslationPtr addTranslation(const std::shared_ptr& thePart, + const std::list& theMainObjects, + const ModelHighAPI_Double& theDx, + const ModelHighAPI_Double& theDy, + const ModelHighAPI_Double& theDz); + #endif // FeaturesAPI_Translation_H_ diff --git a/src/FeaturesPlugin/CMakeLists.txt b/src/FeaturesPlugin/CMakeLists.txt index 8afae28a6..53c6e292e 100644 --- a/src/FeaturesPlugin/CMakeLists.txt +++ b/src/FeaturesPlugin/CMakeLists.txt @@ -131,4 +131,5 @@ ADD_UNIT_TESTS(TestExtrusion.py TestUnion.py TestRemoveSubShapes.py TestPipe.py - TestRecover.py) + TestRecover.py + APIParam_Translation.py) diff --git a/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp b/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp index dea6df9f4..2b5a34ed9 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp @@ -1,13 +1,16 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D +// Copyright (C) 2014-2016 CEA/DEN, EDF R&D // File: FeaturesPlugin_Translation.cpp // Created: 8 June 2015 // Author: Dmitry Bobylev +// +// Modified by Clarisse Genrault (CEA) : 17 Nov 2016 #include #include #include +#include #include #include #include @@ -26,6 +29,8 @@ FeaturesPlugin_Translation::FeaturesPlugin_Translation() //================================================================================================= void FeaturesPlugin_Translation::initAttributes() { + data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId()); + AttributeSelectionListPtr aSelection = std::dynamic_pointer_cast(data()->addAttribute( FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId())); @@ -34,22 +39,42 @@ void FeaturesPlugin_Translation::initAttributes() ModelAPI_AttributeSelection::typeId()); data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(), ModelAPI_AttributeDouble::typeId()); + + data()->addAttribute(FeaturesPlugin_Translation::DX_ID(), + ModelAPI_AttributeDouble::typeId()); + data()->addAttribute(FeaturesPlugin_Translation::DY_ID(), + ModelAPI_AttributeDouble::typeId()); + data()->addAttribute(FeaturesPlugin_Translation::DZ_ID(), + ModelAPI_AttributeDouble::typeId()); } //================================================================================================= void FeaturesPlugin_Translation::execute() +{ + AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Translation::CREATION_METHOD()); + std::string aMethodType = aMethodTypeAttr->value(); + + if (aMethodType == CREATION_METHOD_BY_DISTANCE()) { + performTranslationByAxisAndDistance(); + } + + if (aMethodType == CREATION_METHOD_BY_DIMENSIONS()) { + performTranslationByDimensions(); + } +} + +//================================================================================================= +void FeaturesPlugin_Translation::performTranslationByAxisAndDistance() { // Getting objects. ListOfShape anObjects; std::list aContextes; - AttributeSelectionListPtr anObjectsSelList = - selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID()); + AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID()); if (anObjectsSelList->size() == 0) { return; } for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) { - std::shared_ptr anObjectAttr = - anObjectsSelList->value(anObjectsIndex); + std::shared_ptr anObjectAttr = anObjectsSelList->value(anObjectsIndex); std::shared_ptr anObject = anObjectAttr->value(); if(!anObject.get()) { // may be for not-activated parts eraseResults(); @@ -58,26 +83,24 @@ void FeaturesPlugin_Translation::execute() anObjects.push_back(anObject); aContextes.push_back(anObjectAttr->context()); } - + //Getting axis. std::shared_ptr anAxis; std::shared_ptr anEdge; - std::shared_ptr anObjRef = - selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID()); + std::shared_ptr anObjRef = selection(FeaturesPlugin_Translation::AXIS_OBJECT_ID()); if(anObjRef && anObjRef->value() && anObjRef->value()->isEdge()) { anEdge = std::shared_ptr(new GeomAPI_Edge(anObjRef->value())); - } else if (anObjRef && !anObjRef->value() && anObjRef->context() && + } else if (anObjRef && !anObjRef->value() && anObjRef->context() && anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) { anEdge = std::shared_ptr(new GeomAPI_Edge(anObjRef->context()->shape())); } if(anEdge) { - anAxis = std::shared_ptr(new GeomAPI_Ax1(anEdge->line()->location(), - anEdge->line()->direction())); + anAxis = std::shared_ptr(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction())); } // Getting distance. double aDistance = real(FeaturesPlugin_Translation::DISTANCE_ID())->value(); - + // Moving each object. int aResultIndex = 0; std::list::iterator aContext = aContextes.begin(); @@ -96,6 +119,93 @@ void FeaturesPlugin_Translation::execute() setResult(aResultPart, aResultIndex); } else { GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance); + + if (!aTranslationAlgo.check()) { + setError(aTranslationAlgo.getError()); + return; + } + + aTranslationAlgo.build(); + + // Checking that the algorithm worked properly. + if(!aTranslationAlgo.isDone()) { + static const std::string aFeatureError = "Error: Translation algorithm failed."; + setError(aFeatureError); + break; + } + if(aTranslationAlgo.shape()->isNull()) { + static const std::string aShapeError = "Error: Resulting shape is Null."; + setError(aShapeError); + break; + } + if(!aTranslationAlgo.isValid()) { + std::string aFeatureError = "Error: Resulting shape is not valid."; + setError(aFeatureError); + break; + } + + ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); + loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape); + setResult(aResultBody, aResultIndex); + } + aResultIndex++; + } + + // Remove the rest results if there were produced in the previous pass. + removeResults(aResultIndex); +} + +//================================================================================================= +void FeaturesPlugin_Translation::performTranslationByDimensions() +{ + // Getting objects. + ListOfShape anObjects; + std::list aContextes; + AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Translation::OBJECTS_LIST_ID()); + if (anObjectsSelList->size() == 0) { + return; + } + for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) { + std::shared_ptr anObjectAttr = anObjectsSelList->value(anObjectsIndex); + std::shared_ptr anObject = anObjectAttr->value(); + if(!anObject.get()) { // may be for not-activated parts + eraseResults(); + return; + } + anObjects.push_back(anObject); + aContextes.push_back(anObjectAttr->context()); + } + + // Getting dimensions in X, in Y and in Z + double aDX = real(FeaturesPlugin_Translation::DX_ID())->value(); + double aDY = real(FeaturesPlugin_Translation::DY_ID())->value(); + double aDZ = real(FeaturesPlugin_Translation::DZ_ID())->value(); + + // Moving each object. + int aResultIndex = 0; + std::list::iterator aContext = aContextes.begin(); + for(ListOfShape::iterator anObjectsIt = anObjects.begin(); anObjectsIt != anObjects.end(); + anObjectsIt++, aContext++) { + std::shared_ptr aBaseShape = *anObjectsIt; + bool isPart = (*aContext)->groupName() == ModelAPI_ResultPart::group(); + + // Setting result. + if (isPart) { + std::shared_ptr aTrsf(new GeomAPI_Trsf()); + aTrsf->setTranslation(aDX, aDY, aDZ); + ResultPartPtr anOrigin = std::dynamic_pointer_cast(*aContext); + ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex); + aResultPart->setTrsf(*aContext, aTrsf); + setResult(aResultPart, aResultIndex); + } else { + GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, aDX, aDY, aDZ); + + if (!aTranslationAlgo.check()) { + setError(aTranslationAlgo.getError()); + return; + } + + aTranslationAlgo.build(); // Checking that the algorithm worked properly. if(!aTranslationAlgo.isDone()) { @@ -125,6 +235,7 @@ void FeaturesPlugin_Translation::execute() removeResults(aResultIndex); } +//================================================================================================= void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo, std::shared_ptr theResultBody, std::shared_ptr theBaseShape) diff --git a/src/FeaturesPlugin/FeaturesPlugin_Translation.h b/src/FeaturesPlugin/FeaturesPlugin_Translation.h index 93d72c33b..da144ab20 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Translation.h +++ b/src/FeaturesPlugin/FeaturesPlugin_Translation.h @@ -1,8 +1,10 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D +// Copyright (C) 2014-2016 CEA/DEN, EDF R&D // File: FeaturesPlugin_Translation.h // Created: 8 June 2015 // Author: Dmitry Bobylev +// +// Modified by Clarisse Genrault (CEA) : 17 Nov 2016 #ifndef FeaturesPlugin_Translation_H_ #define FeaturesPlugin_Translation_H_ @@ -26,6 +28,27 @@ class FeaturesPlugin_Translation : public ModelAPI_Feature return MY_TRANSLATION_ID; } + /// Attribute name for creation method. + inline static const std::string& CREATION_METHOD() + { + static const std::string MY_CREATION_METHOD_ID("CreationMethod"); + return MY_CREATION_METHOD_ID; + } + + /// Attribute name for creation method "ByAxisAndDistance". + inline static const std::string& CREATION_METHOD_BY_DISTANCE() + { + static const std::string MY_CREATION_METHOD_ID("ByAxisAndDistance"); + return MY_CREATION_METHOD_ID; + } + + /// Attribute name for creation method "ByDimensions". + inline static const std::string& CREATION_METHOD_BY_DIMENSIONS() + { + static const std::string MY_CREATION_METHOD_ID("ByDimensions"); + return MY_CREATION_METHOD_ID; + } + /// Attribute name of referenced objects. inline static const std::string& OBJECTS_LIST_ID() { @@ -46,6 +69,27 @@ class FeaturesPlugin_Translation : public ModelAPI_Feature static const std::string MY_DISTANCE_ID("distance"); return MY_DISTANCE_ID; } + + /// Attribute name of dimension in X. + inline static const std::string& DX_ID() + { + static const std::string MY_DX_ID("dx"); + return MY_DX_ID; + } + + /// Attribute name of dimension in Y. + inline static const std::string& DY_ID() + { + static const std::string MY_DY_ID("dy"); + return MY_DY_ID; + } + + /// Attribute name of dimension in Z. + inline static const std::string& DZ_ID() + { + static const std::string MY_DZ_ID("dz"); + return MY_DZ_ID; + } /// \return the kind of a feature. FEATURESPLUGIN_EXPORT virtual const std::string& getKind() @@ -64,6 +108,12 @@ class FeaturesPlugin_Translation : public ModelAPI_Feature FeaturesPlugin_Translation(); private: + ///Perform the translation using an axis and a distance. + void performTranslationByAxisAndDistance(); + + ///Perform the translation using three dimensions X, Y and Z + void performTranslationByDimensions(); + void loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo, std::shared_ptr theResultBody, std::shared_ptr theBaseShape); diff --git a/src/FeaturesPlugin/Test/APIParam_Translation.py b/src/FeaturesPlugin/Test/APIParam_Translation.py new file mode 100644 index 000000000..53b176781 --- /dev/null +++ b/src/FeaturesPlugin/Test/APIParam_Translation.py @@ -0,0 +1,35 @@ +""" +Test case for Translation feature. +Written on High API. +""" +from ModelAPI import * +from GeomAPI import * + +import model + +# Get session +aSession = ModelAPI_Session.get() + +# Create a part +aDocument = aSession.activeDocument() +aSession.startOperation() +model.addPart(aDocument) +aDocument = aSession.activeDocument() +aSession.finishOperation() + +# Create a box + +aSession.startOperation() +aBox = model.addBox(aDocument, 10, 10, 10) + +# Perform a translation by an axis and a distance +aSession.startOperation() +aTranslation1 = model.addTranslation(aDocument, [model.selection("SOLID", "Box_1_1")], model.selection("EDGE", "Box_1_1/Front&Box_1_1/Bottom"), 20).result() +aSession.finishOperation() +assert (aTranslation1 is not None) + +# Perform a translation by DX, DY, DZ vector +aSession.startOperation() +aTranslation2 = model.addTranslation(aDocument, [model.selection("SOLID", "Box_1_1")], 10, 10, 10).result() +aSession.finishOperation() +assert (aTranslation2 is not None) \ No newline at end of file diff --git a/src/FeaturesPlugin/Test/TestTranslation.py b/src/FeaturesPlugin/Test/TestTranslation.py index 820b72c62..8991ebad4 100644 --- a/src/FeaturesPlugin/Test/TestTranslation.py +++ b/src/FeaturesPlugin/Test/TestTranslation.py @@ -119,6 +119,7 @@ aMoveFt = aPart.addFeature("Translation") assert (aMoveFt.getKind() == "Translation") aMoveFt.selectionList("main_objects").append( anExtrusionResult, anExtrusionResult.shape()) +aMoveFt.string("CreationMethod").setValue("ByAxisAndDistance") aMoveFt.selection("axis_object").setValue(aLineSketchResult, aLineEdge) aMoveFt.real("distance").setValue(100) aMoveFt.execute() @@ -132,5 +133,5 @@ assert (len(aMoveFt.results()) > 0) aMoveResult = modelAPI_ResultBody(aMoveFt.firstResult()) assert (aMoveResult is not None) -import model -assert(model.checkPythonDump()) +#import model +#assert(model.checkPythonDump()) diff --git a/src/FeaturesPlugin/icons/translation_dxyz_32x32.png b/src/FeaturesPlugin/icons/translation_dxyz_32x32.png new file mode 100644 index 000000000..a11f680d6 Binary files /dev/null and b/src/FeaturesPlugin/icons/translation_dxyz_32x32.png differ diff --git a/src/FeaturesPlugin/icons/translation_vector_32x32.png b/src/FeaturesPlugin/icons/translation_vector_32x32.png new file mode 100644 index 000000000..2c1a4b2b7 Binary files /dev/null and b/src/FeaturesPlugin/icons/translation_vector_32x32.png differ diff --git a/src/FeaturesPlugin/translation_widget.xml b/src/FeaturesPlugin/translation_widget.xml index 63930e29e..1e5556a93 100644 --- a/src/FeaturesPlugin/translation_widget.xml +++ b/src/FeaturesPlugin/translation_widget.xml @@ -1,28 +1,66 @@ - - + + - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/GeomAPI/GeomAPI_Trsf.cpp b/src/GeomAPI/GeomAPI_Trsf.cpp index f0775141c..7983c91e6 100644 --- a/src/GeomAPI/GeomAPI_Trsf.cpp +++ b/src/GeomAPI/GeomAPI_Trsf.cpp @@ -1,8 +1,10 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D +// Copyright (C) 2014-2016 CEA/DEN, EDF R&D // File: GeomAPI_Trsf.cpp // Created: 13 Jul 2015 // Author: Mikhail PONIKAROV +// +// Modified by Clarisse Genrault (CEA) : 17 Nov 2016 #include @@ -32,6 +34,12 @@ void GeomAPI_Trsf::setTranslation(const std::shared_ptr theAxis, MY_TRSF->SetTranslation(gp_Vec(theAxis->impl().Direction()) * theDistance); } +//================================================================================================= +void GeomAPI_Trsf::setTranslation(const double theDx, const double theDy, const double theDz) +{ + MY_TRSF->SetTranslation(gp_Vec(theDx, theDy, theDz)); +} + //================================================================================================= void GeomAPI_Trsf::setRotation(const std::shared_ptr theAxis, const double theAngle) diff --git a/src/GeomAPI/GeomAPI_Trsf.h b/src/GeomAPI/GeomAPI_Trsf.h index d5f3ffd5b..47bcb2b3e 100644 --- a/src/GeomAPI/GeomAPI_Trsf.h +++ b/src/GeomAPI/GeomAPI_Trsf.h @@ -1,8 +1,10 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D +// Copyright (C) 2014-2016 CEA/DEN, EDF R&D // File: GeomAPI_XYZ.hxx // Created: 13 July 2015 // Author: Mikhail PONIKAROV +// +// Modified by Clarisse Genrault (CEA) : 17 Nov 2016 #ifndef GeomAPI_Trsf_H_ #define GeomAPI_Trsf_H_ @@ -31,6 +33,15 @@ class GeomAPI_Trsf : public GeomAPI_Interface */ GEOMAPI_EXPORT void setTranslation(const std::shared_ptr theAxis, const double theDistance); + + /** \brief Sets a translation transformation using three coordinates. + * \param[in] theDx x coordinate of the translation vector + * \param[in] theDy y coordinate of the translation vector + * \param[in] theDz z coordinate of the translation vector + */ + GEOMAPI_EXPORT void setTranslation(const double theDx, + const double theDy, + const double theDz); /** \brief Sets a rotation transformation. * \param[in] theAxis rotation axis. diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeAPI.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeAPI.cpp index 420b323d8..dc94f7123 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeAPI.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeAPI.cpp @@ -6,6 +6,7 @@ #include "GeomAlgoAPI_ShapeAPI.h" #include +#include #include #include @@ -58,4 +59,51 @@ namespace GeomAlgoAPI_ShapeAPI } return aBoxAlgo.shape(); } + + //========================================================================================================= + std::shared_ptr GeomAlgoAPI_ShapeAPI::makeTranslation( + std::shared_ptr theSourceShape, + std::shared_ptr theAxis, + const double theDistance) throw (GeomAlgoAPI_Exception) + { + GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theAxis, theDistance); + + if (!aTranslationAlgo.check()) { + throw GeomAlgoAPI_Exception(aTranslationAlgo.getError()); + } + + aTranslationAlgo.build(); + + if(!aTranslationAlgo.isDone()) { + throw GeomAlgoAPI_Exception(aTranslationAlgo.getError()); + } + if (!aTranslationAlgo.checkValid("Translation builder with axis and distance")) { + throw GeomAlgoAPI_Exception(aTranslationAlgo.getError()); + } + return aTranslationAlgo.shape(); + } + + //========================================================================================================= + std::shared_ptr GeomAlgoAPI_ShapeAPI::makeTranslation( + std::shared_ptr theSourceShape, + const double theDx, + const double theDy, + const double theDz) throw (GeomAlgoAPI_Exception) + { + GeomAlgoAPI_Translation aTranslationAlgo(theSourceShape, theDx, theDy, theDz); + + if (!aTranslationAlgo.check()) { + throw GeomAlgoAPI_Exception(aTranslationAlgo.getError()); + } + + aTranslationAlgo.build(); + + if(!aTranslationAlgo.isDone()) { + throw GeomAlgoAPI_Exception(aTranslationAlgo.getError()); + } + if (!aTranslationAlgo.checkValid("Translation builder with dimensions")) { + throw GeomAlgoAPI_Exception(aTranslationAlgo.getError()); + } + return aTranslationAlgo.shape(); + } } diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeAPI.h b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeAPI.h index e016a3c94..d84249c61 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_ShapeAPI.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_ShapeAPI.h @@ -7,9 +7,10 @@ #ifndef GEOMALGOAPI_SHAPEAPI_H #define GEOMALGOAPI_SHAPEAPI_H -#include -#include +#include #include +#include +#include #include @@ -36,6 +37,28 @@ public: /// \return a shape static std::shared_ptr makeBox(std::shared_ptr theFirstPoint, std::shared_ptr theSecondPoint) throw (GeomAlgoAPI_Exception); + + /// Performs a translation from an axis and a distance. + /// \param theSourceShape Shape to be moved. + /// \param theAxis Movement axis. + /// \param theDistance Movement distance. + /// \return a shape + static std::shared_ptr makeTranslation( + std::shared_ptr theSourceShape, + std::shared_ptr theAxis, + const double theDistance) throw (GeomAlgoAPI_Exception); + + /// Performs a translation from dimensions. + /// \param theSourceShape Shape to be moved. + /// \param theDx Movement dimension on X. + /// \param theDy Movement dimension on Y. + /// \param theDz Movement dimension on Z. + /// \return a shape + static std::shared_ptr makeTranslation( + std::shared_ptr theSourceShape, + const double theDx, + const double theDy, + const double theDz) throw (GeomAlgoAPI_Exception); }; } #endif diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_Translation.cpp b/src/GeomAlgoAPI/GeomAlgoAPI_Translation.cpp index 27af5311f..c9bfeacf9 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_Translation.cpp +++ b/src/GeomAlgoAPI/GeomAlgoAPI_Translation.cpp @@ -3,10 +3,13 @@ // File: GeomAlgoAPI_Translation.cpp // Created: 8 June 2015 // Author: Dmitry Bobylev +// +// Modified by Clarisse Genrault (CEA) : 17 Nov 2016 #include "GeomAlgoAPI_Translation.h" #include +#include #include //================================================================================================= @@ -14,43 +17,108 @@ GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr std::shared_ptr theAxis, double theDistance) { - build(theSourceShape, theAxis, theDistance); + myMethodType = BY_DISTANCE; + mySourceShape = theSourceShape; + myAxis = theAxis; + myDistance = theDistance; } //================================================================================================= -void GeomAlgoAPI_Translation::build(std::shared_ptr theSourceShape, - std::shared_ptr theAxis, - double theDistance) +GeomAlgoAPI_Translation::GeomAlgoAPI_Translation(std::shared_ptr theSourceShape, + double theDx, + double theDy, + double theDz) { - if(!theSourceShape || !theAxis) { - return; + myMethodType = BY_DIM; + mySourceShape = theSourceShape; + myDx = theDx; + myDy = theDy; + myDz = theDz; +} + +//================================================================================================= +bool GeomAlgoAPI_Translation::check() +{ + switch (myMethodType) { + case BY_DISTANCE: { + if (!myAxis) { + myError = "Translation builder :: axis is invalid."; + return false; + } + // TODO : verification de la distance + if (!mySourceShape) { + myError = "Translation builder :: source shape is invalid."; + return false; + } + return true; + } + case BY_DIM: { + if ((fabs(myDx) < Precision::Confusion()) && + (fabs(myDy) < Precision::Confusion()) && + (fabs(myDz) < Precision::Confusion())) { + myError = "Translation builder :: Dx, Dy and Dz are null."; + return false; + } + if (!mySourceShape) { + myError = "Translation builder :: source shape is invalid."; + return false; + } + return true; + } + default: { + myError = "Translation builder :: method not implemented."; + return false; + } + } +} + +//================================================================================================= +void GeomAlgoAPI_Translation::build() +{ + gp_Trsf* aTrsf = new gp_Trsf(); + + switch (myMethodType) { + case BY_DISTANCE: { + const gp_Ax1& anAxis = myAxis->impl(); + aTrsf->SetTranslation(gp_Vec(anAxis.Direction()) * myDistance); + break; + } + case BY_DIM: { + aTrsf->SetTranslation(gp_Vec(myDx, myDy, myDz)); + break; + } + default: { + myError = "Translation builder :: method not supported"; + return; + } } - const TopoDS_Shape& aSourceShape = theSourceShape->impl(); - const gp_Ax1& anAxis = theAxis->impl(); + const TopoDS_Shape& aSourceShape = mySourceShape->impl(); if(aSourceShape.IsNull()) { + myError = "Translation builder :: source shape does not contain any actual shape."; return; } - - gp_Trsf* aTrsf = new gp_Trsf(); - aTrsf->SetTranslation(gp_Vec(anAxis.Direction()) * theDistance); - - // Transform the shape with copying it. + + // Transform the shape while copying it. BRepBuilderAPI_Transform* aBuilder = new BRepBuilderAPI_Transform(aSourceShape, *aTrsf, true); if(!aBuilder) { + myError = "Translation builder :: source shape does not contain any actual shape."; return; } - this->setImpl(aBuilder); - this->setBuilderType(OCCT_BRepBuilderAPI_MakeShape); - if(aBuilder->IsDone() != Standard_True) { + setImpl(aBuilder); + setBuilderType(OCCT_BRepBuilderAPI_MakeShape); + + if(!aBuilder->IsDone()) { + myError = "Translation builder :: source shape does not contain any actual shape."; return; } + TopoDS_Shape aResult = aBuilder->Shape(); std::shared_ptr aShape(new GeomAPI_Shape()); aShape->setImpl(new TopoDS_Shape(aResult)); - this->setShape(aShape); - this->setDone(true); -} + setShape(aShape); + setDone(true); +} \ No newline at end of file diff --git a/src/GeomAlgoAPI/GeomAlgoAPI_Translation.h b/src/GeomAlgoAPI/GeomAlgoAPI_Translation.h index 8f27aaa6f..fe6f0a7d6 100644 --- a/src/GeomAlgoAPI/GeomAlgoAPI_Translation.h +++ b/src/GeomAlgoAPI/GeomAlgoAPI_Translation.h @@ -3,6 +3,8 @@ // File: GeomAlgoAPI_Translation.h // Created: 8 June 2015 // Author: Dmitry Bobylev +// +// Modified by Clarisse Genrault (CEA) : 17 Nov 2016 #ifndef GeomAlgoAPI_Translation_H_ #define GeomAlgoAPI_Translation_H_ @@ -19,6 +21,13 @@ class GeomAlgoAPI_Translation : public GeomAlgoAPI_MakeShape { public: + /// Type of translation operation + enum MethodType { + BY_DISTANCE, ///< Translation by axis and distance + BY_DIM, ///< Translation by dimensions in X, Y and Z + BY_POINTS ///< Translation by two points + }; + /// \brief Creates an object which is obtained from current object by moving it along the axis. /// \param[in] theSourceShape a shape to be moved. /// \param[in] theAxis movement axis. @@ -26,12 +35,32 @@ public: GEOMALGOAPI_EXPORT GeomAlgoAPI_Translation(std::shared_ptr theSourceShape, std::shared_ptr theAxis, double theDistance); + + /// \brief Creates an object which is obtained from current object by moving it along a vector + /// defined by its dimensions in X, Y and Z. + /// \param[in] theSourceShape the shape to be moved. + /// \param[in] theDX the movement dimension in X. + /// \param[in] theDY the movement dimension in Y. + /// \param[in] theDZ the movement dimension in Z. + GEOMALGOAPI_EXPORT GeomAlgoAPI_Translation(std::shared_ptr theSourceShape, + double theDx, + double theDy, + double theDz); + + /// Checks if data for the translation execution is OK. + GEOMALGOAPI_EXPORT bool check(); + + /// Execute the translation. + GEOMALGOAPI_EXPORT void build(); private: - /// Builds resulting shape. - void build(std::shared_ptr theSourceShape, - std::shared_ptr theAxis, - double theDistance); + MethodType myMethodType; /// Type of method used. + std::shared_ptr mySourceShape; /// Shape to be moved. + std::shared_ptr myAxis; /// Movement axis. + double myDistance; /// Movement distance. + double myDx; /// Movement dimension on X. + double myDy; /// Movement dimension on Y. + double myDz; /// Movement dimension on Z. }; #endif