X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FFeaturesPlugin%2FFeaturesPlugin_Translation.cpp;h=37695957df3b59afc4133ca34f8c025ccc7895cd;hb=7551606a6acc65c7ee1bc474ae36f118526c4ca6;hp=26b70c33b9c32b20976c444d538b3baa5803e2af;hpb=7aed285f4f887151149a909fbb3ec51251826f35;p=modules%2Fshaper.git diff --git a/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp b/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp index 26b70c33b..37695957d 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp @@ -1,10 +1,21 @@ -// Copyright (C) 2014-2016 CEA/DEN, EDF R&D - -// File: FeaturesPlugin_Translation.cpp -// Created: 8 June 2015 -// Author: Dmitry Bobylev +// Copyright (C) 2014-2020 CEA/DEN, EDF R&D +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +// +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -// Modified by Clarisse Genrault (CEA) : 17 Nov 2016 #include @@ -16,11 +27,22 @@ #include #include +#include #include #include +#include +#include +#include + +#include +#include +#include +#include #include +static const std::string TRANSLATION_VERSION_1("v9.5"); + //================================================================================================= FeaturesPlugin_Translation::FeaturesPlugin_Translation() { @@ -29,7 +51,8 @@ FeaturesPlugin_Translation::FeaturesPlugin_Translation() //================================================================================================= void FeaturesPlugin_Translation::initAttributes() { - data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId()); + data()->addAttribute(FeaturesPlugin_Translation::CREATION_METHOD(), + ModelAPI_AttributeString::typeId()); AttributeSelectionListPtr aSelection = std::dynamic_pointer_cast(data()->addAttribute( @@ -46,6 +69,16 @@ void FeaturesPlugin_Translation::initAttributes() ModelAPI_AttributeDouble::typeId()); data()->addAttribute(FeaturesPlugin_Translation::DZ_ID(), ModelAPI_AttributeDouble::typeId()); + + data()->addAttribute(FeaturesPlugin_Translation::START_POINT_ID(), + ModelAPI_AttributeSelection::typeId()); + data()->addAttribute(FeaturesPlugin_Translation::END_POINT_ID(), + ModelAPI_AttributeSelection::typeId()); + + if (!aSelection->isInitialized()) { + // new feature, not read from file + data()->setVersion(TRANSLATION_VERSION_1); + } } //================================================================================================= @@ -54,206 +87,162 @@ void FeaturesPlugin_Translation::execute() AttributeStringPtr aMethodTypeAttr = string(FeaturesPlugin_Translation::CREATION_METHOD()); std::string aMethodType = aMethodTypeAttr->value(); - if (aMethodType == CREATION_METHOD_BY_DISTANCE()) { - performTranslationByAxisAndDistance(); - } + GeomTrsfPtr aTrsf; + if (aMethodType == CREATION_METHOD_BY_DISTANCE()) + aTrsf = translationByAxisAndDistance(); + else if (aMethodType == CREATION_METHOD_BY_DIMENSIONS()) + aTrsf = translationByDimensions(); + else if (aMethodType == CREATION_METHOD_BY_TWO_POINTS()) + aTrsf = translationByTwoPoints(); - if (aMethodType == CREATION_METHOD_BY_DIMENSIONS()) { - performTranslationByDimensions(); - } + performTranslation(aTrsf); } //================================================================================================= -void FeaturesPlugin_Translation::performTranslationByAxisAndDistance() +GeomTrsfPtr FeaturesPlugin_Translation::translationByAxisAndDistance() { - // 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; + //Getting axis. + static const std::string aSelectionError = "Error: The axis shape selection is bad."; + AttributeSelectionPtr anObjRef = selection(AXIS_OBJECT_ID()); + GeomShapePtr aShape = anObjRef->value(); + if (!aShape.get()) { + if (anObjRef->context().get()) { + aShape = anObjRef->context()->shape(); } - anObjects.push_back(anObject); - aContextes.push_back(anObjectAttr->context()); + } + if (!aShape.get()) { + setError(aSelectionError); + return GeomTrsfPtr(); } - //Getting axis. - std::shared_ptr anAxis; - std::shared_ptr anEdge; - 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() && - anObjRef->context()->shape() && anObjRef->context()->shape()->isEdge()) { - anEdge = std::shared_ptr(new GeomAPI_Edge(anObjRef->context()->shape())); + GeomEdgePtr anEdge; + if (aShape->isEdge()) + { + anEdge = aShape->edge(); } - if(anEdge) { - anAxis = std::shared_ptr(new GeomAPI_Ax1(anEdge->line()->location(), - anEdge->line()->direction())); + else if (aShape->isCompound()) + { + GeomAPI_ShapeIterator anIt(aShape); + anEdge = anIt.current()->edge(); } + if (!anEdge.get()) + { + setError(aSelectionError); + return GeomTrsfPtr(); + } + + std::shared_ptr anAxis(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(); - 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(anAxis, aDistance); - 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, 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); + GeomTrsfPtr aTrsf(new GeomAPI_Trsf); + aTrsf->setTranslation(anAxis, aDistance); + return aTrsf; } //================================================================================================= -void FeaturesPlugin_Translation::performTranslationByDimensions() +GeomTrsfPtr FeaturesPlugin_Translation::translationByDimensions() { - // 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()) { - 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); + GeomTrsfPtr aTrsf(new GeomAPI_Trsf); + aTrsf->setTranslation(aDX, aDY, aDZ); + return aTrsf; +} + +//================================================================================================= +GeomTrsfPtr FeaturesPlugin_Translation::translationByTwoPoints() +{ + // Getting the start point and the end point + AttributeSelectionPtr aRef1 = data()->selection(FeaturesPlugin_Translation::START_POINT_ID()); + AttributeSelectionPtr aRef2 = data()->selection(FeaturesPlugin_Translation::END_POINT_ID()); + std::shared_ptr aFirstPoint; + std::shared_ptr aSecondPoint; + if ((aRef1.get() != NULL) && (aRef2.get() != NULL)) { + GeomShapePtr aShape1 = aRef1->value(); + if (!aShape1.get()) //If we can't get the points directly, try getting them from the context + aShape1 = aRef1->context()->shape(); + GeomShapePtr aShape2 = aRef2->value(); + if (!aShape2.get()) + aShape2 = aRef2->context()->shape(); + if (aShape1 && aShape2) { + aFirstPoint = GeomAlgoAPI_PointBuilder::point(aShape1); + aSecondPoint = GeomAlgoAPI_PointBuilder::point(aShape2); } - aResultIndex++; } - // Remove the rest results if there were produced in the previous pass. - removeResults(aResultIndex); + GeomTrsfPtr aTrsf(new GeomAPI_Trsf); + if (aFirstPoint && aSecondPoint) { + aTrsf->setTranslation(aFirstPoint, aSecondPoint); + } + return aTrsf; } //================================================================================================= -void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo, - std::shared_ptr theResultBody, - std::shared_ptr theBaseShape) +void FeaturesPlugin_Translation::performTranslation(const GeomTrsfPtr& theTrsf) { - // Store result. - theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape()); + if (!theTrsf) { + setError("Invalid transformation."); + return; + } + + bool isKeepSubShapes = data()->version() == TRANSLATION_VERSION_1; - int aTranslatedTag = 1; - std::string aTranslatedName = "Translated"; - std::shared_ptr aSubShapes = theTranslationAlgo.mapOfSubShapes(); + // Getting objects. + GeomAPI_ShapeHierarchy anObjects; + std::list aParts; + AttributeSelectionListPtr anObjectsSelList = selectionList(OBJECTS_LIST_ID()); + if (!FeaturesPlugin_Tools::shapesFromSelectionList( + anObjectsSelList, isKeepSubShapes, anObjects, aParts)) + return; + + std::string anError; + int aResultIndex = 0; + // Moving each part. + for (std::list::iterator aPRes = aParts.begin(); aPRes != aParts.end(); ++aPRes) { + ResultPartPtr anOrigin = std::dynamic_pointer_cast(*aPRes); + ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex); + aResultPart->setTrsf(anOrigin, theTrsf); + setResult(aResultPart, aResultIndex++); + } + + // Collect transformations for each object in a part. + std::shared_ptr aMakeShapeList(new GeomAlgoAPI_MakeShapeList); + + for (GeomAPI_ShapeHierarchy::iterator anObjectsIt = anObjects.begin(); + anObjectsIt != anObjects.end(); anObjectsIt++) { + std::shared_ptr aBaseShape = *anObjectsIt; + std::shared_ptr aTransformAlgo( + new GeomAlgoAPI_Transform(aBaseShape, theTrsf)); - FeaturesPlugin_Tools::storeModifiedShapes(theTranslationAlgo, theResultBody, - theBaseShape, aTranslatedTag, aTranslatedName, - *aSubShapes.get()); + // Checking that the algorithm worked properly. + if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTransformAlgo, getKind(), anError)) { + setError(anError); + break; + } + + anObjects.markModified(aBaseShape, aTransformAlgo->shape()); + aMakeShapeList->appendAlgo(aTransformAlgo); + } + + // Build results of the operation. + const ListOfShape& anOriginalShapes = anObjects.objects(); + ListOfShape aTopLevel; + anObjects.topLevelObjects(aTopLevel); + for (ListOfShape::iterator anIt = aTopLevel.begin(); anIt != aTopLevel.end(); ++anIt) { + //LoadNamingDS + ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); + FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, anOriginalShapes, ListOfShape(), + aMakeShapeList, *anIt, "Translated"); + setResult(aResultBody, aResultIndex++); + } + + // Remove the rest results if there were produced in the previous pass. + removeResults(aResultIndex); }