Salome HOME
Issue #1369: Removed redundant includes in BuildPlugin_Face.cpp, fixed naming for...
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Wire.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        BuildPlugin_Wire.cpp
4 // Created:     14 April 2016
5 // Author:      Dmitry Bobylev
6
7 #include "BuildPlugin_Wire.h"
8
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_ResultConstruction.h>
12
13 #include <Events_Error.h>
14
15 #include <GeomAPI_PlanarEdges.h>
16 #include <GeomAPI_ShapeExplorer.h>
17
18 #include <GeomAlgoAPI_ShapeTools.h>
19 #include <GeomAlgoAPI_WireBuilder.h>
20
21 #include <algorithm>
22
23 //=================================================================================================
24 BuildPlugin_Wire::BuildPlugin_Wire()
25 {
26 }
27
28 //=================================================================================================
29 void BuildPlugin_Wire::initAttributes()
30 {
31   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
32 }
33
34 //=================================================================================================
35 void BuildPlugin_Wire::execute()
36 {
37   // Get base objects list.
38   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
39   if(!aSelectionList.get()) {
40     setError("Error: Could not get selection list.");
41     return;
42   }
43   if(aSelectionList->size() == 0) {
44     setError("Error: Empty selection list.");
45     return;
46   }
47
48   // Collect base shapes.
49   ListOfShape anEdges;
50   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
51     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
52     GeomShapePtr aShape = aSelection->value();
53     if(!aShape.get()) {
54       aShape = aSelection->context()->shape();
55     }
56     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
57       GeomShapePtr anEdge = anExp.current();
58       anEdges.push_back(anEdge);
59     }
60   }
61
62   // Create wire.
63   GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(anEdges);
64   if(!aWire.get()) {
65     setError("Error: Result wire is empty. Probably it has disconnected edges or non-manifold.");
66     return;
67   }
68
69   // Store result.
70   ResultBodyPtr aResultBody = document()->createBody(data());
71   aResultBody->store(aWire);
72   for(GeomAPI_ShapeExplorer anExp(aWire, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
73     GeomShapePtr anEdgeInResult = anExp.current();
74     for(ListOfShape::const_iterator anIt = anEdges.cbegin(); anIt != anEdges.cend(); ++anIt) {
75       std::shared_ptr<GeomAPI_Edge> anEdgeInList(new GeomAPI_Edge(*anIt));
76       if(anEdgeInList->isEqual(anEdgeInResult)) {
77         aResultBody->modified(anEdgeInList, anEdgeInResult, "Edge");
78         break;
79       }
80     }
81   }
82   setResult(aResultBody);
83 }
84
85 //=================================================================================================
86 bool BuildPlugin_Wire::customAction(const std::string& theActionId)
87 {
88   if(theActionId == "add_contour") {
89     return addContour();
90   } else {
91     Events_Error::send("Error: Feature \"" + getKind() + "\" does not support action \"" + theActionId + "\".");
92   }
93
94   return false;
95 }
96
97 //=================================================================================================
98 bool BuildPlugin_Wire::addContour()
99 {
100   // Get base objects list.
101   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
102   if(aSelectionList->size() == 0) {
103     Events_Error::send("Error: Empty selection list.");
104     return false;
105   }
106
107   // Collect attributes to check.
108   ListOfShape anAddedEdges;
109   std::list<AttributeSelectionPtr> anAttributesToCheck;
110   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
111     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
112     GeomShapePtr anEdgeInList = aSelection->value();
113     if(!anEdgeInList.get()) {
114       continue;
115     }
116
117     // Check that it is edge.
118     if(anEdgeInList->shapeType() != GeomAPI_Shape::EDGE) {
119       continue;
120     }
121
122     // Check that it is edge on sketch.
123     ResultPtr aContext = aSelection->context();
124     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
125     if(!aConstruction.get()) {
126       continue;
127     }
128     GeomShapePtr aContextShape = aConstruction->shape();
129     std::shared_ptr<GeomAPI_PlanarEdges> aPlanarEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
130     if(!aPlanarEdges.get()) {
131       continue;
132     }
133
134     // Check that sketch have faces.
135     if(aConstruction->facesNum() == 0) {
136       continue;
137     }
138
139     anAddedEdges.push_back(anEdgeInList);
140     anAttributesToCheck.push_back(aSelection);
141   }
142
143   // Check if edges have contours.
144   bool isAnyContourAdded = false;
145   for(std::list<AttributeSelectionPtr>::const_iterator aListIt = anAttributesToCheck.cbegin();
146       aListIt != anAttributesToCheck.cend();
147       ++aListIt) {
148     AttributeSelectionPtr aSelection = *aListIt;
149     std::shared_ptr<GeomAPI_Edge> anEdgeInList(new GeomAPI_Edge(aSelection->value()));
150
151     ListOfShape::const_iterator anEdgesIt = anAddedEdges.cbegin();
152     for(; anEdgesIt != anAddedEdges.cend(); ++anEdgesIt) {
153       if(anEdgeInList->isEqual(*anEdgesIt)) {
154         break;
155       }
156     }
157
158     ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
159     std::shared_ptr<GeomAPI_PlanarEdges> aPlanarEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aConstruction->shape());
160
161     // Iterate on faces and add face with this edge.
162     std::shared_ptr<GeomAPI_Face> aFoundFace;
163     for(int anIndex = 0; anIndex < aConstruction->facesNum(); ++anIndex) {
164       std::shared_ptr<GeomAPI_Face> aFace = aConstruction->face(anIndex);
165       for(GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
166         std::shared_ptr<GeomAPI_Edge> anEdgeOnFace(new GeomAPI_Edge(anExp.current()));
167         if(anEdgeInList->isEqual(anEdgeOnFace)) {
168           aFoundFace = aFace;
169           break;
170         }
171       }
172
173       if(aFoundFace.get()) {
174         break;
175       }
176     }
177
178     // If face with the same edge found. Add all other edges to list.
179     if(aFoundFace.get()) {
180       for(GeomAPI_ShapeExplorer anExp(aFoundFace, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
181         std::shared_ptr<GeomAPI_Edge> anEdgeOnFace(new GeomAPI_Edge(anExp.current()));
182         anEdgesIt = anAddedEdges.cbegin();
183         for(; anEdgesIt != anAddedEdges.cend(); ++anEdgesIt) {
184           if(anEdgeOnFace->isEqual(*anEdgesIt)) {
185             break;
186           }
187         }
188         if(anEdgesIt == anAddedEdges.cend()) {
189           isAnyContourAdded = true;
190           anAddedEdges.push_back(anEdgeOnFace);
191           aSelectionList->append(aConstruction, anEdgeOnFace);
192         }
193       }
194     }
195   }
196
197   if(!isAnyContourAdded) {
198     Events_Error::send("Error: Contours already closed or no contours found for selected edges.");
199     return false;
200   }
201
202   return true;
203 }