Salome HOME
Merge commit 'refs/tags/V9_2_0^{}'
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Vertex.cpp
1 // Copyright (C) 2014-2017  CEA/DEN, EDF R&D
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
19 //
20
21 #include "BuildPlugin_Vertex.h"
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultBody.h>
25
26 #include <GeomAlgoAPI_Copy.h>
27 #include <GeomAlgoAPI_Tools.h>
28
29 //=================================================================================================
30 BuildPlugin_Vertex::BuildPlugin_Vertex()
31 {
32 }
33
34 //=================================================================================================
35 void BuildPlugin_Vertex::initAttributes()
36 {
37   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
38 }
39
40 //=================================================================================================
41 void BuildPlugin_Vertex::execute()
42 {
43   // Get base objects list.
44   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
45   if(!aSelectionList.get()) {
46     setError("Error: Could not get selection list.");
47     return;
48   }
49   if(aSelectionList->size() == 0) {
50     setError("Error: Empty selection list.");
51     return;
52   }
53
54   // Collect base shapes.
55   ListOfShape aListOfShapes;
56   int aResultIndex = 0;
57   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
58     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
59     GeomShapePtr aShape = aSelection->value();
60     if(!aShape.get()) {
61       ResultPtr aContext = aSelection->context();
62       if(!aContext.get()) {
63         setError("Error: Attribute has empty context.");
64         return;
65       }
66
67       aShape = aContext->shape();
68     }
69     if(!aShape.get()) {
70       setError("Error: Empty shape selected.");
71       return;
72     }
73
74     if(aShape->shapeType() != GeomAPI_Shape::VERTEX) {
75       setError("Error: Selected shape has wrong type. Only vertices acceptable.");
76       return;
77     }
78
79     // Copy shape.
80     std::shared_ptr<GeomAlgoAPI_Copy> aCopyAlgo(new GeomAlgoAPI_Copy(aShape));
81
82     std::string anError;
83     if (GeomAlgoAPI_Tools::AlgoError::isAlgorithmFailed(aCopyAlgo, getKind(), anError)) {
84       setError(anError);
85       return;
86     }
87
88     // Store result.
89     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
90     aResultBody->storeModified(aShape, aCopyAlgo->shape());
91     setResult(aResultBody, aResultIndex);
92     ++aResultIndex;
93   }
94
95   removeResults(aResultIndex);
96 }