Salome HOME
Add copyright header according to request of CEA from 06.06.2017
[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 email : webmaster.salome@opencascade.com<mailto: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
27 //=================================================================================================
28 BuildPlugin_Vertex::BuildPlugin_Vertex()
29 {
30 }
31
32 //=================================================================================================
33 void BuildPlugin_Vertex::initAttributes()
34 {
35   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
36 }
37
38 //=================================================================================================
39 void BuildPlugin_Vertex::execute()
40 {
41   // Get base objects list.
42   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
43   if(!aSelectionList.get()) {
44     setError("Error: Could not get selection list.");
45     return;
46   }
47   if(aSelectionList->size() == 0) {
48     setError("Error: Empty selection list.");
49     return;
50   }
51
52   // Collect base shapes.
53   ListOfShape aListOfShapes;
54   int aResultIndex = 0;
55   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
56     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
57     GeomShapePtr aShape = aSelection->value();
58     if(!aShape.get()) {
59       ResultPtr aContext = aSelection->context();
60       if(!aContext.get()) {
61         setError("Error: Attribute has empty context.");
62         return;
63       }
64
65       aShape = aContext->shape();
66     }
67     if(!aShape.get()) {
68       setError("Error: Empty shape selected.");
69       return;
70     }
71
72     if(aShape->shapeType() != GeomAPI_Shape::VERTEX) {
73       setError("Error: Selected shape has wrong type. Only vertices acceptable.");
74       return;
75     }
76
77     // Copy shape.
78     GeomAlgoAPI_Copy aCopyAlgo(aShape);
79
80     // Check that algo is done.
81     if(!aCopyAlgo.isDone()) {
82       setError("Error: " + getKind() + " algorithm failed.");
83       return;
84     }
85
86     // Check if shape is not null.
87     if(!aCopyAlgo.shape().get() || aCopyAlgo.shape()->isNull()) {
88       setError("Error: Resulting shape is null.");
89       return;
90     }
91
92     // Check that resulting shape is valid.
93     if(!aCopyAlgo.isValid()) {
94       setError("Error: Resulting shape is not valid.");
95       return;
96     }
97
98     // Store result.
99     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
100     aResultBody->storeModified(aShape, aCopyAlgo.shape());
101     setResult(aResultBody, aResultIndex);
102     ++aResultIndex;
103   }
104
105   removeResults(aResultIndex);
106 }