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