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>
54 //==================================================================================================
55 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
61 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
65 const Standard_Real anEps = 1.e-6;
66 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
67 return aGProps.Mass();
70 //==================================================================================================
71 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
75 return std::shared_ptr<GeomAPI_Pnt>();
77 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
79 return std::shared_ptr<GeomAPI_Pnt>();
82 if(aShape.ShapeType() == TopAbs_VERTEX) {
83 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
84 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
85 BRepGProp::LinearProperties(aShape, aGProps);
86 aCentre = aGProps.CentreOfMass();
88 BRepGProp::SurfaceProperties(aShape, aGProps);
89 aCentre = aGProps.CentreOfMass();
91 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
94 //==================================================================================================
95 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
96 const GeomAPI_Shape::ShapeType theType,
97 ListOfShape& theCombinedShapes,
98 ListOfShape& theFreeShapes)
100 GeomShapePtr aResult = theCompound;
102 if(!theCompound.get()) {
106 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
110 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
111 TopAbs_ShapeEnum aTA = TopAbs_FACE;
112 if(theType == GeomAPI_Shape::COMPSOLID) {
117 theCombinedShapes.clear();
118 theFreeShapes.clear();
121 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
122 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
123 const TopoDS_Shape& aShape = anIter.Value();
124 if(aShape.ShapeType() > aTA) {
125 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
126 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
127 theFreeShapes.push_back(aGeomShape);
131 // Map subshapes and shapes.
132 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
133 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
134 if(aMapSA.IsEmpty()) {
138 // Get all shapes with common subshapes and free shapes.
139 NCollection_Map<TopoDS_Shape> aFreeShapes;
140 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
141 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
142 const TopoDS_Shape& aShape = anIter.Key();
143 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
144 if(aListOfShape.IsEmpty()) {
147 else if(aListOfShape.Size() == 1) {
148 const TopoDS_Shape& aF = aListOfShape.First();
150 aListOfShape.Clear();
152 NCollection_List<TopoDS_Shape> aTempList;
153 NCollection_Map<TopoDS_Shape> aTempMap;
154 const TopoDS_Shape& aF = aListOfShape.First();
155 const TopoDS_Shape& aL = aListOfShape.Last();
156 aTempList.Append(aF);
157 aTempList.Append(aL);
160 aFreeShapes.Remove(aF);
161 aFreeShapes.Remove(aL);
162 aListOfShape.Clear();
163 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
164 const TopoDS_Shape& aTempShape = aTempIter.Value();
165 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
166 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
167 if(aTempListOfShape.IsEmpty()) {
169 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
170 aTempListOfShape.Clear();
171 } else if(aTempListOfShape.Size() > 1) {
172 if(aTempListOfShape.First() == aTempShape) {
173 const TopoDS_Shape& aTL = aTempListOfShape.Last();
174 if(aTempMap.Add(aTL)) {
175 aTempList.Append(aTL);
176 aFreeShapes.Remove(aTL);
178 aTempListOfShape.Clear();
179 } else if(aTempListOfShape.Last() == aTempShape) {
180 const TopoDS_Shape& aTF = aTempListOfShape.First();
181 if(aTempMap.Add(aTF)) {
182 aTempList.Append(aTF);
183 aFreeShapes.Remove(aTF);
185 aTempListOfShape.Clear();
190 aShapesWithCommonSubshapes.Append(aTempMap);
194 // Combine shapes with common subshapes.
195 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
197 TopoDS_CompSolid aCSolid;
198 TopoDS_Builder aBuilder;
199 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
200 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
201 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
202 const TopoDS_Shape& aShape = anExp.Current();
203 if(aShapesMap.Contains(aShape)) {
204 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
205 aShapesMap.Remove(aShape);
208 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
209 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
210 aGeomShape->setImpl<TopoDS_Shape>(aSh);
211 theCombinedShapes.push_back(aGeomShape);
214 // Adding free shapes.
215 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
216 const TopoDS_Shape& aShape = anExp.Current();
217 if(aFreeShapes.Contains(aShape)) {
218 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
219 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
220 theFreeShapes.push_back(aGeomShape);
224 if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
225 aResult = theCombinedShapes.front();
226 } else if(theCombinedShapes.size() == 0 && theFreeShapes.size() == 1) {
227 aResult = theFreeShapes.front();
229 TopoDS_Compound aResultComp;
230 TopoDS_Builder aBuilder;
231 aBuilder.MakeCompound(aResultComp);
232 for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin(); anIter != theCombinedShapes.cend(); anIter++) {
233 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
235 for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin(); anIter != theFreeShapes.cend(); anIter++) {
236 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
238 aResult->setImpl(new TopoDS_Shape(aResultComp));
244 //==================================================================================================
245 static void addSimpleShapeToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
247 if(theShape.IsNull()) {
251 if(theShape.ShapeType() == TopAbs_COMPOUND) {
252 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
253 addSimpleShapeToList(anIt.Value(), theList);
256 theList.Append(theShape);
260 //==================================================================================================
261 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
263 TopoDS_Compound aCompound;
265 BRep_Builder aBuilder;
266 aBuilder.MakeCompound(aCompound);
268 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
269 aBuilder.Add(aCompound, anIt.Value());
275 //==================================================================================================
276 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(const std::shared_ptr<GeomAPI_Shape> theCompound)
278 GeomShapePtr aResult = theCompound;
280 if(!theCompound.get()) {
284 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
285 NCollection_List<TopoDS_Shape> anUngroupedShapes;
286 addSimpleShapeToList(anInShape, anUngroupedShapes);
288 NCollection_Vector<NCollection_List<TopoDS_Shape>> aGroups;
289 while(!anUngroupedShapes.IsEmpty()) {
290 NCollection_List<TopoDS_Shape> aGroupedShapes;
291 aGroupedShapes.Append(anUngroupedShapes.First());
292 anUngroupedShapes.RemoveFirst();
293 for(NCollection_List<TopoDS_Shape>::Iterator aGroupIt(aGroupedShapes); aGroupIt.More(); aGroupIt.Next()) {
294 const TopoDS_Shape& aGroupShape = aGroupIt.Value();
295 for(NCollection_List<TopoDS_Shape>::Iterator anUngroupedIt(anUngroupedShapes); anUngroupedIt.More(); anUngroupedIt.Next()) {
296 const TopoDS_Shape& anUngroupedShape = anUngroupedIt.Value();
297 bool isFound = false;
298 for(TopExp_Explorer aGroupShapeExp(aGroupShape, TopAbs_VERTEX); aGroupShapeExp.More(); aGroupShapeExp.Next()) {
299 const TopoDS_Shape& aVertex1 = aGroupShapeExp.Current();
300 for(TopExp_Explorer anUngroupedShapeExp(anUngroupedShape, TopAbs_VERTEX); anUngroupedShapeExp.More(); anUngroupedShapeExp.Next()) {
301 const TopoDS_Shape& aVertex2 = anUngroupedShapeExp.Current();
302 if(aVertex1.IsSame(aVertex2)) {
303 aGroupedShapes.Append(anUngroupedShape);
304 anUngroupedShapes.Remove(anUngroupedIt);
313 if(!anUngroupedIt.More()) {
318 aGroups.Append(aGroupedShapes);
321 if(aGroups.Size() == 1) {
322 NCollection_List<TopoDS_Shape> aGroup = aGroups.First();
323 GeomShapePtr aGeomShape(new GeomAPI_Shape());
324 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
325 ListOfShape aCompSolids, aFreeSolids;
326 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
327 GeomAPI_Shape::COMPSOLID,
330 aResult = aGeomShape;
332 TopoDS_Compound aCompound;
333 BRep_Builder aBuilder;
334 aBuilder.MakeCompound(aCompound);
335 ListOfShape aCompSolids, aFreeSolids;
336 for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator anIt(aGroups); anIt.More(); anIt.Next()) {
337 NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
338 GeomShapePtr aGeomShape(new GeomAPI_Shape());
339 if(aGroup.Size() == 1) {
340 aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
342 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
343 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
344 GeomAPI_Shape::COMPSOLID,
348 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
351 if(!aCompound.IsNull()) {
352 aResult->setImpl(new TopoDS_Shape(aCompound));
359 //==================================================================================================
360 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
362 // Bounding box of all objects.
366 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
367 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
368 BRepBndLib::Add(aShape, aBndBox);
371 if(theEnlarge != 0.0) {
372 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
373 aBndBox.Enlarge(theEnlarge);
376 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
377 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
378 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
379 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
381 for(int i = 0; i < 2; i++) {
382 for(int j = 0; j < 2; j++) {
383 for(int k = 0; k < 2; k++) {
384 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
385 aResultPoints.push_back(aPnt);
390 return aResultPoints;
393 //==================================================================================================
394 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
397 return std::shared_ptr<GeomAPI_Shape>();
399 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
400 if (aPlaneFace.IsNull())
401 return std::shared_ptr<GeomAPI_Shape>();
403 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
405 return std::shared_ptr<GeomAPI_Shape>();
407 // make an infinity face on the plane
408 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
410 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
411 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
415 //==================================================================================================
416 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
417 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
419 std::shared_ptr<GeomAPI_Shape> aResultShape;
421 if(!thePlane.get()) {
425 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
426 if(aShape.ShapeType() != TopAbs_FACE) {
430 TopoDS_Face aFace = TopoDS::Face(aShape);
431 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
436 GeomLib_IsPlanarSurface isPlanar(aSurf);
437 if(!isPlanar.IsPlanar()) {
441 if(thePoints.size() != 8) {
445 const gp_Pln& aFacePln = isPlanar.Plan();
446 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
447 IntAna_Quadric aQuadric(aFacePln);
448 Standard_Real UMin, UMax, VMin, VMax;
449 UMin = UMax = VMin = VMax = 0;
450 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
451 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
452 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
453 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
454 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
455 Standard_Real aPntU(0), aPntV(0);
456 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
457 if(aPntU < UMin) UMin = aPntU;
458 if(aPntU > UMax) UMax = aPntU;
459 if(aPntV < VMin) VMin = aPntV;
460 if(aPntV > VMax) VMax = aPntV;
462 aResultShape.reset(new GeomAPI_Shape);
463 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
468 //==================================================================================================
469 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
470 std::shared_ptr<GeomAPI_Vertex>& theV1,
471 std::shared_ptr<GeomAPI_Vertex>& theV2)
473 if(!theShape.get()) {
474 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
475 aVertex->setImpl(new TopoDS_Vertex());
481 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
482 TopoDS_Vertex aV1, aV2;
483 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
485 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
486 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
487 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
492 //==================================================================================================
493 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
494 const std::shared_ptr<GeomAPI_Dir> theDirection,
495 const ListOfShape& theWires,
496 ListOfShape& theFaces)
498 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
499 theDirection->impl<gp_Dir>()));
500 TopoDS_Face aFace = aMKFace.Face();
502 BRepAlgo_FaceRestrictor aFRestrictor;
503 aFRestrictor.Init(aFace, Standard_False, Standard_True);
504 for(ListOfShape::const_iterator anIt = theWires.cbegin();
505 anIt != theWires.cend();
507 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
508 aFRestrictor.Add(aWire);
511 aFRestrictor.Perform();
513 if(!aFRestrictor.IsDone()) {
517 for(; aFRestrictor.More(); aFRestrictor.Next()) {
518 GeomShapePtr aShape(new GeomAPI_Shape());
519 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
520 theFaces.push_back(aShape);
524 //==================================================================================================
525 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
527 TopoDS_Compound aCompound;
528 BRep_Builder aBuilder;
529 aBuilder.MakeCompound(aCompound);
531 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
532 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
534 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
536 if(aFindPlane.Found() != Standard_True) {
537 return std::shared_ptr<GeomAPI_Pln>();
540 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
541 gp_Pnt aLoc = aPlane->Location();
542 gp_Dir aDir = aPlane->Axis().Direction();
544 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
545 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
547 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
552 //==================================================================================================
553 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(const std::shared_ptr<GeomAPI_Shape> theSubShape,
554 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
556 if(!theSubShape.get() || !theBaseShape.get()) {
560 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
561 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
563 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
564 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
565 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
567 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
570 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
571 if(aBaseShape.ShapeType() == TopAbs_FACE) {
572 // Check that edge is on face surface.
573 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
574 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
575 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
577 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
581 // Check intersections.
582 TopoDS_Vertex aV1, aV2;
583 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
584 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
585 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
586 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
587 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
588 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
590 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
591 // Edge intersect face bound. Check that it is not on edge begin or end.
592 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
593 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
594 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
595 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
602 // No intersections found. Edge is inside or outside face. Check it.
603 BRepAdaptor_Curve aCurveAdaptor(anEdge);
604 gp_Pnt aPointToCheck = aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() + aCurveAdaptor.LastParameter()) / 2.0);
605 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
606 ShapeAnalysis_Surface aSAS(aSurface);
607 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
608 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
609 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
623 //==================================================================================================
624 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
626 if(!theShape.get()) {
630 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
631 return (aChecker.IsValid() == Standard_True);
634 //==================================================================================================
635 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
637 GeomShapePtr anOuterWire;
639 if(!theFace.get() || !theFace->isFace()) {
643 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
644 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
646 anOuterWire.reset(new GeomAPI_Shape());
647 anOuterWire->setImpl(new TopoDS_Shape(aWire));
652 //==================================================================================================
653 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
654 const std::shared_ptr<GeomAPI_Face> theFace)
656 if(!theEdge.get() || !theFace.get()) {
660 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
661 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
663 BRepExtrema_ExtCF anExt(anEdge, aFace);
664 return anExt.IsParallel() == Standard_True;