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 <GeomAPI_ShapeIterator.h>
59 #include <GeomLib_IsPlanarSurface.hxx>
60 #include <GeomLib_Tool.hxx>
61 #include <GeomAPI_IntCS.hxx>
63 #include <GProp_GProps.hxx>
64 #include <IntAna_IntConicQuad.hxx>
65 #include <IntAna_Quadric.hxx>
66 #include <NCollection_Vector.hxx>
67 #include <ShapeAnalysis.hxx>
68 #include <ShapeAnalysis_Surface.hxx>
69 #include <TopoDS_Builder.hxx>
70 #include <TopoDS_Edge.hxx>
71 #include <TopoDS_Face.hxx>
72 #include <TopoDS_Shape.hxx>
73 #include <TopoDS_Shell.hxx>
74 #include <TopoDS_Vertex.hxx>
77 #include <TopExp_Explorer.hxx>
78 #include <TopTools_ListIteratorOfListOfShape.hxx>
81 #include <BOPAlgo_Builder.hxx>
82 #include <BRepBuilderAPI_MakeVertex.hxx>
83 #include <TopoDS_Edge.hxx>
85 //==================================================================================================
86 static GProp_GProps props(const TopoDS_Shape& theShape)
90 if (theShape.ShapeType() == TopAbs_EDGE || theShape.ShapeType() == TopAbs_WIRE)
92 BRepGProp::LinearProperties(theShape, aGProps);
94 else if (theShape.ShapeType() == TopAbs_FACE || theShape.ShapeType() == TopAbs_SHELL)
96 const Standard_Real anEps = 1.e-6;
97 BRepGProp::SurfaceProperties(theShape, aGProps, anEps);
99 else if (theShape.ShapeType() == TopAbs_SOLID || theShape.ShapeType() == TopAbs_COMPSOLID)
101 BRepGProp::VolumeProperties(theShape, aGProps);
103 else if (theShape.ShapeType() == TopAbs_COMPOUND)
105 for (TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next())
107 aGProps.Add(props(anIt.Value()));
114 //==================================================================================================
115 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
117 GProp_GProps aGProps;
118 if(!theShape.get()) {
121 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
122 if(aShape.IsNull()) {
125 const Standard_Real anEps = 1.e-6;
126 if (aShape.ShapeType() <= TopAbs_SOLID)
127 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
129 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
130 return aGProps.Mass();
133 //==================================================================================================
134 double GeomAlgoAPI_ShapeTools::area (const std::shared_ptr<GeomAPI_Shape> theShape)
136 GProp_GProps aGProps;
137 if(!theShape.get()) {
140 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
141 if(aShape.IsNull()) {
144 const Standard_Real anEps = 1.e-6;
146 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
147 return aGProps.Mass();
150 //==================================================================================================
151 std::shared_ptr<GeomAPI_Pnt>
152 GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
154 GProp_GProps aGProps;
156 return std::shared_ptr<GeomAPI_Pnt>();
158 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
159 if(aShape.IsNull()) {
160 return std::shared_ptr<GeomAPI_Pnt>();
163 if(aShape.ShapeType() == TopAbs_VERTEX) {
164 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
166 aGProps = props(aShape);
167 aCentre = aGProps.CentreOfMass();
170 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
173 //==================================================================================================
174 double GeomAlgoAPI_ShapeTools::radius(const std::shared_ptr<GeomAPI_Face>& theCylinder)
176 double aRadius = -1.0;
177 if (theCylinder->isCylindrical()) {
178 const TopoDS_Shape& aShape = theCylinder->impl<TopoDS_Shape>();
179 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(TopoDS::Face(aShape));
180 Handle(Geom_CylindricalSurface) aCyl = Handle(Geom_CylindricalSurface)::DownCast(aSurf);
182 aRadius = aCyl->Radius();
187 //==================================================================================================
188 double GeomAlgoAPI_ShapeTools::minimalDistance(const GeomShapePtr& theShape1,
189 const GeomShapePtr& theShape2)
191 const TopoDS_Shape& aShape1 = theShape1->impl<TopoDS_Shape>();
192 const TopoDS_Shape& aShape2 = theShape2->impl<TopoDS_Shape>();
194 BRepExtrema_DistShapeShape aDist(aShape1, aShape2);
196 return aDist.IsDone() ? aDist.Value() : Precision::Infinite();
199 //==================================================================================================
200 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(
201 const std::shared_ptr<GeomAPI_Shape> theCompound,
202 const GeomAPI_Shape::ShapeType theType,
203 ListOfShape& theCombinedShapes,
204 ListOfShape& theFreeShapes)
206 GeomShapePtr aResult = theCompound;
208 if(!theCompound.get()) {
212 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
216 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
217 TopAbs_ShapeEnum aTA = TopAbs_FACE;
218 if(theType == GeomAPI_Shape::COMPSOLID) {
223 theCombinedShapes.clear();
224 theFreeShapes.clear();
227 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
228 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
229 const TopoDS_Shape& aShape = anIter.Value();
230 if(aShape.ShapeType() > aTA) {
231 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
232 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
233 theFreeShapes.push_back(aGeomShape);
237 // Map sub-shapes and shapes.
238 TopTools_IndexedDataMapOfShapeListOfShape aMapSA;
239 TopExp::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
240 if(aMapSA.IsEmpty()) {
244 // Get all shapes with common sub-shapes and free shapes.
245 NCollection_Map<TopoDS_Shape> aFreeShapes;
246 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
247 for(TopTools_IndexedDataMapOfShapeListOfShape::Iterator
248 anIter(aMapSA); anIter.More(); anIter.Next()) {
249 const TopoDS_Shape& aShape = anIter.Key();
250 TopTools_ListOfShape& aListOfShape = anIter.ChangeValue();
251 if(aListOfShape.IsEmpty()) {
254 else if(aListOfShape.Size() == 1) {
255 const TopoDS_Shape& aF = aListOfShape.First();
257 aListOfShape.Clear();
259 NCollection_List<TopoDS_Shape> aTempList;
260 NCollection_Map<TopoDS_Shape> aTempMap;
261 for (TopTools_ListOfShape::Iterator aListIt(aListOfShape); aListIt.More(); aListIt.Next()) {
262 aTempList.Append(aListIt.Value());
263 aTempMap.Add(aListIt.Value());
264 aFreeShapes.Remove(aListIt.Value());
266 aListOfShape.Clear();
267 for(NCollection_List<TopoDS_Shape>::Iterator
268 aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
269 const TopoDS_Shape& aTempShape = aTempIter.Value();
270 for(TopTools_IndexedDataMapOfShapeListOfShape::Iterator
271 anIter(aMapSA); anIter.More(); anIter.Next()) {
272 TopTools_ListOfShape& aTempListOfShape = anIter.ChangeValue();
273 if(aTempListOfShape.IsEmpty()) {
275 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
276 aTempListOfShape.Clear();
277 } else if(aTempListOfShape.Size() > 1) {
278 TopTools_ListOfShape::Iterator anIt1(aTempListOfShape);
279 for (; anIt1.More(); anIt1.Next()) {
280 if (anIt1.Value() == aTempShape) {
281 TopTools_ListOfShape::Iterator anIt2(aTempListOfShape);
282 for (; anIt2.More(); anIt2.Next())
284 if (anIt2.Value() != anIt1.Value()) {
285 if (aTempMap.Add(anIt2.Value())) {
286 aTempList.Append(anIt2.Value());
287 aFreeShapes.Remove(anIt2.Value());
291 aTempListOfShape.Clear();
298 aShapesWithCommonSubshapes.Append(aTempMap);
302 // Combine shapes with common sub-shapes.
303 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator
304 anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
306 TopoDS_CompSolid aCSolid;
307 TopoDS_Builder aBuilder;
309 GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
310 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
311 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
312 const TopoDS_Shape& aShape = anExp.Current();
313 if(aShapesMap.Contains(aShape)) {
315 GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
316 aShapesMap.Remove(aShape);
319 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
320 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) :
321 new TopoDS_Shape(aShell);
322 aGeomShape->setImpl<TopoDS_Shape>(aSh);
323 theCombinedShapes.push_back(aGeomShape);
326 // Adding free shapes.
327 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
328 const TopoDS_Shape& aShape = anExp.Current();
329 if(aFreeShapes.Contains(aShape)) {
330 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
331 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
332 theFreeShapes.push_back(aGeomShape);
336 if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
337 aResult = theCombinedShapes.front();
338 } else if(theCombinedShapes.size() == 0 && theFreeShapes.size() == 1) {
339 aResult = theFreeShapes.front();
341 TopoDS_Compound aResultComp;
342 TopoDS_Builder aBuilder;
343 aBuilder.MakeCompound(aResultComp);
344 for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin();
345 anIter != theCombinedShapes.cend(); anIter++) {
346 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
348 for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin();
349 anIter != theFreeShapes.cend(); anIter++) {
350 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
352 aResult->setImpl(new TopoDS_Shape(aResultComp));
358 //==================================================================================================
359 static void addSimpleShapeToList(const TopoDS_Shape& theShape,
360 NCollection_List<TopoDS_Shape>& theList)
362 if(theShape.IsNull()) {
366 if(theShape.ShapeType() == TopAbs_COMPOUND) {
367 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
368 addSimpleShapeToList(anIt.Value(), theList);
371 theList.Append(theShape);
375 //==================================================================================================
376 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
378 TopoDS_Compound aCompound;
380 BRep_Builder aBuilder;
381 aBuilder.MakeCompound(aCompound);
383 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
384 aBuilder.Add(aCompound, anIt.Value());
390 //==================================================================================================
391 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(
392 const std::shared_ptr<GeomAPI_Shape> theCompound)
394 GeomShapePtr aResult = theCompound;
396 if (!theCompound.get()) {
400 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
401 NCollection_List<TopoDS_Shape> anUngroupedShapes, aStillUngroupedShapes;
402 addSimpleShapeToList(anInShape, anUngroupedShapes);
404 // Iterate over all shapes and find shapes with shared vertices.
405 TopTools_ListOfShape allVertices;
406 TopTools_DataMapOfShapeListOfShape aVertexShapesMap;
407 for (NCollection_List<TopoDS_Shape>::Iterator aShapesIt(anUngroupedShapes);
410 const TopoDS_Shape& aShape = aShapesIt.Value();
411 for (TopExp_Explorer aShapeExp(aShape, TopAbs_VERTEX);
414 const TopoDS_Shape& aVertex = aShapeExp.Current();
415 if (!aVertexShapesMap.IsBound(aVertex)) {
416 NCollection_List<TopoDS_Shape> aList;
417 aList.Append(aShape);
418 allVertices.Append(aVertex);
419 aVertexShapesMap.Bind(aVertex, aList);
422 if (!aVertexShapesMap.ChangeFind(aVertex).Contains(aShape)) {
423 aVertexShapesMap.ChangeFind(aVertex).Append(aShape);
429 // Iterate over the map and group shapes.
430 NCollection_Vector<TopTools_MapOfShape> aGroups; // groups of shapes connected by vertices
431 while (!allVertices.IsEmpty()) {
432 // Get first group of shapes in map, and then unbind it.
433 const TopoDS_Shape& aKey = allVertices.First();
434 TopTools_ListOfShape aConnectedShapes = aVertexShapesMap.Find(aKey);
435 aVertexShapesMap.UnBind(aKey);
436 allVertices.Remove(aKey);
437 // Iterate over shapes in this group and add to it shapes from groups in map.
438 for (TopTools_ListOfShape::Iterator aConnectedIt(aConnectedShapes);
439 aConnectedIt.More(); aConnectedIt.Next()) {
440 const TopoDS_Shape& aConnected = aConnectedIt.Value();
441 TopTools_ListOfShape aKeysToUnbind;
442 for (TopTools_ListOfShape::Iterator aKeysIt(allVertices);
445 const TopTools_ListOfShape& anOtherConnected = aVertexShapesMap(aKeysIt.Value());
446 if (!anOtherConnected.Contains(aConnected)) {
447 // Other connected group does not contain shape from our connected group
450 // Other is connected to our, so add them to our connected
451 for (TopTools_ListOfShape::Iterator anOtherIt(anOtherConnected);
454 const TopoDS_Shape& aShape = anOtherIt.Value();
455 if (!aConnectedShapes.Contains(aShape)) {
456 aConnectedShapes.Append(aShape);
459 // Save key to unbind from this map.
460 aKeysToUnbind.Append(aKeysIt.Value());
462 // Unbind groups from map that we added to our group.
463 for (TopTools_ListOfShape::Iterator aKeysIt(aKeysToUnbind);
466 aVertexShapesMap.UnBind(aKeysIt.Value());
467 allVertices.Remove(aKeysIt.Value());
470 // Sort shapes from the most complicated to the simplest ones
471 TopTools_MapOfShape aSortedGroup;
472 for (int aST = TopAbs_COMPOUND; aST <= TopAbs_SHAPE; ++aST) {
473 TopTools_ListOfShape::Iterator anIt(aConnectedShapes);
474 while (anIt.More()) {
475 if (anIt.Value().ShapeType() == aST) {
476 aSortedGroup.Add(anIt.Value());
477 aConnectedShapes.Remove(anIt);
484 aGroups.Append(aSortedGroup);
487 TopoDS_Compound aCompound;
488 BRep_Builder aBuilder;
489 aBuilder.MakeCompound(aCompound);
490 ListOfShape aCompSolids, aFreeSolids;
491 for (NCollection_Vector<TopTools_MapOfShape>::Iterator anIt(aGroups); anIt.More(); anIt.Next()) {
492 const TopTools_MapOfShape& aGroup = anIt.ChangeValue();
493 GeomShapePtr aGeomShape(new GeomAPI_Shape());
494 if(aGroup.Size() == 1) {
495 TopTools_MapOfShape::Iterator aOneShapeIter(aGroup);
496 aGeomShape->setImpl(new TopoDS_Shape(aOneShapeIter.Value()));
498 // make sub-shapes in the group have order same as in original shape
499 TopTools_ListOfShape anOrderedGoup;
500 NCollection_List<TopoDS_Shape>::Iterator anUngrouped(anUngroupedShapes);
501 for (; anUngrouped.More(); anUngrouped.Next()) {
502 if (aGroup.Contains(anUngrouped.Value()))
503 anOrderedGoup.Append(anUngrouped.Value());
505 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(anOrderedGoup)));
506 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
507 GeomAPI_Shape::COMPSOLID,
511 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
514 if(!aCompound.IsNull()) {
515 aResult->setImpl(new TopoDS_Shape(aCompound));
521 //==================================================================================================
522 std::list<std::shared_ptr<GeomAPI_Pnt> >
523 GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
525 // Bounding box of all objects.
529 for (ListOfShape::const_iterator
530 anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
531 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
532 BRepBndLib::Add(aShape, aBndBox);
535 if(theEnlarge != 0.0) {
536 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
537 aBndBox.Enlarge(theEnlarge);
540 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
541 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
542 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
543 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
545 for(int i = 0; i < 2; i++) {
546 for(int j = 0; j < 2; j++) {
547 for(int k = 0; k < 2; k++) {
548 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
549 aResultPoints.push_back(aPnt);
554 return aResultPoints;
557 //==================================================================================================
558 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(
559 const std::shared_ptr<GeomAPI_Shape> thePlane,
560 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
562 std::shared_ptr<GeomAPI_Face> aResultFace;
564 if(!thePlane.get()) {
568 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
569 if(aShape.ShapeType() != TopAbs_FACE) {
573 TopoDS_Face aFace = TopoDS::Face(aShape);
574 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
579 GeomLib_IsPlanarSurface isPlanar(aSurf);
580 if(!isPlanar.IsPlanar()) {
584 if(thePoints.size() != 8) {
588 const gp_Pln& aFacePln = isPlanar.Plan();
589 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
590 IntAna_Quadric aQuadric(aFacePln);
591 Standard_Real UMin, UMax, VMin, VMax;
592 UMin = UMax = VMin = VMax = 0;
593 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
594 aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
595 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
596 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
597 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
598 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
599 Standard_Real aPntU(0), aPntV(0);
600 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
601 if(aPntU < UMin) UMin = aPntU;
602 if(aPntU > UMax) UMax = aPntU;
603 if(aPntV < VMin) VMin = aPntV;
604 if(aPntV > VMax) VMax = aPntV;
606 aResultFace.reset(new GeomAPI_Face());
607 aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
612 //==================================================================================================
613 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
614 std::shared_ptr<GeomAPI_Vertex>& theV1,
615 std::shared_ptr<GeomAPI_Vertex>& theV2)
617 static GeomVertexPtr aVertex;
619 aVertex = GeomVertexPtr(new GeomAPI_Vertex);
620 aVertex->setImpl(new TopoDS_Vertex());
627 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
628 TopoDS_Vertex aV1, aV2;
629 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
631 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
632 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
633 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
639 //==================================================================================================
640 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
641 const std::shared_ptr<GeomAPI_Dir> theDirection,
642 const ListOfShape& theWires,
643 ListOfShape& theFaces)
645 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
646 theDirection->impl<gp_Dir>()));
647 TopoDS_Face aFace = aMKFace.Face();
649 BRepAlgo_FaceRestrictor aFRestrictor;
650 aFRestrictor.Init(aFace, Standard_False, Standard_True);
651 for(ListOfShape::const_iterator anIt = theWires.cbegin();
652 anIt != theWires.cend();
654 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
655 aFRestrictor.Add(aWire);
658 aFRestrictor.Perform();
660 if(!aFRestrictor.IsDone()) {
664 for(; aFRestrictor.More(); aFRestrictor.Next()) {
665 GeomShapePtr aShape(new GeomAPI_Shape());
666 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
667 theFaces.push_back(aShape);
671 //==================================================================================================
672 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
674 TopoDS_Compound aCompound;
675 BRep_Builder aBuilder;
676 aBuilder.MakeCompound(aCompound);
678 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
679 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
681 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
683 if(aFindPlane.Found() != Standard_True) {
684 return std::shared_ptr<GeomAPI_Pln>();
687 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
688 gp_Pnt aLoc = aPlane->Location();
689 gp_Dir aDir = aPlane->Axis().Direction();
691 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
692 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
694 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
699 //==================================================================================================
700 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(
701 const std::shared_ptr<GeomAPI_Shape> theSubShape,
702 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
704 if(!theSubShape.get() || !theBaseShape.get()) {
708 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
709 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
711 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
712 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
713 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
715 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
718 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
719 if(aBaseShape.ShapeType() == TopAbs_FACE) {
720 // Check that edge is on face surface.
721 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
722 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
723 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
725 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
729 // Check intersections.
730 TopoDS_Vertex aV1, aV2;
731 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
732 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
733 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
734 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
735 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
736 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
738 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
739 // Edge intersect face bound. Check that it is not on edge begin or end.
740 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
741 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
742 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
743 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
750 // No intersections found. Edge is inside or outside face. Check it.
751 BRepAdaptor_Curve aCurveAdaptor(anEdge);
752 gp_Pnt aPointToCheck =
753 aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() +
754 aCurveAdaptor.LastParameter()) / 2.0);
755 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
756 ShapeAnalysis_Surface aSAS(aSurface);
757 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
758 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
759 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
773 //==================================================================================================
774 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
776 if(!theShape.get()) {
780 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
781 return (aChecker.IsValid() == Standard_True);
784 //==================================================================================================
785 std::shared_ptr<GeomAPI_Shape>
786 GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
788 GeomShapePtr anOuterWire;
790 if(!theFace.get() || !theFace->isFace()) {
794 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
795 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
797 anOuterWire.reset(new GeomAPI_Shape());
798 anOuterWire->setImpl(new TopoDS_Shape(aWire));
803 //==================================================================================================
804 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
805 const std::shared_ptr<GeomAPI_Face> theFace)
807 if(!theEdge.get() || !theFace.get()) {
811 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
812 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
814 BRepExtrema_ExtCF anExt(anEdge, aFace);
815 return anExt.IsParallel() == Standard_True;
818 //==================================================================================================
819 std::list<std::shared_ptr<GeomAPI_Vertex> > GeomAlgoAPI_ShapeTools::intersect(
820 const std::shared_ptr<GeomAPI_Edge> theEdge, const std::shared_ptr<GeomAPI_Face> theFace,
821 const bool thePointsOutsideFace)
823 std::list<std::shared_ptr<GeomAPI_Vertex> > aResult;
824 if(!theEdge.get() || !theFace.get()) {
828 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
829 double aFirstOnCurve, aLastOnCurve;
830 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
832 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
833 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
835 GeomAPI_IntCS anIntAlgo(aCurve, aSurf);
836 if (!anIntAlgo.IsDone())
838 // searching for points-intersection
839 for(int anIntNum = 1; anIntNum <= anIntAlgo.NbPoints() + anIntAlgo.NbSegments(); anIntNum++) {
841 if (anIntNum <= anIntAlgo.NbPoints()) {
842 anInt = anIntAlgo.Point(anIntNum);
843 } else { // take the middle point on the segment of the intersection
844 Handle(Geom_Curve) anIntCurve = anIntAlgo.Segment(anIntNum - anIntAlgo.NbPoints());
845 anIntCurve->D0((anIntCurve->FirstParameter() + anIntCurve->LastParameter()) / 2., anInt);
847 aResult.push_back(std::shared_ptr<GeomAPI_Vertex>(
848 new GeomAPI_Vertex(anInt.X(), anInt.Y(), anInt.Z())));
853 //==================================================================================================
854 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
855 const GeomAlgoAPI_ShapeTools::PointToRefsMap& thePointsInfo,
856 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
858 // to split shape at least one point should be presented in the points container
859 if (thePointsInfo.empty())
862 // General Fuse to split edge by vertices
863 BOPAlgo_Builder aBOP;
864 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
865 // Rebuild closed edge to place vertex to one of split points.
866 // This will prevent edge to be split on same vertex.
867 if (BRep_Tool::IsClosed(aBaseEdge))
869 Standard_Real aFirst, aLast;
870 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
872 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
873 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
874 gp_Pnt aPoint(aPnt->x(), aPnt->y(), aPnt->z());
876 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
877 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
878 aBaseEdge.Orientation(anOrientation);
880 aBOP.AddArgument(aBaseEdge);
882 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
883 for (; aPIt != thePointsInfo.end(); ++aPIt) {
884 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
885 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
886 aBOP.AddArgument(aV);
890 if (aBOP.HasErrors())
894 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
895 TopTools_ListIteratorOfListOfShape anIt(aSplits);
896 for (; anIt.More(); anIt.Next()) {
897 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
898 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
899 theShapes.insert(anEdge);
903 //==================================================================================================
904 void GeomAlgoAPI_ShapeTools::splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
905 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
906 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
908 // General Fuse to split edge by vertices
909 BOPAlgo_Builder aBOP;
910 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
911 // Rebuild closed edge to place vertex to one of split points.
912 // This will prevent edge to be split on seam vertex.
913 if (BRep_Tool::IsClosed(aBaseEdge))
915 Standard_Real aFirst, aLast;
916 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
918 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
919 gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
921 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
922 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
923 aBaseEdge.Orientation(anOrientation);
925 aBOP.AddArgument(aBaseEdge);
927 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
928 for (; aPtIt != thePoints.end(); ++aPtIt) {
929 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
930 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
931 aBOP.AddArgument(aV);
935 if (aBOP.HasErrors())
939 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
940 TopTools_ListIteratorOfListOfShape anIt(aSplits);
941 for (; anIt.More(); anIt.Next()) {
942 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
943 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
944 theShapes.insert(anEdge);
948 //==================================================================================================
949 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
950 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
951 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
953 std::shared_ptr<GeomAPI_Shape> aResultShape;
955 if (thePoints.size() == 2) {
956 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
957 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
959 std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
961 std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
962 aLast = theShapes.end();
963 for (; anIt != aLast; anIt++) {
964 GeomShapePtr aShape = *anIt;
965 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
967 std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
968 std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
969 if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
970 anEdgeLastPoint->isEqual(aLastPoint))
971 aResultShape = aShape;
979 //==================================================================================================
980 #ifdef FEATURE_MULTIROTATION_TWO_DIRECTIONS
981 std::shared_ptr<GeomAPI_Dir> GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(
982 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
983 const std::shared_ptr<GeomAPI_Ax1> theAxis)
985 gp_Pnt aCentreOfMassPoint =
986 GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape)->impl<gp_Pnt>();
987 Handle(Geom_Line) aLine = new Geom_Line(theAxis->impl<gp_Ax1>());
988 GeomAPI_ProjectPointOnCurve aPrjTool(aCentreOfMassPoint, aLine);
989 gp_Pnt aPoint = aPrjTool.NearestPoint();
991 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(aCentreOfMassPoint.X()-aPoint.X(),
992 aCentreOfMassPoint.Y()-aPoint.Y(),
993 aCentreOfMassPoint.Z()-aPoint.Z()));
998 //==================================================================================================
999 static TopoDS_Wire fixParametricGaps(const TopoDS_Wire& theWire)
1001 TopoDS_Wire aFixedWire;
1002 Handle(Geom_Curve) aPrevCurve;
1003 double aPrevLastParam = 0.0;
1005 BRep_Builder aBuilder;
1006 aBuilder.MakeWire(aFixedWire);
1008 BRepTools_WireExplorer aWExp(theWire);
1009 for (; aWExp.More(); aWExp.Next()) {
1010 TopoDS_Edge anEdge = aWExp.Current();
1011 double aFirst, aLast;
1012 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirst, aLast);
1013 if (aCurve == aPrevCurve) {
1014 // if parametric gap occurs, create new edge based on the copied curve
1015 aCurve = Handle(Geom_Curve)::DownCast(aCurve->Copy());
1016 TopoDS_Vertex aV1, aV2;
1017 TopExp::Vertices(anEdge, aV1, aV2);
1018 anEdge = TopoDS::Edge(anEdge.EmptyCopied());
1019 aBuilder.UpdateEdge(anEdge, aCurve, BRep_Tool::Tolerance(anEdge));
1020 aBuilder.Add(anEdge, aV1);
1021 aBuilder.Add(anEdge, aV2);
1024 aBuilder.Add(aFixedWire, anEdge);
1026 aPrevCurve = aCurve;
1027 aPrevLastParam = aLast;
1033 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_ShapeTools::wireToEdge(
1034 const std::shared_ptr<GeomAPI_Wire>& theWire)
1038 TopoDS_Wire aWire = theWire->impl<TopoDS_Wire>();
1039 // Workaround: when concatenate a wire consisting of two edges based on the same B-spline curve
1040 // (non-periodic, but having equal start and end points), first of which is placed at the end
1041 // on the curve and second is placed at the start, this workaround copies second curve to avoid
1042 // treating these edges as a single curve by setting trim parameters.
1043 aWire = fixParametricGaps(aWire);
1044 TopoDS_Edge aNewEdge = BRepAlgo::ConcatenateWireC0(aWire);
1045 anEdge = GeomEdgePtr(new GeomAPI_Edge);
1046 anEdge->setImpl(new TopoDS_Edge(aNewEdge));
1051 ListOfShape GeomAlgoAPI_ShapeTools::getLowLevelSubShapes(const GeomShapePtr& theShape)
1053 ListOfShape aSubShapes;
1055 if (!theShape->isCompound() && !theShape->isCompSolid() &&
1056 !theShape->isShell() && !theShape->isWire()) {
1060 for (GeomAPI_ShapeIterator anIt(theShape); anIt.more(); anIt.next()) {
1061 GeomShapePtr aSubShape = anIt.current();
1062 if (aSubShape->isVertex() || aSubShape->isEdge() ||
1063 aSubShape->isFace() || aSubShape->isSolid()) {
1064 aSubShapes.push_back(aSubShape);
1066 aSubShapes.splice(aSubShapes.end(), getLowLevelSubShapes(aSubShape));