1 // Copyright (C) 2014-2020 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 email : webmaster.salome@opencascade.com
20 #include "BuildPlugin_Face.h"
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_ResultConstruction.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_PlanarEdges.h>
28 #include <GeomAPI_Pln.h>
29 #include <GeomAPI_ShapeExplorer.h>
31 #include <GeomAlgoAPI_MakeShapeList.h>
32 #include <GeomAlgoAPI_ShapeTools.h>
33 #include <GeomAlgoAPI_SketchBuilder.h>
34 #include <GeomAlgoAPI_Copy.h>
36 //=================================================================================================
37 BuildPlugin_Face::BuildPlugin_Face()
41 //=================================================================================================
42 void BuildPlugin_Face::initAttributes()
44 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
47 //=================================================================================================
48 void BuildPlugin_Face::execute()
50 // Get base objects list.
51 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
52 if(!aSelectionList.get()) {
53 setError("Error: Could not get selection list.");
56 if(aSelectionList->size() == 0) {
57 setError("Error: Empty selection list.");
61 // Collect base shapes.
63 ListOfShape anOriginalFaces;
64 ListOfShape aContexts;
65 getOriginalShapesAndContexts(BASE_OBJECTS_ID(), anOriginalFaces, aContexts);
66 anOriginalFaces.clear();
67 std::list< std::shared_ptr<GeomAPI_Dir> > aListOfNormals;
68 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
69 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
70 GeomShapePtr aShape = aSelection->value();
71 GeomShapePtr aContext = aSelection->context()->shape();
75 if (aShape->shapeType() == GeomAPI_Shape::FACE) {
76 // keep selected faces "as is"
77 anOriginalFaces.push_back(aShape);
80 else if (!aSelection->value() && aShape->shapeType() == GeomAPI_Shape::COMPOUND) {
81 // collect faces from the sketch
82 ResultConstructionPtr aSketch =
83 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
84 if (aSketch && aSketch->facesNum() > 0) {
85 for (int i = 0; i < aSketch->facesNum(); ++i)
86 anOriginalFaces.push_back(aSketch->face(i));
91 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
92 GeomShapePtr anEdge = anExp.current();
93 anEdges.push_back(anEdge);
96 // check whether the context is a sketch, in this case store its normal for further needs
97 std::shared_ptr<GeomAPI_PlanarEdges> aSketch =
98 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContext);
100 aListOfNormals.push_back(aSketch->norm());
103 // Build faces by edges.
105 GeomMakeShapePtr aFaceBuilder;
106 if (!anEdges.empty())
107 buildFacesByEdges(anEdges, aListOfNormals, aFaces, aFaceBuilder);
108 int aNbFacesFromEdges = (int)aFaces.size();
110 // Add faces selected by user.
111 aFaces.insert(aFaces.end(), anOriginalFaces.begin(), anOriginalFaces.end());
115 for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
116 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
117 if (anIndex < aNbFacesFromEdges)
118 aMakeShapeList->appendAlgo(aFaceBuilder);
120 GeomShapePtr aShape = *anIt;
121 GeomMakeShapePtr aCopy(new GeomAlgoAPI_Copy(aShape));
122 aMakeShapeList->appendAlgo(aCopy);
124 ListOfShape aBaseShapes;
125 if (anIndex < aNbFacesFromEdges)
126 aBaseShapes = anEdges;
128 aBaseShapes.push_back(aShape);
129 storeResult(aMakeShapeList, aBaseShapes, aContexts, aCopy->shape(), anIndex++);
132 removeResults(anIndex);
135 void BuildPlugin_Face::buildFacesByEdges(
136 const ListOfShape& theEdges,
137 const std::list< std::shared_ptr<GeomAPI_Dir> >& theNormals,
138 ListOfShape& theFaces,
139 std::shared_ptr<GeomAlgoAPI_MakeShape>& theBuilderAlgo) const
142 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(theEdges);
143 std::shared_ptr<GeomAPI_Dir> aNormal = aPln->direction();
144 bool isReverse = !theNormals.empty();
145 std::list< std::shared_ptr<GeomAPI_Dir> >::const_iterator aNormIt = theNormals.begin();
146 for (; aNormIt != theNormals.end() && isReverse; ++aNormIt)
147 if ((*aNormIt)->dot(aNormal) > 1.e-7)
151 aPln = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aPln->location(), aNormal));
155 std::shared_ptr<GeomAlgoAPI_SketchBuilder> aSketchBuilder(
156 new GeomAlgoAPI_SketchBuilder(aPln, theEdges));
157 theFaces = aSketchBuilder->faces();
158 theBuilderAlgo = aSketchBuilder;
160 // Get wires from faces.
162 for(ListOfShape::const_iterator anIt = theFaces.cbegin(); anIt != theFaces.cend(); ++anIt)
163 aWires.push_back(GeomAlgoAPI_ShapeTools::getFaceOuterWire(*anIt));
165 // Make faces with holes.
167 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(),