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