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 <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, aStillUngroupedShapes;
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& aGroupedShape = aGroupIt.Value();
315 for(TopExp_Explorer aGroupShapeExp(aGroupedShape, TopAbs_VERTEX);
316 aGroupShapeExp.More();
317 aGroupShapeExp.Next()) {
318 // Find all shapes which have same vertex.
319 aStillUngroupedShapes.Clear();
320 const TopoDS_Shape& aVertex1 = aGroupShapeExp.Current();
321 for(NCollection_List<TopoDS_Shape>::Iterator anUngroupedIt(anUngroupedShapes);
322 anUngroupedIt.More();
323 anUngroupedIt.Next()) {
324 const TopoDS_Shape& anUngroupedShape = anUngroupedIt.Value();
325 bool isAdded = false;
326 for(TopExp_Explorer anUgroupedShapeExp(anUngroupedShape, TopAbs_VERTEX);
327 anUgroupedShapeExp.More();
328 anUgroupedShapeExp.Next()) {
329 const TopoDS_Shape& aVertex2 = anUgroupedShapeExp.Current();
330 if(aVertex1.IsSame(aVertex2)) {
331 aGroupedShapes.Append(anUngroupedShape);
337 aStillUngroupedShapes.Append(anUngroupedShape);
340 anUngroupedShapes = aStillUngroupedShapes;
341 if(anUngroupedShapes.IsEmpty()) {
345 if(anUngroupedShapes.IsEmpty()) {
349 aGroups.Append(aGroupedShapes);
352 TopoDS_Compound aCompound;
353 BRep_Builder aBuilder;
354 aBuilder.MakeCompound(aCompound);
355 ListOfShape aCompSolids, aFreeSolids;
356 for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator
357 anIt(aGroups); anIt.More(); anIt.Next()) {
358 NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
359 GeomShapePtr aGeomShape(new GeomAPI_Shape());
360 if(aGroup.Size() == 1) {
361 aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
363 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
364 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
365 GeomAPI_Shape::COMPSOLID,
369 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
372 if(!aCompound.IsNull()) {
373 aResult->setImpl(new TopoDS_Shape(aCompound));
379 //==================================================================================================
380 std::list<std::shared_ptr<GeomAPI_Pnt> >
381 GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
383 // Bounding box of all objects.
387 for (ListOfShape::const_iterator
388 anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
389 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
390 BRepBndLib::Add(aShape, aBndBox);
393 if(theEnlarge != 0.0) {
394 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
395 aBndBox.Enlarge(theEnlarge);
398 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
399 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
400 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
401 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
403 for(int i = 0; i < 2; i++) {
404 for(int j = 0; j < 2; j++) {
405 for(int k = 0; k < 2; k++) {
406 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
407 aResultPoints.push_back(aPnt);
412 return aResultPoints;
415 //==================================================================================================
416 std::shared_ptr<GeomAPI_Shape>
417 GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
420 return std::shared_ptr<GeomAPI_Shape>();
422 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
423 if (aPlaneFace.IsNull())
424 return std::shared_ptr<GeomAPI_Shape>();
426 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
428 return std::shared_ptr<GeomAPI_Shape>();
430 // make an infinity face on the plane
431 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
433 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
434 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
438 //==================================================================================================
439 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(
440 const std::shared_ptr<GeomAPI_Shape> thePlane,
441 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
443 std::shared_ptr<GeomAPI_Face> aResultFace;
445 if(!thePlane.get()) {
449 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
450 if(aShape.ShapeType() != TopAbs_FACE) {
454 TopoDS_Face aFace = TopoDS::Face(aShape);
455 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
460 GeomLib_IsPlanarSurface isPlanar(aSurf);
461 if(!isPlanar.IsPlanar()) {
465 if(thePoints.size() != 8) {
469 const gp_Pln& aFacePln = isPlanar.Plan();
470 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
471 IntAna_Quadric aQuadric(aFacePln);
472 Standard_Real UMin, UMax, VMin, VMax;
473 UMin = UMax = VMin = VMax = 0;
474 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
475 aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
476 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
477 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
478 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
479 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
480 Standard_Real aPntU(0), aPntV(0);
481 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
482 if(aPntU < UMin) UMin = aPntU;
483 if(aPntU > UMax) UMax = aPntU;
484 if(aPntV < VMin) VMin = aPntV;
485 if(aPntV > VMax) VMax = aPntV;
487 aResultFace.reset(new GeomAPI_Face());
488 aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
493 //==================================================================================================
494 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
495 std::shared_ptr<GeomAPI_Vertex>& theV1,
496 std::shared_ptr<GeomAPI_Vertex>& theV2)
498 if(!theShape.get()) {
499 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
500 aVertex->setImpl(new TopoDS_Vertex());
506 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
507 TopoDS_Vertex aV1, aV2;
508 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
510 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
511 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
512 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
517 //==================================================================================================
518 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
519 const std::shared_ptr<GeomAPI_Dir> theDirection,
520 const ListOfShape& theWires,
521 ListOfShape& theFaces)
523 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
524 theDirection->impl<gp_Dir>()));
525 TopoDS_Face aFace = aMKFace.Face();
527 BRepAlgo_FaceRestrictor aFRestrictor;
528 aFRestrictor.Init(aFace, Standard_False, Standard_True);
529 for(ListOfShape::const_iterator anIt = theWires.cbegin();
530 anIt != theWires.cend();
532 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
533 aFRestrictor.Add(aWire);
536 aFRestrictor.Perform();
538 if(!aFRestrictor.IsDone()) {
542 for(; aFRestrictor.More(); aFRestrictor.Next()) {
543 GeomShapePtr aShape(new GeomAPI_Shape());
544 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
545 theFaces.push_back(aShape);
549 //==================================================================================================
550 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
552 TopoDS_Compound aCompound;
553 BRep_Builder aBuilder;
554 aBuilder.MakeCompound(aCompound);
556 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
557 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
559 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
561 if(aFindPlane.Found() != Standard_True) {
562 return std::shared_ptr<GeomAPI_Pln>();
565 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
566 gp_Pnt aLoc = aPlane->Location();
567 gp_Dir aDir = aPlane->Axis().Direction();
569 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
570 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
572 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
577 //==================================================================================================
578 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(
579 const std::shared_ptr<GeomAPI_Shape> theSubShape,
580 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
582 if(!theSubShape.get() || !theBaseShape.get()) {
586 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
587 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
589 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
590 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
591 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
593 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
596 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
597 if(aBaseShape.ShapeType() == TopAbs_FACE) {
598 // Check that edge is on face surface.
599 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
600 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
601 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
603 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
607 // Check intersections.
608 TopoDS_Vertex aV1, aV2;
609 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
610 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
611 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
612 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
613 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
614 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
616 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
617 // Edge intersect face bound. Check that it is not on edge begin or end.
618 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
619 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
620 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
621 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
628 // No intersections found. Edge is inside or outside face. Check it.
629 BRepAdaptor_Curve aCurveAdaptor(anEdge);
630 gp_Pnt aPointToCheck =
631 aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() +
632 aCurveAdaptor.LastParameter()) / 2.0);
633 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
634 ShapeAnalysis_Surface aSAS(aSurface);
635 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
636 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
637 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
651 //==================================================================================================
652 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
654 if(!theShape.get()) {
658 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
659 return (aChecker.IsValid() == Standard_True);
662 //==================================================================================================
663 std::shared_ptr<GeomAPI_Shape>
664 GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
666 GeomShapePtr anOuterWire;
668 if(!theFace.get() || !theFace->isFace()) {
672 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
673 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
675 anOuterWire.reset(new GeomAPI_Shape());
676 anOuterWire->setImpl(new TopoDS_Shape(aWire));
681 //==================================================================================================
682 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
683 const std::shared_ptr<GeomAPI_Face> theFace)
685 if(!theEdge.get() || !theFace.get()) {
689 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
690 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
692 BRepExtrema_ExtCF anExt(anEdge, aFace);
693 return anExt.IsParallel() == Standard_True;
696 //==================================================================================================
697 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
698 const GeomAlgoAPI_ShapeTools::PointToRefsMap& thePointsInfo,
699 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
701 // General Fuse to split edge by vertices
702 BOPAlgo_Builder aBOP;
703 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
704 // Rebuild closed edge to place vertex to one of split points.
705 // This will prevent edge to be split on same vertex.
706 if (BRep_Tool::IsClosed(aBaseEdge))
708 Standard_Real aFirst, aLast;
709 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
711 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
712 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
713 gp_Pnt aPoint(aPnt->x(), aPnt->y(), aPnt->z());
715 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
716 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
717 aBaseEdge.Orientation(anOrientation);
719 aBOP.AddArgument(aBaseEdge);
721 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
722 for (; aPIt != thePointsInfo.end(); ++aPIt) {
723 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
724 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
725 aBOP.AddArgument(aV);
729 if (aBOP.ErrorStatus())
733 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
734 TopTools_ListIteratorOfListOfShape anIt(aSplits);
735 for (; anIt.More(); anIt.Next()) {
736 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
737 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
738 theShapes.insert(anEdge);
742 //==================================================================================================
743 void GeomAlgoAPI_ShapeTools::splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
744 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
745 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
747 // General Fuse to split edge by vertices
748 BOPAlgo_Builder aBOP;
749 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
750 // Rebuild closed edge to place vertex to one of split points.
751 // This will prevent edge to be split on seam vertex.
752 if (BRep_Tool::IsClosed(aBaseEdge))
754 Standard_Real aFirst, aLast;
755 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
757 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
758 gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
760 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
761 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
762 aBaseEdge.Orientation(anOrientation);
764 aBOP.AddArgument(aBaseEdge);
766 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
767 for (; aPtIt != thePoints.end(); ++aPtIt) {
768 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
769 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
770 aBOP.AddArgument(aV);
774 if (aBOP.ErrorStatus())
778 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
779 TopTools_ListIteratorOfListOfShape anIt(aSplits);
780 for (; anIt.More(); anIt.Next()) {
781 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
782 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
783 theShapes.insert(anEdge);
787 //==================================================================================================
788 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
789 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
790 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
792 std::shared_ptr<GeomAPI_Shape> aResultShape;
794 if (thePoints.size() == 2) {
795 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
796 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
798 std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
800 std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
801 aLast = theShapes.end();
802 for (; anIt != aLast; anIt++) {
803 GeomShapePtr aShape = *anIt;
804 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
806 std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
807 std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
808 if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
809 anEdgeLastPoint->isEqual(aLastPoint))
810 aResultShape = aShape;