X-Git-Url: http://git.salome-platform.org/gitweb/?a=blobdiff_plain;f=src%2FBuildPlugin%2FBuildPlugin_Edge.cpp;h=0f85ae3e443842129b491d6e11ee39ea6ccaa76e;hb=65fabda84bcbe9e675ea0ce3c34c1c822255c01d;hp=253de35b573f465e337c72eac1e9af642759c138;hpb=01e7972a8204f4cba29cd9802e12086737d327d9;p=modules%2Fshaper.git diff --git a/src/BuildPlugin/BuildPlugin_Edge.cpp b/src/BuildPlugin/BuildPlugin_Edge.cpp index 253de35b5..0f85ae3e4 100644 --- a/src/BuildPlugin/BuildPlugin_Edge.cpp +++ b/src/BuildPlugin/BuildPlugin_Edge.cpp @@ -1,4 +1,4 @@ -// Copyright (C) 2014-2017 CEA/DEN, EDF R&D +// 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 @@ -12,18 +12,42 @@ // // 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 +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // -// See http://www.salome-platform.org/ or -// email : webmaster.salome@opencascade.com +// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // #include "BuildPlugin_Edge.h" #include +#include #include +#include +#include #include +#include +#include + +#include +#include +#include + +static bool getShape(const AttributeSelectionPtr theAttribute, + GeomShapePtr& theShape, + std::string& theError) +{ + theShape = theAttribute->value(); + if (!theShape.get()) { + ResultPtr aContext = theAttribute->context(); + if (!aContext.get()) { + theError = "Error: Attribute has empty context."; + return false; + } + theShape = aContext->shape(); + } + return true; +} //================================================================================================= BuildPlugin_Edge::BuildPlugin_Edge() @@ -34,11 +58,30 @@ BuildPlugin_Edge::BuildPlugin_Edge() void BuildPlugin_Edge::initAttributes() { data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId()); + + data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId()); + ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CREATION_METHOD()); + + data()->addAttribute(FIRST_POINT(), ModelAPI_AttributeSelection::typeId()); + data()->addAttribute(SECOND_POINT(), ModelAPI_AttributeSelection::typeId()); } //================================================================================================= void BuildPlugin_Edge::execute() { + AttributeStringPtr aCreationMethod = string(CREATION_METHOD()); + if (aCreationMethod && aCreationMethod->isInitialized() && + aCreationMethod->value() == CREATION_BY_POINTS()) + edgeByPoints(); + else + edgesBySegments(); +} + +//================================================================================================= +void BuildPlugin_Edge::edgesBySegments() +{ + string(CREATION_METHOD())->setValue(CREATION_BY_SEGMENTS()); + // Get base objects list. AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID()); if(!aSelectionList.get()) { @@ -52,18 +95,14 @@ void BuildPlugin_Edge::execute() // Collect base shapes. ListOfShape aListOfShapes; + std::string aError; int aResultIndex = 0; for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) { AttributeSelectionPtr aSelection = aSelectionList->value(anIndex); - GeomShapePtr aShape = aSelection->value(); - if(!aShape.get()) { - ResultPtr aContext = aSelection->context(); - if(!aContext.get()) { - setError("Error: Attribute has empty context."); - return; - } - - aShape = aContext->shape(); + GeomShapePtr aShape; + if (!getShape(aSelection, aShape, aError)) { + setError(aError); + return; } if(!aShape.get()) { setError("Error: Empty shape selected."); @@ -76,35 +115,21 @@ void BuildPlugin_Edge::execute() } // Copy shape. - GeomAlgoAPI_Copy aCopyAlgo(aShape); - - // Check that algo is done. - if(!aCopyAlgo.isDone()) { - setError("Error: " + getKind() + " algorithm failed."); - return; - } - - // Check if shape is not null. - if(!aCopyAlgo.shape().get() || aCopyAlgo.shape()->isNull()) { - setError("Error: Resulting shape is null."); - return; - } + GeomMakeShapePtr aCopyAlgo(new GeomAlgoAPI_Copy(aShape)); - // Check that resulting shape is valid. - if(!aCopyAlgo.isValid()) { - setError("Error: Resulting shape is not valid."); + std::string anError; + if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCopyAlgo, getKind(), anError)) { + setError(anError); return; } // Store result. ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); - aResultBody->storeModified(aShape, aCopyAlgo.shape()); - std::shared_ptr aSubShapes = aCopyAlgo.mapOfSubShapes(); - int aModVertexTag = 1; - std::string aModVertexName = "Modified_Vertex"; - aResultBody->loadAndOrientModifiedShapes(&aCopyAlgo, aShape, GeomAPI_Shape::VERTEX, - aModVertexTag, aModVertexName, *aSubShapes.get(), - true); + + ListOfShape aBaseShapes; + aBaseShapes.push_back(aShape); + aResultBody->storeModified(aBaseShapes, aCopyAlgo->shape(), aCopyAlgo); + aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::VERTEX); setResult(aResultBody, aResultIndex); ++aResultIndex; @@ -112,3 +137,54 @@ void BuildPlugin_Edge::execute() removeResults(aResultIndex); } + +void BuildPlugin_Edge::edgeByPoints() +{ + // Get base points. + AttributeSelectionPtr aFirstPointAttr = selection(FIRST_POINT()); + AttributeSelectionPtr aSecondPointAttr = selection(SECOND_POINT()); + if (!aFirstPointAttr.get() || !aSecondPointAttr.get()) { + setError("Error: Not enough points selected."); + return; + } + + std::string aError; + GeomShapePtr aFirstShape, aSecondShape; + if (!getShape(aFirstPointAttr, aFirstShape, aError) || + !getShape(aSecondPointAttr, aSecondShape, aError)) { + setError(aError); + return; + } + if (!aFirstShape.get() || !aSecondShape.get()) { + setError("Error: Empty shape selected."); + return; + } + + if (!aFirstShape->isVertex() || !aSecondShape->isVertex()) { + setError("Error: Selected shape has wrong type. Only vertices acceptable."); + return; + } + + int aResultIndex = 0; + + GeomEdgePtr anEdge = GeomAlgoAPI_EdgeBuilder::line(aFirstShape->vertex()->point(), + aSecondShape->vertex()->point()); + if (!anEdge.get()) { + setError("Error: Algorithm failed."); + return; + } + + // Store result. + ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex); + aResultBody->store(anEdge); + // History of vertices + GeomAPI_ShapeExplorer anExp(anEdge, GeomAPI_Shape::VERTEX); + GeomShapePtr aStartVertex = anExp.current(); + anExp.next(); + GeomShapePtr anEndVertex = anExp.current(); + aResultBody->modified(aFirstShape, aStartVertex); + aResultBody->modified(aSecondShape, anEndVertex); + + setResult(aResultBody, aResultIndex++); + removeResults(aResultIndex); +}