1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: GeomAlgoAPI_ShapeTools.h
4 // Created: 3 August 2015
5 // Author: Dmitry Bobylev
7 #include "GeomAlgoAPI_ShapeTools.h"
9 #include "GeomAlgoAPI_SketchBuilder.h"
11 #include <GeomAPI_Edge.h>
12 #include <GeomAPI_Dir.h>
13 #include <GeomAPI_Face.h>
14 #include <GeomAPI_Pln.h>
15 #include <GeomAPI_Pnt.h>
17 #include <Bnd_Box.hxx>
18 #include <BOPTools.hxx>
19 #include <BRep_Builder.hxx>
20 #include <BRepAdaptor_Curve.hxx>
21 #include <BRepAlgo_FaceRestrictor.hxx>
22 #include <BRepBndLib.hxx>
23 #include <BRepBuilderAPI_FindPlane.hxx>
24 #include <BRepBuilderAPI_MakeFace.hxx>
25 #include <BRepCheck_Analyzer.hxx>
26 #include <BRepExtrema_DistShapeShape.hxx>
27 #include <BRepExtrema_ExtCF.hxx>
28 #include <BRepGProp.hxx>
29 #include <BRepTools.hxx>
30 #include <BRepTopAdaptor_FClass2d.hxx>
31 #include <Geom2d_Curve.hxx>
32 #include <Handle_Geom2d_Curve.hxx>
33 #include <BRepLib_CheckCurveOnSurface.hxx>
34 #include <BRep_Tool.hxx>
35 #include <Geom_Plane.hxx>
36 #include <GeomLib_IsPlanarSurface.hxx>
37 #include <GeomLib_Tool.hxx>
39 #include <GProp_GProps.hxx>
40 #include <IntAna_IntConicQuad.hxx>
41 #include <IntAna_Quadric.hxx>
42 #include <NCollection_Vector.hxx>
43 #include <ShapeAnalysis.hxx>
44 #include <ShapeAnalysis_Surface.hxx>
45 #include <TopoDS_Builder.hxx>
46 #include <TopoDS_Edge.hxx>
47 #include <TopoDS_Face.hxx>
48 #include <TopoDS_Shape.hxx>
49 #include <TopoDS_Shell.hxx>
50 #include <TopoDS_Vertex.hxx>
52 #include <TopExp_Explorer.hxx>
53 #include <TopTools_ListIteratorOfListOfShape.hxx>
55 #include <BOPAlgo_Builder.hxx>
56 #include <BRepBuilderAPI_MakeVertex.hxx>
57 #include <TopoDS_Edge.hxx>
59 //==================================================================================================
60 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
66 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
70 const Standard_Real anEps = 1.e-6;
71 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
72 return aGProps.Mass();
75 //==================================================================================================
76 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
80 return std::shared_ptr<GeomAPI_Pnt>();
82 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
84 return std::shared_ptr<GeomAPI_Pnt>();
87 if(aShape.ShapeType() == TopAbs_VERTEX) {
88 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
89 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
90 BRepGProp::LinearProperties(aShape, aGProps);
91 aCentre = aGProps.CentreOfMass();
93 BRepGProp::SurfaceProperties(aShape, aGProps);
94 aCentre = aGProps.CentreOfMass();
96 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
99 //==================================================================================================
100 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
101 const GeomAPI_Shape::ShapeType theType,
102 ListOfShape& theCombinedShapes,
103 ListOfShape& theFreeShapes)
105 GeomShapePtr aResult = theCompound;
107 if(!theCompound.get()) {
111 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
115 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
116 TopAbs_ShapeEnum aTA = TopAbs_FACE;
117 if(theType == GeomAPI_Shape::COMPSOLID) {
122 theCombinedShapes.clear();
123 theFreeShapes.clear();
126 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
127 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
128 const TopoDS_Shape& aShape = anIter.Value();
129 if(aShape.ShapeType() > aTA) {
130 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
131 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
132 theFreeShapes.push_back(aGeomShape);
136 // Map subshapes and shapes.
137 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
138 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
139 if(aMapSA.IsEmpty()) {
143 // Get all shapes with common subshapes and free shapes.
144 NCollection_Map<TopoDS_Shape> aFreeShapes;
145 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
146 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
147 const TopoDS_Shape& aShape = anIter.Key();
148 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
149 if(aListOfShape.IsEmpty()) {
152 else if(aListOfShape.Size() == 1) {
153 const TopoDS_Shape& aF = aListOfShape.First();
155 aListOfShape.Clear();
157 NCollection_List<TopoDS_Shape> aTempList;
158 NCollection_Map<TopoDS_Shape> aTempMap;
159 const TopoDS_Shape& aF = aListOfShape.First();
160 const TopoDS_Shape& aL = aListOfShape.Last();
161 aTempList.Append(aF);
162 aTempList.Append(aL);
165 aFreeShapes.Remove(aF);
166 aFreeShapes.Remove(aL);
167 aListOfShape.Clear();
168 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
169 const TopoDS_Shape& aTempShape = aTempIter.Value();
170 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
171 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
172 if(aTempListOfShape.IsEmpty()) {
174 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
175 aTempListOfShape.Clear();
176 } else if(aTempListOfShape.Size() > 1) {
177 if(aTempListOfShape.First() == aTempShape) {
178 const TopoDS_Shape& aTL = aTempListOfShape.Last();
179 if(aTempMap.Add(aTL)) {
180 aTempList.Append(aTL);
181 aFreeShapes.Remove(aTL);
183 aTempListOfShape.Clear();
184 } else if(aTempListOfShape.Last() == aTempShape) {
185 const TopoDS_Shape& aTF = aTempListOfShape.First();
186 if(aTempMap.Add(aTF)) {
187 aTempList.Append(aTF);
188 aFreeShapes.Remove(aTF);
190 aTempListOfShape.Clear();
195 aShapesWithCommonSubshapes.Append(aTempMap);
199 // Combine shapes with common subshapes.
200 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
202 TopoDS_CompSolid aCSolid;
203 TopoDS_Builder aBuilder;
204 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
205 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
206 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
207 const TopoDS_Shape& aShape = anExp.Current();
208 if(aShapesMap.Contains(aShape)) {
209 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
210 aShapesMap.Remove(aShape);
213 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
214 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
215 aGeomShape->setImpl<TopoDS_Shape>(aSh);
216 theCombinedShapes.push_back(aGeomShape);
219 // Adding free shapes.
220 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
221 const TopoDS_Shape& aShape = anExp.Current();
222 if(aFreeShapes.Contains(aShape)) {
223 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
224 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
225 theFreeShapes.push_back(aGeomShape);
229 if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
230 aResult = theCombinedShapes.front();
231 } else if(theCombinedShapes.size() == 0 && theFreeShapes.size() == 1) {
232 aResult = theFreeShapes.front();
234 TopoDS_Compound aResultComp;
235 TopoDS_Builder aBuilder;
236 aBuilder.MakeCompound(aResultComp);
237 for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin(); anIter != theCombinedShapes.cend(); anIter++) {
238 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
240 for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin(); anIter != theFreeShapes.cend(); anIter++) {
241 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
243 aResult->setImpl(new TopoDS_Shape(aResultComp));
249 //==================================================================================================
250 static void addSimpleShapeToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
252 if(theShape.IsNull()) {
256 if(theShape.ShapeType() == TopAbs_COMPOUND) {
257 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
258 addSimpleShapeToList(anIt.Value(), theList);
261 theList.Append(theShape);
265 //==================================================================================================
266 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
268 TopoDS_Compound aCompound;
270 BRep_Builder aBuilder;
271 aBuilder.MakeCompound(aCompound);
273 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
274 aBuilder.Add(aCompound, anIt.Value());
280 //==================================================================================================
281 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(const std::shared_ptr<GeomAPI_Shape> theCompound)
283 GeomShapePtr aResult = theCompound;
285 if(!theCompound.get()) {
289 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
290 NCollection_List<TopoDS_Shape> anUngroupedShapes;
291 addSimpleShapeToList(anInShape, anUngroupedShapes);
293 NCollection_Vector<NCollection_List<TopoDS_Shape>> aGroups;
294 while(!anUngroupedShapes.IsEmpty()) {
295 NCollection_List<TopoDS_Shape> aGroupedShapes;
296 aGroupedShapes.Append(anUngroupedShapes.First());
297 anUngroupedShapes.RemoveFirst();
298 for(NCollection_List<TopoDS_Shape>::Iterator aGroupIt(aGroupedShapes); aGroupIt.More(); aGroupIt.Next()) {
299 const TopoDS_Shape& aGroupShape = aGroupIt.Value();
300 for(NCollection_List<TopoDS_Shape>::Iterator anUngroupedIt(anUngroupedShapes); anUngroupedIt.More(); anUngroupedIt.Next()) {
301 const TopoDS_Shape& anUngroupedShape = anUngroupedIt.Value();
302 bool isFound = false;
303 for(TopExp_Explorer aGroupShapeExp(aGroupShape, TopAbs_VERTEX); aGroupShapeExp.More(); aGroupShapeExp.Next()) {
304 const TopoDS_Shape& aVertex1 = aGroupShapeExp.Current();
305 for(TopExp_Explorer anUngroupedShapeExp(anUngroupedShape, TopAbs_VERTEX); anUngroupedShapeExp.More(); anUngroupedShapeExp.Next()) {
306 const TopoDS_Shape& aVertex2 = anUngroupedShapeExp.Current();
307 if(aVertex1.IsSame(aVertex2)) {
308 aGroupedShapes.Append(anUngroupedShape);
309 anUngroupedShapes.Remove(anUngroupedIt);
318 if(!anUngroupedIt.More()) {
323 aGroups.Append(aGroupedShapes);
326 if(aGroups.Size() == 1) {
327 NCollection_List<TopoDS_Shape> aGroup = aGroups.First();
328 GeomShapePtr aGeomShape(new GeomAPI_Shape());
329 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
330 ListOfShape aCompSolids, aFreeSolids;
331 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
332 GeomAPI_Shape::COMPSOLID,
335 aResult = aGeomShape;
337 TopoDS_Compound aCompound;
338 BRep_Builder aBuilder;
339 aBuilder.MakeCompound(aCompound);
340 ListOfShape aCompSolids, aFreeSolids;
341 for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator anIt(aGroups); anIt.More(); anIt.Next()) {
342 NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
343 GeomShapePtr aGeomShape(new GeomAPI_Shape());
344 if(aGroup.Size() == 1) {
345 aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
347 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
348 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
349 GeomAPI_Shape::COMPSOLID,
353 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
356 if(!aCompound.IsNull()) {
357 aResult->setImpl(new TopoDS_Shape(aCompound));
364 //==================================================================================================
365 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
367 // Bounding box of all objects.
371 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
372 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
373 BRepBndLib::Add(aShape, aBndBox);
376 if(theEnlarge != 0.0) {
377 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
378 aBndBox.Enlarge(theEnlarge);
381 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
382 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
383 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
384 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
386 for(int i = 0; i < 2; i++) {
387 for(int j = 0; j < 2; j++) {
388 for(int k = 0; k < 2; k++) {
389 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
390 aResultPoints.push_back(aPnt);
395 return aResultPoints;
398 //==================================================================================================
399 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
402 return std::shared_ptr<GeomAPI_Shape>();
404 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
405 if (aPlaneFace.IsNull())
406 return std::shared_ptr<GeomAPI_Shape>();
408 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
410 return std::shared_ptr<GeomAPI_Shape>();
412 // make an infinity face on the plane
413 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
415 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
416 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
420 //==================================================================================================
421 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
422 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
424 std::shared_ptr<GeomAPI_Shape> aResultShape;
426 if(!thePlane.get()) {
430 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
431 if(aShape.ShapeType() != TopAbs_FACE) {
435 TopoDS_Face aFace = TopoDS::Face(aShape);
436 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
441 GeomLib_IsPlanarSurface isPlanar(aSurf);
442 if(!isPlanar.IsPlanar()) {
446 if(thePoints.size() != 8) {
450 const gp_Pln& aFacePln = isPlanar.Plan();
451 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
452 IntAna_Quadric aQuadric(aFacePln);
453 Standard_Real UMin, UMax, VMin, VMax;
454 UMin = UMax = VMin = VMax = 0;
455 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
456 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
457 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
458 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
459 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
460 Standard_Real aPntU(0), aPntV(0);
461 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
462 if(aPntU < UMin) UMin = aPntU;
463 if(aPntU > UMax) UMax = aPntU;
464 if(aPntV < VMin) VMin = aPntV;
465 if(aPntV > VMax) VMax = aPntV;
467 aResultShape.reset(new GeomAPI_Shape);
468 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
473 //==================================================================================================
474 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
475 std::shared_ptr<GeomAPI_Vertex>& theV1,
476 std::shared_ptr<GeomAPI_Vertex>& theV2)
478 if(!theShape.get()) {
479 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
480 aVertex->setImpl(new TopoDS_Vertex());
486 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
487 TopoDS_Vertex aV1, aV2;
488 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
490 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
491 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
492 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
497 //==================================================================================================
498 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
499 const std::shared_ptr<GeomAPI_Dir> theDirection,
500 const ListOfShape& theWires,
501 ListOfShape& theFaces)
503 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
504 theDirection->impl<gp_Dir>()));
505 TopoDS_Face aFace = aMKFace.Face();
507 BRepAlgo_FaceRestrictor aFRestrictor;
508 aFRestrictor.Init(aFace, Standard_False, Standard_True);
509 for(ListOfShape::const_iterator anIt = theWires.cbegin();
510 anIt != theWires.cend();
512 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
513 aFRestrictor.Add(aWire);
516 aFRestrictor.Perform();
518 if(!aFRestrictor.IsDone()) {
522 for(; aFRestrictor.More(); aFRestrictor.Next()) {
523 GeomShapePtr aShape(new GeomAPI_Shape());
524 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
525 theFaces.push_back(aShape);
529 //==================================================================================================
530 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
532 TopoDS_Compound aCompound;
533 BRep_Builder aBuilder;
534 aBuilder.MakeCompound(aCompound);
536 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
537 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
539 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
541 if(aFindPlane.Found() != Standard_True) {
542 return std::shared_ptr<GeomAPI_Pln>();
545 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
546 gp_Pnt aLoc = aPlane->Location();
547 gp_Dir aDir = aPlane->Axis().Direction();
549 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
550 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
552 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
557 //==================================================================================================
558 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(const std::shared_ptr<GeomAPI_Shape> theSubShape,
559 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
561 if(!theSubShape.get() || !theBaseShape.get()) {
565 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
566 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
568 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
569 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
570 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
572 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
575 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
576 if(aBaseShape.ShapeType() == TopAbs_FACE) {
577 // Check that edge is on face surface.
578 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
579 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
580 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
582 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
586 // Check intersections.
587 TopoDS_Vertex aV1, aV2;
588 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
589 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
590 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
591 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
592 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
593 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
595 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
596 // Edge intersect face bound. Check that it is not on edge begin or end.
597 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
598 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
599 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
600 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
607 // No intersections found. Edge is inside or outside face. Check it.
608 BRepAdaptor_Curve aCurveAdaptor(anEdge);
609 gp_Pnt aPointToCheck = aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() + aCurveAdaptor.LastParameter()) / 2.0);
610 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
611 ShapeAnalysis_Surface aSAS(aSurface);
612 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
613 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
614 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
628 //==================================================================================================
629 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
631 if(!theShape.get()) {
635 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
636 return (aChecker.IsValid() == Standard_True);
639 //==================================================================================================
640 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
642 GeomShapePtr anOuterWire;
644 if(!theFace.get() || !theFace->isFace()) {
648 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
649 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
651 anOuterWire.reset(new GeomAPI_Shape());
652 anOuterWire->setImpl(new TopoDS_Shape(aWire));
657 //==================================================================================================
658 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
659 const std::shared_ptr<GeomAPI_Face> theFace)
661 if(!theEdge.get() || !theFace.get()) {
665 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
666 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
668 BRepExtrema_ExtCF anExt(anEdge, aFace);
669 return anExt.IsParallel() == Standard_True;
672 //==================================================================================================
673 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
674 const std::set<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
675 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
677 // General Fuse to split edge by vertices
678 BOPAlgo_Builder aBOP;
679 const TopoDS_Edge& aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
680 aBOP.AddArgument(aBaseEdge);
682 std::set<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
683 for (; aPtIt != thePoints.end(); ++aPtIt) {
684 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
685 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
686 aBOP.AddArgument(aV);
690 if (aBOP.ErrorStatus())
694 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
695 TopTools_ListIteratorOfListOfShape anIt(aSplits);
696 for (; anIt.More(); anIt.Next()) {
697 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
698 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
699 theShapes.insert(anEdge);