Salome HOME
Task 3.8. Extrusion to any face
[modules/shaper.git] / src / GeomAlgoAPI / GeomAlgoAPI_ShapeTools.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "GeomAlgoAPI_ShapeTools.h"
21
22 #include "GeomAlgoAPI_SketchBuilder.h"
23
24 #include <GeomAPI_Ax1.h>
25 #include <GeomAPI_Edge.h>
26 #include <GeomAPI_Dir.h>
27 #include <GeomAPI_Face.h>
28 #include <GeomAPI_Pln.h>
29 #include <GeomAPI_Pnt.h>
30 #include <GeomAPI_Wire.h>
31
32 #include <Bnd_Box.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35 #include <BRepAlgo.hxx>
36 #include <BRepAlgo_FaceRestrictor.hxx>
37 #include <BRepBndLib.hxx>
38 #include <BRepBuilderAPI_FindPlane.hxx>
39 #include <BRepBuilderAPI_MakeEdge.hxx>
40 #include <BRepBuilderAPI_MakeFace.hxx>
41 #include <BRepCheck_Analyzer.hxx>
42 #include <BRepExtrema_DistShapeShape.hxx>
43 #include <BRepExtrema_ExtCF.hxx>
44 #include <BRepGProp.hxx>
45 #include <BRepTools.hxx>
46 #include <BRepTools_WireExplorer.hxx>
47 #include <BRepTopAdaptor_FClass2d.hxx>
48 #include <BRepClass_FaceClassifier.hxx>
49 #include <Geom2d_Curve.hxx>
50 #include <Geom2d_Curve.hxx>
51 #include <BRepLib_CheckCurveOnSurface.hxx>
52 #include <BRep_Tool.hxx>
53 #include  <Geom_CylindricalSurface.hxx>
54 #include <Geom_Line.hxx>
55 #include <Geom_Plane.hxx>
56 #include <GeomAPI_ProjectPointOnCurve.hxx>
57 #include <GeomAPI_ShapeIterator.h>
58 #include <GeomLib_IsPlanarSurface.hxx>
59 #include <GeomLib_Tool.hxx>
60 #include <GeomAPI_IntCS.hxx>
61 #include <gp_Pln.hxx>
62 #include <GProp_GProps.hxx>
63 #include <IntAna_IntConicQuad.hxx>
64 #include <IntAna_Quadric.hxx>
65 #include <NCollection_Vector.hxx>
66 #include <ShapeAnalysis.hxx>
67 #include <ShapeAnalysis_Surface.hxx>
68 #include <TopoDS_Builder.hxx>
69 #include <TopoDS_Edge.hxx>
70 #include <TopoDS_Face.hxx>
71 #include <TopoDS_Shape.hxx>
72 #include <TopoDS_Shell.hxx>
73 #include <TopoDS_Vertex.hxx>
74 #include <TopoDS.hxx>
75 #include <TopExp.hxx>
76 #include <TopExp_Explorer.hxx>
77 #include <TopTools_ListIteratorOfListOfShape.hxx>
78
79
80 #include <BOPAlgo_Builder.hxx>
81 #include <BRepBuilderAPI_MakeVertex.hxx>
82 #include <TopoDS_Edge.hxx>
83
84 //==================================================================================================
85 static GProp_GProps props(const TopoDS_Shape& theShape)
86 {
87   GProp_GProps aGProps;
88
89   if (theShape.ShapeType() == TopAbs_EDGE || theShape.ShapeType() == TopAbs_WIRE)
90   {
91     BRepGProp::LinearProperties(theShape, aGProps);
92   }
93   else if (theShape.ShapeType() == TopAbs_FACE || theShape.ShapeType() == TopAbs_SHELL)
94   {
95     const Standard_Real anEps = 1.e-6;
96     BRepGProp::SurfaceProperties(theShape, aGProps, anEps);
97   }
98   else if (theShape.ShapeType() == TopAbs_SOLID || theShape.ShapeType() == TopAbs_COMPSOLID)
99   {
100     BRepGProp::VolumeProperties(theShape, aGProps);
101   }
102   else if (theShape.ShapeType() == TopAbs_COMPOUND)
103   {
104     for (TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next())
105     {
106       aGProps.Add(props(anIt.Value()));
107     }
108   }
109
110   return aGProps;
111 }
112
113 //==================================================================================================
114 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
115 {
116   if(!theShape.get()) {
117     return 0.0;
118   }
119   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
120   if(aShape.IsNull()) {
121     return 0.0;
122   }
123   const Standard_Real anEps = 1.e-6;
124   TopExp_Explorer anExp(aShape, TopAbs_SOLID);
125   if (anExp.More()) { // return volume if there is at least one solid
126     double aVolume = 0.0;
127     for (; anExp.More(); anExp.Next()) {
128       GProp_GProps aGProps;
129       BRepGProp::VolumeProperties(anExp.Current(), aGProps, anEps);
130       aVolume += aGProps.Mass();
131     }
132     return aVolume;
133   }
134   // return surfaces area
135   GProp_GProps aGProps;
136   BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
137   return aGProps.Mass();
138 }
139
140 //==================================================================================================
141 double GeomAlgoAPI_ShapeTools::area (const std::shared_ptr<GeomAPI_Shape> theShape)
142 {
143   GProp_GProps aGProps;
144   if(!theShape.get()) {
145     return 0.0;
146   }
147   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
148   if(aShape.IsNull()) {
149     return 0.0;
150   }
151   const Standard_Real anEps = 1.e-6;
152
153   BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
154   return aGProps.Mass();
155 }
156
157 //==================================================================================================
158 std::shared_ptr<GeomAPI_Pnt>
159   GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
160 {
161   GProp_GProps aGProps;
162   if(!theShape) {
163     return std::shared_ptr<GeomAPI_Pnt>();
164   }
165   const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
166   if(aShape.IsNull()) {
167     return std::shared_ptr<GeomAPI_Pnt>();
168   }
169   gp_Pnt aCentre;
170   if(aShape.ShapeType() == TopAbs_VERTEX) {
171     aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
172   } else {
173     aGProps = props(aShape);
174     aCentre = aGProps.CentreOfMass();
175   }
176
177   return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
178 }
179
180 //==================================================================================================
181 double GeomAlgoAPI_ShapeTools::radius(const std::shared_ptr<GeomAPI_Face>& theCylinder)
182 {
183   double aRadius = -1.0;
184   if (theCylinder->isCylindrical()) {
185     const TopoDS_Shape& aShape = theCylinder->impl<TopoDS_Shape>();
186     Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
187     Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
188     if (!aCyl.IsNull())
189       aRadius = aCyl->Radius();
190   }
191   return aRadius;
192 }
193
194 //==================================================================================================
195 double GeomAlgoAPI_ShapeTools::minimalDistance(const GeomShapePtr& theShape1,
196                                                const GeomShapePtr& theShape2)
197 {
198   const TopoDS_Shape& aShape1 = theShape1->impl<TopoDS_Shape>();
199   const TopoDS_Shape& aShape2 = theShape2->impl<TopoDS_Shape>();
200
201   BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
202   aDist.Perform();
203   return aDist.IsDone() ? aDist.Value() : Precision::Infinite();
204 }
205
206 //==================================================================================================
207 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(
208   const std::shared_ptr<GeomAPI_Shape> theCompound,
209   const GeomAPI_Shape::ShapeType theType,
210   ListOfShape& theResuts)
211 {
212
213   ListOfShape aResCombinedShapes;
214   ListOfShape aResFreeShapes;
215
216   GeomShapePtr aResult = theCompound;
217
218   if(!theCompound.get()) {
219     return aResult;
220   }
221
222   if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
223     return aResult;
224   }
225
226   TopAbs_ShapeEnum aTS = TopAbs_EDGE;
227   TopAbs_ShapeEnum aTA = TopAbs_FACE;
228   if(theType == GeomAPI_Shape::COMPSOLID) {
229     aTS = TopAbs_FACE;
230     aTA = TopAbs_SOLID;
231   }
232
233   // map from the resulting shapes to minimal index of the used shape from theCompound list
234   std::map<GeomShapePtr, int> anInputOrder;
235   // map from ancestors-shapes to the index of shapes in theCompound
236   NCollection_DataMap<TopoDS_Shape, int> anAncestorsOrder;
237
238   // Get free shapes.
239   int anOrder = 0;
240   const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
241   for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next(), anOrder++) {
242     const TopoDS_Shape& aShape = anIter.Value();
243     if(aShape.ShapeType() > aTA) {
244       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
245       aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
246       aResFreeShapes.push_back(aGeomShape);
247       anInputOrder[aGeomShape] = anOrder;
248     } else {
249       for(TopExp_Explorer anExp(aShape, aTA); anExp.More(); anExp.Next()) {
250         anAncestorsOrder.Bind(anExp.Current(), anOrder);
251       }
252     }
253   }
254
255   // Map sub-shapes and shapes.
256   TopTools_IndexedDataMapOfShapeListOfShape aMapSA;
257   TopExp::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
258   if(aMapSA.IsEmpty()) {
259     return aResult;
260   }
261   theResuts.clear();
262
263   // Get all shapes with common sub-shapes and free shapes.
264   NCollection_Map<TopoDS_Shape> aFreeShapes;
265   NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
266   for(TopTools_IndexedDataMapOfShapeListOfShape::Iterator
267       anIter(aMapSA); anIter.More(); anIter.Next()) {
268     const TopoDS_Shape& aShape = anIter.Key();
269     TopTools_ListOfShape& aListOfShape = anIter.ChangeValue();
270     if(aListOfShape.IsEmpty()) {
271       continue;
272     }
273     else if(aListOfShape.Size() == 1) {
274       const TopoDS_Shape& aF = aListOfShape.First();
275       aFreeShapes.Add(aF);
276       aListOfShape.Clear();
277     } else {
278       NCollection_List<TopoDS_Shape> aTempList;
279       NCollection_Map<TopoDS_Shape> aTempMap;
280       for (TopTools_ListOfShape::Iterator aListIt(aListOfShape); aListIt.More(); aListIt.Next()) {
281         aTempList.Append(aListIt.Value());
282         aTempMap.Add(aListIt.Value());
283         aFreeShapes.Remove(aListIt.Value());
284       }
285       aListOfShape.Clear();
286       for(NCollection_List<TopoDS_Shape>::Iterator
287           aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
288         const TopoDS_Shape& aTempShape = aTempIter.Value();
289         for(TopTools_IndexedDataMapOfShapeListOfShape::Iterator
290             anIter(aMapSA); anIter.More(); anIter.Next()) {
291           TopTools_ListOfShape& aTempListOfShape = anIter.ChangeValue();
292           if(aTempListOfShape.IsEmpty()) {
293             continue;
294           } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
295             aTempListOfShape.Clear();
296           } else if(aTempListOfShape.Size() > 1) {
297             TopTools_ListOfShape::Iterator anIt1(aTempListOfShape);
298             for (; anIt1.More(); anIt1.Next()) {
299               if (anIt1.Value() == aTempShape) {
300                 TopTools_ListOfShape::Iterator anIt2(aTempListOfShape);
301                 for (; anIt2.More(); anIt2.Next())
302                 {
303                   if (anIt2.Value() != anIt1.Value()) {
304                     if (aTempMap.Add(anIt2.Value())) {
305                       aTempList.Append(anIt2.Value());
306                       aFreeShapes.Remove(anIt2.Value());
307                     }
308                   }
309                 }
310                 aTempListOfShape.Clear();
311                 break;
312               }
313             }
314           }
315         }
316       }
317       aShapesWithCommonSubshapes.Append(aTempMap);
318     }
319   }
320
321   // Combine shapes with common sub-shapes.
322   for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator
323       anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
324     TopoDS_Shell aShell;
325     TopoDS_CompSolid aCSolid;
326     TopoDS_Builder aBuilder;
327     anOrder = -1;
328     theType ==
329       GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
330     NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
331     for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
332       const TopoDS_Shape& aShape = anExp.Current();
333       if(aShapesMap.Contains(aShape)) {
334         theType ==
335           GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
336         aShapesMap.Remove(aShape);
337         int aThisOrder = anAncestorsOrder.Find(aShape);
338         if (anOrder == -1 || aThisOrder < anOrder)
339           anOrder = aThisOrder; // take the minimum order position
340       }
341     }
342     std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
343     TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) :
344                                                               new TopoDS_Shape(aShell);
345     aGeomShape->setImpl<TopoDS_Shape>(aSh);
346     aResCombinedShapes.push_back(aGeomShape);
347     anInputOrder[aGeomShape] = anOrder;
348   }
349
350   // Adding free shapes.
351   for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
352     const TopoDS_Shape& aShape = anExp.Current();
353     if(aFreeShapes.Contains(aShape)) {
354       std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
355       aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
356       aResFreeShapes.push_back(aGeomShape);
357       anInputOrder[aGeomShape] = anAncestorsOrder.Find(aShape);
358     }
359   }
360
361   if(aResCombinedShapes.size() == 1 && aResFreeShapes.size() == 0) {
362     aResult = aResCombinedShapes.front();
363     theResuts.push_back(aResult);
364   } else if(aResCombinedShapes.size() == 0 && aResFreeShapes.size() == 1) {
365     aResult = aResFreeShapes.front();
366     theResuts.push_back(aResult);
367   } else {
368     TopoDS_Compound aResultComp;
369     TopoDS_Builder aBuilder;
370     aBuilder.MakeCompound(aResultComp);
371     // put to result compound and result list in accordance to the order numbers
372     std::map<GeomShapePtr, int>::iterator anInputIter = anInputOrder.begin();
373     std::map<int, GeomShapePtr> aNums;
374     for(; anInputIter != anInputOrder.end(); anInputIter++)
375       aNums[anInputIter->second] = anInputIter->first;
376     std::map<int, GeomShapePtr>::iterator aNumsIter = aNums.begin();
377     for(; aNumsIter != aNums.end(); aNumsIter++) {
378       aBuilder.Add(aResultComp, (aNumsIter->second)->impl<TopoDS_Shape>());
379       theResuts.push_back(aNumsIter->second);
380     }
381     aResult->setImpl(new TopoDS_Shape(aResultComp));
382   }
383
384   return aResult;
385 }
386
387 //==================================================================================================
388 static void addSimpleShapeToList(const TopoDS_Shape& theShape,
389                                  NCollection_List<TopoDS_Shape>& theList)
390 {
391   if(theShape.IsNull()) {
392     return;
393   }
394
395   if(theShape.ShapeType() == TopAbs_COMPOUND) {
396     for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
397       addSimpleShapeToList(anIt.Value(), theList);
398     }
399   } else {
400     theList.Append(theShape);
401   }
402 }
403
404 //==================================================================================================
405 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
406 {
407   TopoDS_Compound aCompound;
408
409   BRep_Builder aBuilder;
410   aBuilder.MakeCompound(aCompound);
411
412   for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
413     aBuilder.Add(aCompound, anIt.Value());
414   }
415
416   return aCompound;
417 }
418
419 //==================================================================================================
420 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(
421   const std::shared_ptr<GeomAPI_Shape> theCompound)
422 {
423   GeomShapePtr aResult = theCompound;
424
425   if (!theCompound.get()) {
426     return aResult;
427   }
428
429   TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
430   NCollection_List<TopoDS_Shape> anUngroupedShapes, aStillUngroupedShapes;
431   addSimpleShapeToList(anInShape, anUngroupedShapes);
432
433   // Iterate over all shapes and find shapes with shared vertices.
434   TopTools_ListOfShape allVertices;
435   TopTools_DataMapOfShapeListOfShape aVertexShapesMap;
436   for (NCollection_List<TopoDS_Shape>::Iterator aShapesIt(anUngroupedShapes);
437     aShapesIt.More();
438     aShapesIt.Next()) {
439     const TopoDS_Shape& aShape = aShapesIt.Value();
440     for (TopExp_Explorer aShapeExp(aShape, TopAbs_VERTEX);
441       aShapeExp.More();
442       aShapeExp.Next()) {
443       const TopoDS_Shape& aVertex = aShapeExp.Current();
444       if (!aVertexShapesMap.IsBound(aVertex)) {
445         NCollection_List<TopoDS_Shape> aList;
446         aList.Append(aShape);
447         allVertices.Append(aVertex);
448         aVertexShapesMap.Bind(aVertex, aList);
449       }
450       else {
451         if (!aVertexShapesMap.ChangeFind(aVertex).Contains(aShape)) {
452           aVertexShapesMap.ChangeFind(aVertex).Append(aShape);
453         }
454       }
455     }
456   }
457
458   // Iterate over the map and group shapes.
459   NCollection_Vector<TopTools_MapOfShape> aGroups; // groups of shapes connected by vertices
460   while (!allVertices.IsEmpty()) {
461     // Get first group of shapes in map, and then unbind it.
462     const TopoDS_Shape& aKey = allVertices.First();
463     TopTools_ListOfShape aConnectedShapes = aVertexShapesMap.Find(aKey);
464     aVertexShapesMap.UnBind(aKey);
465     allVertices.Remove(aKey);
466     // Iterate over shapes in this group and add to it shapes from groups in map.
467     for (TopTools_ListOfShape::Iterator aConnectedIt(aConnectedShapes);
468       aConnectedIt.More(); aConnectedIt.Next()) {
469       const TopoDS_Shape& aConnected = aConnectedIt.Value();
470       TopTools_ListOfShape aKeysToUnbind;
471       for (TopTools_ListOfShape::Iterator aKeysIt(allVertices);
472         aKeysIt.More();
473         aKeysIt.Next()) {
474         const TopTools_ListOfShape& anOtherConnected = aVertexShapesMap(aKeysIt.Value());
475         if (!anOtherConnected.Contains(aConnected)) {
476           // Other connected group does not contain shape from our connected group
477           continue;
478         }
479         // Other is connected to our, so add them to our connected
480         for (TopTools_ListOfShape::Iterator anOtherIt(anOtherConnected);
481           anOtherIt.More();
482           anOtherIt.Next()) {
483           const TopoDS_Shape& aShape = anOtherIt.Value();
484           if (!aConnectedShapes.Contains(aShape)) {
485             aConnectedShapes.Append(aShape);
486           }
487         }
488         // Save key to unbind from this map.
489         aKeysToUnbind.Append(aKeysIt.Value());
490       }
491       // Unbind groups from map that we added to our group.
492       for (TopTools_ListOfShape::Iterator aKeysIt(aKeysToUnbind);
493         aKeysIt.More();
494         aKeysIt.Next()) {
495         aVertexShapesMap.UnBind(aKeysIt.Value());
496         allVertices.Remove(aKeysIt.Value());
497       }
498     }
499     // Sort shapes from the most complicated to the simplest ones
500     TopTools_MapOfShape aSortedGroup;
501     for (int aST = TopAbs_COMPOUND; aST <= TopAbs_SHAPE; ++aST) {
502       TopTools_ListOfShape::Iterator anIt(aConnectedShapes);
503       while (anIt.More()) {
504         if (anIt.Value().ShapeType() == aST) {
505           aSortedGroup.Add(anIt.Value());
506           aConnectedShapes.Remove(anIt);
507         }
508         else {
509           anIt.Next();
510         }
511       }
512     }
513     aGroups.Append(aSortedGroup);
514   }
515
516   TopoDS_Compound aCompound;
517   BRep_Builder aBuilder;
518   aBuilder.MakeCompound(aCompound);
519   ListOfShape aSolids;
520   for (NCollection_Vector<TopTools_MapOfShape>::Iterator anIt(aGroups); anIt.More(); anIt.Next()) {
521     const TopTools_MapOfShape& aGroup = anIt.ChangeValue();
522     GeomShapePtr aGeomShape(new GeomAPI_Shape());
523     if(aGroup.Size() == 1) {
524       TopTools_MapOfShape::Iterator aOneShapeIter(aGroup);
525       aGeomShape->setImpl(new TopoDS_Shape(aOneShapeIter.Value()));
526     } else {
527       // make sub-shapes in the group have order same as in original shape
528       TopTools_ListOfShape anOrderedGoup;
529       NCollection_List<TopoDS_Shape>::Iterator anUngrouped(anUngroupedShapes);
530       for (; anUngrouped.More(); anUngrouped.Next()) {
531         if (aGroup.Contains(anUngrouped.Value()))
532           anOrderedGoup.Append(anUngrouped.Value());
533       }
534       aGeomShape->setImpl(new TopoDS_Shape(makeCompound(anOrderedGoup)));
535       aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
536                                                          GeomAPI_Shape::COMPSOLID,
537                                                          aSolids);
538     }
539     aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
540   }
541
542   if(!aCompound.IsNull()) {
543     aResult->setImpl(new TopoDS_Shape(aCompound));
544   }
545
546   return aResult;
547 }
548
549 //==================================================================================================
550 bool GeomAlgoAPI_ShapeTools::hasSharedTopology(const ListOfShape& theShapes,
551                                                const GeomAPI_Shape::ShapeType theShapeType)
552 {
553   TopTools_IndexedMapOfShape aSubs;
554   for (ListOfShape::const_iterator anIt = theShapes.begin(); anIt != theShapes.end(); ++anIt) {
555     TopTools_IndexedMapOfShape aCurSubs;
556     TopExp::MapShapes((*anIt)->impl<TopoDS_Shape>(), (TopAbs_ShapeEnum)theShapeType, aCurSubs);
557     for (TopTools_IndexedMapOfShape::Iterator aSubIt(aCurSubs); aSubIt.More(); aSubIt.Next()) {
558       if (aSubs.Contains(aSubIt.Value()))
559         return true;
560       else
561         aSubs.Add(aSubIt.Value());
562     }
563   }
564   return false;
565 }
566
567 //==================================================================================================
568 std::list<std::shared_ptr<GeomAPI_Pnt> >
569   GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
570 {
571   // Bounding box of all objects.
572   Bnd_Box aBndBox;
573
574   // Getting box.
575   for (ListOfShape::const_iterator
576     anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
577     const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
578     BRepBndLib::Add(aShape, aBndBox);
579   }
580
581   if(theEnlarge != 0.0) {
582     // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
583     aBndBox.Enlarge(theEnlarge);
584   }
585
586   Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
587   Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
588   Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
589   std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
590   int aNum = 0;
591   for(int i = 0; i < 2; i++) {
592     for(int j = 0; j < 2; j++) {
593       for(int k = 0; k < 2; k++) {
594         std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
595         aResultPoints.push_back(aPnt);
596       }
597     }
598   }
599
600   return aResultPoints;
601 }
602
603 //==================================================================================================
604 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(
605   const std::shared_ptr<GeomAPI_Shape> thePlane,
606   const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
607 {
608   std::shared_ptr<GeomAPI_Face> aResultFace;
609
610   if(!thePlane.get()) {
611     return aResultFace;
612   }
613
614   const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
615   if(aShape.ShapeType() != TopAbs_FACE) {
616     return aResultFace;
617   }
618
619   TopoDS_Face aFace = TopoDS::Face(aShape);
620   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
621   if(aSurf.IsNull()) {
622     return aResultFace;
623   }
624
625   GeomLib_IsPlanarSurface isPlanar(aSurf);
626   if(!isPlanar.IsPlanar()) {
627     return aResultFace;
628   }
629
630   if(thePoints.size() != 8) {
631     return aResultFace;
632   }
633
634   const gp_Pln& aFacePln = isPlanar.Plan();
635   Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
636   IntAna_Quadric aQuadric(aFacePln);
637   Standard_Real UMin, UMax, VMin, VMax;
638   UMin = UMax = VMin = VMax = 0;
639   for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
640        aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
641     const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
642     gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
643     IntAna_IntConicQuad anIntAna(aLin, aQuadric);
644     const gp_Pnt& aPntOnFace = anIntAna.Point(1);
645     Standard_Real aPntU(0), aPntV(0);
646     GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
647     if(aPntU < UMin) UMin = aPntU;
648     if(aPntU > UMax) UMax = aPntU;
649     if(aPntV < VMin) VMin = aPntV;
650     if(aPntV > VMax) VMax = aPntV;
651   }
652   aResultFace.reset(new GeomAPI_Face());
653   aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
654
655   return aResultFace;
656 }
657
658 //==================================================================================================
659 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
660                                         std::shared_ptr<GeomAPI_Vertex>& theV1,
661                                         std::shared_ptr<GeomAPI_Vertex>& theV2)
662 {
663   static GeomVertexPtr aVertex;
664   if (!aVertex) {
665     aVertex = GeomVertexPtr(new GeomAPI_Vertex);
666     aVertex->setImpl(new TopoDS_Vertex());
667   }
668
669   theV1 = aVertex;
670   theV2 = aVertex;
671
672   if (theShape) {
673     const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
674     TopoDS_Vertex aV1, aV2;
675     ShapeAnalysis::FindBounds(aShape, aV1, aV2);
676
677     std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
678     aGeomV1->setImpl(new TopoDS_Vertex(aV1));
679     aGeomV2->setImpl(new TopoDS_Vertex(aV2));
680     theV1 = aGeomV1;
681     theV2 = aGeomV2;
682   }
683 }
684
685 //==================================================================================================
686 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
687                                                 const std::shared_ptr<GeomAPI_Dir> theDirection,
688                                                 const ListOfShape& theWires,
689                                                 ListOfShape& theFaces)
690 {
691   BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
692                                           theDirection->impl<gp_Dir>()));
693   TopoDS_Face aFace = aMKFace.Face();
694
695   BRepAlgo_FaceRestrictor aFRestrictor;
696   aFRestrictor.Init(aFace, Standard_False, Standard_True);
697   for(ListOfShape::const_iterator anIt = theWires.cbegin();
698       anIt != theWires.cend();
699       ++anIt) {
700     TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
701     aFRestrictor.Add(aWire);
702   }
703
704   aFRestrictor.Perform();
705
706   if(!aFRestrictor.IsDone()) {
707     return;
708   }
709
710   for(; aFRestrictor.More(); aFRestrictor.Next()) {
711     GeomShapePtr aShape(new GeomAPI_Shape());
712     aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
713     theFaces.push_back(aShape);
714   }
715 }
716
717 //==================================================================================================
718 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
719 {
720   TopoDS_Compound aCompound;
721   BRep_Builder aBuilder;
722   aBuilder.MakeCompound(aCompound);
723
724   for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
725     aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
726   }
727   BRepBuilderAPI_FindPlane aFindPlane(aCompound);
728
729   if(aFindPlane.Found() != Standard_True) {
730     return std::shared_ptr<GeomAPI_Pln>();
731   }
732
733   Handle(Geom_Plane) aPlane = aFindPlane.Plane();
734   gp_Pnt aLoc = aPlane->Location();
735   gp_Dir aDir = aPlane->Axis().Direction();
736
737   std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
738   std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
739
740   std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
741
742   return aPln;
743 }
744
745 //==================================================================================================
746 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(
747   const std::shared_ptr<GeomAPI_Shape> theSubShape,
748   const std::shared_ptr<GeomAPI_Shape> theBaseShape)
749 {
750   if(!theSubShape.get() || !theBaseShape.get()) {
751     return false;
752   }
753
754   const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
755   const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
756
757   if(aSubShape.ShapeType() == TopAbs_VERTEX) {
758     // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
759     BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
760     aDist.Perform();
761     if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
762       return false;
763     }
764   } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
765     if(aBaseShape.ShapeType() == TopAbs_FACE) {
766       // Check that edge is on face surface.
767       TopoDS_Face aFace = TopoDS::Face(aBaseShape);
768       TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
769       BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
770       aCheck.Perform();
771       if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
772         return false;
773       }
774
775       // Check intersections.
776       TopoDS_Vertex aV1, aV2;
777       ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
778       gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
779       gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
780       for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
781         const TopoDS_Shape& anEdgeOnFace = anExp.Current();
782         BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
783         aDist.Perform();
784         if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
785           // Edge intersect face bound. Check that it is not on edge begin or end.
786           for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
787             gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
788             if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
789                 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
790               return false;
791             }
792           }
793         }
794       }
795
796       // No intersections found. Edge is inside or outside face. Check it.
797       BRepAdaptor_Curve aCurveAdaptor(anEdge);
798       gp_Pnt aPointToCheck =
799         aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() +
800                               aCurveAdaptor.LastParameter()) / 2.0);
801       Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
802       ShapeAnalysis_Surface aSAS(aSurface);
803       gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
804       BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
805       if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
806         return false;
807       }
808
809     } else {
810       return false;
811     }
812   } else {
813     return false;
814   }
815
816   return true;
817 }
818
819 //==================================================================================================
820 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
821 {
822   if(!theShape.get()) {
823     return false;
824   }
825
826   BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
827   return (aChecker.IsValid() == Standard_True);
828 }
829
830 //==================================================================================================
831 std::shared_ptr<GeomAPI_Shape>
832   GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
833 {
834   GeomShapePtr anOuterWire;
835
836   if(!theFace.get() || !theFace->isFace()) {
837     return anOuterWire;
838   }
839
840   TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
841   TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
842
843   anOuterWire.reset(new GeomAPI_Shape());
844   anOuterWire->setImpl(new TopoDS_Shape(aWire));
845
846   return anOuterWire;
847 }
848
849 //==================================================================================================
850 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
851                                         const std::shared_ptr<GeomAPI_Face> theFace)
852 {
853   if(!theEdge.get() || !theFace.get()) {
854     return false;
855   }
856
857   TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
858   TopoDS_Face aFace  = TopoDS::Face(theFace->impl<TopoDS_Shape>());
859
860   BRepExtrema_ExtCF anExt(anEdge, aFace);
861   return anExt.IsParallel() == Standard_True;
862 }
863
864 //==================================================================================================
865 std::list<std::shared_ptr<GeomAPI_Vertex> > GeomAlgoAPI_ShapeTools::intersect(
866   const std::shared_ptr<GeomAPI_Edge> theEdge, const std::shared_ptr<GeomAPI_Face> theFace,
867   const bool thePointsOutsideFace)
868 {
869   std::list<std::shared_ptr<GeomAPI_Vertex> > aResult;
870   if(!theEdge.get() || !theFace.get()) {
871     return aResult;
872   }
873
874   TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
875   double aFirstOnCurve, aLastOnCurve;
876   Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
877
878   TopoDS_Face aFace  = TopoDS::Face(theFace->impl<TopoDS_Shape>());
879   Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
880
881   GeomAPI_IntCS anIntAlgo(aCurve, aSurf);
882   if (!anIntAlgo.IsDone())
883     return aResult;
884   // searching for points-intersection
885   for(int anIntNum = 1; anIntNum <= anIntAlgo.NbPoints() + anIntAlgo.NbSegments(); anIntNum++) {
886     gp_Pnt anInt;
887     if (anIntNum <= anIntAlgo.NbPoints()) {
888       anInt = anIntAlgo.Point(anIntNum);
889     } else { // take the middle point on the segment of the intersection
890       Handle(Geom_Curve) anIntCurve = anIntAlgo.Segment(anIntNum - anIntAlgo.NbPoints());
891       anIntCurve->D0((anIntCurve->FirstParameter() + anIntCurve->LastParameter()) / 2., anInt);
892     }
893     aResult.push_back(std::shared_ptr<GeomAPI_Vertex>(
894       new GeomAPI_Vertex(anInt.X(), anInt.Y(), anInt.Z())));
895   }
896   return aResult;
897 }
898
899 //==================================================================================================
900 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
901                                       const GeomAlgoAPI_ShapeTools::PointToRefsMap& thePointsInfo,
902                                       std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
903 {
904   // to split shape at least one point should be presented in the points container
905   if (thePointsInfo.empty())
906     return;
907
908     // General Fuse to split edge by vertices
909   BOPAlgo_Builder aBOP;
910   TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
911   // Rebuild closed edge to place vertex to one of split points.
912   // This will prevent edge to be split on same vertex.
913   if (BRep_Tool::IsClosed(aBaseEdge))
914   {
915     Standard_Real aFirst, aLast;
916     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
917
918     PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
919     std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
920     gp_Pnt aPoint(aPnt->x(), aPnt->y(), aPnt->z());
921
922     TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
923     aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
924     aBaseEdge.Orientation(anOrientation);
925   }
926   aBOP.AddArgument(aBaseEdge);
927
928   PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
929   for (; aPIt != thePointsInfo.end(); ++aPIt) {
930     std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
931     TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
932     aBOP.AddArgument(aV);
933   }
934
935   aBOP.Perform();
936   if (aBOP.HasErrors())
937     return;
938
939   // Collect splits
940   const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
941   TopTools_ListIteratorOfListOfShape anIt(aSplits);
942   for (; anIt.More(); anIt.Next()) {
943     std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
944     anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
945     theShapes.insert(anEdge);
946   }
947 }
948
949 //==================================================================================================
950 void GeomAlgoAPI_ShapeTools::splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
951                                           const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
952                                           std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
953 {
954   // General Fuse to split edge by vertices
955   BOPAlgo_Builder aBOP;
956   TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
957   // Rebuild closed edge to place vertex to one of split points.
958   // This will prevent edge to be split on seam vertex.
959   if (BRep_Tool::IsClosed(aBaseEdge))
960   {
961     Standard_Real aFirst, aLast;
962     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
963
964     std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
965     gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
966
967     TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
968     aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
969     aBaseEdge.Orientation(anOrientation);
970   }
971   aBOP.AddArgument(aBaseEdge);
972
973   std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
974   for (; aPtIt != thePoints.end(); ++aPtIt) {
975     std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
976     TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
977     aBOP.AddArgument(aV);
978   }
979
980   aBOP.Perform();
981   if (aBOP.HasErrors())
982     return;
983
984   // Collect splits
985   const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
986   TopTools_ListIteratorOfListOfShape anIt(aSplits);
987   for (; anIt.More(); anIt.Next()) {
988     std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
989     anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
990     theShapes.insert(anEdge);
991   }
992 }
993
994 //==================================================================================================
995 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
996                                   const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
997                                   const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
998 {
999   std::shared_ptr<GeomAPI_Shape> aResultShape;
1000
1001   if (thePoints.size() == 2) {
1002     std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
1003     std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
1004     aPntIt++;
1005     std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
1006
1007     std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
1008                                                               aLast = theShapes.end();
1009     for (; anIt != aLast; anIt++) {
1010       GeomShapePtr aShape = *anIt;
1011       std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
1012       if (anEdge.get()) {
1013         std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
1014         std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
1015         if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
1016             anEdgeLastPoint->isEqual(aLastPoint))
1017             aResultShape = aShape;
1018       }
1019     }
1020   }
1021
1022   return aResultShape;
1023 }
1024
1025 //==================================================================================================
1026 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
1027 std::shared_ptr<GeomAPI_Dir> GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(
1028                                     const std::shared_ptr<GeomAPI_Shape> theBaseShape,
1029                                     const std::shared_ptr<GeomAPI_Ax1> theAxis)
1030 {
1031   gp_Pnt aCentreOfMassPoint =
1032     GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape)->impl<gp_Pnt>();
1033   Handle(Geom_Line) aLine = new Geom_Line(theAxis->impl<gp_Ax1>());
1034   GeomAPI_ProjectPointOnCurve aPrjTool(aCentreOfMassPoint, aLine);
1035   gp_Pnt aPoint = aPrjTool.NearestPoint();
1036
1037   std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(aCentreOfMassPoint.X()-aPoint.X(),
1038                                                     aCentreOfMassPoint.Y()-aPoint.Y(),
1039                                                     aCentreOfMassPoint.Z()-aPoint.Z()));
1040   return aDir;
1041 }
1042 #endif
1043
1044 //==================================================================================================
1045 static TopoDS_Wire fixParametricGaps(const TopoDS_Wire& theWire)
1046 {
1047   TopoDS_Wire aFixedWire;
1048   Handle(Geom_Curve) aPrevCurve;
1049   double aPrevLastParam = 0.0;
1050
1051   BRep_Builder aBuilder;
1052   aBuilder.MakeWire(aFixedWire);
1053
1054   BRepTools_WireExplorer aWExp(theWire);
1055   for (; aWExp.More(); aWExp.Next()) {
1056     TopoDS_Edge anEdge = aWExp.Current();
1057     double aFirst, aLast;
1058     Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
1059     if (aCurve == aPrevCurve) {
1060       // if parametric gap occurs, create new edge based on the copied curve
1061       aCurve = Handle(Geom_Curve)::DownCast(aCurve->Copy());
1062       TopoDS_Vertex aV1, aV2;
1063       TopExp::Vertices(anEdge, aV1, aV2);
1064       anEdge = TopoDS::Edge(anEdge.EmptyCopied());
1065       aBuilder.UpdateEdge(anEdge, aCurve, BRep_Tool::Tolerance(anEdge));
1066       aBuilder.Add(anEdge, aV1);
1067       aBuilder.Add(anEdge, aV2);
1068     }
1069
1070     aBuilder.Add(aFixedWire, anEdge);
1071
1072     aPrevCurve = aCurve;
1073     aPrevLastParam = aLast;
1074   }
1075
1076   return aFixedWire;
1077 }
1078
1079 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_ShapeTools::wireToEdge(
1080       const std::shared_ptr<GeomAPI_Wire>& theWire)
1081 {
1082   GeomEdgePtr anEdge;
1083   if (theWire) {
1084     TopoDS_Wire aWire = theWire->impl<TopoDS_Wire>();
1085     // Workaround: when concatenate a wire consisting of two edges based on the same B-spline curve
1086     // (non-periodic, but having equal start and end points), first of which is placed at the end
1087     // on the curve and second is placed at the start, this workaround copies second curve to avoid
1088     // treating these edges as a single curve by setting trim parameters.
1089     aWire = fixParametricGaps(aWire);
1090     TopoDS_Edge aNewEdge = BRepAlgo::ConcatenateWireC0(aWire);
1091     anEdge = GeomEdgePtr(new GeomAPI_Edge);
1092     anEdge->setImpl(new TopoDS_Edge(aNewEdge));
1093   }
1094   return anEdge;
1095 }
1096
1097 ListOfShape GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(const GeomShapePtr& theShape)
1098 {
1099   ListOfShape aSubShapes;
1100
1101   if (!theShape->isCompound() && !theShape->isCompSolid() &&
1102       !theShape->isShell() && !theShape->isWire()) {
1103     return aSubShapes;
1104   }
1105
1106   for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
1107     GeomShapePtr aSubShape = anIt.current();
1108     if (aSubShape->isVertex() || aSubShape->isEdge() ||
1109         aSubShape->isFace() || aSubShape->isSolid()) {
1110       aSubShapes.push_back(aSubShape);
1111     } else {
1112       aSubShapes.splice(aSubShapes.end(), getLowLevelSubShapes(aSubShape));
1113     }
1114   }
1115
1116   return aSubShapes;
1117 }