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