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