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