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