X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FFeaturesPlugin%2FFeaturesPlugin_Symmetry.cpp;h=a243e55f30b0137cf373080a386e9d27a515da81;hb=97917d3698f5a2f7fc9596e7c755ff8f6751e373;hp=d254eeef70bc3e9ff461c135abc20d7a2a1b06d0;hpb=5f971a92f964594a68189c5fd7c8131758e8c20a;p=modules%2Fshaper.git diff --git a/src/FeaturesPlugin/FeaturesPlugin_Symmetry.cpp b/src/FeaturesPlugin/FeaturesPlugin_Symmetry.cpp index d254eeef7..a243e55f3 100644 --- a/src/FeaturesPlugin/FeaturesPlugin_Symmetry.cpp +++ b/src/FeaturesPlugin/FeaturesPlugin_Symmetry.cpp @@ -1,25 +1,45 @@ -// Copyright (C) 2014-2016 CEA/DEN, EDF R&D - -// File: FeaturesPlugin_Symmetry.cpp -// Created: 30 Nov 2016 -// Author: Clarisse Genrault (CEA) +// 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 #include #include #include #include +#include +#include +#include #include #include #include #include -#include -#include +#include //================================================================================================= FeaturesPlugin_Symmetry::FeaturesPlugin_Symmetry() @@ -44,6 +64,9 @@ void FeaturesPlugin_Symmetry::initAttributes() data()->addAttribute(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID(), ModelAPI_AttributeSelection::typeId()); + + data()->addAttribute(FeaturesPlugin_Symmetry::KEEP_ORIGINAL_RESULT(), + ModelAPI_AttributeBoolean::typeId()); } //================================================================================================= @@ -66,27 +89,35 @@ void FeaturesPlugin_Symmetry::execute() } //================================================================================================= -void FeaturesPlugin_Symmetry::performSymmetryByPoint() +bool FeaturesPlugin_Symmetry::collectSourceObjects(ListOfShape& theSourceShapes, + std::list& theSourceResults) { - // Getting objects. - ListOfShape anObjects; - std::list aContextes; AttributeSelectionListPtr anObjectsSelList = selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID()); if (anObjectsSelList->size() == 0) { - return; + return false; } - for(int anObjectsIndex = 0; anObjectsIndex < anObjectsSelList->size(); anObjectsIndex++) { + 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; + if (!anObject.get()) { // may be for not-activated parts + return false; } - anObjects.push_back(anObject); - aContextes.push_back(anObjectAttr->context()); + theSourceShapes.push_back(anObject); + theSourceResults.push_back(anObjectAttr->context()); } + return true; +} + +//================================================================================================= +void FeaturesPlugin_Symmetry::performSymmetryByPoint() +{ + // Getting objects. + ListOfShape anObjects; + std::list aContextes; + if (!collectSourceObjects(anObjects, aContextes)) + return; //Getting point. std::shared_ptr aPoint; @@ -103,51 +134,39 @@ void FeaturesPlugin_Symmetry::performSymmetryByPoint() } // 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) { std::shared_ptr aTrsf(new GeomAPI_Trsf()); aTrsf->setSymmetry(aPoint); ResultPartPtr anOrigin = std::dynamic_pointer_cast(*aContext); - ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex); - aResultPart->setTrsf(*aContext, aTrsf); - setResult(aResultPart, aResultIndex); - } else { - GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPoint); + buildResult(anOrigin, aTrsf, aResultIndex); + } + else { + std::shared_ptr aSymmetryAlgo( + new GeomAlgoAPI_Symmetry(aBaseShape, aPoint)); - if (!aSymmetryAlgo.check()) { - setError(aSymmetryAlgo.getError()); + if (!aSymmetryAlgo->check()) { + setError(aSymmetryAlgo->getError()); return; } - aSymmetryAlgo.build(); + aSymmetryAlgo->build(); // Checking that the algorithm worked properly. - if(!aSymmetryAlgo.isDone()) { - static const std::string aFeatureError = "Error: Symmetry algorithm failed."; - setError(aFeatureError); - break; - } - if(aSymmetryAlgo.shape()->isNull()) { - static const std::string aShapeError = "Error: Resulting shape is Null."; - setError(aShapeError); - break; - } - if(!aSymmetryAlgo.isValid()) { - std::string aFeatureError = "Error: Resulting shape is not valid."; - setError(aFeatureError); + if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, getKind(), anError)) { + setError(anError); break; } - ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); - loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape); - setResult(aResultBody, aResultIndex); + buildResult(aSymmetryAlgo, aBaseShape, aResultIndex); } aResultIndex++; } @@ -162,85 +181,78 @@ void FeaturesPlugin_Symmetry::performSymmetryByAxis() // Getting objects. ListOfShape anObjects; std::list aContextes; - AttributeSelectionListPtr anObjectsSelList = - selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID()); - if (anObjectsSelList->size() == 0) { + if (!collectSourceObjects(anObjects, aContextes)) 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; } - //Getting axis. - std::shared_ptr anAxis; - std::shared_ptr anEdge; - std::shared_ptr anObjRef = - selection(FeaturesPlugin_Symmetry::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; + } + + std::shared_ptr anAxis (new GeomAPI_Ax1(anEdge->line()->location(), + anEdge->line()->direction())); + + // 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) { std::shared_ptr aTrsf(new GeomAPI_Trsf()); aTrsf->setSymmetry(anAxis); ResultPartPtr anOrigin = std::dynamic_pointer_cast(*aContext); - ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex); - aResultPart->setTrsf(*aContext, aTrsf); - setResult(aResultPart, aResultIndex); - } else { - GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, anAxis); + buildResult(anOrigin, aTrsf, aResultIndex); + } + else { + std::shared_ptr aSymmetryAlgo( + new GeomAlgoAPI_Symmetry(aBaseShape, anAxis)); - if (!aSymmetryAlgo.check()) { - setError(aSymmetryAlgo.getError()); + if (!aSymmetryAlgo->check()) { + setError(aSymmetryAlgo->getError()); return; } - aSymmetryAlgo.build(); + aSymmetryAlgo->build(); // Checking that the algorithm worked properly. - if(!aSymmetryAlgo.isDone()) { - static const std::string aFeatureError = "Error: Symmetry algorithm failed."; - setError(aFeatureError); - break; - } - if(aSymmetryAlgo.shape()->isNull()) { - static const std::string aShapeError = "Error: Resulting shape is Null."; - setError(aShapeError); - break; - } - if(!aSymmetryAlgo.isValid()) { - std::string aFeatureError = "Error: Resulting shape is not valid."; - setError(aFeatureError); + if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, getKind(), anError)) { + setError(anError); break; } - ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); - loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape); - setResult(aResultBody, aResultIndex); + buildResult(aSymmetryAlgo, aBaseShape, aResultIndex); } aResultIndex++; } @@ -255,87 +267,77 @@ void FeaturesPlugin_Symmetry::performSymmetryByPlane() // Getting objects. ListOfShape anObjects; std::list aContextes; - AttributeSelectionListPtr anObjectsSelList = - selectionList(FeaturesPlugin_Symmetry::OBJECTS_LIST_ID()); - if (anObjectsSelList->size() == 0) { + if (!collectSourceObjects(anObjects, aContextes)) 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 plane. + static const std::string aSelectionError = "Error: The plane shape selection is bad."; + AttributeSelectionPtr anObjRef = selection(PLANE_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; } - //Getting axis. - std::shared_ptr aPlane; - std::shared_ptr aPln; - std::shared_ptr anObjRef = - selection(FeaturesPlugin_Symmetry::PLANE_OBJECT_ID()); - if (anObjRef && anObjRef->value() && anObjRef->value()->isFace()) { - aPln = std::shared_ptr(new GeomAPI_Face(anObjRef->value()))->getPlane(); + GeomFacePtr aFace; + if (aShape->isFace()) + { + aFace = aShape->face(); } - else if (anObjRef && !anObjRef->value() && anObjRef->context() && - anObjRef->context()->shape() && anObjRef->context()->shape()->isFace()) { - aPln = - std::shared_ptr(new GeomAPI_Face(anObjRef->context()->shape()))->getPlane(); + else if (aShape->isCompound()) + { + GeomAPI_ShapeIterator anIt(aShape); + aFace = anIt.current()->face(); } - if (aPln) { - aPlane = std::shared_ptr(new GeomAPI_Ax2(aPln->location(), - aPln->direction())); + + if (!aFace.get()) + { + setError(aSelectionError); + return; } + std::shared_ptr aPlane(new GeomAPI_Ax2(aFace->getPlane()->location(), + aFace->getPlane()->direction())); + + // 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) { std::shared_ptr aTrsf(new GeomAPI_Trsf()); aTrsf->setSymmetry(aPlane); ResultPartPtr anOrigin = std::dynamic_pointer_cast(*aContext); - ResultPartPtr aResultPart = document()->copyPart(anOrigin, data(), aResultIndex); - aResultPart->setTrsf(*aContext, aTrsf); - setResult(aResultPart, aResultIndex); + buildResult(anOrigin, aTrsf, aResultIndex); } else { - GeomAlgoAPI_Symmetry aSymmetryAlgo(aBaseShape, aPlane); + std::shared_ptr aSymmetryAlgo( + new GeomAlgoAPI_Symmetry(aBaseShape, aPlane)); - if (!aSymmetryAlgo.check()) { - setError(aSymmetryAlgo.getError()); + if (!aSymmetryAlgo->check()) { + setError(aSymmetryAlgo->getError()); return; } - aSymmetryAlgo.build(); + aSymmetryAlgo->build(); // Checking that the algorithm worked properly. - if(!aSymmetryAlgo.isDone()) { - static const std::string aFeatureError = "Error: Symmetry algorithm failed."; - setError(aFeatureError); - break; - } - if(aSymmetryAlgo.shape()->isNull()) { - static const std::string aShapeError = "Error: Resulting shape is Null."; - setError(aShapeError); - break; - } - if(!aSymmetryAlgo.isValid()) { - std::string aFeatureError = "Error: Resulting shape is not valid."; - setError(aFeatureError); + if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aSymmetryAlgo, getKind(), anError)) { + setError(anError); break; } - ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); - loadNamingDS(aSymmetryAlgo, aResultBody, aBaseShape); - setResult(aResultBody, aResultIndex); + buildResult(aSymmetryAlgo, aBaseShape, aResultIndex); } aResultIndex++; } @@ -345,18 +347,47 @@ void FeaturesPlugin_Symmetry::performSymmetryByPlane() } //================================================================================================= -void FeaturesPlugin_Symmetry::loadNamingDS(GeomAlgoAPI_Symmetry& theSymmetryAlgo, - std::shared_ptr theResultBody, - std::shared_ptr theBaseShape) +void FeaturesPlugin_Symmetry::buildResult( + std::shared_ptr& theSymmetryAlgo, + std::shared_ptr theBaseShape, int theResultIndex) { + std::shared_ptr anAlgoList(new GeomAlgoAPI_MakeShapeList()); + anAlgoList->appendAlgo(theSymmetryAlgo); + // Compose source shape and the result of symmetry. + GeomShapePtr aCompound; + if (boolean(KEEP_ORIGINAL_RESULT())->value()) { + ListOfShape aShapes; + // add a copy of a base shape otherwise selection of this base shape is bad (2592) + std::shared_ptr aCopyAlgo(new GeomAlgoAPI_Copy(theBaseShape)); + aShapes.push_back(aCopyAlgo->shape()); + anAlgoList->appendAlgo(aCopyAlgo); + + aShapes.push_back(theSymmetryAlgo->shape()); + aCompound = GeomAlgoAPI_CompoundBuilder::compound(aShapes); + } else + aCompound = theSymmetryAlgo->shape(); + // Store and name the result. - theResultBody->storeModified(theBaseShape, theSymmetryAlgo.shape()); - - // Name the faces - std::shared_ptr aSubShapes = theSymmetryAlgo.mapOfSubShapes(); - int aReflectedTag = 1; - std::string aReflectedName = "Symmetried"; - theResultBody->loadAndOrientModifiedShapes(&theSymmetryAlgo, - theBaseShape, GeomAPI_Shape::FACE, - aReflectedTag, aReflectedName, *aSubShapes.get()); + ResultBodyPtr aResultBody = document()->createBody(data(), theResultIndex); + aResultBody->storeModified(theBaseShape, aCompound); + FeaturesPlugin_Tools::loadModifiedShapes(aResultBody, theBaseShape, anAlgoList, "Symmetried"); + setResult(aResultBody, theResultIndex); +} + +//================================================================================================= +void FeaturesPlugin_Symmetry::buildResult(ResultPartPtr theOriginal, + std::shared_ptr theTrsf, + int& theResultIndex) +{ + if (boolean(KEEP_ORIGINAL_RESULT())->value()) { + std::shared_ptr anIdentity(new GeomAPI_Trsf()); + ResultPartPtr aCopy = document()->copyPart(theOriginal, data(), theResultIndex); + aCopy->setTrsf(theOriginal, anIdentity); + setResult(aCopy, theResultIndex); + ++theResultIndex; + } + + ResultPartPtr aResultPart = document()->copyPart(theOriginal, data(), theResultIndex); + aResultPart->setTrsf(theOriginal, theTrsf); + setResult(aResultPart, theResultIndex); }