1 // Copyright (C) 2014-2023 CEA, EDF
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>
35 #include <GeomAlgoAPI_NonPlanarFace.h>
37 //=================================================================================================
38 BuildPlugin_Face::BuildPlugin_Face()
42 //=================================================================================================
43 void BuildPlugin_Face::initAttributes()
45 data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
48 //=================================================================================================
49 void BuildPlugin_Face::execute()
51 // Get base objects list.
52 AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
53 if(!aSelectionList.get()) {
54 setError("Error: Could not get selection list.");
57 if(aSelectionList->size() == 0) {
58 setError("Error: Empty selection list.");
62 // Collect base shapes.
64 ListOfShape aNonPlanarEdges;
65 ListOfShape anOriginalFaces;
66 ListOfShape aContexts;
68 getOriginalShapesAndContexts(BASE_OBJECTS_ID(), anOriginalFaces, aContexts);
69 anOriginalFaces.clear();
70 std::list< std::shared_ptr<GeomAPI_Dir> > aListOfNormals;
71 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
72 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
73 GeomShapePtr aShape = aSelection->value();
74 GeomShapePtr aContext = aSelection->context()->shape();
78 if (aShape->shapeType() == GeomAPI_Shape::FACE) {
79 // keep selected faces "as is"
80 anOriginalFaces.push_back(aShape);
83 else if (!aSelection->value() && aShape->shapeType() == GeomAPI_Shape::COMPOUND) {
84 // collect faces from the sketch
85 ResultConstructionPtr aSketch =
86 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
87 if (aSketch && aSketch->facesNum() > 0) {
88 for (int i = 0; i < aSketch->facesNum(); ++i)
89 anOriginalFaces.push_back(aSketch->face(i));
94 // check whether the context is a sketch, in this case store its normal for further needs
95 std::shared_ptr<GeomAPI_PlanarEdges> aSketch =
96 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContext);
98 aListOfNormals.push_back(aSketch->norm());
100 bool isPlanar(aShape->isPlanar() || aSketch);
102 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
103 GeomShapePtr anEdge = anExp.current();
104 isPlanar? anEdges.push_back(anEdge) : aNonPlanarEdges.push_back(anEdge);
108 if (!anEdges.empty())
110 //check is planar objects belong to nke
111 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
114 aNonPlanarEdges.insert(aNonPlanarEdges.end(), anEdges.begin(), anEdges.end());
119 // Build faces by edges.
121 GeomMakeShapePtr aFaceBuilder;
122 if (!anEdges.empty())
123 buildFacesByEdges(anEdges, aListOfNormals, aFaces, aFaceBuilder);
124 int aNbFacesFromEdges = (int)aFaces.size();
126 // Build non-planar faces by edges.
127 GeomMakeShapePtr aNonPlanarFaceBuilder;
128 int aNbNonPlanarFaces = (int)aFaces.size();
129 if(!aNonPlanarEdges.empty())
131 ListOfShape aNonPlanarFaces;
132 buildNonPlanarFacesByEdges(aNonPlanarEdges, aNonPlanarFaces, aNonPlanarFaceBuilder);
133 // Add non-planar faces to common faces list.
134 aFaces.insert(aFaces.end(), aNonPlanarFaces.begin(), aNonPlanarFaces.end());
135 aNbNonPlanarFaces += (int)aNonPlanarFaces.size();
138 // Add faces selected by user.
139 aFaces.insert(aFaces.end(), anOriginalFaces.begin(), anOriginalFaces.end());
143 for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
144 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
145 if (anIndex < aNbFacesFromEdges)
146 aMakeShapeList->appendAlgo(aFaceBuilder);
147 else if(anIndex < aNbNonPlanarFaces)
148 aMakeShapeList->appendAlgo(aNonPlanarFaceBuilder);
150 GeomShapePtr aShape = *anIt;
151 GeomMakeShapePtr aCopy(new GeomAlgoAPI_Copy(aShape));
152 aMakeShapeList->appendAlgo(aCopy);
154 ListOfShape aBaseShapes;
155 if (anIndex < aNbFacesFromEdges)
156 aBaseShapes = anEdges;
157 else if(anIndex < aNbNonPlanarFaces)
158 aBaseShapes = aNonPlanarEdges;
160 aBaseShapes.push_back(aShape);
161 storeResult(aMakeShapeList, aBaseShapes, aContexts, aCopy->shape(), anIndex++);
164 removeResults(anIndex);
167 //==================================================================================================
168 void BuildPlugin_Face::buildFacesByEdges(
169 const ListOfShape& theEdges,
170 const std::list< std::shared_ptr<GeomAPI_Dir> >& theNormals,
171 ListOfShape& theFaces,
172 std::shared_ptr<GeomAlgoAPI_MakeShape>& theBuilderAlgo) const
175 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(theEdges);
176 std::shared_ptr<GeomAPI_Dir> aNormal = aPln->direction();
177 bool isReverse = !theNormals.empty();
178 std::list< std::shared_ptr<GeomAPI_Dir> >::const_iterator aNormIt = theNormals.begin();
179 for (; aNormIt != theNormals.end() && isReverse; ++aNormIt)
180 if ((*aNormIt)->dot(aNormal) > 1.e-7)
184 aPln = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aPln->location(), aNormal));
188 std::shared_ptr<GeomAlgoAPI_SketchBuilder> aSketchBuilder(
189 new GeomAlgoAPI_SketchBuilder(aPln, theEdges));
190 theFaces = aSketchBuilder->faces();
191 theBuilderAlgo = aSketchBuilder;
193 // Get wires from faces.
195 for(ListOfShape::const_iterator anIt = theFaces.cbegin(); anIt != theFaces.cend(); ++anIt)
196 aWires.push_back(GeomAlgoAPI_ShapeTools::getFaceOuterWire(*anIt));
198 // Make faces with holes.
200 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(),
204 //==================================================================================================
205 void BuildPlugin_Face::buildNonPlanarFacesByEdges(
206 const ListOfShape& theShapes,
207 ListOfShape& theFaces,
208 std::shared_ptr<GeomAlgoAPI_MakeShape>& theBuilderAlgo) const
212 std::shared_ptr<GeomAlgoAPI_NonPlanarFace> aNonPlanarFaceBuilder
213 (new GeomAlgoAPI_NonPlanarFace(theShapes));
214 theFaces = aNonPlanarFaceBuilder->faces();
215 theBuilderAlgo = aNonPlanarFaceBuilder;