1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "BuildPlugin_Face.h"
23 #include <ModelAPI_AttributeSelectionList.h>
24 #include <ModelAPI_ResultBody.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_PlanarEdges.h>
28 #include <GeomAPI_Pln.h>
29 #include <GeomAPI_ShapeExplorer.h>
31 #include <GeomAlgoAPI_ShapeTools.h>
32 #include <GeomAlgoAPI_SketchBuilder.h>
33 #include <GeomAlgoAPI_Copy.h>
35 //=================================================================================================
36 BuildPlugin_Face::BuildPlugin_Face()
40 //=================================================================================================
41 void BuildPlugin_Face::initAttributes()
43 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
46 //=================================================================================================
47 void BuildPlugin_Face::execute()
49 // Get base objects list.
50 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
51 if(!aSelectionList.get()) {
52 setError("Error: Could not get selection list.");
55 if(aSelectionList->size() == 0) {
56 setError("Error: Empty selection list.");
60 // Collect base shapes.
62 std::list< std::shared_ptr<GeomAPI_Dir> > aListOfNormals;
63 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
64 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
65 GeomShapePtr aShape = aSelection->value();
66 GeomShapePtr aContext = aSelection->context()->shape();
70 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
71 GeomShapePtr anEdge = anExp.current();
72 anEdges.push_back(anEdge);
75 // check whether the context is a sketch, in this case store its normal for further needs
76 std::shared_ptr<GeomAPI_PlanarEdges> aSketch =
77 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContext);
79 aListOfNormals.push_back(aSketch->norm());
83 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
84 std::shared_ptr<GeomAPI_Dir> aNormal = aPln->direction();
85 bool isReverse = !aListOfNormals.empty();
86 std::list< std::shared_ptr<GeomAPI_Dir> >::const_iterator aNormIt = aListOfNormals.begin();
87 for (; aNormIt != aListOfNormals.end() && isReverse; ++aNormIt)
88 if ((*aNormIt)->dot(aNormal) > 1.e-7)
92 aPln = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aPln->location(), aNormal));
97 GeomAlgoAPI_SketchBuilder::createFaces(aPln->location(), aPln->xDirection(),
98 aPln->direction(), anEdges, aFaces);
100 // Get wires from faces.
102 for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
103 aWires.push_back(GeomAlgoAPI_ShapeTools::getFaceOuterWire(*anIt));
104 //for(GeomAPI_ShapeExplorer anExp(*anIt, GeomAPI_Shape::WIRE); anExp.more(); anExp.next()) {
105 // if(anExp.current()->orientation() == GeomAPI_Shape::REVERSED) {
108 // aWires.push_back(anExp.current());
112 // Make faces with holes.
114 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(), aWires, aFaces);
118 for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
119 ResultBodyPtr aResultBody = document()->createBody(data(), anIndex);
120 GeomShapePtr aShape = *anIt;
121 GeomAlgoAPI_Copy aCopy(aShape);
122 aShape = aCopy.shape();
123 aResultBody->store(aShape);
127 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
128 GeomShapePtr anEdge = anExp.current();
129 aResultBody->generated(anEdge, "Edge_" + std::to_string((long long)anEdgeIndex), anEdgeIndex);
133 setResult(aResultBody, anIndex);
137 removeResults(anIndex);