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_Error.h>
15 #include <GeomAPI_DataMapOfShapeShape.h>
16 #include <GeomAPI_PlanarEdges.h>
17 #include <GeomAPI_ShapeExplorer.h>
19 #include <GeomAlgoAPI_ShapeTools.h>
20 #include <GeomAlgoAPI_WireBuilder.h>
24 //=================================================================================================
25 BuildPlugin_Wire::BuildPlugin_Wire()
29 //=================================================================================================
30 void BuildPlugin_Wire::initAttributes()
32 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
35 //=================================================================================================
36 void BuildPlugin_Wire::execute()
38 // Get base objects list.
39 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
40 if(!aSelectionList.get()) {
41 setError("Error: Could not get selection list.");
44 if(aSelectionList->size() == 0) {
45 setError("Error: Empty selection list.");
49 // Collect base shapes.
50 ListOfShape aListOfShapes;
51 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
52 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
53 GeomShapePtr aShape = aSelection->value();
55 setError("Error: Empty shape selected.");
59 if(aShape->shapeType() != GeomAPI_Shape::EDGE && aShape->shapeType() != GeomAPI_Shape::WIRE) {
60 setError("Error: Selected shape has wrong type. Only edges and wires acceptable.");
64 aListOfShapes.push_back(aShape);
68 GeomShapePtr aWire = GeomAlgoAPI_WireBuilder::wire(aListOfShapes);
70 setError("Error: Result wire empty. Probably it has disconnected edges or non-manifold.");
75 ResultBodyPtr aResultBody = document()->createBody(data());
76 aResultBody->store(aWire);
77 setResult(aResultBody);
80 //=================================================================================================
81 bool BuildPlugin_Wire::customAction(const std::string& theActionId)
83 if(theActionId == "add_contour") {
86 Events_Error::send("Error: Feature \"" + getKind() + "\" does not support action \"" + theActionId + "\".");
92 //=================================================================================================
93 bool BuildPlugin_Wire::addContour()
95 // Get base objects list.
96 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
97 if(aSelectionList->size() == 0) {
98 Events_Error::send("Error: Empty selection list.");
102 // Collect attributes to check.
103 std::list<AttributeSelectionPtr> anAttributesToCheck;
104 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
105 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
106 GeomShapePtr anEdgeInList = aSelection->value();
107 if(!anEdgeInList.get()) {
111 // Check that it is edge.
112 if(anEdgeInList->shapeType() != GeomAPI_Shape::EDGE) {
116 // Check that it is edge on sketch.
117 ResultPtr aContext = aSelection->context();
118 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aContext);
119 if(!aConstruction.get()) {
122 GeomShapePtr aContextShape = aConstruction->shape();
123 std::shared_ptr<GeomAPI_PlanarEdges> aPlanarEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContextShape);
124 if(!aPlanarEdges.get()) {
128 // Check that sketch have faces.
129 if(aConstruction->facesNum() == 0) {
133 anAttributesToCheck.push_back(aSelection);
136 // Check if edges have contours.
137 ListOfShape anAddedEdges;
138 bool isAnyContourFound = false;
139 for(std::list<AttributeSelectionPtr>::const_iterator aListIt = anAttributesToCheck.cbegin();
140 aListIt != anAttributesToCheck.cend();
142 AttributeSelectionPtr aSelection = *aListIt;
143 std::shared_ptr<GeomAPI_Edge> anEdgeInList(new GeomAPI_Edge(aSelection->value()));
145 ListOfShape::const_iterator anEdgesIt = anAddedEdges.cbegin();
146 for(; anEdgesIt != anAddedEdges.cend(); ++anEdgesIt) {
147 if(anEdgeInList->isEqual(*anEdgesIt)) {
151 if(anEdgesIt != anAddedEdges.cend()) {
152 // This edge is already in list.
156 ResultConstructionPtr aConstruction = std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
157 std::shared_ptr<GeomAPI_PlanarEdges> aPlanarEdges = std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aConstruction->shape());
159 // Iterate on faces and add face with this edge.
160 std::shared_ptr<GeomAPI_Face> aFoundFace;
161 for(int anIndex = 0; anIndex < aConstruction->facesNum(); ++anIndex) {
162 std::shared_ptr<GeomAPI_Face> aFace = aConstruction->face(anIndex);
163 for(GeomAPI_ShapeExplorer anExp(aFace, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
164 std::shared_ptr<GeomAPI_Edge> anEdgeOnFace(new GeomAPI_Edge(anExp.current()));
165 if(anEdgeInList->isEqual(anEdgeOnFace)) {
171 if(aFoundFace.get()) {
176 // If face with the same edge found. Add all other edges to list.
177 if(aFoundFace.get()) {
178 isAnyContourFound = true;
179 anAddedEdges.push_back(anEdgeInList);
180 for(GeomAPI_ShapeExplorer anExp(aFoundFace, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
181 std::shared_ptr<GeomAPI_Edge> anEdgeOnFace(new GeomAPI_Edge(anExp.current()));
182 anEdgesIt = anAddedEdges.cbegin();
183 for(; anEdgesIt != anAddedEdges.cend(); ++anEdgesIt) {
184 if(anEdgeOnFace->isEqual(*anEdgesIt)) {
188 if(anEdgesIt == anAddedEdges.cend()) {
189 anAddedEdges.push_back(anEdgeOnFace);
190 aSelectionList->append(aConstruction, anEdgeOnFace);
196 if(!isAnyContourFound) {
197 Events_Error::send("Error: Contours already closed or no contours found for selected edges.");