1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: BuildPlugin_Face.cpp
4 // Created: 14 April 2016
5 // Author: Dmitry Bobylev
7 #include "BuildPlugin_Face.h"
9 #include <ModelAPI_AttributeSelectionList.h>
10 #include <ModelAPI_ResultBody.h>
12 #include <GeomAPI_Edge.h>
13 #include <GeomAPI_PlanarEdges.h>
14 #include <GeomAPI_Pln.h>
15 #include <GeomAPI_ShapeExplorer.h>
17 #include <GeomAlgoAPI_ShapeTools.h>
18 #include <GeomAlgoAPI_SketchBuilder.h>
20 //=================================================================================================
21 BuildPlugin_Face::BuildPlugin_Face()
25 //=================================================================================================
26 void BuildPlugin_Face::initAttributes()
28 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
31 //=================================================================================================
32 void BuildPlugin_Face::execute()
34 // Get base objects list.
35 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
36 if(!aSelectionList.get()) {
37 setError("Error: Could not get selection list.");
40 if(aSelectionList->size() == 0) {
41 setError("Error: Empty selection list.");
45 // Collect base shapes.
47 std::list< std::shared_ptr<GeomAPI_Dir> > aListOfNormals;
48 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
49 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
50 GeomShapePtr aShape = aSelection->value();
51 GeomShapePtr aContext = aSelection->context()->shape();
55 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
56 GeomShapePtr anEdge = anExp.current();
57 anEdges.push_back(anEdge);
60 // check whether the context is a sketch, in this case store its normal for further needs
61 std::shared_ptr<GeomAPI_PlanarEdges> aSketch =
62 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContext);
64 aListOfNormals.push_back(aSketch->norm());
68 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
69 std::shared_ptr<GeomAPI_Dir> aNormal = aPln->direction();
70 bool isReverse = !aListOfNormals.empty();
71 std::list< std::shared_ptr<GeomAPI_Dir> >::const_iterator aNormIt = aListOfNormals.begin();
72 for (; aNormIt != aListOfNormals.end() && isReverse; ++aNormIt)
73 if ((*aNormIt)->dot(aNormal) > 1.e-7)
77 aPln = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aPln->location(), aNormal));
82 GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(), aPln->direction(), anEdges, aFaces);
84 // Get wires from faces.
86 for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
87 aWires.push_back(GeomAlgoAPI_ShapeTools::getFaceOuterWire(*anIt));
88 //for(GeomAPI_ShapeExplorer anExp(*anIt, GeomAPI_Shape::WIRE); anExp.more(); anExp.next()) {
89 // if(anExp.current()->orientation() == GeomAPI_Shape::REVERSED) {
92 // aWires.push_back(anExp.current());
96 // Make faces with holes.
98 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(), aWires, aFaces);
102 for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
103 ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
104 GeomShapePtr aShape = *anIt;
105 aResultBody->store(aShape);
109 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
110 GeomShapePtr anEdgeInResult = anExp.current();
111 for(ListOfShape::const_iterator anIt = anEdges.cbegin(); anIt != anEdges.cend(); ++anIt) {
112 std::shared_ptr<GeomAPI_Edge> anEdgeInList(new GeomAPI_Edge(*anIt));
113 if(anEdgeInList->isEqual(anEdgeInResult)) {
114 aResultBody->modified(anEdgeInList, anEdgeInResult, "Edge_" + std::to_string((long long)anEdgeIndex), anEdgeIndex);
121 setResult(aResultBody, anIndex);
125 removeResults(anIndex);