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