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 <BRep_Builder.hxx>
35 #include <BRepAdaptor_Curve.hxx>
36 #include <BRepAlgo.hxx>
37 #include <BRepAlgo_FaceRestrictor.hxx>
38 #include <BRepBndLib.hxx>
39 #include <BRepBuilderAPI_FindPlane.hxx>
40 #include <BRepBuilderAPI_MakeEdge.hxx>
41 #include <BRepBuilderAPI_MakeFace.hxx>
42 #include <BRepCheck_Analyzer.hxx>
43 #include <BRepExtrema_DistShapeShape.hxx>
44 #include <BRepExtrema_ExtCF.hxx>
45 #include <BRepGProp.hxx>
46 #include <BRepTools.hxx>
47 #include <BRepTools_WireExplorer.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_CylindricalSurface.hxx>
55 #include <Geom_Line.hxx>
56 #include <Geom_Plane.hxx>
57 #include <GeomAPI_ProjectPointOnCurve.hxx>
58 #include <GeomLib_IsPlanarSurface.hxx>
59 #include <GeomLib_Tool.hxx>
60 #include <GeomAPI_IntCS.hxx>
62 #include <GProp_GProps.hxx>
63 #include <IntAna_IntConicQuad.hxx>
64 #include <IntAna_Quadric.hxx>
65 #include <NCollection_Vector.hxx>
66 #include <ShapeAnalysis.hxx>
67 #include <ShapeAnalysis_Surface.hxx>
68 #include <TopoDS_Builder.hxx>
69 #include <TopoDS_Edge.hxx>
70 #include <TopoDS_Face.hxx>
71 #include <TopoDS_Shape.hxx>
72 #include <TopoDS_Shell.hxx>
73 #include <TopoDS_Vertex.hxx>
76 #include <TopExp_Explorer.hxx>
77 #include <TopTools_ListIteratorOfListOfShape.hxx>
80 #include <BOPAlgo_Builder.hxx>
81 #include <BRepBuilderAPI_MakeVertex.hxx>
82 #include <TopoDS_Edge.hxx>
84 //==================================================================================================
85 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
91 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
95 const Standard_Real anEps = 1.e-6;
96 if (aShape.ShapeType() <= TopAbs_SOLID)
97 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
99 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
100 return aGProps.Mass();
103 //==================================================================================================
104 double GeomAlgoAPI_ShapeTools::area (const std::shared_ptr<GeomAPI_Shape> theShape)
106 GProp_GProps aGProps;
107 if(!theShape.get()) {
110 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
111 if(aShape.IsNull()) {
114 const Standard_Real anEps = 1.e-6;
116 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
117 return aGProps.Mass();
120 //==================================================================================================
121 std::shared_ptr<GeomAPI_Pnt>
122 GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
124 GProp_GProps aGProps;
126 return std::shared_ptr<GeomAPI_Pnt>();
128 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
129 if(aShape.IsNull()) {
130 return std::shared_ptr<GeomAPI_Pnt>();
133 if(aShape.ShapeType() == TopAbs_VERTEX) {
134 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
135 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
136 BRepGProp::LinearProperties(aShape, aGProps);
137 aCentre = aGProps.CentreOfMass();
139 const Standard_Real anEps = 1.e-6;
140 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
141 aCentre = aGProps.CentreOfMass();
143 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
146 //==================================================================================================
147 double GeomAlgoAPI_ShapeTools::radius(const std::shared_ptr<GeomAPI_Face>& theCylinder)
149 double aRadius = -1.0;
150 if (theCylinder->isCylindrical()) {
151 const TopoDS_Shape& aShape = theCylinder->impl<TopoDS_Shape>();
152 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
153 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
155 aRadius = aCyl->Radius();
160 //==================================================================================================
161 double GeomAlgoAPI_ShapeTools::minimalDistance(const GeomShapePtr& theShape1,
162 const GeomShapePtr& theShape2)
164 const TopoDS_Shape& aShape1 = theShape1->impl<TopoDS_Shape>();
165 const TopoDS_Shape& aShape2 = theShape2->impl<TopoDS_Shape>();
167 BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
169 return aDist.IsDone() ? aDist.Value() : Precision::Infinite();
172 //==================================================================================================
173 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(
174 const std::shared_ptr<GeomAPI_Shape> theCompound,
175 const GeomAPI_Shape::ShapeType theType,
176 ListOfShape& theCombinedShapes,
177 ListOfShape& theFreeShapes)
179 GeomShapePtr aResult = theCompound;
181 if(!theCompound.get()) {
185 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
189 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
190 TopAbs_ShapeEnum aTA = TopAbs_FACE;
191 if(theType == GeomAPI_Shape::COMPSOLID) {
196 theCombinedShapes.clear();
197 theFreeShapes.clear();
200 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
201 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
202 const TopoDS_Shape& aShape = anIter.Value();
203 if(aShape.ShapeType() > aTA) {
204 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
205 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
206 theFreeShapes.push_back(aGeomShape);
210 // Map subshapes and shapes.
211 TopTools_IndexedDataMapOfShapeListOfShape aMapSA;
212 TopExp::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
213 if(aMapSA.IsEmpty()) {
217 // Get all shapes with common subshapes and free shapes.
218 NCollection_Map<TopoDS_Shape> aFreeShapes;
219 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
220 for(TopTools_IndexedDataMapOfShapeListOfShape::Iterator
221 anIter(aMapSA); anIter.More(); anIter.Next()) {
222 const TopoDS_Shape& aShape = anIter.Key();
223 TopTools_ListOfShape& aListOfShape = anIter.ChangeValue();
224 if(aListOfShape.IsEmpty()) {
227 else if(aListOfShape.Size() == 1) {
228 const TopoDS_Shape& aF = aListOfShape.First();
230 aListOfShape.Clear();
232 NCollection_List<TopoDS_Shape> aTempList;
233 NCollection_Map<TopoDS_Shape> aTempMap;
234 for (TopTools_ListOfShape::Iterator aListIt(aListOfShape); aListIt.More(); aListIt.Next()) {
235 aTempList.Append(aListIt.Value());
236 aTempMap.Add(aListIt.Value());
237 aFreeShapes.Remove(aListIt.Value());
239 aListOfShape.Clear();
240 for(NCollection_List<TopoDS_Shape>::Iterator
241 aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
242 const TopoDS_Shape& aTempShape = aTempIter.Value();
243 for(TopTools_IndexedDataMapOfShapeListOfShape::Iterator
244 anIter(aMapSA); anIter.More(); anIter.Next()) {
245 TopTools_ListOfShape& aTempListOfShape = anIter.ChangeValue();
246 if(aTempListOfShape.IsEmpty()) {
248 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
249 aTempListOfShape.Clear();
250 } else if(aTempListOfShape.Size() > 1) {
251 TopTools_ListOfShape::Iterator anIt1(aTempListOfShape);
252 for (; anIt1.More(); anIt1.Next()) {
253 if (anIt1.Value() == aTempShape) {
254 TopTools_ListOfShape::Iterator anIt2(aTempListOfShape);
255 for (; anIt2.More(); anIt2.Next())
257 if (anIt2.Value() != anIt1.Value()) {
258 if (aTempMap.Add(anIt2.Value())) {
259 aTempList.Append(anIt2.Value());
260 aFreeShapes.Remove(anIt2.Value());
264 aTempListOfShape.Clear();
271 aShapesWithCommonSubshapes.Append(aTempMap);
275 // Combine shapes with common subshapes.
276 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator
277 anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
279 TopoDS_CompSolid aCSolid;
280 TopoDS_Builder aBuilder;
282 GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
283 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
284 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
285 const TopoDS_Shape& aShape = anExp.Current();
286 if(aShapesMap.Contains(aShape)) {
288 GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
289 aShapesMap.Remove(aShape);
292 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
293 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) :
294 new TopoDS_Shape(aShell);
295 aGeomShape->setImpl<TopoDS_Shape>(aSh);
296 theCombinedShapes.push_back(aGeomShape);
299 // Adding free shapes.
300 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
301 const TopoDS_Shape& aShape = anExp.Current();
302 if(aFreeShapes.Contains(aShape)) {
303 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
304 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
305 theFreeShapes.push_back(aGeomShape);
309 if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
310 aResult = theCombinedShapes.front();
311 } else if(theCombinedShapes.size() == 0 && theFreeShapes.size() == 1) {
312 aResult = theFreeShapes.front();
314 TopoDS_Compound aResultComp;
315 TopoDS_Builder aBuilder;
316 aBuilder.MakeCompound(aResultComp);
317 for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin();
318 anIter != theCombinedShapes.cend(); anIter++) {
319 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
321 for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin();
322 anIter != theFreeShapes.cend(); anIter++) {
323 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
325 aResult->setImpl(new TopoDS_Shape(aResultComp));
331 //==================================================================================================
332 static void addSimpleShapeToList(const TopoDS_Shape& theShape,
333 NCollection_List<TopoDS_Shape>& theList)
335 if(theShape.IsNull()) {
339 if(theShape.ShapeType() == TopAbs_COMPOUND) {
340 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
341 addSimpleShapeToList(anIt.Value(), theList);
344 theList.Append(theShape);
348 //==================================================================================================
349 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
351 TopoDS_Compound aCompound;
353 BRep_Builder aBuilder;
354 aBuilder.MakeCompound(aCompound);
356 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
357 aBuilder.Add(aCompound, anIt.Value());
363 //==================================================================================================
364 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(
365 const std::shared_ptr<GeomAPI_Shape> theCompound)
367 GeomShapePtr aResult = theCompound;
369 if (!theCompound.get()) {
373 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
374 NCollection_List<TopoDS_Shape> anUngroupedShapes, aStillUngroupedShapes;
375 addSimpleShapeToList(anInShape, anUngroupedShapes);
377 // Iterate over all shapes and find shapes with shared vertices.
378 TopTools_ListOfShape allVertices;
379 TopTools_DataMapOfShapeListOfShape aVertexShapesMap;
380 for (NCollection_List<TopoDS_Shape>::Iterator aShapesIt(anUngroupedShapes);
383 const TopoDS_Shape& aShape = aShapesIt.Value();
384 for (TopExp_Explorer aShapeExp(aShape, TopAbs_VERTEX);
387 const TopoDS_Shape& aVertex = aShapeExp.Current();
388 if (!aVertexShapesMap.IsBound(aVertex)) {
389 NCollection_List<TopoDS_Shape> aList;
390 aList.Append(aShape);
391 allVertices.Append(aVertex);
392 aVertexShapesMap.Bind(aVertex, aList);
395 if (!aVertexShapesMap.ChangeFind(aVertex).Contains(aShape)) {
396 aVertexShapesMap.ChangeFind(aVertex).Append(aShape);
402 // Iterate over the map and group shapes.
403 NCollection_Vector<TopTools_ListOfShape> aGroups; // groups of shapes connected by vertices
404 while (!allVertices.IsEmpty()) {
405 // Get first group of shapes in map, and then unbind it.
406 const TopoDS_Shape& aKey = allVertices.First();
407 TopTools_ListOfShape aConnectedShapes = aVertexShapesMap.Find(aKey);
408 aVertexShapesMap.UnBind(aKey);
409 allVertices.Remove(aKey);
410 // Iterate over shapes in this group and add to it shapes from groups in map.
411 for (TopTools_ListOfShape::Iterator aConnectedIt(aConnectedShapes);
412 aConnectedIt.More(); aConnectedIt.Next()) {
413 const TopoDS_Shape& aConnected = aConnectedIt.Value();
414 TopTools_ListOfShape aKeysToUnbind;
415 for (TopTools_ListOfShape::Iterator aKeysIt(allVertices);
418 const TopTools_ListOfShape& anOtherConnected = aVertexShapesMap(aKeysIt.Value());
419 if (!anOtherConnected.Contains(aConnected)) {
420 // Other connected group does not containt shape from our connected group
423 // Other is connected to our, so add them to our connected
424 for (TopTools_ListOfShape::Iterator anOtherIt(anOtherConnected);
427 const TopoDS_Shape& aShape = anOtherIt.Value();
428 if (!aConnectedShapes.Contains(aShape)) {
429 aConnectedShapes.Append(aShape);
432 // Save key to unbind from this map.
433 aKeysToUnbind.Append(aKeysIt.Value());
435 // Unbind groups from map that we added to our group.
436 for (TopTools_ListOfShape::Iterator aKeysIt(aKeysToUnbind);
439 aVertexShapesMap.UnBind(aKeysIt.Value());
440 allVertices.Remove(aKeysIt.Value());
443 // Sort shapes from the most complicated to the simplest ones
444 TopTools_ListOfShape aSortedGroup;
445 for (int aST = TopAbs_COMPOUND; aST <= TopAbs_SHAPE; ++aST) {
446 TopTools_ListOfShape::Iterator anIt(aConnectedShapes);
447 while (anIt.More()) {
448 if (anIt.Value().ShapeType() == aST) {
449 aSortedGroup.Append(anIt.Value());
450 aConnectedShapes.Remove(anIt);
457 aGroups.Append(aSortedGroup);
460 TopoDS_Compound aCompound;
461 BRep_Builder aBuilder;
462 aBuilder.MakeCompound(aCompound);
463 ListOfShape aCompSolids, aFreeSolids;
464 for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator
465 anIt(aGroups); anIt.More(); anIt.Next()) {
466 NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
467 GeomShapePtr aGeomShape(new GeomAPI_Shape());
468 if(aGroup.Size() == 1) {
469 aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
471 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
472 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
473 GeomAPI_Shape::COMPSOLID,
477 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
480 if(!aCompound.IsNull()) {
481 aResult->setImpl(new TopoDS_Shape(aCompound));
487 //==================================================================================================
488 std::list<std::shared_ptr<GeomAPI_Pnt> >
489 GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
491 // Bounding box of all objects.
495 for (ListOfShape::const_iterator
496 anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
497 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
498 BRepBndLib::Add(aShape, aBndBox);
501 if(theEnlarge != 0.0) {
502 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
503 aBndBox.Enlarge(theEnlarge);
506 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
507 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
508 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
509 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
511 for(int i = 0; i < 2; i++) {
512 for(int j = 0; j < 2; j++) {
513 for(int k = 0; k < 2; k++) {
514 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
515 aResultPoints.push_back(aPnt);
520 return aResultPoints;
523 //==================================================================================================
524 std::shared_ptr<GeomAPI_Shape>
525 GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
528 return std::shared_ptr<GeomAPI_Shape>();
530 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
531 if (aPlaneFace.IsNull())
532 return std::shared_ptr<GeomAPI_Shape>();
534 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
536 return std::shared_ptr<GeomAPI_Shape>();
538 // make an infinity face on the plane
539 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
541 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
542 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
546 //==================================================================================================
547 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(
548 const std::shared_ptr<GeomAPI_Shape> thePlane,
549 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
551 std::shared_ptr<GeomAPI_Face> aResultFace;
553 if(!thePlane.get()) {
557 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
558 if(aShape.ShapeType() != TopAbs_FACE) {
562 TopoDS_Face aFace = TopoDS::Face(aShape);
563 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
568 GeomLib_IsPlanarSurface isPlanar(aSurf);
569 if(!isPlanar.IsPlanar()) {
573 if(thePoints.size() != 8) {
577 const gp_Pln& aFacePln = isPlanar.Plan();
578 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
579 IntAna_Quadric aQuadric(aFacePln);
580 Standard_Real UMin, UMax, VMin, VMax;
581 UMin = UMax = VMin = VMax = 0;
582 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
583 aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
584 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
585 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
586 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
587 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
588 Standard_Real aPntU(0), aPntV(0);
589 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
590 if(aPntU < UMin) UMin = aPntU;
591 if(aPntU > UMax) UMax = aPntU;
592 if(aPntV < VMin) VMin = aPntV;
593 if(aPntV > VMax) VMax = aPntV;
595 aResultFace.reset(new GeomAPI_Face());
596 aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
601 //==================================================================================================
602 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
603 std::shared_ptr<GeomAPI_Vertex>& theV1,
604 std::shared_ptr<GeomAPI_Vertex>& theV2)
606 if(!theShape.get()) {
607 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
608 aVertex->setImpl(new TopoDS_Vertex());
614 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
615 TopoDS_Vertex aV1, aV2;
616 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
618 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
619 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
620 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
625 //==================================================================================================
626 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
627 const std::shared_ptr<GeomAPI_Dir> theDirection,
628 const ListOfShape& theWires,
629 ListOfShape& theFaces)
631 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
632 theDirection->impl<gp_Dir>()));
633 TopoDS_Face aFace = aMKFace.Face();
635 BRepAlgo_FaceRestrictor aFRestrictor;
636 aFRestrictor.Init(aFace, Standard_False, Standard_True);
637 for(ListOfShape::const_iterator anIt = theWires.cbegin();
638 anIt != theWires.cend();
640 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
641 aFRestrictor.Add(aWire);
644 aFRestrictor.Perform();
646 if(!aFRestrictor.IsDone()) {
650 for(; aFRestrictor.More(); aFRestrictor.Next()) {
651 GeomShapePtr aShape(new GeomAPI_Shape());
652 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
653 theFaces.push_back(aShape);
657 //==================================================================================================
658 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
660 TopoDS_Compound aCompound;
661 BRep_Builder aBuilder;
662 aBuilder.MakeCompound(aCompound);
664 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
665 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
667 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
669 if(aFindPlane.Found() != Standard_True) {
670 return std::shared_ptr<GeomAPI_Pln>();
673 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
674 gp_Pnt aLoc = aPlane->Location();
675 gp_Dir aDir = aPlane->Axis().Direction();
677 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
678 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
680 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
685 //==================================================================================================
686 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(
687 const std::shared_ptr<GeomAPI_Shape> theSubShape,
688 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
690 if(!theSubShape.get() || !theBaseShape.get()) {
694 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
695 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
697 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
698 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
699 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
701 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
704 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
705 if(aBaseShape.ShapeType() == TopAbs_FACE) {
706 // Check that edge is on face surface.
707 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
708 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
709 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
711 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
715 // Check intersections.
716 TopoDS_Vertex aV1, aV2;
717 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
718 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
719 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
720 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
721 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
722 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
724 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
725 // Edge intersect face bound. Check that it is not on edge begin or end.
726 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
727 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
728 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
729 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
736 // No intersections found. Edge is inside or outside face. Check it.
737 BRepAdaptor_Curve aCurveAdaptor(anEdge);
738 gp_Pnt aPointToCheck =
739 aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() +
740 aCurveAdaptor.LastParameter()) / 2.0);
741 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
742 ShapeAnalysis_Surface aSAS(aSurface);
743 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
744 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
745 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
759 //==================================================================================================
760 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
762 if(!theShape.get()) {
766 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
767 return (aChecker.IsValid() == Standard_True);
770 //==================================================================================================
771 std::shared_ptr<GeomAPI_Shape>
772 GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
774 GeomShapePtr anOuterWire;
776 if(!theFace.get() || !theFace->isFace()) {
780 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
781 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
783 anOuterWire.reset(new GeomAPI_Shape());
784 anOuterWire->setImpl(new TopoDS_Shape(aWire));
789 //==================================================================================================
790 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
791 const std::shared_ptr<GeomAPI_Face> theFace)
793 if(!theEdge.get() || !theFace.get()) {
797 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
798 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
800 BRepExtrema_ExtCF anExt(anEdge, aFace);
801 return anExt.IsParallel() == Standard_True;
804 //==================================================================================================
805 std::list<std::shared_ptr<GeomAPI_Vertex> > GeomAlgoAPI_ShapeTools::intersect(
806 const std::shared_ptr<GeomAPI_Edge> theEdge, const std::shared_ptr<GeomAPI_Face> theFace,
807 const bool thePointsOutsideFace)
809 std::list<std::shared_ptr<GeomAPI_Vertex> > aResult;
810 if(!theEdge.get() || !theFace.get()) {
814 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
815 double aFirstOnCurve, aLastOnCurve;
816 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
818 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
819 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
821 GeomAPI_IntCS anIntAlgo(aCurve, aSurf);
822 if (!anIntAlgo.IsDone())
824 // searching for points-intersection
825 for(int anIntNum = 1; anIntNum <= anIntAlgo.NbPoints() + anIntAlgo.NbSegments(); anIntNum++) {
827 if (anIntNum <= anIntAlgo.NbPoints()) {
828 anInt = anIntAlgo.Point(anIntNum);
829 } else { // take the middle point on the segment of the intersection
830 Handle(Geom_Curve) anIntCurve = anIntAlgo.Segment(anIntNum - anIntAlgo.NbPoints());
831 anIntCurve->D0((anIntCurve->FirstParameter() + anIntCurve->LastParameter()) / 2., anInt);
833 aResult.push_back(std::shared_ptr<GeomAPI_Vertex>(
834 new GeomAPI_Vertex(anInt.X(), anInt.Y(), anInt.Z())));
839 //==================================================================================================
840 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
841 const GeomAlgoAPI_ShapeTools::PointToRefsMap& thePointsInfo,
842 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
844 // to split shape at least one point should be presented in the points container
845 if (thePointsInfo.empty())
848 // General Fuse to split edge by vertices
849 BOPAlgo_Builder aBOP;
850 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
851 // Rebuild closed edge to place vertex to one of split points.
852 // This will prevent edge to be split on same vertex.
853 if (BRep_Tool::IsClosed(aBaseEdge))
855 Standard_Real aFirst, aLast;
856 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
858 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
859 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
860 gp_Pnt aPoint(aPnt->x(), aPnt->y(), aPnt->z());
862 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
863 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
864 aBaseEdge.Orientation(anOrientation);
866 aBOP.AddArgument(aBaseEdge);
868 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
869 for (; aPIt != thePointsInfo.end(); ++aPIt) {
870 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
871 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
872 aBOP.AddArgument(aV);
876 if (aBOP.HasErrors())
880 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
881 TopTools_ListIteratorOfListOfShape anIt(aSplits);
882 for (; anIt.More(); anIt.Next()) {
883 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
884 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
885 theShapes.insert(anEdge);
889 //==================================================================================================
890 void GeomAlgoAPI_ShapeTools::splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
891 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
892 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
894 // General Fuse to split edge by vertices
895 BOPAlgo_Builder aBOP;
896 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
897 // Rebuild closed edge to place vertex to one of split points.
898 // This will prevent edge to be split on seam vertex.
899 if (BRep_Tool::IsClosed(aBaseEdge))
901 Standard_Real aFirst, aLast;
902 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
904 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
905 gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
907 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
908 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
909 aBaseEdge.Orientation(anOrientation);
911 aBOP.AddArgument(aBaseEdge);
913 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
914 for (; aPtIt != thePoints.end(); ++aPtIt) {
915 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
916 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
917 aBOP.AddArgument(aV);
921 if (aBOP.HasErrors())
925 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
926 TopTools_ListIteratorOfListOfShape anIt(aSplits);
927 for (; anIt.More(); anIt.Next()) {
928 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
929 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
930 theShapes.insert(anEdge);
934 //==================================================================================================
935 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
936 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
937 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
939 std::shared_ptr<GeomAPI_Shape> aResultShape;
941 if (thePoints.size() == 2) {
942 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
943 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
945 std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
947 std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
948 aLast = theShapes.end();
949 for (; anIt != aLast; anIt++) {
950 GeomShapePtr aShape = *anIt;
951 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
953 std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
954 std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
955 if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
956 anEdgeLastPoint->isEqual(aLastPoint))
957 aResultShape = aShape;
965 //==================================================================================================
966 std::shared_ptr<GeomAPI_Dir> GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(
967 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
968 const std::shared_ptr<GeomAPI_Ax1> theAxis)
970 gp_Pnt aCentreOfMassPoint =
971 GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape)->impl<gp_Pnt>();
972 Handle(Geom_Line) aLine = new Geom_Line(theAxis->impl<gp_Ax1>());
973 GeomAPI_ProjectPointOnCurve aPrjTool(aCentreOfMassPoint, aLine);
974 gp_Pnt aPoint = aPrjTool.NearestPoint();
976 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(aCentreOfMassPoint.X()-aPoint.X(),
977 aCentreOfMassPoint.Y()-aPoint.Y(),
978 aCentreOfMassPoint.Z()-aPoint.Z()));
982 //==================================================================================================
983 static TopoDS_Wire fixParametricGaps(const TopoDS_Wire& theWire)
985 TopoDS_Wire aFixedWire;
986 Handle(Geom_Curve) aPrevCurve;
987 double aPrevLastParam = 0.0;
989 BRep_Builder aBuilder;
990 aBuilder.MakeWire(aFixedWire);
992 BRepTools_WireExplorer aWExp(theWire);
993 for (; aWExp.More(); aWExp.Next()) {
994 TopoDS_Edge anEdge = aWExp.Current();
995 double aFirst, aLast;
996 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
997 if (aCurve == aPrevCurve) {
998 // if parametric gap occurs, create new edge based on the copied curve
999 aCurve = Handle(Geom_Curve)::DownCast(aCurve->Copy());
1000 TopoDS_Vertex aV1, aV2;
1001 TopExp::Vertices(anEdge, aV1, aV2);
1002 anEdge = TopoDS::Edge(anEdge.EmptyCopied());
1003 aBuilder.UpdateEdge(anEdge, aCurve, BRep_Tool::Tolerance(anEdge));
1004 aBuilder.Add(anEdge, aV1);
1005 aBuilder.Add(anEdge, aV2);
1008 aBuilder.Add(aFixedWire, anEdge);
1010 aPrevCurve = aCurve;
1011 aPrevLastParam = aLast;
1017 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_ShapeTools::wireToEdge(
1018 const std::shared_ptr<GeomAPI_Wire>& theWire)
1022 TopoDS_Wire aWire = theWire->impl<TopoDS_Wire>();
1023 // Workaround: when concatenate a wire consisting of two edges based on the same B-spline curve
1024 // (non-periodic, but having equal start and end points), first of which is placed at the end
1025 // on the curve and second is placed at the start, this workaround copies second curve to avoid
1026 // treating these edges as a single curve by setting trim parameters.
1027 aWire = fixParametricGaps(aWire);
1028 TopoDS_Edge aNewEdge = BRepAlgo::ConcatenateWireC0(aWire);
1029 anEdge = GeomEdgePtr(new GeomAPI_Edge);
1030 anEdge->setImpl(new TopoDS_Edge(aNewEdge));