Salome HOME
89df91b89740ea6592bd46c293d1bfdd4583d166
[modules/shaper.git] / src / BuildPlugin / BuildPlugin_Face.cpp
1 // Copyright (C) 2014-2024  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 anAllEdges;
64   ListOfShape aPlanarEdges;
65   ListOfShape aNonPlanarEdges;
66   ListOfShape anOriginalFaces;
67   ListOfShape aContexts;
68
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();
76     if(!aShape.get()) {
77       aShape = aContext;
78     }
79     if (aShape->shapeType() == GeomAPI_Shape::FACE) {
80       // keep selected faces "as is"
81       anOriginalFaces.push_back(aShape);
82       continue;
83     }
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));
91         continue;
92       }
93     }
94
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);
98     if (aSketch)
99       aListOfNormals.push_back(aSketch->norm());
100
101     bool isPlanar(aShape->isPlanar() || aSketch);
102
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);
107     }
108   }
109
110   bool isAllPlanar(false);
111   if(!anAllEdges.empty())
112   {
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()) 
116     {
117       aPlanarEdges.insert(aPlanarEdges.end(), aNonPlanarEdges.begin(), aNonPlanarEdges.end());
118       aNonPlanarEdges.clear();
119       isAllPlanar = true;
120     }
121   }
122
123   if (!isAllPlanar && !aPlanarEdges.empty())
124   {
125     //check is planar objects belong to one plane
126     std::shared_ptr<GeomAPI_Pln> aPln = GeomAlgoAPI_ShapeTools::findPlane(aPlanarEdges);
127     if(!aPln.get())
128     {
129       aNonPlanarEdges.insert(aNonPlanarEdges.end(), aPlanarEdges.begin(), aPlanarEdges.end());
130       aPlanarEdges.clear();
131     }
132   }
133
134   // Build faces by edges.
135   ListOfShape aFaces;
136   GeomMakeShapePtr aFaceBuilder;
137   if (!aPlanarEdges.empty())
138     buildFacesByEdges(aPlanarEdges, aListOfNormals, aFaces, aFaceBuilder);
139   int aNbFacesFromEdges = (int)aFaces.size();
140
141   // Build non-planar faces by edges.
142   GeomMakeShapePtr aNonPlanarFaceBuilder;
143   int aNbNonPlanarFaces = (int)aFaces.size();
144   if(!aNonPlanarEdges.empty())
145   {
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();
151   }
152
153   // Add faces selected by user.
154   aFaces.insert(aFaces.end(), anOriginalFaces.begin(), anOriginalFaces.end());
155
156   // Store result.
157   int anIndex = 0;
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);
164
165     GeomShapePtr aShape = *anIt;
166     GeomMakeShapePtr aCopy(new GeomAlgoAPI_Copy(aShape));
167     aMakeShapeList->appendAlgo(aCopy);
168
169     ListOfShape aBaseShapes;
170     if (anIndex < aNbFacesFromEdges)
171       aBaseShapes = aPlanarEdges;
172     else if(anIndex < aNbNonPlanarFaces)
173       aBaseShapes = aNonPlanarEdges;
174     else
175       aBaseShapes.push_back(aShape);
176     storeResult(aMakeShapeList, aBaseShapes, aContexts, aCopy->shape(), anIndex++);
177   }
178
179   removeResults(anIndex);
180 }
181
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
188 {
189   // Get plane.
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)
196       isReverse = false;
197   if (isReverse) {
198     aNormal->reverse();
199     aPln = std::shared_ptr<GeomAPI_Pln>(new GeomAPI_Pln(aPln->location(), aNormal));
200   }
201
202   // Get faces.
203   std::shared_ptr<GeomAlgoAPI_SketchBuilder> aSketchBuilder(
204       new GeomAlgoAPI_SketchBuilder(aPln, theEdges));
205   theFaces = aSketchBuilder->faces();
206   theBuilderAlgo = aSketchBuilder;
207
208   // Get wires from faces.
209   ListOfShape aWires;
210   for(ListOfShape::const_iterator anIt = theFaces.cbegin(); anIt != theFaces.cend(); ++anIt)
211     aWires.push_back(GeomAlgoAPI_ShapeTools::getFaceOuterWire(*anIt));
212
213   // Make faces with holes.
214   theFaces.clear();
215   GeomAlgoAPI_ShapeTools::makeFacesWithHoles(aPln->location(), aPln->direction(),
216                                              aWires, theFaces);
217 }
218
219 //==================================================================================================
220 void BuildPlugin_Face::buildNonPlanarFacesByEdges(
221     const ListOfShape& theShapes,
222     ListOfShape& theFaces,
223     std::shared_ptr<GeomAlgoAPI_MakeShape>& theBuilderAlgo) const
224 {
225
226     // Get faces.
227     std::shared_ptr<GeomAlgoAPI_NonPlanarFace> aNonPlanarFaceBuilder
228             (new GeomAlgoAPI_NonPlanarFace(theShapes));
229     theFaces = aNonPlanarFaceBuilder->faces();
230     theBuilderAlgo = aNonPlanarFaceBuilder;
231
232 }