1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: BuildPlugin_Wire.cpp
4 // Created: 14 April 2016
5 // Author: Dmitry Bobylev
7 #include "BuildPlugin_Wire.h"
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultBody.h>
11 #include <ModelAPI_ResultConstruction.h>
13 #include <Events_InfoMessage.h>
15 #include <GeomAPI_PlanarEdges.h>
16 #include <GeomAPI_ShapeExplorer.h>
18 #include <GeomAlgoAPI_ShapeTools.h>
19 #include <GeomAlgoAPI_WireBuilder.h>
23 //=================================================================================================
24 BuildPlugin_Wire::BuildPlugin_Wire()
28 //=================================================================================================
29 void BuildPlugin_Wire::initAttributes()
31 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
34 //=================================================================================================
35 void BuildPlugin_Wire::execute()
37 // Get base objects list.
38 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
39 if(!aSelectionList.get()) {
40 setError("Error: Could not get selection list.");
43 if(aSelectionList->size() == 0) {
44 setError("Error: Empty selection list.");
48 // Collect base shapes.
50 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
51 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
52 GeomShapePtr aShape = aSelection->value();
54 aShape = aSelection->context()->shape();
56 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
57 GeomShapePtr anEdge = anExp.current();
58 anEdges.push_back(anEdge);
63 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(anEdges);
65 setError("Error: Result wire is empty. Probably it has disconnected edges or non-manifold.");
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");
82 setResult(aResultBody);
85 //=================================================================================================
86 bool BuildPlugin_Wire::customAction(const std::string& theActionId)
88 if(theActionId == ADD_CONTOUR_ACTION_ID()) {
91 std::string aMsg = "Error: Feature \"%1\" does not support action \"%2\".";
92 Events_InfoMessage("BuildPlugin_Wire", aMsg).arg(getKind()).arg(theActionId).send();
98 //=================================================================================================
99 bool BuildPlugin_Wire::addContour()
101 // Get base objects list.
102 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
103 if(aSelectionList->size() == 0) {
104 Events_InfoMessage("BuildPlugin_Wire", "Error: Empty selection list.").send();
108 // Collect attributes to check.
109 ListOfShape anAddedEdges;
110 std::list<AttributeSelectionPtr> anAttributesToCheck;
111 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
112 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
113 GeomShapePtr anEdgeInList = aSelection->value();
114 if(!anEdgeInList.get()) {
118 // Check that it is edge.
119 if(anEdgeInList->shapeType() != GeomAPI_Shape::EDGE) {
123 // Check that it is edge on sketch.
124 ResultPtr aContext = aSelection->context();
125 ResultConstructionPtr aConstruction =
126 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
127 if(!aConstruction.get()) {
130 GeomShapePtr aContextShape = aConstruction->shape();
131 std::shared_ptr<GeomAPI_PlanarEdges> aPlanarEdges =
132 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
133 if(!aPlanarEdges.get()) {
137 // Check that sketch have faces.
138 if(aConstruction->facesNum() == 0) {
142 anAddedEdges.push_back(anEdgeInList);
143 anAttributesToCheck.push_back(aSelection);
146 // Check if edges have contours.
147 bool isAnyContourAdded = false;
148 for(std::list<AttributeSelectionPtr>::const_iterator aListIt = anAttributesToCheck.cbegin();
149 aListIt != anAttributesToCheck.cend();
151 AttributeSelectionPtr aSelection = *aListIt;
152 std::shared_ptr<GeomAPI_Edge> anEdgeInList(new GeomAPI_Edge(aSelection->value()));
154 ResultConstructionPtr aConstruction =
155 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
157 // Iterate on wires and add wire with this edge.
158 std::shared_ptr<GeomAPI_Shape> aFoundWire;
159 for(int anIndex = 0; anIndex < aConstruction->facesNum(); ++anIndex) {
160 std::shared_ptr<GeomAPI_Face> aFace = aConstruction->face(anIndex);
161 for(GeomAPI_ShapeExplorer
162 aWireExp(aFace, GeomAPI_Shape::WIRE); aWireExp.more(); aWireExp.next()) {
163 GeomShapePtr aWireOnFace = aWireExp.current();
164 for(GeomAPI_ShapeExplorer
165 anExp(aWireOnFace, 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 aFoundWire = aWireOnFace;
173 if(aFoundWire.get()) {
178 // If wire with the same edge found. Add all other edges to list.
179 if(aFoundWire.get()) {
180 for(GeomAPI_ShapeExplorer
181 anExp(aFoundWire, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
182 std::shared_ptr<GeomAPI_Edge> anEdgeOnFace(new GeomAPI_Edge(anExp.current()));
183 ListOfShape::const_iterator anEdgesIt = anAddedEdges.cbegin();
184 for(; anEdgesIt != anAddedEdges.cend(); ++anEdgesIt) {
185 if(anEdgeOnFace->isEqual(*anEdgesIt)) {
189 if(anEdgesIt == anAddedEdges.cend()) {
190 isAnyContourAdded = true;
191 anAddedEdges.push_back(anEdgeOnFace);
192 aSelectionList->append(aConstruction, anEdgeOnFace);
198 if(!isAnyContourAdded) {
199 Events_InfoMessage("BuildPlugin_Wire",
200 "Error: Contours already closed or no contours found for selected edges.").send();