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_Edge.h>
26 #include <GeomAPI_Dir.h>
27 #include <GeomAPI_Face.h>
28 #include <GeomAPI_Pln.h>
29 #include <GeomAPI_Pnt.h>
31 #include <Bnd_Box.hxx>
32 #include <BOPTools.hxx>
33 #include <BRep_Builder.hxx>
34 #include <BRepAdaptor_Curve.hxx>
35 #include <BRepAlgo_FaceRestrictor.hxx>
36 #include <BRepBndLib.hxx>
37 #include <BRepBuilderAPI_FindPlane.hxx>
38 #include <BRepBuilderAPI_MakeEdge.hxx>
39 #include <BRepBuilderAPI_MakeFace.hxx>
40 #include <BRepCheck_Analyzer.hxx>
41 #include <BRepExtrema_DistShapeShape.hxx>
42 #include <BRepExtrema_ExtCF.hxx>
43 #include <BRepGProp.hxx>
44 #include <BRepTools.hxx>
45 #include <BRepTopAdaptor_FClass2d.hxx>
46 #include <BRepClass_FaceClassifier.hxx>
47 #include <Geom2d_Curve.hxx>
48 #include <Geom2d_Curve.hxx>
49 #include <BRepLib_CheckCurveOnSurface.hxx>
50 #include <BRep_Tool.hxx>
51 #include <Geom_Plane.hxx>
52 #include <GeomLib_IsPlanarSurface.hxx>
53 #include <GeomLib_Tool.hxx>
54 #include <GeomAPI_ExtremaCurveSurface.hxx>
56 #include <GProp_GProps.hxx>
57 #include <IntAna_IntConicQuad.hxx>
58 #include <IntAna_Quadric.hxx>
59 #include <NCollection_Vector.hxx>
60 #include <ShapeAnalysis.hxx>
61 #include <ShapeAnalysis_Surface.hxx>
62 #include <TopoDS_Builder.hxx>
63 #include <TopoDS_Edge.hxx>
64 #include <TopoDS_Face.hxx>
65 #include <TopoDS_Shape.hxx>
66 #include <TopoDS_Shell.hxx>
67 #include <TopoDS_Vertex.hxx>
69 #include <TopExp_Explorer.hxx>
70 #include <TopTools_ListIteratorOfListOfShape.hxx>
73 #include <BOPAlgo_Builder.hxx>
74 #include <BRepBuilderAPI_MakeVertex.hxx>
75 #include <TopoDS_Edge.hxx>
77 //==================================================================================================
78 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
84 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
88 const Standard_Real anEps = 1.e-6;
89 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
90 return aGProps.Mass();
93 //==================================================================================================
94 std::shared_ptr<GeomAPI_Pnt>
95 GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
99 return std::shared_ptr<GeomAPI_Pnt>();
101 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
102 if(aShape.IsNull()) {
103 return std::shared_ptr<GeomAPI_Pnt>();
106 if(aShape.ShapeType() == TopAbs_VERTEX) {
107 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
108 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
109 BRepGProp::LinearProperties(aShape, aGProps);
110 aCentre = aGProps.CentreOfMass();
112 BRepGProp::SurfaceProperties(aShape, aGProps);
113 aCentre = aGProps.CentreOfMass();
115 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
118 //==================================================================================================
119 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(
120 const std::shared_ptr<GeomAPI_Shape> theCompound,
121 const GeomAPI_Shape::ShapeType theType,
122 ListOfShape& theCombinedShapes,
123 ListOfShape& theFreeShapes)
125 GeomShapePtr aResult = theCompound;
127 if(!theCompound.get()) {
131 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
135 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
136 TopAbs_ShapeEnum aTA = TopAbs_FACE;
137 if(theType == GeomAPI_Shape::COMPSOLID) {
142 theCombinedShapes.clear();
143 theFreeShapes.clear();
146 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
147 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
148 const TopoDS_Shape& aShape = anIter.Value();
149 if(aShape.ShapeType() > aTA) {
150 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
151 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
152 theFreeShapes.push_back(aGeomShape);
156 // Map subshapes and shapes.
157 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
158 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
159 if(aMapSA.IsEmpty()) {
163 // Get all shapes with common subshapes and free shapes.
164 NCollection_Map<TopoDS_Shape> aFreeShapes;
165 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
166 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator
167 anIter(aMapSA); anIter.More(); anIter.Next()) {
168 const TopoDS_Shape& aShape = anIter.Key();
169 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
170 if(aListOfShape.IsEmpty()) {
173 else if(aListOfShape.Size() == 1) {
174 const TopoDS_Shape& aF = aListOfShape.First();
176 aListOfShape.Clear();
178 NCollection_List<TopoDS_Shape> aTempList;
179 NCollection_Map<TopoDS_Shape> aTempMap;
180 const TopoDS_Shape& aF = aListOfShape.First();
181 const TopoDS_Shape& aL = aListOfShape.Last();
182 aTempList.Append(aF);
183 aTempList.Append(aL);
186 aFreeShapes.Remove(aF);
187 aFreeShapes.Remove(aL);
188 aListOfShape.Clear();
189 for(NCollection_List<TopoDS_Shape>::Iterator
190 aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
191 const TopoDS_Shape& aTempShape = aTempIter.Value();
192 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator
193 anIter(aMapSA); anIter.More(); anIter.Next()) {
194 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
195 if(aTempListOfShape.IsEmpty()) {
197 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
198 aTempListOfShape.Clear();
199 } else if(aTempListOfShape.Size() > 1) {
200 if(aTempListOfShape.First() == aTempShape) {
201 const TopoDS_Shape& aTL = aTempListOfShape.Last();
202 if(aTempMap.Add(aTL)) {
203 aTempList.Append(aTL);
204 aFreeShapes.Remove(aTL);
206 aTempListOfShape.Clear();
207 } else if(aTempListOfShape.Last() == aTempShape) {
208 const TopoDS_Shape& aTF = aTempListOfShape.First();
209 if(aTempMap.Add(aTF)) {
210 aTempList.Append(aTF);
211 aFreeShapes.Remove(aTF);
213 aTempListOfShape.Clear();
218 aShapesWithCommonSubshapes.Append(aTempMap);
222 // Combine shapes with common subshapes.
223 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator
224 anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
226 TopoDS_CompSolid aCSolid;
227 TopoDS_Builder aBuilder;
229 GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
230 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
231 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
232 const TopoDS_Shape& aShape = anExp.Current();
233 if(aShapesMap.Contains(aShape)) {
235 GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
236 aShapesMap.Remove(aShape);
239 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
240 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) :
241 new TopoDS_Shape(aShell);
242 aGeomShape->setImpl<TopoDS_Shape>(aSh);
243 theCombinedShapes.push_back(aGeomShape);
246 // Adding free shapes.
247 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
248 const TopoDS_Shape& aShape = anExp.Current();
249 if(aFreeShapes.Contains(aShape)) {
250 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
251 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
252 theFreeShapes.push_back(aGeomShape);
256 if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
257 aResult = theCombinedShapes.front();
258 } else if(theCombinedShapes.size() == 0 && theFreeShapes.size() == 1) {
259 aResult = theFreeShapes.front();
261 TopoDS_Compound aResultComp;
262 TopoDS_Builder aBuilder;
263 aBuilder.MakeCompound(aResultComp);
264 for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin();
265 anIter != theCombinedShapes.cend(); anIter++) {
266 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
268 for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin();
269 anIter != theFreeShapes.cend(); anIter++) {
270 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
272 aResult->setImpl(new TopoDS_Shape(aResultComp));
278 //==================================================================================================
279 static void addSimpleShapeToList(const TopoDS_Shape& theShape,
280 NCollection_List<TopoDS_Shape>& theList)
282 if(theShape.IsNull()) {
286 if(theShape.ShapeType() == TopAbs_COMPOUND) {
287 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
288 addSimpleShapeToList(anIt.Value(), theList);
291 theList.Append(theShape);
295 //==================================================================================================
296 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
298 TopoDS_Compound aCompound;
300 BRep_Builder aBuilder;
301 aBuilder.MakeCompound(aCompound);
303 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
304 aBuilder.Add(aCompound, anIt.Value());
310 //==================================================================================================
311 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(
312 const std::shared_ptr<GeomAPI_Shape> theCompound)
314 GeomShapePtr aResult = theCompound;
316 if(!theCompound.get()) {
320 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
321 NCollection_List<TopoDS_Shape> anUngroupedShapes, aStillUngroupedShapes;
322 addSimpleShapeToList(anInShape, anUngroupedShapes);
324 // Iterate over all shapes and find shapes with shared vertices.
325 TopTools_ListOfShape aMapOrder;
326 BOPCol_DataMapOfShapeListOfShape aVertexShapesMap;
327 for(NCollection_List<TopoDS_Shape>::Iterator aShapesIt(anUngroupedShapes);
330 const TopoDS_Shape& aShape = aShapesIt.Value();
331 for(TopExp_Explorer aShapeExp(aShape, TopAbs_VERTEX);
334 const TopoDS_Shape& aVertex = aShapeExp.Current();
335 if (!aVertexShapesMap.IsBound(aVertex)) {
336 NCollection_List<TopoDS_Shape> aList;
337 aList.Append(aShape);
338 aMapOrder.Append(aVertex);
339 aVertexShapesMap.Bind(aVertex, aList);
341 if(!aVertexShapesMap.ChangeFind(aVertex).Contains(aShape)) {
342 aVertexShapesMap.ChangeFind(aVertex).Append(aShape);
348 // Iterate over the map and group shapes.
349 NCollection_Vector<TopTools_ListOfShape> aGroups;
350 while (!aMapOrder.IsEmpty()) {
351 // Get first group of shapes in map, and then unbind it.
352 const TopoDS_Shape& aKey = aMapOrder.First();
353 TopTools_ListOfShape aGroupedShapes = aVertexShapesMap.Find(aKey);
354 aVertexShapesMap.UnBind(aKey);
355 aMapOrder.Remove(aKey);
356 // Iterate over shapes in this group and add to it shapes from groups in map.
357 for(TopTools_ListOfShape::Iterator aGroupIt(aGroupedShapes);
358 aGroupIt.More(); aGroupIt.Next()) {
359 const TopoDS_Shape& aGroupedShape = aGroupIt.Value();
360 TopTools_ListOfShape aKeysToUnbind;
361 for(TopTools_ListOfShape::Iterator aKeysIt(aMapOrder);
364 const TopTools_ListOfShape& aGroupInMap = aVertexShapesMap(aKeysIt.Value());
365 if(!aGroupInMap.Contains(aGroupedShape)) {
366 // Group in map does not containt shape from our group, so go to the next group in map.
369 // Iterate over shape in group in map, and add new shapes into our group.
370 for(TopTools_ListOfShape::Iterator aGroupInMapIt(aGroupInMap);
371 aGroupInMapIt.More();
372 aGroupInMapIt.Next()) {
373 const TopoDS_Shape& aShape = aGroupInMapIt.Value();
374 if (!aGroupedShapes.Contains(aShape)) {
375 aGroupedShapes.Append(aShape);
378 // Save key to unbind from this map.
379 aKeysToUnbind.Append(aKeysIt.Value());
381 // Unbind groups from map that we added to our group.
382 for(TopTools_ListOfShape::Iterator aKeysIt(aKeysToUnbind);
385 aVertexShapesMap.UnBind(aKeysIt.Value());
386 aMapOrder.Remove(aKeysIt.Value());
390 TopTools_ListOfShape aSortedGroup;
391 for(int aST = TopAbs_COMPOUND; aST <= TopAbs_SHAPE; ++aST) {
392 TopTools_ListOfShape::Iterator anIt(aGroupedShapes);
393 while (anIt.More()) {
394 if(anIt.Value().ShapeType() == aST) {
395 aSortedGroup.Append(anIt.Value());
396 aGroupedShapes.Remove(anIt);
402 aGroups.Append(aSortedGroup);
405 TopoDS_Compound aCompound;
406 BRep_Builder aBuilder;
407 aBuilder.MakeCompound(aCompound);
408 ListOfShape aCompSolids, aFreeSolids;
409 for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator
410 anIt(aGroups); anIt.More(); anIt.Next()) {
411 NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
412 GeomShapePtr aGeomShape(new GeomAPI_Shape());
413 if(aGroup.Size() == 1) {
414 aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
416 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
417 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
418 GeomAPI_Shape::COMPSOLID,
422 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
425 if(!aCompound.IsNull()) {
426 aResult->setImpl(new TopoDS_Shape(aCompound));
432 //==================================================================================================
433 std::list<std::shared_ptr<GeomAPI_Pnt> >
434 GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
436 // Bounding box of all objects.
440 for (ListOfShape::const_iterator
441 anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
442 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
443 BRepBndLib::Add(aShape, aBndBox);
446 if(theEnlarge != 0.0) {
447 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
448 aBndBox.Enlarge(theEnlarge);
451 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
452 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
453 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
454 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
456 for(int i = 0; i < 2; i++) {
457 for(int j = 0; j < 2; j++) {
458 for(int k = 0; k < 2; k++) {
459 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
460 aResultPoints.push_back(aPnt);
465 return aResultPoints;
468 //==================================================================================================
469 std::shared_ptr<GeomAPI_Shape>
470 GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
473 return std::shared_ptr<GeomAPI_Shape>();
475 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
476 if (aPlaneFace.IsNull())
477 return std::shared_ptr<GeomAPI_Shape>();
479 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
481 return std::shared_ptr<GeomAPI_Shape>();
483 // make an infinity face on the plane
484 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
486 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
487 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
491 //==================================================================================================
492 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(
493 const std::shared_ptr<GeomAPI_Shape> thePlane,
494 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
496 std::shared_ptr<GeomAPI_Face> aResultFace;
498 if(!thePlane.get()) {
502 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
503 if(aShape.ShapeType() != TopAbs_FACE) {
507 TopoDS_Face aFace = TopoDS::Face(aShape);
508 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
513 GeomLib_IsPlanarSurface isPlanar(aSurf);
514 if(!isPlanar.IsPlanar()) {
518 if(thePoints.size() != 8) {
522 const gp_Pln& aFacePln = isPlanar.Plan();
523 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
524 IntAna_Quadric aQuadric(aFacePln);
525 Standard_Real UMin, UMax, VMin, VMax;
526 UMin = UMax = VMin = VMax = 0;
527 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
528 aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
529 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
530 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
531 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
532 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
533 Standard_Real aPntU(0), aPntV(0);
534 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
535 if(aPntU < UMin) UMin = aPntU;
536 if(aPntU > UMax) UMax = aPntU;
537 if(aPntV < VMin) VMin = aPntV;
538 if(aPntV > VMax) VMax = aPntV;
540 aResultFace.reset(new GeomAPI_Face());
541 aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
546 //==================================================================================================
547 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
548 std::shared_ptr<GeomAPI_Vertex>& theV1,
549 std::shared_ptr<GeomAPI_Vertex>& theV2)
551 if(!theShape.get()) {
552 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
553 aVertex->setImpl(new TopoDS_Vertex());
559 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
560 TopoDS_Vertex aV1, aV2;
561 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
563 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
564 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
565 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
570 //==================================================================================================
571 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
572 const std::shared_ptr<GeomAPI_Dir> theDirection,
573 const ListOfShape& theWires,
574 ListOfShape& theFaces)
576 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
577 theDirection->impl<gp_Dir>()));
578 TopoDS_Face aFace = aMKFace.Face();
580 BRepAlgo_FaceRestrictor aFRestrictor;
581 aFRestrictor.Init(aFace, Standard_False, Standard_True);
582 for(ListOfShape::const_iterator anIt = theWires.cbegin();
583 anIt != theWires.cend();
585 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
586 aFRestrictor.Add(aWire);
589 aFRestrictor.Perform();
591 if(!aFRestrictor.IsDone()) {
595 for(; aFRestrictor.More(); aFRestrictor.Next()) {
596 GeomShapePtr aShape(new GeomAPI_Shape());
597 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
598 theFaces.push_back(aShape);
602 //==================================================================================================
603 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
605 TopoDS_Compound aCompound;
606 BRep_Builder aBuilder;
607 aBuilder.MakeCompound(aCompound);
609 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
610 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
612 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
614 if(aFindPlane.Found() != Standard_True) {
615 return std::shared_ptr<GeomAPI_Pln>();
618 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
619 gp_Pnt aLoc = aPlane->Location();
620 gp_Dir aDir = aPlane->Axis().Direction();
622 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
623 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
625 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
630 //==================================================================================================
631 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(
632 const std::shared_ptr<GeomAPI_Shape> theSubShape,
633 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
635 if(!theSubShape.get() || !theBaseShape.get()) {
639 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
640 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
642 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
643 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
644 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
646 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
649 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
650 if(aBaseShape.ShapeType() == TopAbs_FACE) {
651 // Check that edge is on face surface.
652 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
653 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
654 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
656 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
660 // Check intersections.
661 TopoDS_Vertex aV1, aV2;
662 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
663 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
664 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
665 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
666 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
667 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
669 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
670 // Edge intersect face bound. Check that it is not on edge begin or end.
671 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
672 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
673 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
674 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
681 // No intersections found. Edge is inside or outside face. Check it.
682 BRepAdaptor_Curve aCurveAdaptor(anEdge);
683 gp_Pnt aPointToCheck =
684 aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() +
685 aCurveAdaptor.LastParameter()) / 2.0);
686 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
687 ShapeAnalysis_Surface aSAS(aSurface);
688 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
689 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
690 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
704 //==================================================================================================
705 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
707 if(!theShape.get()) {
711 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
712 return (aChecker.IsValid() == Standard_True);
715 //==================================================================================================
716 std::shared_ptr<GeomAPI_Shape>
717 GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
719 GeomShapePtr anOuterWire;
721 if(!theFace.get() || !theFace->isFace()) {
725 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
726 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
728 anOuterWire.reset(new GeomAPI_Shape());
729 anOuterWire->setImpl(new TopoDS_Shape(aWire));
734 //==================================================================================================
735 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
736 const std::shared_ptr<GeomAPI_Face> theFace)
738 if(!theEdge.get() || !theFace.get()) {
742 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
743 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
745 BRepExtrema_ExtCF anExt(anEdge, aFace);
746 return anExt.IsParallel() == Standard_True;
749 //==================================================================================================
750 std::shared_ptr<GeomAPI_Vertex> GeomAlgoAPI_ShapeTools::intersect(
751 const std::shared_ptr<GeomAPI_Edge> theEdge, const std::shared_ptr<GeomAPI_Face> theFace)
753 if(!theEdge.get() || !theFace.get()) {
754 return std::shared_ptr<GeomAPI_Vertex>();
757 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
758 double aFirstOnCurve, aLastOnCurve;
759 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
761 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
762 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
764 GeomAPI_ExtremaCurveSurface anExt(aCurve, aSurf);
765 // searching for the best point-intersection
768 for(int anIntNum = 1; anIntNum <= anExt.NbExtrema(); anIntNum++) {
769 if (anExt.Distance(anIntNum) > Precision::Confusion())
771 Standard_Real aW, aU, aV;
772 anExt.Parameters(anIntNum, aW, aU, aV);
773 gp_Pnt2d aPointOfSurf(aU, aV);
774 // level of the intersection: if it is inside of edge and/or face the level is higher
775 int aIntLevel = aW > aFirstOnCurve && aW < aLastOnCurve ? 2 : 1;
776 BRepClass_FaceClassifier aFClass(aFace, aPointOfSurf, Precision::Confusion());
777 if (aFClass.State() == TopAbs_IN) // "in" is better than "on"
779 else if (aFClass.State() == TopAbs_ON)
781 if (aMaxLevel < aIntLevel) {
782 aMaxLevel = anIntNum;
783 anExt.Points(anIntNum, aResult, aResult);
786 if (aMaxLevel > 0) { // intersection found
787 return std::shared_ptr<GeomAPI_Vertex>(
788 new GeomAPI_Vertex(aResult.X(), aResult.Y(), aResult.Z()));
790 return std::shared_ptr<GeomAPI_Vertex>(); // no intersection found
793 //==================================================================================================
794 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
795 const GeomAlgoAPI_ShapeTools::PointToRefsMap& thePointsInfo,
796 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
798 // to split shape at least one point should be presented in the points container
799 if (thePointsInfo.empty())
802 // General Fuse to split edge by vertices
803 BOPAlgo_Builder aBOP;
804 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
805 // Rebuild closed edge to place vertex to one of split points.
806 // This will prevent edge to be split on same vertex.
807 if (BRep_Tool::IsClosed(aBaseEdge))
809 Standard_Real aFirst, aLast;
810 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
812 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
813 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
814 gp_Pnt aPoint(aPnt->x(), aPnt->y(), aPnt->z());
816 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
817 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
818 aBaseEdge.Orientation(anOrientation);
820 aBOP.AddArgument(aBaseEdge);
822 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
823 for (; aPIt != thePointsInfo.end(); ++aPIt) {
824 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
825 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
826 aBOP.AddArgument(aV);
830 if (aBOP.ErrorStatus())
834 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
835 TopTools_ListIteratorOfListOfShape anIt(aSplits);
836 for (; anIt.More(); anIt.Next()) {
837 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
838 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
839 theShapes.insert(anEdge);
843 //==================================================================================================
844 void GeomAlgoAPI_ShapeTools::splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
845 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
846 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
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 seam vertex.
853 if (BRep_Tool::IsClosed(aBaseEdge))
855 Standard_Real aFirst, aLast;
856 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
858 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
859 gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
861 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
862 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
863 aBaseEdge.Orientation(anOrientation);
865 aBOP.AddArgument(aBaseEdge);
867 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
868 for (; aPtIt != thePoints.end(); ++aPtIt) {
869 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
870 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
871 aBOP.AddArgument(aV);
875 if (aBOP.ErrorStatus())
879 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
880 TopTools_ListIteratorOfListOfShape anIt(aSplits);
881 for (; anIt.More(); anIt.Next()) {
882 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
883 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
884 theShapes.insert(anEdge);
888 //==================================================================================================
889 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
890 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
891 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
893 std::shared_ptr<GeomAPI_Shape> aResultShape;
895 if (thePoints.size() == 2) {
896 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
897 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
899 std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
901 std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
902 aLast = theShapes.end();
903 for (; anIt != aLast; anIt++) {
904 GeomShapePtr aShape = *anIt;
905 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
907 std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
908 std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
909 if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
910 anEdgeLastPoint->isEqual(aLastPoint))
911 aResultShape = aShape;