X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FFeaturesPlugin%2FFeaturesPlugin_Translation.cpp;h=eff54dc689f759480f280cca2cc47ef3a11f5a39;hb=c6071088b2f55168f5706ce97802095af868b5e3;hp=41a93ddba767bfc57799a41b1967e4a253ee333c;hpb=bbda7e734357dc8f5e16ab7affb48f75e6e8e3c6;p=modules%2Fshaper.git diff --git a/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp b/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp index 41a93ddba..eff54dc68 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Translation.cpp @@ -1,13 +1,27 @@ -// Copyright (C) 2014-20xx CEA/DEN, EDF R&D - -// File: FeaturesPlugin_Translation.cpp -// Created: 8 June 2015 -// Author: Dmitry Bobylev +// Copyright (C) 2014-2019 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 +// #include #include #include +#include #include #include #include @@ -15,6 +29,13 @@ #include #include +#include +#include + +#include +#include + +#include //================================================================================================= FeaturesPlugin_Translation::FeaturesPlugin_Translation() @@ -24,31 +45,66 @@ FeaturesPlugin_Translation::FeaturesPlugin_Translation() //================================================================================================= void FeaturesPlugin_Translation::initAttributes() { - AttributeSelectionListPtr aSelection = + data()->addAttribute(FeaturesPlugin_Translation::CREATION_METHOD(), + ModelAPI_AttributeString::typeId()); + + AttributeSelectionListPtr aSelection = std::dynamic_pointer_cast(data()->addAttribute( FeaturesPlugin_Translation::OBJECTS_LIST_ID(), ModelAPI_AttributeSelectionList::typeId())); - // revolution works with faces always - aSelection->setSelectionType("SOLID"); - data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(), ModelAPI_AttributeSelection::typeId()); - data()->addAttribute(FeaturesPlugin_Translation::DISTANCE_ID(), ModelAPI_AttributeDouble::typeId()); + data()->addAttribute(FeaturesPlugin_Translation::AXIS_OBJECT_ID(), + 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()); + + data()->addAttribute(FeaturesPlugin_Translation::START_POINT_ID(), + ModelAPI_AttributeSelection::typeId()); + data()->addAttribute(FeaturesPlugin_Translation::END_POINT_ID(), + ModelAPI_AttributeSelection::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(); + } + + if (aMethodType == CREATION_METHOD_BY_TWO_POINTS()) { + performTranslationByTwoPoints(); + } +} + +//================================================================================================= +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(); return; } anObjects.push_back(anObject); @@ -56,29 +112,51 @@ void FeaturesPlugin_Translation::execute() } //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())); + 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(); + } + } + if (!aShape.get()) { + setError(aSelectionError); + return; + } + + GeomEdgePtr anEdge; + if (aShape->isEdge()) + { + anEdge = aShape->edge(); + } + else if (aShape->isCompound()) + { + GeomAPI_ShapeIterator anIt(aShape); + anEdge = anIt.current()->edge(); } - if(anEdge) { - anAxis = std::shared_ptr(new GeomAPI_Ax1(anEdge->line()->location(), anEdge->line()->direction())); + + if (!anEdge.get()) + { + setError(aSelectionError); + return; } + 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. + std::string anError; 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(); + bool isPart = aContext->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group(); // Setting result. if (isPart) { @@ -87,29 +165,114 @@ void FeaturesPlugin_Translation::execute() ResultPartPtr anOrigin = std::dynamic_pointer_cast(*aContext); ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex); aResultPart->setTrsf(*aContext, aTrsf); - setResult(aResultPart); + setResult(aResultPart, aResultIndex); } else { - GeomAlgoAPI_Translation aTranslationAlgo(aBaseShape, anAxis, aDistance); + std::shared_ptr aTranslationAlgo( + new GeomAlgoAPI_Translation(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 = "Translation algorithm failed"; - setError(aFeatureError); + if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) { + setError(anError); break; } - if(aTranslationAlgo.shape()->isNull()) { - static const std::string aShapeError = "Resulting shape is Null"; - setError(aShapeError); - break; + + ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); + + ListOfShape aShapes; + aShapes.push_back(aBaseShape); + FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, + aShapes, + ListOfShape(), + aTranslationAlgo, + aTranslationAlgo->shape(), + "Translated"); + 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 + 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. + std::string anError; + 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->get() && (*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 { + std::shared_ptr aTranslationAlgo( + new GeomAlgoAPI_Translation(aBaseShape, aDX, aDY, aDZ)); + + if (!aTranslationAlgo->check()) { + setError(aTranslationAlgo->getError()); + return; } - if(!aTranslationAlgo.isValid()) { - std::string aFeatureError = "Warning: resulting shape is not valid"; - setError(aFeatureError); + + aTranslationAlgo->build(); + + // Checking that the algorithm worked properly. + if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) { + setError(anError); break; } ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); - loadNamingDS(aTranslationAlgo, aResultBody, aBaseShape); + + ListOfShape aShapes; + aShapes.push_back(aBaseShape); + FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, + aShapes, + ListOfShape(), + aTranslationAlgo, + aTranslationAlgo->shape(), + "Translated"); setResult(aResultBody, aResultIndex); } aResultIndex++; @@ -119,19 +282,95 @@ void FeaturesPlugin_Translation::execute() removeResults(aResultIndex); } -void FeaturesPlugin_Translation::loadNamingDS(GeomAlgoAPI_Translation& theTranslationAlgo, - std::shared_ptr theResultBody, - std::shared_ptr theBaseShape) +//================================================================================================= +void FeaturesPlugin_Translation::performTranslationByTwoPoints() { - // Store result. - theResultBody->storeModified(theBaseShape, theTranslationAlgo.shape()); + // 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 + return; + } + anObjects.push_back(anObject); + aContextes.push_back(anObjectAttr->context()); + } - std::shared_ptr aSubShapes = theTranslationAlgo.mapOfSubShapes(); + // 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); + } + } + + // Moving each object. + std::string anError; + 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->get() && (*aContext)->groupName() == ModelAPI_ResultPart::group(); - int aTranslatedTag = 1; - std::string aTranslatedName = "Translated"; - theResultBody->loadAndOrientModifiedShapes(&theTranslationAlgo, - theBaseShape, GeomAPI_Shape::FACE, - aTranslatedTag, aTranslatedName, *aSubShapes.get()); + // Setting result. + if (isPart) { + std::shared_ptr aTrsf(new GeomAPI_Trsf()); + aTrsf->setTranslation(aFirstPoint, aSecondPoint); + ResultPartPtr anOrigin = std::dynamic_pointer_cast(*aContext); + ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex); + aResultPart->setTrsf(*aContext, aTrsf); + setResult(aResultPart, aResultIndex); + } else { + std::shared_ptr aTranslationAlgo( + new GeomAlgoAPI_Translation(aBaseShape, aFirstPoint, aSecondPoint)); + if (!aTranslationAlgo->check()) { + setError(aTranslationAlgo->getError()); + return; + } + + aTranslationAlgo->build(); + + // Checking that the algorithm worked properly. + if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aTranslationAlgo, getKind(), anError)) { + setError(anError); + break; + } + + ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); + + ListOfShape aShapes; + aShapes.push_back(aBaseShape); + FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, + aShapes, + ListOfShape(), + aTranslationAlgo, + aTranslationAlgo->shape(), + "Translated"); + setResult(aResultBody, aResultIndex); + } + aResultIndex++; + } + + // Remove the rest results if there were produced in the previous pass. + removeResults(aResultIndex); }