1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_ShapeTools.h"
23 #include "GeomAlgoAPI_SketchBuilder.h"
25 #include <GeomAPI_Ax1.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Dir.h>
28 #include <GeomAPI_Face.h>
29 #include <GeomAPI_Pln.h>
30 #include <GeomAPI_Pnt.h>
31 #include <GeomAPI_Wire.h>
33 #include <Bnd_Box.hxx>
34 #include <BOPTools.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRepAdaptor_Curve.hxx>
37 #include <BRepAlgo.hxx>
38 #include <BRepAlgo_FaceRestrictor.hxx>
39 #include <BRepBndLib.hxx>
40 #include <BRepBuilderAPI_FindPlane.hxx>
41 #include <BRepBuilderAPI_MakeEdge.hxx>
42 #include <BRepBuilderAPI_MakeFace.hxx>
43 #include <BRepCheck_Analyzer.hxx>
44 #include <BRepExtrema_DistShapeShape.hxx>
45 #include <BRepExtrema_ExtCF.hxx>
46 #include <BRepGProp.hxx>
47 #include <BRepTools.hxx>
48 #include <BRepTopAdaptor_FClass2d.hxx>
49 #include <BRepClass_FaceClassifier.hxx>
50 #include <Geom2d_Curve.hxx>
51 #include <Geom2d_Curve.hxx>
52 #include <BRepLib_CheckCurveOnSurface.hxx>
53 #include <BRep_Tool.hxx>
54 #include <Geom_Line.hxx>
55 #include <Geom_Plane.hxx>
56 #include <GeomAPI_ProjectPointOnCurve.hxx>
57 #include <GeomLib_IsPlanarSurface.hxx>
58 #include <GeomLib_Tool.hxx>
59 #include <GeomAPI_IntCS.hxx>
61 #include <GProp_GProps.hxx>
62 #include <IntAna_IntConicQuad.hxx>
63 #include <IntAna_Quadric.hxx>
64 #include <NCollection_Vector.hxx>
65 #include <ShapeAnalysis.hxx>
66 #include <ShapeAnalysis_Surface.hxx>
67 #include <TopoDS_Builder.hxx>
68 #include <TopoDS_Edge.hxx>
69 #include <TopoDS_Face.hxx>
70 #include <TopoDS_Shape.hxx>
71 #include <TopoDS_Shell.hxx>
72 #include <TopoDS_Vertex.hxx>
74 #include <TopExp_Explorer.hxx>
75 #include <TopTools_ListIteratorOfListOfShape.hxx>
78 #include <BOPAlgo_Builder.hxx>
79 #include <BRepBuilderAPI_MakeVertex.hxx>
80 #include <TopoDS_Edge.hxx>
82 //==================================================================================================
83 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
89 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
93 const Standard_Real anEps = 1.e-6;
94 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
95 return aGProps.Mass();
98 //==================================================================================================
99 double GeomAlgoAPI_ShapeTools::area (const std::shared_ptr<GeomAPI_Shape> theShape)
101 GProp_GProps aGProps;
102 if(!theShape.get()) {
105 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
106 if(aShape.IsNull()) {
109 const Standard_Real anEps = 1.e-6;
111 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
112 return aGProps.Mass();
115 //==================================================================================================
116 std::shared_ptr<GeomAPI_Pnt>
117 GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
119 GProp_GProps aGProps;
121 return std::shared_ptr<GeomAPI_Pnt>();
123 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
124 if(aShape.IsNull()) {
125 return std::shared_ptr<GeomAPI_Pnt>();
128 if(aShape.ShapeType() == TopAbs_VERTEX) {
129 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
130 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
131 BRepGProp::LinearProperties(aShape, aGProps);
132 aCentre = aGProps.CentreOfMass();
134 const Standard_Real anEps = 1.e-6;
135 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
136 aCentre = aGProps.CentreOfMass();
138 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
141 //==================================================================================================
142 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(
143 const std::shared_ptr<GeomAPI_Shape> theCompound,
144 const GeomAPI_Shape::ShapeType theType,
145 ListOfShape& theCombinedShapes,
146 ListOfShape& theFreeShapes)
148 GeomShapePtr aResult = theCompound;
150 if(!theCompound.get()) {
154 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
158 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
159 TopAbs_ShapeEnum aTA = TopAbs_FACE;
160 if(theType == GeomAPI_Shape::COMPSOLID) {
165 theCombinedShapes.clear();
166 theFreeShapes.clear();
169 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
170 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
171 const TopoDS_Shape& aShape = anIter.Value();
172 if(aShape.ShapeType() > aTA) {
173 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
174 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
175 theFreeShapes.push_back(aGeomShape);
179 // Map subshapes and shapes.
180 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
181 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
182 if(aMapSA.IsEmpty()) {
186 // Get all shapes with common subshapes and free shapes.
187 NCollection_Map<TopoDS_Shape> aFreeShapes;
188 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
189 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator
190 anIter(aMapSA); anIter.More(); anIter.Next()) {
191 const TopoDS_Shape& aShape = anIter.Key();
192 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
193 if(aListOfShape.IsEmpty()) {
196 else if(aListOfShape.Size() == 1) {
197 const TopoDS_Shape& aF = aListOfShape.First();
199 aListOfShape.Clear();
201 NCollection_List<TopoDS_Shape> aTempList;
202 NCollection_Map<TopoDS_Shape> aTempMap;
203 const TopoDS_Shape& aF = aListOfShape.First();
204 const TopoDS_Shape& aL = aListOfShape.Last();
205 aTempList.Append(aF);
206 aTempList.Append(aL);
209 aFreeShapes.Remove(aF);
210 aFreeShapes.Remove(aL);
211 aListOfShape.Clear();
212 for(NCollection_List<TopoDS_Shape>::Iterator
213 aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
214 const TopoDS_Shape& aTempShape = aTempIter.Value();
215 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator
216 anIter(aMapSA); anIter.More(); anIter.Next()) {
217 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
218 if(aTempListOfShape.IsEmpty()) {
220 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
221 aTempListOfShape.Clear();
222 } else if(aTempListOfShape.Size() > 1) {
223 if(aTempListOfShape.First() == aTempShape) {
224 const TopoDS_Shape& aTL = aTempListOfShape.Last();
225 if(aTempMap.Add(aTL)) {
226 aTempList.Append(aTL);
227 aFreeShapes.Remove(aTL);
229 aTempListOfShape.Clear();
230 } else if(aTempListOfShape.Last() == aTempShape) {
231 const TopoDS_Shape& aTF = aTempListOfShape.First();
232 if(aTempMap.Add(aTF)) {
233 aTempList.Append(aTF);
234 aFreeShapes.Remove(aTF);
236 aTempListOfShape.Clear();
241 aShapesWithCommonSubshapes.Append(aTempMap);
245 // Combine shapes with common subshapes.
246 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator
247 anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
249 TopoDS_CompSolid aCSolid;
250 TopoDS_Builder aBuilder;
252 GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
253 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
254 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
255 const TopoDS_Shape& aShape = anExp.Current();
256 if(aShapesMap.Contains(aShape)) {
258 GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
259 aShapesMap.Remove(aShape);
262 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
263 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) :
264 new TopoDS_Shape(aShell);
265 aGeomShape->setImpl<TopoDS_Shape>(aSh);
266 theCombinedShapes.push_back(aGeomShape);
269 // Adding free shapes.
270 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
271 const TopoDS_Shape& aShape = anExp.Current();
272 if(aFreeShapes.Contains(aShape)) {
273 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
274 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
275 theFreeShapes.push_back(aGeomShape);
279 if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
280 aResult = theCombinedShapes.front();
281 } else if(theCombinedShapes.size() == 0 && theFreeShapes.size() == 1) {
282 aResult = theFreeShapes.front();
284 TopoDS_Compound aResultComp;
285 TopoDS_Builder aBuilder;
286 aBuilder.MakeCompound(aResultComp);
287 for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin();
288 anIter != theCombinedShapes.cend(); anIter++) {
289 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
291 for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin();
292 anIter != theFreeShapes.cend(); anIter++) {
293 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
295 aResult->setImpl(new TopoDS_Shape(aResultComp));
301 //==================================================================================================
302 static void addSimpleShapeToList(const TopoDS_Shape& theShape,
303 NCollection_List<TopoDS_Shape>& theList)
305 if(theShape.IsNull()) {
309 if(theShape.ShapeType() == TopAbs_COMPOUND) {
310 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
311 addSimpleShapeToList(anIt.Value(), theList);
314 theList.Append(theShape);
318 //==================================================================================================
319 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
321 TopoDS_Compound aCompound;
323 BRep_Builder aBuilder;
324 aBuilder.MakeCompound(aCompound);
326 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
327 aBuilder.Add(aCompound, anIt.Value());
333 //==================================================================================================
334 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(
335 const std::shared_ptr<GeomAPI_Shape> theCompound)
337 GeomShapePtr aResult = theCompound;
339 if(!theCompound.get()) {
343 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
344 NCollection_List<TopoDS_Shape> anUngroupedShapes, aStillUngroupedShapes;
345 addSimpleShapeToList(anInShape, anUngroupedShapes);
347 // Iterate over all shapes and find shapes with shared vertices.
348 TopTools_ListOfShape aMapOrder;
349 BOPCol_DataMapOfShapeListOfShape aVertexShapesMap;
350 for(NCollection_List<TopoDS_Shape>::Iterator aShapesIt(anUngroupedShapes);
353 const TopoDS_Shape& aShape = aShapesIt.Value();
354 for(TopExp_Explorer aShapeExp(aShape, TopAbs_VERTEX);
357 const TopoDS_Shape& aVertex = aShapeExp.Current();
358 if (!aVertexShapesMap.IsBound(aVertex)) {
359 NCollection_List<TopoDS_Shape> aList;
360 aList.Append(aShape);
361 aMapOrder.Append(aVertex);
362 aVertexShapesMap.Bind(aVertex, aList);
364 if(!aVertexShapesMap.ChangeFind(aVertex).Contains(aShape)) {
365 aVertexShapesMap.ChangeFind(aVertex).Append(aShape);
371 // Iterate over the map and group shapes.
372 NCollection_Vector<TopTools_ListOfShape> aGroups;
373 while (!aMapOrder.IsEmpty()) {
374 // Get first group of shapes in map, and then unbind it.
375 const TopoDS_Shape& aKey = aMapOrder.First();
376 TopTools_ListOfShape aGroupedShapes = aVertexShapesMap.Find(aKey);
377 aVertexShapesMap.UnBind(aKey);
378 aMapOrder.Remove(aKey);
379 // Iterate over shapes in this group and add to it shapes from groups in map.
380 for(TopTools_ListOfShape::Iterator aGroupIt(aGroupedShapes);
381 aGroupIt.More(); aGroupIt.Next()) {
382 const TopoDS_Shape& aGroupedShape = aGroupIt.Value();
383 TopTools_ListOfShape aKeysToUnbind;
384 for(TopTools_ListOfShape::Iterator aKeysIt(aMapOrder);
387 const TopTools_ListOfShape& aGroupInMap = aVertexShapesMap(aKeysIt.Value());
388 if(!aGroupInMap.Contains(aGroupedShape)) {
389 // Group in map does not containt shape from our group, so go to the next group in map.
392 // Iterate over shape in group in map, and add new shapes into our group.
393 for(TopTools_ListOfShape::Iterator aGroupInMapIt(aGroupInMap);
394 aGroupInMapIt.More();
395 aGroupInMapIt.Next()) {
396 const TopoDS_Shape& aShape = aGroupInMapIt.Value();
397 if (!aGroupedShapes.Contains(aShape)) {
398 aGroupedShapes.Append(aShape);
401 // Save key to unbind from this map.
402 aKeysToUnbind.Append(aKeysIt.Value());
404 // Unbind groups from map that we added to our group.
405 for(TopTools_ListOfShape::Iterator aKeysIt(aKeysToUnbind);
408 aVertexShapesMap.UnBind(aKeysIt.Value());
409 aMapOrder.Remove(aKeysIt.Value());
413 TopTools_ListOfShape aSortedGroup;
414 for(int aST = TopAbs_COMPOUND; aST <= TopAbs_SHAPE; ++aST) {
415 TopTools_ListOfShape::Iterator anIt(aGroupedShapes);
416 while (anIt.More()) {
417 if(anIt.Value().ShapeType() == aST) {
418 aSortedGroup.Append(anIt.Value());
419 aGroupedShapes.Remove(anIt);
425 aGroups.Append(aSortedGroup);
428 TopoDS_Compound aCompound;
429 BRep_Builder aBuilder;
430 aBuilder.MakeCompound(aCompound);
431 ListOfShape aCompSolids, aFreeSolids;
432 for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator
433 anIt(aGroups); anIt.More(); anIt.Next()) {
434 NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
435 GeomShapePtr aGeomShape(new GeomAPI_Shape());
436 if(aGroup.Size() == 1) {
437 aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
439 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
440 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
441 GeomAPI_Shape::COMPSOLID,
445 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
448 if(!aCompound.IsNull()) {
449 aResult->setImpl(new TopoDS_Shape(aCompound));
455 //==================================================================================================
456 std::list<std::shared_ptr<GeomAPI_Pnt> >
457 GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
459 // Bounding box of all objects.
463 for (ListOfShape::const_iterator
464 anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
465 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
466 BRepBndLib::Add(aShape, aBndBox);
469 if(theEnlarge != 0.0) {
470 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
471 aBndBox.Enlarge(theEnlarge);
474 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
475 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
476 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
477 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
479 for(int i = 0; i < 2; i++) {
480 for(int j = 0; j < 2; j++) {
481 for(int k = 0; k < 2; k++) {
482 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
483 aResultPoints.push_back(aPnt);
488 return aResultPoints;
491 //==================================================================================================
492 std::shared_ptr<GeomAPI_Shape>
493 GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
496 return std::shared_ptr<GeomAPI_Shape>();
498 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
499 if (aPlaneFace.IsNull())
500 return std::shared_ptr<GeomAPI_Shape>();
502 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
504 return std::shared_ptr<GeomAPI_Shape>();
506 // make an infinity face on the plane
507 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
509 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
510 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
514 //==================================================================================================
515 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(
516 const std::shared_ptr<GeomAPI_Shape> thePlane,
517 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
519 std::shared_ptr<GeomAPI_Face> aResultFace;
521 if(!thePlane.get()) {
525 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
526 if(aShape.ShapeType() != TopAbs_FACE) {
530 TopoDS_Face aFace = TopoDS::Face(aShape);
531 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
536 GeomLib_IsPlanarSurface isPlanar(aSurf);
537 if(!isPlanar.IsPlanar()) {
541 if(thePoints.size() != 8) {
545 const gp_Pln& aFacePln = isPlanar.Plan();
546 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
547 IntAna_Quadric aQuadric(aFacePln);
548 Standard_Real UMin, UMax, VMin, VMax;
549 UMin = UMax = VMin = VMax = 0;
550 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
551 aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
552 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
553 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
554 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
555 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
556 Standard_Real aPntU(0), aPntV(0);
557 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
558 if(aPntU < UMin) UMin = aPntU;
559 if(aPntU > UMax) UMax = aPntU;
560 if(aPntV < VMin) VMin = aPntV;
561 if(aPntV > VMax) VMax = aPntV;
563 aResultFace.reset(new GeomAPI_Face());
564 aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
569 //==================================================================================================
570 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
571 std::shared_ptr<GeomAPI_Vertex>& theV1,
572 std::shared_ptr<GeomAPI_Vertex>& theV2)
574 if(!theShape.get()) {
575 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
576 aVertex->setImpl(new TopoDS_Vertex());
582 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
583 TopoDS_Vertex aV1, aV2;
584 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
586 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
587 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
588 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
593 //==================================================================================================
594 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
595 const std::shared_ptr<GeomAPI_Dir> theDirection,
596 const ListOfShape& theWires,
597 ListOfShape& theFaces)
599 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
600 theDirection->impl<gp_Dir>()));
601 TopoDS_Face aFace = aMKFace.Face();
603 BRepAlgo_FaceRestrictor aFRestrictor;
604 aFRestrictor.Init(aFace, Standard_False, Standard_True);
605 for(ListOfShape::const_iterator anIt = theWires.cbegin();
606 anIt != theWires.cend();
608 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
609 aFRestrictor.Add(aWire);
612 aFRestrictor.Perform();
614 if(!aFRestrictor.IsDone()) {
618 for(; aFRestrictor.More(); aFRestrictor.Next()) {
619 GeomShapePtr aShape(new GeomAPI_Shape());
620 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
621 theFaces.push_back(aShape);
625 //==================================================================================================
626 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
628 TopoDS_Compound aCompound;
629 BRep_Builder aBuilder;
630 aBuilder.MakeCompound(aCompound);
632 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
633 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
635 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
637 if(aFindPlane.Found() != Standard_True) {
638 return std::shared_ptr<GeomAPI_Pln>();
641 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
642 gp_Pnt aLoc = aPlane->Location();
643 gp_Dir aDir = aPlane->Axis().Direction();
645 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
646 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
648 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
653 //==================================================================================================
654 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(
655 const std::shared_ptr<GeomAPI_Shape> theSubShape,
656 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
658 if(!theSubShape.get() || !theBaseShape.get()) {
662 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
663 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
665 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
666 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
667 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
669 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
672 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
673 if(aBaseShape.ShapeType() == TopAbs_FACE) {
674 // Check that edge is on face surface.
675 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
676 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
677 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
679 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
683 // Check intersections.
684 TopoDS_Vertex aV1, aV2;
685 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
686 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
687 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
688 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
689 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
690 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
692 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
693 // Edge intersect face bound. Check that it is not on edge begin or end.
694 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
695 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
696 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
697 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
704 // No intersections found. Edge is inside or outside face. Check it.
705 BRepAdaptor_Curve aCurveAdaptor(anEdge);
706 gp_Pnt aPointToCheck =
707 aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() +
708 aCurveAdaptor.LastParameter()) / 2.0);
709 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
710 ShapeAnalysis_Surface aSAS(aSurface);
711 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
712 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
713 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
727 //==================================================================================================
728 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
730 if(!theShape.get()) {
734 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
735 return (aChecker.IsValid() == Standard_True);
738 //==================================================================================================
739 std::shared_ptr<GeomAPI_Shape>
740 GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
742 GeomShapePtr anOuterWire;
744 if(!theFace.get() || !theFace->isFace()) {
748 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
749 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
751 anOuterWire.reset(new GeomAPI_Shape());
752 anOuterWire->setImpl(new TopoDS_Shape(aWire));
757 //==================================================================================================
758 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
759 const std::shared_ptr<GeomAPI_Face> theFace)
761 if(!theEdge.get() || !theFace.get()) {
765 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
766 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
768 BRepExtrema_ExtCF anExt(anEdge, aFace);
769 return anExt.IsParallel() == Standard_True;
772 //==================================================================================================
773 std::list<std::shared_ptr<GeomAPI_Vertex> > GeomAlgoAPI_ShapeTools::intersect(
774 const std::shared_ptr<GeomAPI_Edge> theEdge, const std::shared_ptr<GeomAPI_Face> theFace,
775 const bool thePointsOutsideFace)
777 std::list<std::shared_ptr<GeomAPI_Vertex> > aResult;
778 if(!theEdge.get() || !theFace.get()) {
782 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
783 double aFirstOnCurve, aLastOnCurve;
784 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
786 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
787 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
789 GeomAPI_IntCS anIntAlgo(aCurve, aSurf);
790 if (!anIntAlgo.IsDone())
792 // searching for points-intersection
793 for(int anIntNum = 1; anIntNum <= anIntAlgo.NbPoints() + anIntAlgo.NbSegments(); anIntNum++) {
795 if (anIntNum <= anIntAlgo.NbPoints()) {
796 anInt = anIntAlgo.Point(anIntNum);
797 } else { // take the middle point on the segment of the intersection
798 Handle(Geom_Curve) anIntCurve = anIntAlgo.Segment(anIntNum - anIntAlgo.NbPoints());
799 anIntCurve->D0((anIntCurve->FirstParameter() + anIntCurve->LastParameter()) / 2., anInt);
801 aResult.push_back(std::shared_ptr<GeomAPI_Vertex>(
802 new GeomAPI_Vertex(anInt.X(), anInt.Y(), anInt.Z())));
807 //==================================================================================================
808 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
809 const GeomAlgoAPI_ShapeTools::PointToRefsMap& thePointsInfo,
810 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
812 // to split shape at least one point should be presented in the points container
813 if (thePointsInfo.empty())
816 // General Fuse to split edge by vertices
817 BOPAlgo_Builder aBOP;
818 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
819 // Rebuild closed edge to place vertex to one of split points.
820 // This will prevent edge to be split on same vertex.
821 if (BRep_Tool::IsClosed(aBaseEdge))
823 Standard_Real aFirst, aLast;
824 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
826 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
827 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
828 gp_Pnt aPoint(aPnt->x(), aPnt->y(), aPnt->z());
830 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
831 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
832 aBaseEdge.Orientation(anOrientation);
834 aBOP.AddArgument(aBaseEdge);
836 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
837 for (; aPIt != thePointsInfo.end(); ++aPIt) {
838 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
839 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
840 aBOP.AddArgument(aV);
845 if (aBOP.HasErrors())
848 if (aBOP.ErrorStatus())
853 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
854 TopTools_ListIteratorOfListOfShape anIt(aSplits);
855 for (; anIt.More(); anIt.Next()) {
856 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
857 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
858 theShapes.insert(anEdge);
862 //==================================================================================================
863 void GeomAlgoAPI_ShapeTools::splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
864 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
865 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
867 // General Fuse to split edge by vertices
868 BOPAlgo_Builder aBOP;
869 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
870 // Rebuild closed edge to place vertex to one of split points.
871 // This will prevent edge to be split on seam vertex.
872 if (BRep_Tool::IsClosed(aBaseEdge))
874 Standard_Real aFirst, aLast;
875 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
877 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
878 gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
880 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
881 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
882 aBaseEdge.Orientation(anOrientation);
884 aBOP.AddArgument(aBaseEdge);
886 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
887 for (; aPtIt != thePoints.end(); ++aPtIt) {
888 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
889 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
890 aBOP.AddArgument(aV);
895 if (aBOP.HasErrors())
898 if (aBOP.ErrorStatus())
903 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
904 TopTools_ListIteratorOfListOfShape anIt(aSplits);
905 for (; anIt.More(); anIt.Next()) {
906 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
907 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
908 theShapes.insert(anEdge);
912 //==================================================================================================
913 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
914 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
915 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
917 std::shared_ptr<GeomAPI_Shape> aResultShape;
919 if (thePoints.size() == 2) {
920 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
921 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
923 std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
925 std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
926 aLast = theShapes.end();
927 for (; anIt != aLast; anIt++) {
928 GeomShapePtr aShape = *anIt;
929 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
931 std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
932 std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
933 if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
934 anEdgeLastPoint->isEqual(aLastPoint))
935 aResultShape = aShape;
943 //==================================================================================================
944 std::shared_ptr<GeomAPI_Dir> GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(
945 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
946 const std::shared_ptr<GeomAPI_Ax1> theAxis)
948 gp_Pnt aCentreOfMassPoint =
949 GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape)->impl<gp_Pnt>();
950 Handle(Geom_Line) aLine = new Geom_Line(theAxis->impl<gp_Ax1>());
951 GeomAPI_ProjectPointOnCurve aPrjTool(aCentreOfMassPoint, aLine);
952 gp_Pnt aPoint = aPrjTool.NearestPoint();
954 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(aCentreOfMassPoint.X()-aPoint.X(),
955 aCentreOfMassPoint.Y()-aPoint.Y(),
956 aCentreOfMassPoint.Z()-aPoint.Z()));
960 //==================================================================================================
961 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_ShapeTools::wireToEdge(
962 const std::shared_ptr<GeomAPI_Wire>& theWire)
966 const TopoDS_Wire& aWire = theWire->impl<TopoDS_Wire>();
967 TopoDS_Edge aNewEdge = BRepAlgo::ConcatenateWireC0(aWire);
968 anEdge = GeomEdgePtr(new GeomAPI_Edge);
969 anEdge->setImpl(new TopoDS_Edge(aNewEdge));