]> SALOME platform Git repositories - modules/shaper.git/blob - src/GeomAlgoAPI/GeomAlgoAPI_ShapeTools.cpp
Salome HOME
Issue #1366: Remove Sub-Shapes feature added.
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeTools.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        GeomAlgoAPI_ShapeTools.h
4 // Created:     3 August 2015
5 // Author:      Dmitry Bobylev
6
7 #include "GeomAlgoAPI_ShapeTools.h"
8
9 #include "GeomAlgoAPI_SketchBuilder.h"
10
11 #include <GeomAPI_Dir.h>
12 #include <GeomAPI_PlanarEdges.h>
13 #include <GeomAPI_Pln.h>
14 #include <GeomAPI_Pnt.h>
15
16 #include <Bnd_Box.hxx>
17 #include <BOPTools.hxx>
18 #include <BRep_Builder.hxx>
19 #include <BRepAdaptor_Curve.hxx>
20 #include <BRepAlgo_FaceRestrictor.hxx>
21 #include <BRepBndLib.hxx>
22 #include <BRepBuilderAPI_FindPlane.hxx>
23 #include <BRepBuilderAPI_MakeEdge.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <BRepCheck_Analyzer.hxx>
26 #include <BRepExtrema_DistShapeShape.hxx>
27 #include <BRepGProp.hxx>
28 #include <BRepTopAdaptor_FClass2d.hxx>
29 #include <Geom_Curve.hxx>
30 #include <Geom2d_Curve.hxx>
31 #include <BRepLib_CheckCurveOnSurface.hxx>
32 #include <BRep_Tool.hxx>
33 #include <Geom_Plane.hxx>
34 #include <GeomLib_IsPlanarSurface.hxx>
35 #include <GeomLib_Tool.hxx>
36 #include <GeomProjLib.hxx>
37 #include <gp_Pln.hxx>
38 #include <GProp_GProps.hxx>
39 #include <IntAna_IntConicQuad.hxx>
40 #include <IntAna_Quadric.hxx>
41 #include <NCollection_Vector.hxx>
42 #include <ShapeAnalysis.hxx>
43 #include <ShapeAnalysis_Surface.hxx>
44 #include <TopoDS_Builder.hxx>
45 #include <TopoDS_Edge.hxx>
46 #include <TopoDS_Face.hxx>
47 #include <TopoDS_Shape.hxx>
48 #include <TopoDS_Shell.hxx>
49 #include <TopoDS_Vertex.hxx>
50 #include <TopoDS.hxx>
51 #include <TopExp_Explorer.hxx>
52
53 //==================================================================================================
54 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
55 {
56   GProp_GProps aGProps;
57   if(!theShape.get()) {
58     return 0.0;
59   }
60   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
61   if(aShape.IsNull()) {
62     return 0.0;
63   }
64   const Standard_Real anEps = 1.e-6;
65   BRepGProp::VolumeProperties(aShape, aGProps, anEps);
66   return aGProps.Mass();
67 }
68
69 //==================================================================================================
70 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
71 {
72   GProp_GProps aGProps;
73   if(!theShape) {
74     return std::shared_ptr<GeomAPI_Pnt>();
75   }
76   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
77   if(aShape.IsNull()) {
78     return std::shared_ptr<GeomAPI_Pnt>();
79   }
80   gp_Pnt aCentre;
81   if(aShape.ShapeType() == TopAbs_VERTEX) {
82     aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
83   } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
84     BRepGProp::LinearProperties(aShape, aGProps);
85     aCentre = aGProps.CentreOfMass();
86   } else {
87     BRepGProp::SurfaceProperties(aShape, aGProps);
88     aCentre = aGProps.CentreOfMass();
89   }
90   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
91 }
92
93 //==================================================================================================
94 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
95                                                                      const GeomAPI_Shape::ShapeType theType,
96                                                                      ListOfShape& theCombinedShapes,
97                                                                      ListOfShape& theFreeShapes)
98 {
99   GeomShapePtr aResult = theCompound;
100
101   if(!theCompound.get()) {
102     return aResult;
103   }
104
105   if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
106     return aResult;
107   }
108
109   TopAbs_ShapeEnum aTS = TopAbs_EDGE;
110   TopAbs_ShapeEnum aTA = TopAbs_FACE;
111   if(theType == GeomAPI_Shape::COMPSOLID) {
112     aTS = TopAbs_FACE;
113     aTA = TopAbs_SOLID;
114   }
115
116   theCombinedShapes.clear();
117   theFreeShapes.clear();
118
119   // Get free shapes.
120   const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
121   for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
122     const TopoDS_Shape& aShape = anIter.Value();
123     if(aShape.ShapeType() > aTA) {
124       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
125       aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
126       theFreeShapes.push_back(aGeomShape);
127     }
128   }
129
130   // Map subshapes and shapes.
131   BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
132   BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
133   if(aMapSA.IsEmpty()) {
134     return aResult;
135   }
136
137   // Get all shapes with common subshapes and free shapes.
138   NCollection_Map<TopoDS_Shape> aFreeShapes;
139   NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
140   for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
141     const TopoDS_Shape& aShape = anIter.Key();
142     BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
143     if(aListOfShape.IsEmpty()) {
144       continue;
145     }
146     else if(aListOfShape.Size() == 1) {
147       const TopoDS_Shape& aF = aListOfShape.First();
148       aFreeShapes.Add(aF);
149       aListOfShape.Clear();
150     } else {
151       NCollection_List<TopoDS_Shape> aTempList;
152       NCollection_Map<TopoDS_Shape> aTempMap;
153       const TopoDS_Shape& aF = aListOfShape.First();
154       const TopoDS_Shape& aL = aListOfShape.Last();
155       aTempList.Append(aF);
156       aTempList.Append(aL);
157       aTempMap.Add(aF);
158       aTempMap.Add(aL);
159       aFreeShapes.Remove(aF);
160       aFreeShapes.Remove(aL);
161       aListOfShape.Clear();
162       for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
163         const TopoDS_Shape& aTempShape = aTempIter.Value();
164         for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
165           BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
166           if(aTempListOfShape.IsEmpty()) {
167             continue;
168           } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
169             aTempListOfShape.Clear();
170           } else if(aTempListOfShape.Size() > 1) {
171             if(aTempListOfShape.First() == aTempShape) {
172               const TopoDS_Shape& aTL = aTempListOfShape.Last();
173               if(aTempMap.Add(aTL)) {
174                 aTempList.Append(aTL);
175                 aFreeShapes.Remove(aTL);
176               }
177               aTempListOfShape.Clear();
178             } else if(aTempListOfShape.Last() == aTempShape) {
179               const TopoDS_Shape& aTF = aTempListOfShape.First();
180               if(aTempMap.Add(aTF)) {
181                 aTempList.Append(aTF);
182                 aFreeShapes.Remove(aTF);
183               }
184               aTempListOfShape.Clear();
185             }
186           }
187         }
188       }
189       aShapesWithCommonSubshapes.Append(aTempMap);
190     }
191   }
192
193   // Combine shapes with common subshapes.
194   for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
195     TopoDS_Shell aShell;
196     TopoDS_CompSolid aCSolid;
197     TopoDS_Builder aBuilder;
198     theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
199     NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
200     for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
201       const TopoDS_Shape& aShape = anExp.Current();
202       if(aShapesMap.Contains(aShape)) {
203         theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
204         aShapesMap.Remove(aShape);
205       }
206     }
207     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
208     TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
209     aGeomShape->setImpl<TopoDS_Shape>(aSh);
210     theCombinedShapes.push_back(aGeomShape);
211   }
212
213   // Adding free shapes.
214   for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
215     const TopoDS_Shape& aShape = anExp.Current();
216     if(aFreeShapes.Contains(aShape)) {
217       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
218       aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
219       theFreeShapes.push_back(aGeomShape);
220     }
221   }
222
223   if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
224     aResult = theCombinedShapes.front();
225   } else if (theCombinedShapes.size() > 1 || (theCombinedShapes.size() >= 1 && theFreeShapes.size() >= 1)) {
226     TopoDS_Compound aResultComp;
227     TopoDS_Builder aBuilder;
228     aBuilder.MakeCompound(aResultComp);
229     for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin(); anIter != theCombinedShapes.cend(); anIter++) {
230       aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
231     }
232     for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin(); anIter != theFreeShapes.cend(); anIter++) {
233       aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
234     }
235     aResult->setImpl(new TopoDS_Shape(aResultComp));
236   }
237
238   return aResult;
239 }
240
241 //==================================================================================================
242 static void addSimpleShapeToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
243 {
244   if(theShape.IsNull()) {
245     return;
246   }
247
248   if(theShape.ShapeType() == TopAbs_COMPOUND) {
249     for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
250       addSimpleShapeToList(anIt.Value(), theList);
251     }
252   } else {
253     theList.Append(theShape);
254   }
255 }
256
257 //==================================================================================================
258 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
259 {
260   TopoDS_Compound aCompound;
261
262   BRep_Builder aBuilder;
263   aBuilder.MakeCompound(aCompound);
264
265   for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
266     aBuilder.Add(aCompound, anIt.Value());
267   }
268
269   return aCompound;
270 }
271
272 //==================================================================================================
273 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(const std::shared_ptr<GeomAPI_Shape> theCompound)
274 {
275   GeomShapePtr aResult = theCompound;
276
277   if(!theCompound.get()) {
278     return aResult;
279   }
280
281   TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
282   NCollection_List<TopoDS_Shape> anUngroupedShapes;
283   addSimpleShapeToList(anInShape, anUngroupedShapes);
284
285   NCollection_Vector<NCollection_List<TopoDS_Shape>> aGroups;
286   while(!anUngroupedShapes.IsEmpty()) {
287     NCollection_List<TopoDS_Shape> aGroupedShapes;
288     aGroupedShapes.Append(anUngroupedShapes.First());
289     anUngroupedShapes.RemoveFirst();
290     for(NCollection_List<TopoDS_Shape>::Iterator aGroupIt(aGroupedShapes); aGroupIt.More(); aGroupIt.Next()) {
291       const TopoDS_Shape& aGroupShape = aGroupIt.Value();
292       for(NCollection_List<TopoDS_Shape>::Iterator anUngroupedIt(anUngroupedShapes); anUngroupedIt.More(); anUngroupedIt.Next()) {
293         const TopoDS_Shape& anUngroupedShape = anUngroupedIt.Value();
294         bool isFound = false;
295         for(TopExp_Explorer aGroupShapeExp(aGroupShape, TopAbs_VERTEX); aGroupShapeExp.More(); aGroupShapeExp.Next()) {
296           const TopoDS_Shape& aVertex1 = aGroupShapeExp.Current();
297           for(TopExp_Explorer anUngroupedShapeExp(anUngroupedShape, TopAbs_VERTEX); anUngroupedShapeExp.More(); anUngroupedShapeExp.Next()) {
298             const TopoDS_Shape& aVertex2 = anUngroupedShapeExp.Current();
299             if(aVertex1.IsSame(aVertex2)) {
300               aGroupedShapes.Append(anUngroupedShape);
301               anUngroupedShapes.Remove(anUngroupedIt);
302               isFound = true;
303               break;
304             }
305           }
306           if(isFound) {
307             break;
308           }
309         }
310         if(!anUngroupedIt.More()) {
311           break;
312         }
313       }
314     }
315     aGroups.Append(aGroupedShapes);
316   }
317
318   TopoDS_Compound aCompound;
319   BRep_Builder aBuilder;
320   aBuilder.MakeCompound(aCompound);
321   ListOfShape aCompSolids, aFreeSolids;
322   for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator anIt(aGroups); anIt.More(); anIt.Next()) {
323     TopoDS_Compound aGroupCompound = makeCompound(anIt.Value());
324     GeomShapePtr aGeomShape(new GeomAPI_Shape());
325     aGeomShape->setImpl(new TopoDS_Shape(aGroupCompound));
326     aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
327                                                        GeomAPI_Shape::COMPSOLID,
328                                                        aCompSolids,
329                                                        aFreeSolids);
330     aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
331   }
332
333   if(!aCompound.IsNull()) {
334     aResult->setImpl(new TopoDS_Shape(aCompound));
335   }
336
337   return aResult;
338 }
339
340 //==================================================================================================
341 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
342 {
343   // Bounding box of all objects.
344   Bnd_Box aBndBox;
345
346   // Getting box.
347   for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
348     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
349     BRepBndLib::Add(aShape, aBndBox);
350   }
351
352   if(theEnlarge != 0.0) {
353     // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
354     aBndBox.Enlarge(theEnlarge);
355   }
356
357   Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
358   Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
359   Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
360   std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
361   int aNum = 0;
362   for(int i = 0; i < 2; i++) {
363     for(int j = 0; j < 2; j++) {
364       for(int k = 0; k < 2; k++) {
365         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
366         aResultPoints.push_back(aPnt);
367       }
368     }
369   }
370
371   return aResultPoints;
372 }
373
374 //==================================================================================================
375 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
376 {
377   if (!theFace.get())
378     return std::shared_ptr<GeomAPI_Shape>();
379
380   TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
381   if (aPlaneFace.IsNull())
382     return std::shared_ptr<GeomAPI_Shape>();
383
384   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
385   if (aPlane.IsNull())
386     return std::shared_ptr<GeomAPI_Shape>();
387
388   // make an infinity face on the plane
389   TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
390
391   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
392   aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
393   return aResult;
394 }
395
396 //==================================================================================================
397 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
398                                                                      const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
399 {
400   std::shared_ptr<GeomAPI_Shape> aResultShape;
401
402   if(!thePlane.get()) {
403     return aResultShape;
404   }
405
406   const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
407   if(aShape.ShapeType() != TopAbs_FACE) {
408     return aResultShape;
409   }
410
411   TopoDS_Face aFace = TopoDS::Face(aShape);
412   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
413   if(aSurf.IsNull()) {
414     return aResultShape;
415   }
416
417   GeomLib_IsPlanarSurface isPlanar(aSurf);
418   if(!isPlanar.IsPlanar()) {
419     return aResultShape;
420   }
421
422   if(thePoints.size() != 8) {
423     return aResultShape;
424   }
425
426   const gp_Pln& aFacePln = isPlanar.Plan();
427   Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
428   IntAna_Quadric aQuadric(aFacePln);
429   Standard_Real UMin, UMax, VMin, VMax;
430   UMin = UMax = VMin = VMax = 0;
431   for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
432     const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
433     gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
434     IntAna_IntConicQuad anIntAna(aLin, aQuadric);
435     const gp_Pnt& aPntOnFace = anIntAna.Point(1);
436     Standard_Real aPntU(0), aPntV(0);
437     GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
438     if(aPntU < UMin) UMin = aPntU;
439     if(aPntU > UMax) UMax = aPntU;
440     if(aPntV < VMin) VMin = aPntV;
441     if(aPntV > VMax) VMax = aPntV;
442   }
443   aResultShape.reset(new GeomAPI_Shape);
444   aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
445
446   return aResultShape;
447 }
448
449 //==================================================================================================
450 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
451                                         std::shared_ptr<GeomAPI_Vertex>& theV1,
452                                         std::shared_ptr<GeomAPI_Vertex>& theV2)
453 {
454   if(!theShape.get()) {
455     std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
456     aVertex->setImpl(new TopoDS_Vertex());
457     theV1 = aVertex;
458     theV2 = aVertex;
459     return;
460   }
461
462   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
463   TopoDS_Vertex aV1, aV2;
464   ShapeAnalysis::FindBounds(aShape, aV1, aV2);
465
466   std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
467   aGeomV1->setImpl(new TopoDS_Vertex(aV1));
468   aGeomV2->setImpl(new TopoDS_Vertex(aV2));
469   theV1 = aGeomV1;
470   theV2 = aGeomV2;
471 }
472
473 //==================================================================================================
474 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
475                                                 const std::shared_ptr<GeomAPI_Dir> theDirection,
476                                                 const ListOfShape& theWires,
477                                                 ListOfShape& theFaces)
478 {
479   BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
480                                           theDirection->impl<gp_Dir>()));
481   TopoDS_Face aFace = aMKFace.Face();
482
483   BRepAlgo_FaceRestrictor aFRestrictor;
484   aFRestrictor.Init(aFace, Standard_False, Standard_True);
485   for(ListOfShape::const_iterator anIt = theWires.cbegin();
486       anIt != theWires.cend();
487       ++anIt) {
488     TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
489     aFRestrictor.Add(aWire);
490   }
491
492   aFRestrictor.Perform();
493
494   if(!aFRestrictor.IsDone()) {
495     return;
496   }
497
498   for(; aFRestrictor.More(); aFRestrictor.Next()) {
499     GeomShapePtr aShape(new GeomAPI_Shape());
500     aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
501     theFaces.push_back(aShape);
502   }
503 }
504
505 //==================================================================================================
506 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
507 {
508   TopoDS_Compound aCompound;
509   BRep_Builder aBuilder;
510   aBuilder.MakeCompound(aCompound);
511
512   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
513     aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
514   }
515   BRepBuilderAPI_FindPlane aFindPlane(aCompound);
516
517   if(aFindPlane.Found() != Standard_True) {
518     return std::shared_ptr<GeomAPI_Pln>();
519   }
520
521   Handle(Geom_Plane) aPlane = aFindPlane.Plane();
522   gp_Pnt aLoc = aPlane->Location();
523   gp_Dir aDir = aPlane->Axis().Direction();
524
525   std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
526   std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
527
528   std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
529
530   return aPln;
531 }
532
533 //==================================================================================================
534 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(const std::shared_ptr<GeomAPI_Shape> theSubShape,
535                                                    const std::shared_ptr<GeomAPI_Shape> theBaseShape)
536 {
537   if(!theSubShape.get() || !theBaseShape.get()) {
538     return false;
539   }
540
541   const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
542   const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
543
544   if(aSubShape.ShapeType() == TopAbs_VERTEX) {
545     // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
546     BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
547     aDist.Perform();
548     if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
549       return false;
550     }
551   } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
552     if(aBaseShape.ShapeType() == TopAbs_FACE) {
553       // Check that edge is on face surface.
554       TopoDS_Face aFace = TopoDS::Face(aBaseShape);
555       TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
556       BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
557       aCheck.Perform();
558       if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
559         return false;
560       }
561
562       // Check intersections.
563       TopoDS_Vertex aV1, aV2;
564       ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
565       gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
566       gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
567       for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
568         const TopoDS_Shape& anEdgeOnFace = anExp.Current();
569         BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
570         aDist.Perform();
571         if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
572           // Edge intersect face bound. Check that it is not on edge begin or end.
573           for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
574             gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
575             if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
576                 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
577               return false;
578             }
579           }
580         }
581       }
582
583       // No intersections found. Edge is inside or outside face. Check it.
584       BRepAdaptor_Curve aCurveAdaptor(anEdge);
585       gp_Pnt aPointToCheck = aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() + aCurveAdaptor.LastParameter()) / 2.0);
586       Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
587       ShapeAnalysis_Surface aSAS(aSurface);
588       gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
589       BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
590       if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
591         return false;
592       }
593
594     } else {
595       return false;
596     }
597   } else {
598     return false;
599   }
600
601   return true;
602 }
603
604 //==================================================================================================
605 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
606 {
607   if(!theShape.get()) {
608     return false;
609   }
610
611   BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
612   return (aChecker.IsValid() == Standard_True);
613 }