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_MakeEdge.hxx>
25 #include <BRepBuilderAPI_MakeFace.hxx>
26 #include <BRepCheck_Analyzer.hxx>
27 #include <BRepExtrema_DistShapeShape.hxx>
28 #include <BRepExtrema_ExtCF.hxx>
29 #include <BRepGProp.hxx>
30 #include <BRepTools.hxx>
31 #include <BRepTopAdaptor_FClass2d.hxx>
32 #include <Geom2d_Curve.hxx>
33 #include <Handle_Geom2d_Curve.hxx>
34 #include <BRepLib_CheckCurveOnSurface.hxx>
35 #include <BRep_Tool.hxx>
36 #include <Geom_Plane.hxx>
37 #include <GeomLib_IsPlanarSurface.hxx>
38 #include <GeomLib_Tool.hxx>
40 #include <GProp_GProps.hxx>
41 #include <IntAna_IntConicQuad.hxx>
42 #include <IntAna_Quadric.hxx>
43 #include <NCollection_Vector.hxx>
44 #include <ShapeAnalysis.hxx>
45 #include <ShapeAnalysis_Surface.hxx>
46 #include <TopoDS_Builder.hxx>
47 #include <TopoDS_Edge.hxx>
48 #include <TopoDS_Face.hxx>
49 #include <TopoDS_Shape.hxx>
50 #include <TopoDS_Shell.hxx>
51 #include <TopoDS_Vertex.hxx>
53 #include <TopExp_Explorer.hxx>
54 #include <TopTools_ListIteratorOfListOfShape.hxx>
56 #include <BOPAlgo_Builder.hxx>
57 #include <BRepBuilderAPI_MakeVertex.hxx>
58 #include <TopoDS_Edge.hxx>
60 //==================================================================================================
61 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
67 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
71 const Standard_Real anEps = 1.e-6;
72 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
73 return aGProps.Mass();
76 //==================================================================================================
77 std::shared_ptr<GeomAPI_Pnt> GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
81 return std::shared_ptr<GeomAPI_Pnt>();
83 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
85 return std::shared_ptr<GeomAPI_Pnt>();
88 if(aShape.ShapeType() == TopAbs_VERTEX) {
89 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
90 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
91 BRepGProp::LinearProperties(aShape, aGProps);
92 aCentre = aGProps.CentreOfMass();
94 BRepGProp::SurfaceProperties(aShape, aGProps);
95 aCentre = aGProps.CentreOfMass();
97 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
100 //==================================================================================================
101 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(const std::shared_ptr<GeomAPI_Shape> theCompound,
102 const GeomAPI_Shape::ShapeType theType,
103 ListOfShape& theCombinedShapes,
104 ListOfShape& theFreeShapes)
106 GeomShapePtr aResult = theCompound;
108 if(!theCompound.get()) {
112 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
116 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
117 TopAbs_ShapeEnum aTA = TopAbs_FACE;
118 if(theType == GeomAPI_Shape::COMPSOLID) {
123 theCombinedShapes.clear();
124 theFreeShapes.clear();
127 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
128 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
129 const TopoDS_Shape& aShape = anIter.Value();
130 if(aShape.ShapeType() > aTA) {
131 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
132 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
133 theFreeShapes.push_back(aGeomShape);
137 // Map subshapes and shapes.
138 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
139 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
140 if(aMapSA.IsEmpty()) {
144 // Get all shapes with common subshapes and free shapes.
145 NCollection_Map<TopoDS_Shape> aFreeShapes;
146 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
147 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
148 const TopoDS_Shape& aShape = anIter.Key();
149 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
150 if(aListOfShape.IsEmpty()) {
153 else if(aListOfShape.Size() == 1) {
154 const TopoDS_Shape& aF = aListOfShape.First();
156 aListOfShape.Clear();
158 NCollection_List<TopoDS_Shape> aTempList;
159 NCollection_Map<TopoDS_Shape> aTempMap;
160 const TopoDS_Shape& aF = aListOfShape.First();
161 const TopoDS_Shape& aL = aListOfShape.Last();
162 aTempList.Append(aF);
163 aTempList.Append(aL);
166 aFreeShapes.Remove(aF);
167 aFreeShapes.Remove(aL);
168 aListOfShape.Clear();
169 for(NCollection_List<TopoDS_Shape>::Iterator aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
170 const TopoDS_Shape& aTempShape = aTempIter.Value();
171 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator anIter(aMapSA); anIter.More(); anIter.Next()) {
172 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
173 if(aTempListOfShape.IsEmpty()) {
175 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
176 aTempListOfShape.Clear();
177 } else if(aTempListOfShape.Size() > 1) {
178 if(aTempListOfShape.First() == aTempShape) {
179 const TopoDS_Shape& aTL = aTempListOfShape.Last();
180 if(aTempMap.Add(aTL)) {
181 aTempList.Append(aTL);
182 aFreeShapes.Remove(aTL);
184 aTempListOfShape.Clear();
185 } else if(aTempListOfShape.Last() == aTempShape) {
186 const TopoDS_Shape& aTF = aTempListOfShape.First();
187 if(aTempMap.Add(aTF)) {
188 aTempList.Append(aTF);
189 aFreeShapes.Remove(aTF);
191 aTempListOfShape.Clear();
196 aShapesWithCommonSubshapes.Append(aTempMap);
200 // Combine shapes with common subshapes.
201 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
203 TopoDS_CompSolid aCSolid;
204 TopoDS_Builder aBuilder;
205 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
206 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
207 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
208 const TopoDS_Shape& aShape = anExp.Current();
209 if(aShapesMap.Contains(aShape)) {
210 theType == GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
211 aShapesMap.Remove(aShape);
214 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
215 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) : new TopoDS_Shape(aShell);
216 aGeomShape->setImpl<TopoDS_Shape>(aSh);
217 theCombinedShapes.push_back(aGeomShape);
220 // Adding free shapes.
221 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
222 const TopoDS_Shape& aShape = anExp.Current();
223 if(aFreeShapes.Contains(aShape)) {
224 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
225 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
226 theFreeShapes.push_back(aGeomShape);
230 if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
231 aResult = theCombinedShapes.front();
232 } else if(theCombinedShapes.size() == 0 && theFreeShapes.size() == 1) {
233 aResult = theFreeShapes.front();
235 TopoDS_Compound aResultComp;
236 TopoDS_Builder aBuilder;
237 aBuilder.MakeCompound(aResultComp);
238 for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin(); anIter != theCombinedShapes.cend(); anIter++) {
239 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
241 for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin(); anIter != theFreeShapes.cend(); anIter++) {
242 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
244 aResult->setImpl(new TopoDS_Shape(aResultComp));
250 //==================================================================================================
251 static void addSimpleShapeToList(const TopoDS_Shape& theShape, NCollection_List<TopoDS_Shape>& theList)
253 if(theShape.IsNull()) {
257 if(theShape.ShapeType() == TopAbs_COMPOUND) {
258 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
259 addSimpleShapeToList(anIt.Value(), theList);
262 theList.Append(theShape);
266 //==================================================================================================
267 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
269 TopoDS_Compound aCompound;
271 BRep_Builder aBuilder;
272 aBuilder.MakeCompound(aCompound);
274 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
275 aBuilder.Add(aCompound, anIt.Value());
281 //==================================================================================================
282 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(const std::shared_ptr<GeomAPI_Shape> theCompound)
284 GeomShapePtr aResult = theCompound;
286 if(!theCompound.get()) {
290 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
291 NCollection_List<TopoDS_Shape> anUngroupedShapes;
292 addSimpleShapeToList(anInShape, anUngroupedShapes);
294 NCollection_Vector<NCollection_List<TopoDS_Shape>> aGroups;
295 while(!anUngroupedShapes.IsEmpty()) {
296 NCollection_List<TopoDS_Shape> aGroupedShapes;
297 aGroupedShapes.Append(anUngroupedShapes.First());
298 anUngroupedShapes.RemoveFirst();
299 for(NCollection_List<TopoDS_Shape>::Iterator aGroupIt(aGroupedShapes); aGroupIt.More(); aGroupIt.Next()) {
300 const TopoDS_Shape& aGroupShape = aGroupIt.Value();
301 for(NCollection_List<TopoDS_Shape>::Iterator anUngroupedIt(anUngroupedShapes); anUngroupedIt.More(); anUngroupedIt.Next()) {
302 const TopoDS_Shape& anUngroupedShape = anUngroupedIt.Value();
303 bool isFound = false;
304 for(TopExp_Explorer aGroupShapeExp(aGroupShape, TopAbs_VERTEX); aGroupShapeExp.More(); aGroupShapeExp.Next()) {
305 const TopoDS_Shape& aVertex1 = aGroupShapeExp.Current();
306 for(TopExp_Explorer anUngroupedShapeExp(anUngroupedShape, TopAbs_VERTEX); anUngroupedShapeExp.More(); anUngroupedShapeExp.Next()) {
307 const TopoDS_Shape& aVertex2 = anUngroupedShapeExp.Current();
308 if(aVertex1.IsSame(aVertex2)) {
309 aGroupedShapes.Append(anUngroupedShape);
310 anUngroupedShapes.Remove(anUngroupedIt);
319 if(!anUngroupedIt.More()) {
324 aGroups.Append(aGroupedShapes);
327 TopoDS_Compound aCompound;
328 BRep_Builder aBuilder;
329 aBuilder.MakeCompound(aCompound);
330 ListOfShape aCompSolids, aFreeSolids;
331 for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator anIt(aGroups); anIt.More(); anIt.Next()) {
332 NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
333 GeomShapePtr aGeomShape(new GeomAPI_Shape());
334 if(aGroup.Size() == 1) {
335 aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
337 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
338 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
339 GeomAPI_Shape::COMPSOLID,
343 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
346 if(!aCompound.IsNull()) {
347 aResult->setImpl(new TopoDS_Shape(aCompound));
353 //==================================================================================================
354 std::list<std::shared_ptr<GeomAPI_Pnt> > GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
356 // Bounding box of all objects.
360 for (ListOfShape::const_iterator anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
361 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
362 BRepBndLib::Add(aShape, aBndBox);
365 if(theEnlarge != 0.0) {
366 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
367 aBndBox.Enlarge(theEnlarge);
370 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
371 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
372 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
373 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
375 for(int i = 0; i < 2; i++) {
376 for(int j = 0; j < 2; j++) {
377 for(int k = 0; k < 2; k++) {
378 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
379 aResultPoints.push_back(aPnt);
384 return aResultPoints;
387 //==================================================================================================
388 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
391 return std::shared_ptr<GeomAPI_Shape>();
393 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
394 if (aPlaneFace.IsNull())
395 return std::shared_ptr<GeomAPI_Shape>();
397 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
399 return std::shared_ptr<GeomAPI_Shape>();
401 // make an infinity face on the plane
402 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
404 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
405 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
409 //==================================================================================================
410 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(const std::shared_ptr<GeomAPI_Shape> thePlane,
411 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
413 std::shared_ptr<GeomAPI_Face> aResultFace;
415 if(!thePlane.get()) {
419 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
420 if(aShape.ShapeType() != TopAbs_FACE) {
424 TopoDS_Face aFace = TopoDS::Face(aShape);
425 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
430 GeomLib_IsPlanarSurface isPlanar(aSurf);
431 if(!isPlanar.IsPlanar()) {
435 if(thePoints.size() != 8) {
439 const gp_Pln& aFacePln = isPlanar.Plan();
440 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
441 IntAna_Quadric aQuadric(aFacePln);
442 Standard_Real UMin, UMax, VMin, VMax;
443 UMin = UMax = VMin = VMax = 0;
444 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
445 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
446 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
447 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
448 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
449 Standard_Real aPntU(0), aPntV(0);
450 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
451 if(aPntU < UMin) UMin = aPntU;
452 if(aPntU > UMax) UMax = aPntU;
453 if(aPntV < VMin) VMin = aPntV;
454 if(aPntV > VMax) VMax = aPntV;
456 aResultFace.reset(new GeomAPI_Face());
457 aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
462 //==================================================================================================
463 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
464 std::shared_ptr<GeomAPI_Vertex>& theV1,
465 std::shared_ptr<GeomAPI_Vertex>& theV2)
467 if(!theShape.get()) {
468 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
469 aVertex->setImpl(new TopoDS_Vertex());
475 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
476 TopoDS_Vertex aV1, aV2;
477 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
479 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
480 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
481 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
486 //==================================================================================================
487 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
488 const std::shared_ptr<GeomAPI_Dir> theDirection,
489 const ListOfShape& theWires,
490 ListOfShape& theFaces)
492 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
493 theDirection->impl<gp_Dir>()));
494 TopoDS_Face aFace = aMKFace.Face();
496 BRepAlgo_FaceRestrictor aFRestrictor;
497 aFRestrictor.Init(aFace, Standard_False, Standard_True);
498 for(ListOfShape::const_iterator anIt = theWires.cbegin();
499 anIt != theWires.cend();
501 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
502 aFRestrictor.Add(aWire);
505 aFRestrictor.Perform();
507 if(!aFRestrictor.IsDone()) {
511 for(; aFRestrictor.More(); aFRestrictor.Next()) {
512 GeomShapePtr aShape(new GeomAPI_Shape());
513 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
514 theFaces.push_back(aShape);
518 //==================================================================================================
519 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
521 TopoDS_Compound aCompound;
522 BRep_Builder aBuilder;
523 aBuilder.MakeCompound(aCompound);
525 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
526 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
528 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
530 if(aFindPlane.Found() != Standard_True) {
531 return std::shared_ptr<GeomAPI_Pln>();
534 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
535 gp_Pnt aLoc = aPlane->Location();
536 gp_Dir aDir = aPlane->Axis().Direction();
538 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
539 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
541 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
546 //==================================================================================================
547 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(const std::shared_ptr<GeomAPI_Shape> theSubShape,
548 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
550 if(!theSubShape.get() || !theBaseShape.get()) {
554 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
555 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
557 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
558 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
559 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
561 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
564 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
565 if(aBaseShape.ShapeType() == TopAbs_FACE) {
566 // Check that edge is on face surface.
567 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
568 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
569 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
571 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
575 // Check intersections.
576 TopoDS_Vertex aV1, aV2;
577 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
578 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
579 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
580 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
581 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
582 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
584 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
585 // Edge intersect face bound. Check that it is not on edge begin or end.
586 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
587 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
588 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
589 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
596 // No intersections found. Edge is inside or outside face. Check it.
597 BRepAdaptor_Curve aCurveAdaptor(anEdge);
598 gp_Pnt aPointToCheck = aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() + aCurveAdaptor.LastParameter()) / 2.0);
599 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
600 ShapeAnalysis_Surface aSAS(aSurface);
601 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
602 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
603 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
617 //==================================================================================================
618 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
620 if(!theShape.get()) {
624 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
625 return (aChecker.IsValid() == Standard_True);
628 //==================================================================================================
629 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
631 GeomShapePtr anOuterWire;
633 if(!theFace.get() || !theFace->isFace()) {
637 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
638 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
640 anOuterWire.reset(new GeomAPI_Shape());
641 anOuterWire->setImpl(new TopoDS_Shape(aWire));
646 //==================================================================================================
647 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
648 const std::shared_ptr<GeomAPI_Face> theFace)
650 if(!theEdge.get() || !theFace.get()) {
654 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
655 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
657 BRepExtrema_ExtCF anExt(anEdge, aFace);
658 return anExt.IsParallel() == Standard_True;
661 //==================================================================================================
662 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
663 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
664 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
666 // General Fuse to split edge by vertices
667 BOPAlgo_Builder aBOP;
668 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
669 // Rebuild closed edge to place vertex to one of split points.
670 // This will prevent edge to be split on seam vertex.
671 if (BRep_Tool::IsClosed(aBaseEdge))
673 Standard_Real aFirst, aLast;
674 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
676 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
677 gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
679 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
680 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
681 aBaseEdge.Orientation(anOrientation);
683 aBOP.AddArgument(aBaseEdge);
685 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
686 for (; aPtIt != thePoints.end(); ++aPtIt) {
687 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
688 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
689 aBOP.AddArgument(aV);
693 if (aBOP.ErrorStatus())
697 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
698 TopTools_ListIteratorOfListOfShape anIt(aSplits);
699 for (; anIt.More(); anIt.Next()) {
700 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
701 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
702 theShapes.insert(anEdge);
706 //==================================================================================================
707 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
708 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
709 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
711 std::shared_ptr<GeomAPI_Shape> aResultShape;
713 if (thePoints.size() == 2) {
714 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
715 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
717 std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
719 std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
720 aLast = theShapes.end();
721 for (; anIt != aLast; anIt++) {
722 GeomShapePtr aShape = *anIt;
723 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
725 std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
726 std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
727 if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
728 anEdgeLastPoint->isEqual(aLastPoint))
729 aResultShape = aShape;