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