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.
63 ListOfShape anAllEdges;
64 ListOfShape aPlanarEdges;
65 ListOfShape aNonPlanarEdges;
66 ListOfShape anOriginalFaces;
67 ListOfShape aContexts;
69 getOriginalShapesAndContexts(BASE_OBJECTS_ID(), anOriginalFaces, aContexts);
70 anOriginalFaces.clear();
71 std::list< std::shared_ptr<GeomAPI_Dir> > aListOfNormals;
72 for(int anIndex = 0; anIndex < aSelectionList->size(); ++anIndex) {
73 AttributeSelectionPtr aSelection = aSelectionList->value(anIndex);
74 GeomShapePtr aShape = aSelection->value();
75 GeomShapePtr aContext = aSelection->context()->shape();
79 if (aShape->shapeType() == GeomAPI_Shape::FACE) {
80 // keep selected faces "as is"
81 anOriginalFaces.push_back(aShape);
84 else if (!aSelection->value() && aShape->shapeType() == GeomAPI_Shape::COMPOUND) {
85 // collect faces from the sketch
86 ResultConstructionPtr aSketch =
87 std::dynamic_pointer_cast<ModelAPI_ResultConstruction>(aSelection->context());
88 if (aSketch && aSketch->facesNum() > 0) {
89 for (int i = 0; i < aSketch->facesNum(); ++i)
90 anOriginalFaces.push_back(aSketch->face(i));
95 // check whether the context is a sketch, in this case store its normal for further needs
96 std::shared_ptr<GeomAPI_PlanarEdges> aSketch =
97 std::dynamic_pointer_cast<GeomAPI_PlanarEdges>(aContext);
99 aListOfNormals.push_back(aSketch->norm());
101 bool isPlanar(aShape->isPlanar() || aSketch);
103 for(GeomAPI_ShapeExplorer anExp(aShape, GeomAPI_Shape::EDGE); anExp.more(); anExp.next()) {
104 GeomShapePtr anEdge = anExp.current();
105 anAllEdges.push_back(anEdge);
106 isPlanar? aPlanarEdges.push_back(anEdge) : aNonPlanarEdges.push_back(anEdge);
110 bool isAllPlanar(false);
111 if(!anAllEdges.empty())
113 //check if all the object actually belong to one plane.
114 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anAllEdges);
115 if(aPln.get() && !aNonPlanarEdges.empty())
117 aPlanarEdges.insert(aPlanarEdges.end(), aNonPlanarEdges.begin(), aNonPlanarEdges.end());
118 aNonPlanarEdges.clear();
123 if (!isAllPlanar && !aPlanarEdges.empty())
125 //check is planar objects belong to one plane
126 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(aPlanarEdges);
129 aNonPlanarEdges.insert(aNonPlanarEdges.end(), aPlanarEdges.begin(), aPlanarEdges.end());
130 aPlanarEdges.clear();
134 // Build faces by edges.
136 GeomMakeShapePtr aFaceBuilder;
137 if (!aPlanarEdges.empty())
138 buildFacesByEdges(aPlanarEdges, aListOfNormals, aFaces, aFaceBuilder);
139 int aNbFacesFromEdges = (int)aFaces.size();
141 // Build non-planar faces by edges.
142 GeomMakeShapePtr aNonPlanarFaceBuilder;
143 int aNbNonPlanarFaces = (int)aFaces.size();
144 if(!aNonPlanarEdges.empty())
146 ListOfShape aNonPlanarFaces;
147 buildNonPlanarFacesByEdges(aNonPlanarEdges, aNonPlanarFaces, aNonPlanarFaceBuilder);
148 // Add non-planar faces to common faces list.
149 aFaces.insert(aFaces.end(), aNonPlanarFaces.begin(), aNonPlanarFaces.end());
150 aNbNonPlanarFaces += (int)aNonPlanarFaces.size();
153 // Add faces selected by user.
154 aFaces.insert(aFaces.end(), anOriginalFaces.begin(), anOriginalFaces.end());
158 for(ListOfShape::const_iterator anIt = aFaces.cbegin(); anIt != aFaces.cend(); ++anIt) {
159 std::shared_ptr<GeomAlgoAPI_MakeShapeList> aMakeShapeList(new GeomAlgoAPI_MakeShapeList);
160 if (anIndex < aNbFacesFromEdges)
161 aMakeShapeList->appendAlgo(aFaceBuilder);
162 else if(anIndex < aNbNonPlanarFaces)
163 aMakeShapeList->appendAlgo(aNonPlanarFaceBuilder);
165 GeomShapePtr aShape = *anIt;
166 GeomMakeShapePtr aCopy(new GeomAlgoAPI_Copy(aShape));
167 aMakeShapeList->appendAlgo(aCopy);
169 ListOfShape aBaseShapes;
170 if (anIndex < aNbFacesFromEdges)
171 aBaseShapes = aPlanarEdges;
172 else if(anIndex < aNbNonPlanarFaces)
173 aBaseShapes = aNonPlanarEdges;
175 aBaseShapes.push_back(aShape);
176 storeResult(aMakeShapeList, aBaseShapes, aContexts, aCopy->shape(), anIndex++);
179 removeResults(anIndex);
182 //==================================================================================================
183 void BuildPlugin_Face::buildFacesByEdges(
184 const ListOfShape& theEdges,
185 const std::list< std::shared_ptr<GeomAPI_Dir> >& theNormals,
186 ListOfShape& theFaces,
187 std::shared_ptr<GeomAlgoAPI_MakeShape>& theBuilderAlgo) const
190 std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(theEdges);
191 std::shared_ptr<GeomAPI_Dir> aNormal = aPln->direction();
192 bool isReverse = !theNormals.empty();
193 std::list< std::shared_ptr<GeomAPI_Dir> >::const_iterator aNormIt = theNormals.begin();
194 for (; aNormIt != theNormals.end() && isReverse; ++aNormIt)
195 if ((*aNormIt)->dot(aNormal) > 1.e-7)
199 aPln = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aPln->location(), aNormal));
203 std::shared_ptr<GeomAlgoAPI_SketchBuilder> aSketchBuilder(
204 new GeomAlgoAPI_SketchBuilder(aPln, theEdges));
205 theFaces = aSketchBuilder->faces();
206 theBuilderAlgo = aSketchBuilder;
208 // Get wires from faces.
210 for(ListOfShape::const_iterator anIt = theFaces.cbegin(); anIt != theFaces.cend(); ++anIt)
211 aWires.push_back(GeomAlgoAPI_ShapeTools::getFaceOuterWire(*anIt));
213 // Make faces with holes.
215 GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(),
219 //==================================================================================================
220 void BuildPlugin_Face::buildNonPlanarFacesByEdges(
221 const ListOfShape& theShapes,
222 ListOfShape& theFaces,
223 std::shared_ptr<GeomAlgoAPI_MakeShape>& theBuilderAlgo) const
227 std::shared_ptr<GeomAlgoAPI_NonPlanarFace> aNonPlanarFaceBuilder
228 (new GeomAlgoAPI_NonPlanarFace(theShapes));
229 theFaces = aNonPlanarFaceBuilder->faces();
230 theBuilderAlgo = aNonPlanarFaceBuilder;