Salome HOME
Fix for the issue #2439 : Naming of vertices incorrect after build an edge
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Edge.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_Edge.h"
22
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultBody.h>
25
26 #include <GeomAlgoAPI_Copy.h>
27
28 //=================================================================================================
29 BuildPlugin_Edge::BuildPlugin_Edge()
30 {
31 }
32
33 //=================================================================================================
34 void BuildPlugin_Edge::initAttributes()
35 {
36   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
37 }
38
39 //=================================================================================================
40 void BuildPlugin_Edge::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::EDGE) {
74       setError("Error: Selected shape has wrong type. Only edges acceptable.");
75       return;
76     }
77
78     // Copy shape.
79     GeomAlgoAPI_Copy aCopyAlgo(aShape);
80
81     // Check that algo is done.
82     if(!aCopyAlgo.isDone()) {
83       setError("Error: " + getKind() + " algorithm failed.");
84       return;
85     }
86
87     // Check if shape is not null.
88     if(!aCopyAlgo.shape().get() || aCopyAlgo.shape()->isNull()) {
89       setError("Error: Resulting shape is null.");
90       return;
91     }
92
93     // Check that resulting shape is valid.
94     if(!aCopyAlgo.isValid()) {
95       setError("Error: Resulting shape is not valid.");
96       return;
97     }
98
99     // Store result.
100     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
101     aResultBody->storeModified(aShape, aCopyAlgo.shape());
102     std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = aCopyAlgo.mapOfSubShapes();
103     int aModVertexTag = 1;
104     std::string aModVertexName = "Modified_Vertex";
105     aResultBody->loadAndOrientModifiedShapes(&aCopyAlgo, aShape, GeomAPI_Shape::VERTEX,
106                                              aModVertexTag, aModVertexName, *aSubShapes.get(),
107                                              true);
108
109     setResult(aResultBody, aResultIndex);
110     ++aResultIndex;
111   }
112
113   removeResults(aResultIndex);
114 }