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_Dir.h>
12 #include <GeomAPI_Face.h>
13 #include <GeomAPI_PlanarEdges.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_MakeEdge.hxx>
25 #include <BRepBuilderAPI_MakeFace.hxx>
26 #include <BRepBuilderAPI_MakeVertex.hxx>
27 #include <BRepCheck_Analyzer.hxx>
28 #include <BRepExtrema_DistShapeShape.hxx>
29 #include <BRepExtrema_ExtCF.hxx>
30 #include <BRepGProp.hxx>
31 #include <BRepTools.hxx>
32 #include <BRepTopAdaptor_FClass2d.hxx>
33 #include <GCPnts_AbscissaPoint.hxx>
34 #include <Geom_Curve.hxx>
35 #include <Geom2d_Curve.hxx>
36 #include <BRepLib_CheckCurveOnSurface.hxx>
37 #include <BRep_Tool.hxx>
38 #include <Geom_Plane.hxx>
39 #include <GeomLib_IsPlanarSurface.hxx>
40 #include <GeomLib_Tool.hxx>
41 #include <GeomProjLib.hxx>
43 #include <GProp_GProps.hxx>
44 #include <IntAna_IntConicQuad.hxx>
45 #include <IntAna_Quadric.hxx>
46 #include <NCollection_Vector.hxx>
47 #include <ShapeAnalysis.hxx>
48 #include <ShapeAnalysis_Surface.hxx>
49 #include <TopoDS_Builder.hxx>
50 #include <TopoDS_Edge.hxx>
51 #include <TopoDS_Face.hxx>
52 #include <TopoDS_Shape.hxx>
53 #include <TopoDS_Shell.hxx>
54 #include <TopoDS_Vertex.hxx>
56 #include <TopExp_Explorer.hxx>
58 //==================================================================================================
59 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
65 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
69 const Standard_Real anEps = 1.e-6;
70 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
71 return aGProps.Mass();
74 //==================================================================================================
75 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
79 return std::shared_ptr<GeomAPI_Pnt>();
81 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
83 return std::shared_ptr<GeomAPI_Pnt>();
86 if(aShape.ShapeType() == TopAbs_VERTEX) {
87 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
88 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
89 BRepGProp::LinearProperties(aShape, aGProps);
90 aCentre = aGProps.CentreOfMass();
92 BRepGProp::SurfaceProperties(aShape, aGProps);
93 aCentre = aGProps.CentreOfMass();
95 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
98 //==================================================================================================
99 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
100 const GeomAPI_Shape::ShapeType theType,
101 ListOfShape& theCombinedShapes,
102 ListOfShape& theFreeShapes)
104 GeomShapePtr aResult = theCompound;
106 if(!theCompound.get()) {
110 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
114 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
115 TopAbs_ShapeEnum aTA = TopAbs_FACE;
116 if(theType == GeomAPI_Shape::COMPSOLID) {
121 theCombinedShapes.clear();
122 theFreeShapes.clear();
125 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
126 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
127 const TopoDS_Shape& aShape = anIter.Value();
128 if(aShape.ShapeType() > aTA) {
129 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
130 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
131 theFreeShapes.push_back(aGeomShape);
135 // Map subshapes and shapes.
136 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
137 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
138 if(aMapSA.IsEmpty()) {
142 // Get all shapes with common subshapes and free shapes.
143 NCollection_Map<TopoDS_Shape> aFreeShapes;
144 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
145 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
146 const TopoDS_Shape& aShape = anIter.Key();
147 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
148 if(aListOfShape.IsEmpty()) {
151 else if(aListOfShape.Size() == 1) {
152 const TopoDS_Shape& aF = aListOfShape.First();
154 aListOfShape.Clear();
156 NCollection_List<TopoDS_Shape> aTempList;
157 NCollection_Map<TopoDS_Shape> aTempMap;
158 const TopoDS_Shape& aF = aListOfShape.First();
159 const TopoDS_Shape& aL = aListOfShape.Last();
160 aTempList.Append(aF);
161 aTempList.Append(aL);
164 aFreeShapes.Remove(aF);
165 aFreeShapes.Remove(aL);
166 aListOfShape.Clear();
167 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
168 const TopoDS_Shape& aTempShape = aTempIter.Value();
169 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
170 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
171 if(aTempListOfShape.IsEmpty()) {
173 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
174 aTempListOfShape.Clear();
175 } else if(aTempListOfShape.Size() > 1) {
176 if(aTempListOfShape.First() == aTempShape) {
177 const TopoDS_Shape& aTL = aTempListOfShape.Last();
178 if(aTempMap.Add(aTL)) {
179 aTempList.Append(aTL);
180 aFreeShapes.Remove(aTL);
182 aTempListOfShape.Clear();
183 } else if(aTempListOfShape.Last() == aTempShape) {
184 const TopoDS_Shape& aTF = aTempListOfShape.First();
185 if(aTempMap.Add(aTF)) {
186 aTempList.Append(aTF);
187 aFreeShapes.Remove(aTF);
189 aTempListOfShape.Clear();
194 aShapesWithCommonSubshapes.Append(aTempMap);
198 // Combine shapes with common subshapes.
199 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
201 TopoDS_CompSolid aCSolid;
202 TopoDS_Builder aBuilder;
203 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
204 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
205 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
206 const TopoDS_Shape& aShape = anExp.Current();
207 if(aShapesMap.Contains(aShape)) {
208 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
209 aShapesMap.Remove(aShape);
212 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
213 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
214 aGeomShape->setImpl<TopoDS_Shape>(aSh);
215 theCombinedShapes.push_back(aGeomShape);
218 // Adding free shapes.
219 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
220 const TopoDS_Shape& aShape = anExp.Current();
221 if(aFreeShapes.Contains(aShape)) {
222 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
223 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
224 theFreeShapes.push_back(aGeomShape);
228 if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
229 aResult = theCombinedShapes.front();
230 } else if(theCombinedShapes.size() == 0 && theFreeShapes.size() == 1) {
231 aResult = theFreeShapes.front();
233 TopoDS_Compound aResultComp;
234 TopoDS_Builder aBuilder;
235 aBuilder.MakeCompound(aResultComp);
236 for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin(); anIter != theCombinedShapes.cend(); anIter++) {
237 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
239 for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin(); anIter != theFreeShapes.cend(); anIter++) {
240 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
242 aResult->setImpl(new TopoDS_Shape(aResultComp));
248 //==================================================================================================
249 static void addSimpleShapeToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
251 if(theShape.IsNull()) {
255 if(theShape.ShapeType() == TopAbs_COMPOUND) {
256 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
257 addSimpleShapeToList(anIt.Value(), theList);
260 theList.Append(theShape);
264 //==================================================================================================
265 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
267 TopoDS_Compound aCompound;
269 BRep_Builder aBuilder;
270 aBuilder.MakeCompound(aCompound);
272 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
273 aBuilder.Add(aCompound, anIt.Value());
279 //==================================================================================================
280 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(const std::shared_ptr<GeomAPI_Shape> theCompound)
282 GeomShapePtr aResult = theCompound;
284 if(!theCompound.get()) {
288 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
289 NCollection_List<TopoDS_Shape> anUngroupedShapes;
290 addSimpleShapeToList(anInShape, anUngroupedShapes);
292 NCollection_Vector<NCollection_List<TopoDS_Shape>> aGroups;
293 while(!anUngroupedShapes.IsEmpty()) {
294 NCollection_List<TopoDS_Shape> aGroupedShapes;
295 aGroupedShapes.Append(anUngroupedShapes.First());
296 anUngroupedShapes.RemoveFirst();
297 for(NCollection_List<TopoDS_Shape>::Iterator aGroupIt(aGroupedShapes); aGroupIt.More(); aGroupIt.Next()) {
298 const TopoDS_Shape& aGroupShape = aGroupIt.Value();
299 for(NCollection_List<TopoDS_Shape>::Iterator anUngroupedIt(anUngroupedShapes); anUngroupedIt.More(); anUngroupedIt.Next()) {
300 const TopoDS_Shape& anUngroupedShape = anUngroupedIt.Value();
301 bool isFound = false;
302 for(TopExp_Explorer aGroupShapeExp(aGroupShape, TopAbs_VERTEX); aGroupShapeExp.More(); aGroupShapeExp.Next()) {
303 const TopoDS_Shape& aVertex1 = aGroupShapeExp.Current();
304 for(TopExp_Explorer anUngroupedShapeExp(anUngroupedShape, TopAbs_VERTEX); anUngroupedShapeExp.More(); anUngroupedShapeExp.Next()) {
305 const TopoDS_Shape& aVertex2 = anUngroupedShapeExp.Current();
306 if(aVertex1.IsSame(aVertex2)) {
307 aGroupedShapes.Append(anUngroupedShape);
308 anUngroupedShapes.Remove(anUngroupedIt);
317 if(!anUngroupedIt.More()) {
322 aGroups.Append(aGroupedShapes);
325 if(aGroups.Size() == 1) {
326 NCollection_List<TopoDS_Shape> aGroup = aGroups.First();
327 GeomShapePtr aGeomShape(new GeomAPI_Shape());
328 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
329 ListOfShape aCompSolids, aFreeSolids;
330 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
331 GeomAPI_Shape::COMPSOLID,
334 aResult = aGeomShape;
336 TopoDS_Compound aCompound;
337 BRep_Builder aBuilder;
338 aBuilder.MakeCompound(aCompound);
339 ListOfShape aCompSolids, aFreeSolids;
340 for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator anIt(aGroups); anIt.More(); anIt.Next()) {
341 NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
342 GeomShapePtr aGeomShape(new GeomAPI_Shape());
343 if(aGroup.Size() == 1) {
344 aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
346 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
347 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
348 GeomAPI_Shape::COMPSOLID,
352 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
355 if(!aCompound.IsNull()) {
356 aResult->setImpl(new TopoDS_Shape(aCompound));
363 //==================================================================================================
364 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
366 // Bounding box of all objects.
370 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
371 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
372 BRepBndLib::Add(aShape, aBndBox);
375 if(theEnlarge != 0.0) {
376 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
377 aBndBox.Enlarge(theEnlarge);
380 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
381 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
382 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
383 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
385 for(int i = 0; i < 2; i++) {
386 for(int j = 0; j < 2; j++) {
387 for(int k = 0; k < 2; k++) {
388 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
389 aResultPoints.push_back(aPnt);
394 return aResultPoints;
397 //==================================================================================================
398 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
401 return std::shared_ptr<GeomAPI_Shape>();
403 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
404 if (aPlaneFace.IsNull())
405 return std::shared_ptr<GeomAPI_Shape>();
407 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
409 return std::shared_ptr<GeomAPI_Shape>();
411 // make an infinity face on the plane
412 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
414 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
415 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
419 //==================================================================================================
420 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
421 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
423 std::shared_ptr<GeomAPI_Shape> aResultShape;
425 if(!thePlane.get()) {
429 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
430 if(aShape.ShapeType() != TopAbs_FACE) {
434 TopoDS_Face aFace = TopoDS::Face(aShape);
435 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
440 GeomLib_IsPlanarSurface isPlanar(aSurf);
441 if(!isPlanar.IsPlanar()) {
445 if(thePoints.size() != 8) {
449 const gp_Pln& aFacePln = isPlanar.Plan();
450 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
451 IntAna_Quadric aQuadric(aFacePln);
452 Standard_Real UMin, UMax, VMin, VMax;
453 UMin = UMax = VMin = VMax = 0;
454 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
455 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
456 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
457 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
458 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
459 Standard_Real aPntU(0), aPntV(0);
460 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
461 if(aPntU < UMin) UMin = aPntU;
462 if(aPntU > UMax) UMax = aPntU;
463 if(aPntV < VMin) VMin = aPntV;
464 if(aPntV > VMax) VMax = aPntV;
466 aResultShape.reset(new GeomAPI_Shape);
467 aResultShape->setImpl(new TopoDS_Shape(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
472 //==================================================================================================
473 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
474 std::shared_ptr<GeomAPI_Vertex>& theV1,
475 std::shared_ptr<GeomAPI_Vertex>& theV2)
477 if(!theShape.get()) {
478 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
479 aVertex->setImpl(new TopoDS_Vertex());
485 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
486 TopoDS_Vertex aV1, aV2;
487 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
489 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
490 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
491 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
496 //==================================================================================================
497 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
498 const std::shared_ptr<GeomAPI_Dir> theDirection,
499 const ListOfShape& theWires,
500 ListOfShape& theFaces)
502 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
503 theDirection->impl<gp_Dir>()));
504 TopoDS_Face aFace = aMKFace.Face();
506 BRepAlgo_FaceRestrictor aFRestrictor;
507 aFRestrictor.Init(aFace, Standard_False, Standard_True);
508 for(ListOfShape::const_iterator anIt = theWires.cbegin();
509 anIt != theWires.cend();
511 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
512 aFRestrictor.Add(aWire);
515 aFRestrictor.Perform();
517 if(!aFRestrictor.IsDone()) {
521 for(; aFRestrictor.More(); aFRestrictor.Next()) {
522 GeomShapePtr aShape(new GeomAPI_Shape());
523 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
524 theFaces.push_back(aShape);
528 //==================================================================================================
529 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
531 TopoDS_Compound aCompound;
532 BRep_Builder aBuilder;
533 aBuilder.MakeCompound(aCompound);
535 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
536 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
538 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
540 if(aFindPlane.Found() != Standard_True) {
541 return std::shared_ptr<GeomAPI_Pln>();
544 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
545 gp_Pnt aLoc = aPlane->Location();
546 gp_Dir aDir = aPlane->Axis().Direction();
548 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
549 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
551 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
556 //==================================================================================================
557 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(const std::shared_ptr<GeomAPI_Shape> theSubShape,
558 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
560 if(!theSubShape.get() || !theBaseShape.get()) {
564 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
565 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
567 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
568 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
569 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
571 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
574 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
575 if(aBaseShape.ShapeType() == TopAbs_FACE) {
576 // Check that edge is on face surface.
577 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
578 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
579 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
581 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
585 // Check intersections.
586 TopoDS_Vertex aV1, aV2;
587 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
588 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
589 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
590 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
591 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
592 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
594 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
595 // Edge intersect face bound. Check that it is not on edge begin or end.
596 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
597 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
598 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
599 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
606 // No intersections found. Edge is inside or outside face. Check it.
607 BRepAdaptor_Curve aCurveAdaptor(anEdge);
608 gp_Pnt aPointToCheck = aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() + aCurveAdaptor.LastParameter()) / 2.0);
609 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
610 ShapeAnalysis_Surface aSAS(aSurface);
611 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
612 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
613 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
627 //==================================================================================================
628 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
630 if(!theShape.get()) {
634 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
635 return (aChecker.IsValid() == Standard_True);
638 //==================================================================================================
639 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
641 GeomShapePtr anOuterWire;
643 if(!theFace.get() || !theFace->isFace()) {
647 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
648 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
650 anOuterWire.reset(new GeomAPI_Shape());
651 anOuterWire->setImpl(new TopoDS_Shape(aWire));
656 //==================================================================================================
657 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
658 const std::shared_ptr<GeomAPI_Face> theFace)
660 if(!theEdge.get() || !theFace.get()) {
664 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
665 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
667 BRepExtrema_ExtCF anExt(anEdge, aFace);
668 return anExt.IsParallel() == Standard_True;
671 //==================================================================================================
672 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_ShapeTools::findVertexOnEdge(const std::shared_ptr<GeomAPI_Edge> theEdge,
673 const double theValue,
674 const bool theIsPercent,
675 const bool theIsReverse)
677 std::shared_ptr<GeomAPI_Vertex> aVertex;
683 double aValue = theValue;
685 aValue = theEdge->length() / 100.0 * aValue;
688 const TopoDS_Edge& anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
689 Standard_Real aUFirst, aULast;
690 Handle(Geom_Curve) anEdgeCurve = BRep_Tool::Curve(anEdge, aUFirst, aULast);
692 if(!anEdgeCurve.IsNull() ) {
693 Handle(Geom_Curve) aReOrientedCurve = anEdgeCurve;
696 aReOrientedCurve = anEdgeCurve->Reversed();
697 aUFirst = anEdgeCurve->ReversedParameter(aULast);
700 // Get the point by length
701 GeomAdaptor_Curve anAdapCurve = GeomAdaptor_Curve(aReOrientedCurve);
702 GCPnts_AbscissaPoint anAbsPnt(anAdapCurve, aValue, aUFirst);
703 Standard_Real aParam = anAbsPnt.Parameter();
704 gp_Pnt aPnt = anAdapCurve.Value(aParam);
705 BRepBuilderAPI_MakeVertex aMkVertex(aPnt);
706 const TopoDS_Vertex& aShape = aMkVertex.Vertex();
707 aVertex.reset(new GeomAPI_Vertex());
708 aVertex->setImpl(new TopoDS_Vertex(aShape));