Salome HOME
Stabilize orientation of face built by BuildPlugin_Feature (issue #1559)
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Edge.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        BuildPlugin_Edge.cpp
4 // Created:     18 April 2016
5 // Author:      Dmitry Bobylev
6
7 #include "BuildPlugin_Edge.h"
8
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultBody.h>
11
12 #include <GeomAlgoAPI_Copy.h>
13
14 //=================================================================================================
15 BuildPlugin_Edge::BuildPlugin_Edge()
16 {
17 }
18
19 //=================================================================================================
20 void BuildPlugin_Edge::initAttributes()
21 {
22   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
23 }
24
25 //=================================================================================================
26 void BuildPlugin_Edge::execute()
27 {
28   // Get base objects list.
29   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
30   if(!aSelectionList.get()) {
31     setError("Error: Could not get selection list.");
32     return;
33   }
34   if(aSelectionList->size() == 0) {
35     setError("Error: Empty selection list.");
36     return;
37   }
38
39   // Collect base shapes.
40   ListOfShape aListOfShapes;
41   int aResultIndex = 0;
42   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
43     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
44     GeomShapePtr aShape = aSelection->value();
45     if(!aShape.get()) {
46       ResultPtr aContext = aSelection->context();
47       if(!aContext.get()) {
48         setError("Error: Attribute has empty context.");
49         return;
50       }
51
52       aShape = aContext->shape();
53     }
54     if(!aShape.get()) {
55       setError("Error: Empty shape selected.");
56       return;
57     }
58
59     if(aShape->shapeType() != GeomAPI_Shape::EDGE) {
60       setError("Error: Selected shape has wrong type. Only edges acceptable.");
61       return;
62     }
63
64     // Copy shape.
65     GeomAlgoAPI_Copy aCopyAlgo(aShape);
66
67     // Check that algo is done.
68     if(!aCopyAlgo.isDone()) {
69       setError("Error: " + getKind() + " algorithm failed.");
70       return;
71     }
72
73     // Check if shape is not null.
74     if(!aCopyAlgo.shape().get() || aCopyAlgo.shape()->isNull()) {
75       setError("Error: Resulting shape is null.");
76       return;
77     }
78
79     // Check that resulting shape is valid.
80     if(!aCopyAlgo.isValid()) {
81       setError("Error: Resulting shape is not valid.");
82       return;
83     }
84
85     // Store result.
86     ResultBodyPtr aResultBody = document()->createBody(data(), aResultIndex);
87     aResultBody->storeModified(aShape, aCopyAlgo.shape());
88     std::shared_ptr<GeomAPI_DataMapOfShapeShape> aSubShapes = aCopyAlgo.mapOfSubShapes();
89     int aModVertexTag = 1;
90     std::string aModVertexName = "Modified_Vertex";
91     aResultBody->loadAndOrientModifiedShapes(&aCopyAlgo, aShape, GeomAPI_Shape::VERTEX,
92                                              aModVertexTag, aModVertexName, *aSubShapes.get());
93
94     setResult(aResultBody, aResultIndex);
95     ++aResultIndex;
96   }
97
98   removeResults(aResultIndex);
99 }