1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
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.
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.
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
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "BuildPlugin_Wire.h"
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultBody.h>
25 #include <ModelAPI_ResultConstruction.h>
27 #include <Events_InfoMessage.h>
29 #include <GeomAPI_PlanarEdges.h>
30 #include <GeomAPI_ShapeExplorer.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_WireBuilder.h>
37 //=================================================================================================
38 BuildPlugin_Wire::BuildPlugin_Wire()
42 //=================================================================================================
43 void BuildPlugin_Wire::initAttributes()
45 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
48 //=================================================================================================
49 void BuildPlugin_Wire::execute()
51 // Get base objects list.
52 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
53 if(!aSelectionList.get()) {
54 setError("Error: Could not get selection list.");
57 if(aSelectionList->size() == 0) {
58 setError("Error: Empty selection list.");
62 // Collect base shapes.
64 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
65 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
66 GeomShapePtr aShape = aSelection->value();
68 aShape = aSelection->context()->shape();
70 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
71 GeomShapePtr anEdge = anExp.current();
72 anEdges.push_back(anEdge);
77 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(anEdges);
79 setError("Error: Result wire is empty. Probably it has disconnected edges or non-manifold.");
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, "Edge");
96 setResult(aResultBody);
99 //=================================================================================================
100 bool BuildPlugin_Wire::customAction(const std::string& theActionId)
102 if(theActionId == ADD_CONTOUR_ACTION_ID()) {
105 std::string aMsg = "Error: Feature \"%1\" does not support action \"%2\".";
106 Events_InfoMessage("BuildPlugin_Wire", aMsg).arg(getKind()).arg(theActionId).send();
112 //=================================================================================================
113 bool BuildPlugin_Wire::addContour()
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();
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()) {
132 // Check that it is edge.
133 if(anEdgeInList->shapeType() != GeomAPI_Shape::EDGE) {
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()) {
144 GeomShapePtr aContextShape = aConstruction->shape();
145 std::shared_ptr<GeomAPI_PlanarEdges> aPlanarEdges =
146 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
147 if(!aPlanarEdges.get()) {
151 // Check that sketch have faces.
152 if(aConstruction->facesNum() == 0) {
156 anAddedEdges.push_back(anEdgeInList);
157 anAttributesToCheck.push_back(aSelection);
160 // Check if edges have contours.
161 bool isAnyContourAdded = false;
162 for(std::list<AttributeSelectionPtr>::const_iterator aListIt = anAttributesToCheck.cbegin();
163 aListIt != anAttributesToCheck.cend();
165 AttributeSelectionPtr aSelection = *aListIt;
166 std::shared_ptr<GeomAPI_Edge> anEdgeInList(new GeomAPI_Edge(aSelection->value()));
168 ResultConstructionPtr aConstruction =
169 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
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;
187 if(aFoundWire.get()) {
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)) {
203 if(anEdgesIt == anAddedEdges.cend()) {
204 isAnyContourAdded = true;
205 anAddedEdges.push_back(anEdgeOnFace);
206 aSelectionList->append(aConstruction, anEdgeOnFace);
212 if(!isAnyContourAdded) {
213 Events_InfoMessage("BuildPlugin_Wire",
214 "Error: Contours already closed or no contours found for selected edges.").send();