1 // Copyright (C) 2014-2019 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 email : webmaster.salome@opencascade.com
20 #include "BuildPlugin_Wire.h"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_ResultConstruction.h>
26 #include <Events_InfoMessage.h>
28 #include <GeomAPI_PlanarEdges.h>
29 #include <GeomAPI_ShapeExplorer.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAlgoAPI_WireBuilder.h>
36 //=================================================================================================
37 BuildPlugin_Wire::BuildPlugin_Wire()
41 //=================================================================================================
42 void BuildPlugin_Wire::initAttributes()
44 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
47 //=================================================================================================
48 void BuildPlugin_Wire::execute()
50 // Get base objects list.
51 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
52 if(!aSelectionList.get()) {
53 setError("Error: Could not get selection list.");
56 if(aSelectionList->size() == 0) {
57 setError("Error: Empty selection list.");
61 // Collect base shapes.
63 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
64 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
65 GeomShapePtr aShape = aSelection->value();
67 aShape = aSelection->context()->shape();
69 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
70 GeomShapePtr anEdge = anExp.current();
71 anEdges.push_back(anEdge);
76 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(anEdges);
78 setError("Error: Result wire is empty. Probably it has disconnected edges or non-manifold.");
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);
95 setResult(aResultBody);
98 //=================================================================================================
99 bool BuildPlugin_Wire::customAction(const std::string& theActionId)
101 if(theActionId == ADD_CONTOUR_ACTION_ID()) {
104 std::string aMsg = "Error: Feature \"%1\" does not support action \"%2\".";
105 Events_InfoMessage("BuildPlugin_Wire", aMsg).arg(getKind()).arg(theActionId).send();
111 //=================================================================================================
112 bool BuildPlugin_Wire::addContour()
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();
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()) {
131 // Check that it is edge.
132 if(anEdgeInList->shapeType() != GeomAPI_Shape::EDGE) {
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()) {
143 GeomShapePtr aContextShape = aConstruction->shape();
144 std::shared_ptr<GeomAPI_PlanarEdges> aPlanarEdges =
145 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
146 if(!aPlanarEdges.get()) {
150 // Check that sketch have faces.
151 if(aConstruction->facesNum() == 0) {
155 anAddedEdges.push_back(anEdgeInList);
156 anAttributesToCheck.push_back(aSelection);
159 // Check if edges have contours.
160 bool isAnyContourAdded = false;
161 for(std::list<AttributeSelectionPtr>::const_iterator aListIt = anAttributesToCheck.cbegin();
162 aListIt != anAttributesToCheck.cend();
164 AttributeSelectionPtr aSelection = *aListIt;
165 std::shared_ptr<GeomAPI_Edge> anEdgeInList(new GeomAPI_Edge(aSelection->value()));
167 ResultConstructionPtr aConstruction =
168 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
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;
186 if(aFoundWire.get()) {
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)) {
202 if(anEdgesIt == anAddedEdges.cend()) {
203 isAnyContourAdded = true;
204 anAddedEdges.push_back(anEdgeOnFace);
205 aSelectionList->append(aConstruction, anEdgeOnFace);
211 if(!isAnyContourAdded) {
212 Events_InfoMessage("BuildPlugin_Wire",
213 "Error: Contours already closed or no contours found for selected edges.").send();