Salome HOME
36ef2b3e8a73f3dfb9bf35c7ff3ebcf7454d69f3
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Face.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        BuildPlugin_Face.cpp
4 // Created:     14 April 2016
5 // Author:      Dmitry Bobylev
6
7 #include "BuildPlugin_Face.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_Pln.h>
18 #include <GeomAPI_ShapeExplorer.h>
19
20 #include <GeomAlgoAPI_ShapeTools.h>
21 #include <GeomAlgoAPI_SketchBuilder.h>
22 #include <GeomAlgoAPI_WireBuilder.h>
23
24 #include <algorithm>
25 #include <sstream>
26
27 //=================================================================================================
28 BuildPlugin_Face::BuildPlugin_Face()
29 {
30 }
31
32 //=================================================================================================
33 void BuildPlugin_Face::initAttributes()
34 {
35   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
36 }
37
38 //=================================================================================================
39 void BuildPlugin_Face::execute()
40 {
41   // Get base objects list.
42   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
43   if(!aSelectionList.get()) {
44     setError("Error: Could not get selection list.");
45     return;
46   }
47   if(aSelectionList->size() == 0) {
48     setError("Error: Empty selection list.");
49     return;
50   }
51
52   // Collect base shapes.
53   ListOfShape anEdges;
54   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
55     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
56     GeomShapePtr aShape = aSelection->value();
57     if(!aShape.get()) {
58       aShape = aSelection->context()->shape();
59     }
60     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
61       GeomShapePtr anEdge = anExp.current();
62       anEdges.push_back(anEdge);
63     }
64   }
65
66   // Get plane.
67   std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
68
69   // Get faces.
70   ListOfShape aFaces;
71   GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(), aPln->direction(), anEdges, aFaces);
72
73   // Get wires from faces.
74   ListOfShape aWires;
75   for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
76     for(GeomAPI_ShapeExplorer anExp(*anIt, GeomAPI_Shape::WIRE); anExp.more(); anExp.next()) {
77       aWires.push_back(anExp.current());
78     }
79   }
80
81   // Make faces with holes.
82   aFaces.clear();
83   GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(), aWires, aFaces);
84
85   // Store result.
86   int anIndex = 0;
87   for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
88     ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
89     GeomShapePtr aShape = *anIt;
90     aResultBody->store(aShape);
91
92     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
93       GeomShapePtr anEdgeInResult = anExp.current();
94       for(ListOfShape::const_iterator anIt = anEdges.cbegin(); anIt != anEdges.cend(); ++anIt) {
95         std::shared_ptr<GeomAPI_Edge> anEdgeInList(new GeomAPI_Edge(*anIt));
96         if(anEdgeInList->isEqual(anEdgeInResult)) {
97           aResultBody->modified(anEdgeInList, anEdgeInResult, "Edge");
98           break;
99         }
100       }
101     }
102
103     setResult(aResultBody, anIndex);
104     ++anIndex;
105   }
106
107   removeResults(anIndex);
108 }