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>
78 GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
82 return std::shared_ptr<GeomAPI_Pnt>();
84 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
86 return std::shared_ptr<GeomAPI_Pnt>();
89 if(aShape.ShapeType() == TopAbs_VERTEX) {
90 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
91 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
92 BRepGProp::LinearProperties(aShape, aGProps);
93 aCentre = aGProps.CentreOfMass();
95 BRepGProp::SurfaceProperties(aShape, aGProps);
96 aCentre = aGProps.CentreOfMass();
98 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
101 //==================================================================================================
102 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(
103 const std::shared_ptr<GeomAPI_Shape> theCompound,
104 const GeomAPI_Shape::ShapeType theType,
105 ListOfShape& theCombinedShapes,
106 ListOfShape& theFreeShapes)
108 GeomShapePtr aResult = theCompound;
110 if(!theCompound.get()) {
114 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
118 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
119 TopAbs_ShapeEnum aTA = TopAbs_FACE;
120 if(theType == GeomAPI_Shape::COMPSOLID) {
125 theCombinedShapes.clear();
126 theFreeShapes.clear();
129 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
130 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
131 const TopoDS_Shape& aShape = anIter.Value();
132 if(aShape.ShapeType() > aTA) {
133 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
134 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
135 theFreeShapes.push_back(aGeomShape);
139 // Map subshapes and shapes.
140 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
141 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
142 if(aMapSA.IsEmpty()) {
146 // Get all shapes with common subshapes and free shapes.
147 NCollection_Map<TopoDS_Shape> aFreeShapes;
148 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
149 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator
150 anIter(aMapSA); anIter.More(); anIter.Next()) {
151 const TopoDS_Shape& aShape = anIter.Key();
152 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
153 if(aListOfShape.IsEmpty()) {
156 else if(aListOfShape.Size() == 1) {
157 const TopoDS_Shape& aF = aListOfShape.First();
159 aListOfShape.Clear();
161 NCollection_List<TopoDS_Shape> aTempList;
162 NCollection_Map<TopoDS_Shape> aTempMap;
163 const TopoDS_Shape& aF = aListOfShape.First();
164 const TopoDS_Shape& aL = aListOfShape.Last();
165 aTempList.Append(aF);
166 aTempList.Append(aL);
169 aFreeShapes.Remove(aF);
170 aFreeShapes.Remove(aL);
171 aListOfShape.Clear();
172 for(NCollection_List<TopoDS_Shape>::Iterator
173 aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
174 const TopoDS_Shape& aTempShape = aTempIter.Value();
175 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator
176 anIter(aMapSA); anIter.More(); anIter.Next()) {
177 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
178 if(aTempListOfShape.IsEmpty()) {
180 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
181 aTempListOfShape.Clear();
182 } else if(aTempListOfShape.Size() > 1) {
183 if(aTempListOfShape.First() == aTempShape) {
184 const TopoDS_Shape& aTL = aTempListOfShape.Last();
185 if(aTempMap.Add(aTL)) {
186 aTempList.Append(aTL);
187 aFreeShapes.Remove(aTL);
189 aTempListOfShape.Clear();
190 } else if(aTempListOfShape.Last() == aTempShape) {
191 const TopoDS_Shape& aTF = aTempListOfShape.First();
192 if(aTempMap.Add(aTF)) {
193 aTempList.Append(aTF);
194 aFreeShapes.Remove(aTF);
196 aTempListOfShape.Clear();
201 aShapesWithCommonSubshapes.Append(aTempMap);
205 // Combine shapes with common subshapes.
206 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator
207 anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
209 TopoDS_CompSolid aCSolid;
210 TopoDS_Builder aBuilder;
212 GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
213 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
214 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
215 const TopoDS_Shape& aShape = anExp.Current();
216 if(aShapesMap.Contains(aShape)) {
218 GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
219 aShapesMap.Remove(aShape);
222 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
223 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) :
224 new TopoDS_Shape(aShell);
225 aGeomShape->setImpl<TopoDS_Shape>(aSh);
226 theCombinedShapes.push_back(aGeomShape);
229 // Adding free shapes.
230 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
231 const TopoDS_Shape& aShape = anExp.Current();
232 if(aFreeShapes.Contains(aShape)) {
233 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
234 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
235 theFreeShapes.push_back(aGeomShape);
239 if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
240 aResult = theCombinedShapes.front();
241 } else if(theCombinedShapes.size() == 0 && theFreeShapes.size() == 1) {
242 aResult = theFreeShapes.front();
244 TopoDS_Compound aResultComp;
245 TopoDS_Builder aBuilder;
246 aBuilder.MakeCompound(aResultComp);
247 for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin();
248 anIter != theCombinedShapes.cend(); anIter++) {
249 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
251 for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin();
252 anIter != theFreeShapes.cend(); anIter++) {
253 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
255 aResult->setImpl(new TopoDS_Shape(aResultComp));
261 //==================================================================================================
262 static void addSimpleShapeToList(const TopoDS_Shape& theShape,
263 NCollection_List<TopoDS_Shape>& theList)
265 if(theShape.IsNull()) {
269 if(theShape.ShapeType() == TopAbs_COMPOUND) {
270 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
271 addSimpleShapeToList(anIt.Value(), theList);
274 theList.Append(theShape);
278 //==================================================================================================
279 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
281 TopoDS_Compound aCompound;
283 BRep_Builder aBuilder;
284 aBuilder.MakeCompound(aCompound);
286 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
287 aBuilder.Add(aCompound, anIt.Value());
293 //==================================================================================================
294 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(
295 const std::shared_ptr<GeomAPI_Shape> theCompound)
297 GeomShapePtr aResult = theCompound;
299 if(!theCompound.get()) {
303 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
304 NCollection_List<TopoDS_Shape> anUngroupedShapes;
305 addSimpleShapeToList(anInShape, anUngroupedShapes);
307 NCollection_Vector<NCollection_List<TopoDS_Shape>> aGroups;
308 while(!anUngroupedShapes.IsEmpty()) {
309 NCollection_List<TopoDS_Shape> aGroupedShapes;
310 aGroupedShapes.Append(anUngroupedShapes.First());
311 anUngroupedShapes.RemoveFirst();
312 for(NCollection_List<TopoDS_Shape>::Iterator aGroupIt(aGroupedShapes);
313 aGroupIt.More(); aGroupIt.Next()) {
314 const TopoDS_Shape& aGroupShape = aGroupIt.Value();
315 for(NCollection_List<TopoDS_Shape>::Iterator anUngroupedIt(anUngroupedShapes);
316 anUngroupedIt.More(); anUngroupedIt.Next()) {
317 const TopoDS_Shape& anUngroupedShape = anUngroupedIt.Value();
318 bool isFound = false;
319 for(TopExp_Explorer aGroupShapeExp(aGroupShape, TopAbs_VERTEX);
320 aGroupShapeExp.More(); aGroupShapeExp.Next()) {
321 const TopoDS_Shape& aVertex1 = aGroupShapeExp.Current();
322 for(TopExp_Explorer anUngroupedShapeExp(anUngroupedShape, TopAbs_VERTEX);
323 anUngroupedShapeExp.More(); anUngroupedShapeExp.Next()) {
324 const TopoDS_Shape& aVertex2 = anUngroupedShapeExp.Current();
325 if(aVertex1.IsSame(aVertex2)) {
326 aGroupedShapes.Append(anUngroupedShape);
327 anUngroupedShapes.Remove(anUngroupedIt);
336 if(!anUngroupedIt.More()) {
341 aGroups.Append(aGroupedShapes);
344 TopoDS_Compound aCompound;
345 BRep_Builder aBuilder;
346 aBuilder.MakeCompound(aCompound);
347 ListOfShape aCompSolids, aFreeSolids;
348 for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator
349 anIt(aGroups); anIt.More(); anIt.Next()) {
350 NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
351 GeomShapePtr aGeomShape(new GeomAPI_Shape());
352 if(aGroup.Size() == 1) {
353 aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
355 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
356 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
357 GeomAPI_Shape::COMPSOLID,
361 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
364 if(!aCompound.IsNull()) {
365 aResult->setImpl(new TopoDS_Shape(aCompound));
371 //==================================================================================================
372 std::list<std::shared_ptr<GeomAPI_Pnt> >
373 GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
375 // Bounding box of all objects.
379 for (ListOfShape::const_iterator
380 anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
381 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
382 BRepBndLib::Add(aShape, aBndBox);
385 if(theEnlarge != 0.0) {
386 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
387 aBndBox.Enlarge(theEnlarge);
390 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
391 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
392 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
393 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
395 for(int i = 0; i < 2; i++) {
396 for(int j = 0; j < 2; j++) {
397 for(int k = 0; k < 2; k++) {
398 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
399 aResultPoints.push_back(aPnt);
404 return aResultPoints;
407 //==================================================================================================
408 std::shared_ptr<GeomAPI_Shape>
409 GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
412 return std::shared_ptr<GeomAPI_Shape>();
414 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
415 if (aPlaneFace.IsNull())
416 return std::shared_ptr<GeomAPI_Shape>();
418 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
420 return std::shared_ptr<GeomAPI_Shape>();
422 // make an infinity face on the plane
423 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
425 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
426 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
430 //==================================================================================================
431 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(
432 const std::shared_ptr<GeomAPI_Shape> thePlane,
433 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
435 std::shared_ptr<GeomAPI_Face> aResultFace;
437 if(!thePlane.get()) {
441 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
442 if(aShape.ShapeType() != TopAbs_FACE) {
446 TopoDS_Face aFace = TopoDS::Face(aShape);
447 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
452 GeomLib_IsPlanarSurface isPlanar(aSurf);
453 if(!isPlanar.IsPlanar()) {
457 if(thePoints.size() != 8) {
461 const gp_Pln& aFacePln = isPlanar.Plan();
462 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
463 IntAna_Quadric aQuadric(aFacePln);
464 Standard_Real UMin, UMax, VMin, VMax;
465 UMin = UMax = VMin = VMax = 0;
466 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
467 aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
468 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
469 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
470 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
471 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
472 Standard_Real aPntU(0), aPntV(0);
473 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
474 if(aPntU < UMin) UMin = aPntU;
475 if(aPntU > UMax) UMax = aPntU;
476 if(aPntV < VMin) VMin = aPntV;
477 if(aPntV > VMax) VMax = aPntV;
479 aResultFace.reset(new GeomAPI_Face());
480 aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
485 //==================================================================================================
486 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
487 std::shared_ptr<GeomAPI_Vertex>& theV1,
488 std::shared_ptr<GeomAPI_Vertex>& theV2)
490 if(!theShape.get()) {
491 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
492 aVertex->setImpl(new TopoDS_Vertex());
498 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
499 TopoDS_Vertex aV1, aV2;
500 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
502 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
503 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
504 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
509 //==================================================================================================
510 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
511 const std::shared_ptr<GeomAPI_Dir> theDirection,
512 const ListOfShape& theWires,
513 ListOfShape& theFaces)
515 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
516 theDirection->impl<gp_Dir>()));
517 TopoDS_Face aFace = aMKFace.Face();
519 BRepAlgo_FaceRestrictor aFRestrictor;
520 aFRestrictor.Init(aFace, Standard_False, Standard_True);
521 for(ListOfShape::const_iterator anIt = theWires.cbegin();
522 anIt != theWires.cend();
524 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
525 aFRestrictor.Add(aWire);
528 aFRestrictor.Perform();
530 if(!aFRestrictor.IsDone()) {
534 for(; aFRestrictor.More(); aFRestrictor.Next()) {
535 GeomShapePtr aShape(new GeomAPI_Shape());
536 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
537 theFaces.push_back(aShape);
541 //==================================================================================================
542 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
544 TopoDS_Compound aCompound;
545 BRep_Builder aBuilder;
546 aBuilder.MakeCompound(aCompound);
548 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
549 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
551 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
553 if(aFindPlane.Found() != Standard_True) {
554 return std::shared_ptr<GeomAPI_Pln>();
557 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
558 gp_Pnt aLoc = aPlane->Location();
559 gp_Dir aDir = aPlane->Axis().Direction();
561 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
562 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
564 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
569 //==================================================================================================
570 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(
571 const std::shared_ptr<GeomAPI_Shape> theSubShape,
572 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
574 if(!theSubShape.get() || !theBaseShape.get()) {
578 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
579 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
581 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
582 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
583 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
585 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
588 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
589 if(aBaseShape.ShapeType() == TopAbs_FACE) {
590 // Check that edge is on face surface.
591 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
592 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
593 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
595 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
599 // Check intersections.
600 TopoDS_Vertex aV1, aV2;
601 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
602 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
603 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
604 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
605 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
606 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
608 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
609 // Edge intersect face bound. Check that it is not on edge begin or end.
610 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
611 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
612 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
613 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
620 // No intersections found. Edge is inside or outside face. Check it.
621 BRepAdaptor_Curve aCurveAdaptor(anEdge);
622 gp_Pnt aPointToCheck =
623 aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() +
624 aCurveAdaptor.LastParameter()) / 2.0);
625 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
626 ShapeAnalysis_Surface aSAS(aSurface);
627 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
628 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
629 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
643 //==================================================================================================
644 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
646 if(!theShape.get()) {
650 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
651 return (aChecker.IsValid() == Standard_True);
654 //==================================================================================================
655 std::shared_ptr<GeomAPI_Shape>
656 GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
658 GeomShapePtr anOuterWire;
660 if(!theFace.get() || !theFace->isFace()) {
664 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
665 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
667 anOuterWire.reset(new GeomAPI_Shape());
668 anOuterWire->setImpl(new TopoDS_Shape(aWire));
673 //==================================================================================================
674 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
675 const std::shared_ptr<GeomAPI_Face> theFace)
677 if(!theEdge.get() || !theFace.get()) {
681 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
682 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
684 BRepExtrema_ExtCF anExt(anEdge, aFace);
685 return anExt.IsParallel() == Standard_True;
688 //==================================================================================================
689 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
690 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
691 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
693 // General Fuse to split edge by vertices
694 BOPAlgo_Builder aBOP;
695 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
696 // Rebuild closed edge to place vertex to one of split points.
697 // This will prevent edge to be split on seam vertex.
698 if (BRep_Tool::IsClosed(aBaseEdge))
700 Standard_Real aFirst, aLast;
701 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
703 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
704 gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
706 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
707 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
708 aBaseEdge.Orientation(anOrientation);
710 aBOP.AddArgument(aBaseEdge);
712 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
713 for (; aPtIt != thePoints.end(); ++aPtIt) {
714 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
715 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
716 aBOP.AddArgument(aV);
720 if (aBOP.ErrorStatus())
724 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
725 TopTools_ListIteratorOfListOfShape anIt(aSplits);
726 for (; anIt.More(); anIt.Next()) {
727 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
728 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
729 theShapes.insert(anEdge);
733 //==================================================================================================
734 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
735 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
736 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
738 std::shared_ptr<GeomAPI_Shape> aResultShape;
740 if (thePoints.size() == 2) {
741 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
742 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
744 std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
746 std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
747 aLast = theShapes.end();
748 for (; anIt != aLast; anIt++) {
749 GeomShapePtr aShape = *anIt;
750 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
752 std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
753 std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
754 if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
755 anEdgeLastPoint->isEqual(aLastPoint))
756 aResultShape = aShape;