Salome HOME
Correction to do not show DOF message until plane of the sketch is not selected.
[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
12 #include <GeomAPI_Edge.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_ShapeExplorer.h>
15
16 #include <GeomAlgoAPI_ShapeTools.h>
17 #include <GeomAlgoAPI_SketchBuilder.h>
18
19 //=================================================================================================
20 BuildPlugin_Face::BuildPlugin_Face()
21 {
22 }
23
24 //=================================================================================================
25 void BuildPlugin_Face::initAttributes()
26 {
27   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
28 }
29
30 //=================================================================================================
31 void BuildPlugin_Face::execute()
32 {
33   // Get base objects list.
34   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
35   if(!aSelectionList.get()) {
36     setError("Error: Could not get selection list.");
37     return;
38   }
39   if(aSelectionList->size() == 0) {
40     setError("Error: Empty selection list.");
41     return;
42   }
43
44   // Collect base shapes.
45   ListOfShape anEdges;
46   for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
47     AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
48     GeomShapePtr aShape = aSelection->value();
49     if(!aShape.get()) {
50       aShape = aSelection->context()->shape();
51     }
52     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
53       GeomShapePtr anEdge = anExp.current();
54       anEdges.push_back(anEdge);
55     }
56   }
57
58   // Get plane.
59   std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
60
61   // Get faces.
62   ListOfShape aFaces;
63   GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(), aPln->direction(), anEdges, aFaces);
64
65   // Get wires from faces.
66   ListOfShape aWires;
67   for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
68     aWires.push_back(GeomAlgoAPI_ShapeTools::getFaceOuterWire(*anIt));
69     //for(GeomAPI_ShapeExplorer anExp(*anIt, GeomAPI_Shape::WIRE); anExp.more(); anExp.next()) {
70     //  if(anExp.current()->orientation() == GeomAPI_Shape::REVERSED) {
71     //    continue;
72     //  }
73     //  aWires.push_back(anExp.current());
74     //}
75   }
76
77   // Make faces with holes.
78   aFaces.clear();
79   GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(), aWires, aFaces);
80
81   // Store result.
82   int anIndex = 0;
83   for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
84     ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
85     GeomShapePtr aShape = *anIt;
86     aResultBody->store(aShape);
87
88     for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
89       GeomShapePtr anEdgeInResult = anExp.current();
90       for(ListOfShape::const_iterator anIt = anEdges.cbegin(); anIt != anEdges.cend(); ++anIt) {
91         std::shared_ptr<GeomAPI_Edge> anEdgeInList(new GeomAPI_Edge(*anIt));
92         if(anEdgeInList->isEqual(anEdgeInResult)) {
93           aResultBody->modified(anEdgeInList, anEdgeInResult, "Edge");
94           break;
95         }
96       }
97     }
98
99     setResult(aResultBody, anIndex);
100     ++anIndex;
101   }
102
103   removeResults(anIndex);
104 }