Salome HOME
Non planar faces 35156
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Face.cpp
1 // Copyright (C) 2014-2023  CEA, EDF
2 //
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.
7 //
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "BuildPlugin_Face.h"
21
22 #include <ModelAPI_AttributeSelectionList.h>
23 #include <ModelAPI_ResultBody.h>
24 #include <ModelAPI_ResultConstruction.h>
25
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_PlanarEdges.h>
28 #include <GeomAPI_Pln.h>
29 #include <GeomAPI_ShapeExplorer.h>
30
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>
36
37 //=================================================================================================
38 BuildPlugin_Face::BuildPlugin_Face()
39 {
40 }
41
42 //=================================================================================================
43 void BuildPlugin_Face::initAttributes()
44 {
45   data()->addAttribute(BASE_OBJECTS_ID(), ModelAPI_AttributeSelectionList::typeId());
46 }
47
48 //=================================================================================================
49 void BuildPlugin_Face::execute()
50 {
51   // Get base objects list.
52   AttributeSelectionListPtr aSelectionList = selectionList(BASE_OBJECTS_ID());
53   if(!aSelectionList.get()) {
54     setError("Error: Could not get selection list.");
55     return;
56   }
57   if(aSelectionList->size() == 0) {
58     setError("Error: Empty selection list.");
59     return;
60   }
61
62   // Collect base shapes.
63   ListOfShape anEdges;
64   ListOfShape aNonPlanarEdges;
65   ListOfShape anOriginalFaces;
66   ListOfShape aContexts;
67
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();
75     if(!aShape.get()) {
76       aShape = aContext;
77     }
78     if (aShape->shapeType() == GeomAPI_Shape::FACE) {
79       // keep selected faces "as is"
80       anOriginalFaces.push_back(aShape);
81       continue;
82     }
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));
90         continue;
91       }
92     }
93
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);
97     if (aSketch)
98       aListOfNormals.push_back(aSketch->norm());
99
100     bool isPlanar(aShape->isPlanar() || aSketch);
101
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);
105     }
106   }
107
108   if (!anEdges.empty())
109   {
110     //check is planar objects belong to nke
111     std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(anEdges);
112     if(!aPln.get())
113     {
114       aNonPlanarEdges.insert(aNonPlanarEdges.end(), anEdges.begin(), anEdges.end());
115       anEdges.clear();
116     }
117   }
118
119   // Build faces by edges.
120   ListOfShape aFaces;
121   GeomMakeShapePtr aFaceBuilder;
122   if (!anEdges.empty())
123     buildFacesByEdges(anEdges, aListOfNormals, aFaces, aFaceBuilder);
124   int aNbFacesFromEdges = (int)aFaces.size();
125
126   // Build non-planar faces by edges.
127   GeomMakeShapePtr aNonPlanarFaceBuilder;
128   int aNbNonPlanarFaces = (int)aFaces.size();
129   if(!aNonPlanarEdges.empty())
130   {
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();
136   }
137
138   // Add faces selected by user.
139   aFaces.insert(aFaces.end(), anOriginalFaces.begin(), anOriginalFaces.end());
140
141   // Store result.
142   int anIndex = 0;
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);
149
150     GeomShapePtr aShape = *anIt;
151     GeomMakeShapePtr aCopy(new GeomAlgoAPI_Copy(aShape));
152     aMakeShapeList->appendAlgo(aCopy);
153
154     ListOfShape aBaseShapes;
155     if (anIndex < aNbFacesFromEdges)
156       aBaseShapes = anEdges;
157     else if(anIndex < aNbNonPlanarFaces)
158       aBaseShapes = aNonPlanarEdges;
159     else
160       aBaseShapes.push_back(aShape);
161     storeResult(aMakeShapeList, aBaseShapes, aContexts, aCopy->shape(), anIndex++);
162   }
163
164   removeResults(anIndex);
165 }
166
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
173 {
174   // Get plane.
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)
181       isReverse = false;
182   if (isReverse) {
183     aNormal->reverse();
184     aPln = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aPln->location(), aNormal));
185   }
186
187   // Get faces.
188   std::shared_ptr<GeomAlgoAPI_SketchBuilder> aSketchBuilder(
189       new GeomAlgoAPI_SketchBuilder(aPln, theEdges));
190   theFaces = aSketchBuilder->faces();
191   theBuilderAlgo = aSketchBuilder;
192
193   // Get wires from faces.
194   ListOfShape aWires;
195   for(ListOfShape::const_iterator anIt = theFaces.cbegin(); anIt != theFaces.cend(); ++anIt)
196     aWires.push_back(GeomAlgoAPI_ShapeTools::getFaceOuterWire(*anIt));
197
198   // Make faces with holes.
199   theFaces.clear();
200   GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(),
201                                              aWires, theFaces);
202 }
203
204 //==================================================================================================
205 void BuildPlugin_Face::buildNonPlanarFacesByEdges(
206     const ListOfShape& theShapes,
207     ListOfShape& theFaces,
208     std::shared_ptr<GeomAlgoAPI_MakeShape>& theBuilderAlgo) const
209 {
210
211     // Get faces.
212     std::shared_ptr<GeomAlgoAPI_NonPlanarFace> aNonPlanarFaceBuilder
213             (new GeomAlgoAPI_NonPlanarFace(theShapes));
214     theFaces = aNonPlanarFaceBuilder->faces();
215     theBuilderAlgo = aNonPlanarFaceBuilder;
216
217 }