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