Salome HOME
Added option to create Construction Point by projection point on plane. Fixed CPP...
[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_Edge.h>
12 #include <GeomAPI_Dir.h>
13 #include <GeomAPI_Face.h>
14 #include <GeomAPI_Pln.h>
15 #include <GeomAPI_Pnt.h>
16
17 #include <Bnd_Box.hxx>
18 #include <BOPTools.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRepAdaptor_Curve.hxx>
21 #include <BRepAlgo_FaceRestrictor.hxx>
22 #include <BRepBndLib.hxx>
23 #include <BRepBuilderAPI_FindPlane.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <BRepCheck_Analyzer.hxx>
26 #include <BRepExtrema_DistShapeShape.hxx>
27 #include <BRepExtrema_ExtCF.hxx>
28 #include <BRepGProp.hxx>
29 #include <BRepTools.hxx>
30 #include <BRepTopAdaptor_FClass2d.hxx>
31 #include <Geom2d_Curve.hxx>
32 #include <Handle_Geom2d_Curve.hxx>
33 #include <BRepLib_CheckCurveOnSurface.hxx>
34 #include <BRep_Tool.hxx>
35 #include <Geom_Plane.hxx>
36 #include <GeomLib_IsPlanarSurface.hxx>
37 #include <GeomLib_Tool.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() == 0 && theFreeShapes.size() == 1) {
227     aResult = theFreeShapes.front();
228   } else {
229     TopoDS_Compound aResultComp;
230     TopoDS_Builder aBuilder;
231     aBuilder.MakeCompound(aResultComp);
232     for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin(); anIter != theCombinedShapes.cend(); anIter++) {
233       aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
234     }
235     for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin(); anIter != theFreeShapes.cend(); anIter++) {
236       aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
237     }
238     aResult->setImpl(new TopoDS_Shape(aResultComp));
239   }
240
241   return aResult;
242 }
243
244 //==================================================================================================
245 static void addSimpleShapeToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
246 {
247   if(theShape.IsNull()) {
248     return;
249   }
250
251   if(theShape.ShapeType() == TopAbs_COMPOUND) {
252     for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
253       addSimpleShapeToList(anIt.Value(), theList);
254     }
255   } else {
256     theList.Append(theShape);
257   }
258 }
259
260 //==================================================================================================
261 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
262 {
263   TopoDS_Compound aCompound;
264
265   BRep_Builder aBuilder;
266   aBuilder.MakeCompound(aCompound);
267
268   for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
269     aBuilder.Add(aCompound, anIt.Value());
270   }
271
272   return aCompound;
273 }
274
275 //==================================================================================================
276 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(const std::shared_ptr<GeomAPI_Shape> theCompound)
277 {
278   GeomShapePtr aResult = theCompound;
279
280   if(!theCompound.get()) {
281     return aResult;
282   }
283
284   TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
285   NCollection_List<TopoDS_Shape> anUngroupedShapes;
286   addSimpleShapeToList(anInShape, anUngroupedShapes);
287
288   NCollection_Vector<NCollection_List<TopoDS_Shape>> aGroups;
289   while(!anUngroupedShapes.IsEmpty()) {
290     NCollection_List<TopoDS_Shape> aGroupedShapes;
291     aGroupedShapes.Append(anUngroupedShapes.First());
292     anUngroupedShapes.RemoveFirst();
293     for(NCollection_List<TopoDS_Shape>::Iterator aGroupIt(aGroupedShapes); aGroupIt.More(); aGroupIt.Next()) {
294       const TopoDS_Shape& aGroupShape = aGroupIt.Value();
295       for(NCollection_List<TopoDS_Shape>::Iterator anUngroupedIt(anUngroupedShapes); anUngroupedIt.More(); anUngroupedIt.Next()) {
296         const TopoDS_Shape& anUngroupedShape = anUngroupedIt.Value();
297         bool isFound = false;
298         for(TopExp_Explorer aGroupShapeExp(aGroupShape, TopAbs_VERTEX); aGroupShapeExp.More(); aGroupShapeExp.Next()) {
299           const TopoDS_Shape& aVertex1 = aGroupShapeExp.Current();
300           for(TopExp_Explorer anUngroupedShapeExp(anUngroupedShape, TopAbs_VERTEX); anUngroupedShapeExp.More(); anUngroupedShapeExp.Next()) {
301             const TopoDS_Shape& aVertex2 = anUngroupedShapeExp.Current();
302             if(aVertex1.IsSame(aVertex2)) {
303               aGroupedShapes.Append(anUngroupedShape);
304               anUngroupedShapes.Remove(anUngroupedIt);
305               isFound = true;
306               break;
307             }
308           }
309           if(isFound) {
310             break;
311           }
312         }
313         if(!anUngroupedIt.More()) {
314           break;
315         }
316       }
317     }
318     aGroups.Append(aGroupedShapes);
319   }
320
321   if(aGroups.Size() == 1) {
322     NCollection_List<TopoDS_Shape> aGroup = aGroups.First();
323     GeomShapePtr aGeomShape(new GeomAPI_Shape());
324     aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
325     ListOfShape aCompSolids, aFreeSolids;
326     aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
327                                                         GeomAPI_Shape::COMPSOLID,
328                                                         aCompSolids,
329                                                         aFreeSolids);
330     aResult = aGeomShape;
331   } else {
332     TopoDS_Compound aCompound;
333     BRep_Builder aBuilder;
334     aBuilder.MakeCompound(aCompound);
335     ListOfShape aCompSolids, aFreeSolids;
336     for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator anIt(aGroups); anIt.More(); anIt.Next()) {
337       NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
338       GeomShapePtr aGeomShape(new GeomAPI_Shape());
339       if(aGroup.Size() == 1) {
340         aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
341       } else {
342         aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
343         aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
344                                                            GeomAPI_Shape::COMPSOLID,
345                                                            aCompSolids,
346                                                            aFreeSolids);
347       }
348       aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
349     }
350
351     if(!aCompound.IsNull()) {
352       aResult->setImpl(new TopoDS_Shape(aCompound));
353     }
354   }
355
356   return aResult;
357 }
358
359 //==================================================================================================
360 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
361 {
362   // Bounding box of all objects.
363   Bnd_Box aBndBox;
364
365   // Getting box.
366   for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
367     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
368     BRepBndLib::Add(aShape, aBndBox);
369   }
370
371   if(theEnlarge != 0.0) {
372     // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
373     aBndBox.Enlarge(theEnlarge);
374   }
375
376   Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
377   Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
378   Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
379   std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
380   int aNum = 0;
381   for(int i = 0; i < 2; i++) {
382     for(int j = 0; j < 2; j++) {
383       for(int k = 0; k < 2; k++) {
384         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
385         aResultPoints.push_back(aPnt);
386       }
387     }
388   }
389
390   return aResultPoints;
391 }
392
393 //==================================================================================================
394 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
395 {
396   if (!theFace.get())
397     return std::shared_ptr<GeomAPI_Shape>();
398
399   TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
400   if (aPlaneFace.IsNull())
401     return std::shared_ptr<GeomAPI_Shape>();
402
403   Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
404   if (aPlane.IsNull())
405     return std::shared_ptr<GeomAPI_Shape>();
406
407   // make an infinity face on the plane
408   TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
409
410   std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
411   aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
412   return aResult;
413 }
414
415 //==================================================================================================
416 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
417                                                                      const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
418 {
419   std::shared_ptr<GeomAPI_Shape> aResultShape;
420
421   if(!thePlane.get()) {
422     return aResultShape;
423   }
424
425   const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
426   if(aShape.ShapeType() != TopAbs_FACE) {
427     return aResultShape;
428   }
429
430   TopoDS_Face aFace = TopoDS::Face(aShape);
431   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
432   if(aSurf.IsNull()) {
433     return aResultShape;
434   }
435
436   GeomLib_IsPlanarSurface isPlanar(aSurf);
437   if(!isPlanar.IsPlanar()) {
438     return aResultShape;
439   }
440
441   if(thePoints.size() != 8) {
442     return aResultShape;
443   }
444
445   const gp_Pln& aFacePln = isPlanar.Plan();
446   Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
447   IntAna_Quadric aQuadric(aFacePln);
448   Standard_Real UMin, UMax, VMin, VMax;
449   UMin = UMax = VMin = VMax = 0;
450   for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
451     const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
452     gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
453     IntAna_IntConicQuad anIntAna(aLin, aQuadric);
454     const gp_Pnt& aPntOnFace = anIntAna.Point(1);
455     Standard_Real aPntU(0), aPntV(0);
456     GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
457     if(aPntU < UMin) UMin = aPntU;
458     if(aPntU > UMax) UMax = aPntU;
459     if(aPntV < VMin) VMin = aPntV;
460     if(aPntV > VMax) VMax = aPntV;
461   }
462   aResultShape.reset(new GeomAPI_Shape);
463   aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
464
465   return aResultShape;
466 }
467
468 //==================================================================================================
469 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
470                                         std::shared_ptr<GeomAPI_Vertex>& theV1,
471                                         std::shared_ptr<GeomAPI_Vertex>& theV2)
472 {
473   if(!theShape.get()) {
474     std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
475     aVertex->setImpl(new TopoDS_Vertex());
476     theV1 = aVertex;
477     theV2 = aVertex;
478     return;
479   }
480
481   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
482   TopoDS_Vertex aV1, aV2;
483   ShapeAnalysis::FindBounds(aShape, aV1, aV2);
484
485   std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
486   aGeomV1->setImpl(new TopoDS_Vertex(aV1));
487   aGeomV2->setImpl(new TopoDS_Vertex(aV2));
488   theV1 = aGeomV1;
489   theV2 = aGeomV2;
490 }
491
492 //==================================================================================================
493 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
494                                                 const std::shared_ptr<GeomAPI_Dir> theDirection,
495                                                 const ListOfShape& theWires,
496                                                 ListOfShape& theFaces)
497 {
498   BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
499                                           theDirection->impl<gp_Dir>()));
500   TopoDS_Face aFace = aMKFace.Face();
501
502   BRepAlgo_FaceRestrictor aFRestrictor;
503   aFRestrictor.Init(aFace, Standard_False, Standard_True);
504   for(ListOfShape::const_iterator anIt = theWires.cbegin();
505       anIt != theWires.cend();
506       ++anIt) {
507     TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
508     aFRestrictor.Add(aWire);
509   }
510
511   aFRestrictor.Perform();
512
513   if(!aFRestrictor.IsDone()) {
514     return;
515   }
516
517   for(; aFRestrictor.More(); aFRestrictor.Next()) {
518     GeomShapePtr aShape(new GeomAPI_Shape());
519     aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
520     theFaces.push_back(aShape);
521   }
522 }
523
524 //==================================================================================================
525 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
526 {
527   TopoDS_Compound aCompound;
528   BRep_Builder aBuilder;
529   aBuilder.MakeCompound(aCompound);
530
531   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
532     aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
533   }
534   BRepBuilderAPI_FindPlane aFindPlane(aCompound);
535
536   if(aFindPlane.Found() != Standard_True) {
537     return std::shared_ptr<GeomAPI_Pln>();
538   }
539
540   Handle(Geom_Plane) aPlane = aFindPlane.Plane();
541   gp_Pnt aLoc = aPlane->Location();
542   gp_Dir aDir = aPlane->Axis().Direction();
543
544   std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
545   std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
546
547   std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
548
549   return aPln;
550 }
551
552 //==================================================================================================
553 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(const std::shared_ptr<GeomAPI_Shape> theSubShape,
554                                                    const std::shared_ptr<GeomAPI_Shape> theBaseShape)
555 {
556   if(!theSubShape.get() || !theBaseShape.get()) {
557     return false;
558   }
559
560   const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
561   const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
562
563   if(aSubShape.ShapeType() == TopAbs_VERTEX) {
564     // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
565     BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
566     aDist.Perform();
567     if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
568       return false;
569     }
570   } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
571     if(aBaseShape.ShapeType() == TopAbs_FACE) {
572       // Check that edge is on face surface.
573       TopoDS_Face aFace = TopoDS::Face(aBaseShape);
574       TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
575       BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
576       aCheck.Perform();
577       if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
578         return false;
579       }
580
581       // Check intersections.
582       TopoDS_Vertex aV1, aV2;
583       ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
584       gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
585       gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
586       for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
587         const TopoDS_Shape& anEdgeOnFace = anExp.Current();
588         BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
589         aDist.Perform();
590         if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
591           // Edge intersect face bound. Check that it is not on edge begin or end.
592           for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
593             gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
594             if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
595                 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
596               return false;
597             }
598           }
599         }
600       }
601
602       // No intersections found. Edge is inside or outside face. Check it.
603       BRepAdaptor_Curve aCurveAdaptor(anEdge);
604       gp_Pnt aPointToCheck = aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() + aCurveAdaptor.LastParameter()) / 2.0);
605       Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
606       ShapeAnalysis_Surface aSAS(aSurface);
607       gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
608       BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
609       if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
610         return false;
611       }
612
613     } else {
614       return false;
615     }
616   } else {
617     return false;
618   }
619
620   return true;
621 }
622
623 //==================================================================================================
624 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
625 {
626   if(!theShape.get()) {
627     return false;
628   }
629
630   BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
631   return (aChecker.IsValid() == Standard_True);
632 }
633
634 //==================================================================================================
635 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
636 {
637   GeomShapePtr anOuterWire;
638
639   if(!theFace.get() || !theFace->isFace()) {
640     return anOuterWire;
641   }
642
643   TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
644   TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
645
646   anOuterWire.reset(new GeomAPI_Shape());
647   anOuterWire->setImpl(new TopoDS_Shape(aWire));
648
649   return anOuterWire;
650 }
651
652 //==================================================================================================
653 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
654                                         const std::shared_ptr<GeomAPI_Face> theFace)
655 {
656   if(!theEdge.get() || !theFace.get()) {
657     return false;
658   }
659
660   TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
661   TopoDS_Face aFace  = TopoDS::Face(theFace->impl<TopoDS_Shape>());
662
663   BRepExtrema_ExtCF anExt(anEdge, aFace);
664   return anExt.IsParallel() == Standard_True;
665 }