1 // Copyright (C) 2014-2019 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "BuildPlugin_Edge.h"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_AttributeString.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_Session.h>
26 #include <ModelAPI_Validator.h>
28 #include <GeomAlgoAPI_Copy.h>
29 #include <GeomAlgoAPI_EdgeBuilder.h>
30 #include <GeomAlgoAPI_Tools.h>
32 #include <GeomAPI_Edge.h>
33 #include <GeomAPI_ShapeExplorer.h>
34 #include <GeomAPI_Vertex.h>
36 static bool getShape(const AttributeSelectionPtr theAttribute,
37 GeomShapePtr& theShape,
38 std::string& theError)
40 theShape = theAttribute->value();
41 if (!theShape.get()) {
42 ResultPtr aContext = theAttribute->context();
43 if (!aContext.get()) {
44 theError = "Error: Attribute has empty context.";
47 theShape = aContext->shape();
52 //=================================================================================================
53 BuildPlugin_Edge::BuildPlugin_Edge()
57 //=================================================================================================
58 void BuildPlugin_Edge::initAttributes()
60 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
62 data()->addAttribute(CREATION_METHOD(), ModelAPI_AttributeString::typeId());
63 ModelAPI_Session::get()->validators()->registerNotObligatory(getKind(), CREATION_METHOD());
65 data()->addAttribute(FIRST_POINT(), ModelAPI_AttributeSelection::typeId());
66 data()->addAttribute(SECOND_POINT(), ModelAPI_AttributeSelection::typeId());
69 //=================================================================================================
70 void BuildPlugin_Edge::execute()
72 AttributeStringPtr aCreationMethod = string(CREATION_METHOD());
73 if (aCreationMethod && aCreationMethod->isInitialized() &&
74 aCreationMethod->value() == CREATION_BY_POINTS())
80 //=================================================================================================
81 void BuildPlugin_Edge::edgesBySegments()
83 string(CREATION_METHOD())->setValue(CREATION_BY_SEGMENTS());
85 // Get base objects list.
86 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
87 if(!aSelectionList.get()) {
88 setError("Error: Could not get selection list.");
91 if(aSelectionList->size() == 0) {
92 setError("Error: Empty selection list.");
96 // Collect base shapes.
97 ListOfShape aListOfShapes;
100 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
101 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
103 if (!getShape(aSelection, aShape, aError)) {
108 setError("Error: Empty shape selected.");
112 if(aShape->shapeType() != GeomAPI_Shape::EDGE) {
113 setError("Error: Selected shape has wrong type. Only edges acceptable.");
118 GeomMakeShapePtr aCopyAlgo(new GeomAlgoAPI_Copy(aShape));
121 if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCopyAlgo, getKind(), anError)) {
127 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
129 ListOfShape aBaseShapes;
130 aBaseShapes.push_back(aShape);
131 aResultBody->storeModified(aBaseShapes, aCopyAlgo->shape(), aCopyAlgo);
132 aResultBody->loadModifiedShapes(aCopyAlgo, aShape, GeomAPI_Shape::VERTEX);
134 setResult(aResultBody, aResultIndex);
138 removeResults(aResultIndex);
141 void BuildPlugin_Edge::edgeByPoints()
144 AttributeSelectionPtr aFirstPointAttr = selection(FIRST_POINT());
145 AttributeSelectionPtr aSecondPointAttr = selection(SECOND_POINT());
146 if (!aFirstPointAttr.get() || !aSecondPointAttr.get()) {
147 setError("Error: Not enough points selected.");
152 GeomShapePtr aFirstShape, aSecondShape;
153 if (!getShape(aFirstPointAttr, aFirstShape, aError) ||
154 !getShape(aSecondPointAttr, aSecondShape, aError)) {
158 if (!aFirstShape.get() || !aSecondShape.get()) {
159 setError("Error: Empty shape selected.");
163 if (!aFirstShape->isVertex() || !aSecondShape->isVertex()) {
164 setError("Error: Selected shape has wrong type. Only vertices acceptable.");
168 int aResultIndex = 0;
170 GeomEdgePtr anEdge = GeomAlgoAPI_EdgeBuilder::line(aFirstShape->vertex()->point(),
171 aSecondShape->vertex()->point());
173 setError("Error: Algorithm failed.");
178 ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
179 aResultBody->store(anEdge);
180 // History of vertices
181 GeomAPI_ShapeExplorer anExp(anEdge, GeomAPI_Shape::VERTEX);
182 GeomShapePtr aStartVertex = anExp.current();
184 GeomShapePtr anEndVertex = anExp.current();
185 aResultBody->modified(aFirstShape, aStartVertex);
186 aResultBody->modified(aSecondShape, anEndVertex);
188 setResult(aResultBody, aResultIndex++);
189 removeResults(aResultIndex);