1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or
18 // email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
21 #include "GeomAlgoAPI_ShapeTools.h"
23 #include "GeomAlgoAPI_SketchBuilder.h"
25 #include <GeomAPI_Ax1.h>
26 #include <GeomAPI_Edge.h>
27 #include <GeomAPI_Dir.h>
28 #include <GeomAPI_Face.h>
29 #include <GeomAPI_Pln.h>
30 #include <GeomAPI_Pnt.h>
31 #include <GeomAPI_Wire.h>
33 #include <Bnd_Box.hxx>
34 #include <BOPTools.hxx>
35 #include <BRep_Builder.hxx>
36 #include <BRepAdaptor_Curve.hxx>
37 #include <BRepAlgo.hxx>
38 #include <BRepAlgo_FaceRestrictor.hxx>
39 #include <BRepBndLib.hxx>
40 #include <BRepBuilderAPI_FindPlane.hxx>
41 #include <BRepBuilderAPI_MakeEdge.hxx>
42 #include <BRepBuilderAPI_MakeFace.hxx>
43 #include <BRepCheck_Analyzer.hxx>
44 #include <BRepExtrema_DistShapeShape.hxx>
45 #include <BRepExtrema_ExtCF.hxx>
46 #include <BRepGProp.hxx>
47 #include <BRepTools.hxx>
48 #include <BRepTopAdaptor_FClass2d.hxx>
49 #include <BRepClass_FaceClassifier.hxx>
50 #include <Geom2d_Curve.hxx>
51 #include <Geom2d_Curve.hxx>
52 #include <BRepLib_CheckCurveOnSurface.hxx>
53 #include <BRep_Tool.hxx>
54 #include <Geom_Line.hxx>
55 #include <Geom_Plane.hxx>
56 #include <GeomAPI_ProjectPointOnCurve.hxx>
57 #include <GeomLib_IsPlanarSurface.hxx>
58 #include <GeomLib_Tool.hxx>
59 #include <GeomAPI_IntCS.hxx>
61 #include <GProp_GProps.hxx>
62 #include <IntAna_IntConicQuad.hxx>
63 #include <IntAna_Quadric.hxx>
64 #include <NCollection_Vector.hxx>
65 #include <ShapeAnalysis.hxx>
66 #include <ShapeAnalysis_Surface.hxx>
67 #include <TopoDS_Builder.hxx>
68 #include <TopoDS_Edge.hxx>
69 #include <TopoDS_Face.hxx>
70 #include <TopoDS_Shape.hxx>
71 #include <TopoDS_Shell.hxx>
72 #include <TopoDS_Vertex.hxx>
74 #include <TopExp_Explorer.hxx>
75 #include <TopTools_ListIteratorOfListOfShape.hxx>
78 #include <BOPAlgo_Builder.hxx>
79 #include <BRepBuilderAPI_MakeVertex.hxx>
80 #include <TopoDS_Edge.hxx>
82 //==================================================================================================
83 double GeomAlgoAPI_ShapeTools::volume(const std::shared_ptr<GeomAPI_Shape> theShape)
89 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
93 const Standard_Real anEps = 1.e-6;
94 BRepGProp::VolumeProperties(aShape, aGProps, anEps);
95 return aGProps.Mass();
98 //==================================================================================================
99 double GeomAlgoAPI_ShapeTools::area (const std::shared_ptr<GeomAPI_Shape> theShape)
101 GProp_GProps aGProps;
102 if(!theShape.get()) {
105 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
106 if(aShape.IsNull()) {
109 const Standard_Real anEps = 1.e-6;
111 BRepGProp::SurfaceProperties(aShape, aGProps, anEps);
112 return aGProps.Mass();
115 //==================================================================================================
116 std::shared_ptr<GeomAPI_Pnt>
117 GeomAlgoAPI_ShapeTools::centreOfMass(const std::shared_ptr<GeomAPI_Shape> theShape)
119 GProp_GProps aGProps;
121 return std::shared_ptr<GeomAPI_Pnt>();
123 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
124 if(aShape.IsNull()) {
125 return std::shared_ptr<GeomAPI_Pnt>();
128 if(aShape.ShapeType() == TopAbs_VERTEX) {
129 aCentre = BRep_Tool::Pnt(TopoDS::Vertex(aShape));
130 } else if(aShape.ShapeType() == TopAbs_EDGE || aShape.ShapeType() == TopAbs_WIRE) {
131 BRepGProp::LinearProperties(aShape, aGProps);
132 aCentre = aGProps.CentreOfMass();
134 BRepGProp::SurfaceProperties(aShape, aGProps);
135 aCentre = aGProps.CentreOfMass();
137 return std::shared_ptr<GeomAPI_Pnt>(new GeomAPI_Pnt(aCentre.X(), aCentre.Y(), aCentre.Z()));
140 //==================================================================================================
141 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::combineShapes(
142 const std::shared_ptr<GeomAPI_Shape> theCompound,
143 const GeomAPI_Shape::ShapeType theType,
144 ListOfShape& theCombinedShapes,
145 ListOfShape& theFreeShapes)
147 GeomShapePtr aResult = theCompound;
149 if(!theCompound.get()) {
153 if(theType != GeomAPI_Shape::SHELL && theType != GeomAPI_Shape::COMPSOLID) {
157 TopAbs_ShapeEnum aTS = TopAbs_EDGE;
158 TopAbs_ShapeEnum aTA = TopAbs_FACE;
159 if(theType == GeomAPI_Shape::COMPSOLID) {
164 theCombinedShapes.clear();
165 theFreeShapes.clear();
168 const TopoDS_Shape& aShapesComp = theCompound->impl<TopoDS_Shape>();
169 for(TopoDS_Iterator anIter(aShapesComp); anIter.More(); anIter.Next() ) {
170 const TopoDS_Shape& aShape = anIter.Value();
171 if(aShape.ShapeType() > aTA) {
172 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
173 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
174 theFreeShapes.push_back(aGeomShape);
178 // Map subshapes and shapes.
179 BOPCol_IndexedDataMapOfShapeListOfShape aMapSA;
180 BOPTools::MapShapesAndAncestors(aShapesComp, aTS, aTA, aMapSA);
181 if(aMapSA.IsEmpty()) {
185 // Get all shapes with common subshapes and free shapes.
186 NCollection_Map<TopoDS_Shape> aFreeShapes;
187 NCollection_Vector<NCollection_Map<TopoDS_Shape>> aShapesWithCommonSubshapes;
188 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator
189 anIter(aMapSA); anIter.More(); anIter.Next()) {
190 const TopoDS_Shape& aShape = anIter.Key();
191 BOPCol_ListOfShape& aListOfShape = anIter.ChangeValue();
192 if(aListOfShape.IsEmpty()) {
195 else if(aListOfShape.Size() == 1) {
196 const TopoDS_Shape& aF = aListOfShape.First();
198 aListOfShape.Clear();
200 NCollection_List<TopoDS_Shape> aTempList;
201 NCollection_Map<TopoDS_Shape> aTempMap;
202 const TopoDS_Shape& aF = aListOfShape.First();
203 const TopoDS_Shape& aL = aListOfShape.Last();
204 aTempList.Append(aF);
205 aTempList.Append(aL);
208 aFreeShapes.Remove(aF);
209 aFreeShapes.Remove(aL);
210 aListOfShape.Clear();
211 for(NCollection_List<TopoDS_Shape>::Iterator
212 aTempIter(aTempList); aTempIter.More(); aTempIter.Next()) {
213 const TopoDS_Shape& aTempShape = aTempIter.Value();
214 for(BOPCol_IndexedDataMapOfShapeListOfShape::Iterator
215 anIter(aMapSA); anIter.More(); anIter.Next()) {
216 BOPCol_ListOfShape& aTempListOfShape = anIter.ChangeValue();
217 if(aTempListOfShape.IsEmpty()) {
219 } else if(aTempListOfShape.Size() == 1 && aTempListOfShape.First() == aTempShape) {
220 aTempListOfShape.Clear();
221 } else if(aTempListOfShape.Size() > 1) {
222 if(aTempListOfShape.First() == aTempShape) {
223 const TopoDS_Shape& aTL = aTempListOfShape.Last();
224 if(aTempMap.Add(aTL)) {
225 aTempList.Append(aTL);
226 aFreeShapes.Remove(aTL);
228 aTempListOfShape.Clear();
229 } else if(aTempListOfShape.Last() == aTempShape) {
230 const TopoDS_Shape& aTF = aTempListOfShape.First();
231 if(aTempMap.Add(aTF)) {
232 aTempList.Append(aTF);
233 aFreeShapes.Remove(aTF);
235 aTempListOfShape.Clear();
240 aShapesWithCommonSubshapes.Append(aTempMap);
244 // Combine shapes with common subshapes.
245 for(NCollection_Vector<NCollection_Map<TopoDS_Shape>>::Iterator
246 anIter(aShapesWithCommonSubshapes); anIter.More(); anIter.Next()) {
248 TopoDS_CompSolid aCSolid;
249 TopoDS_Builder aBuilder;
251 GeomAPI_Shape::COMPSOLID ? aBuilder.MakeCompSolid(aCSolid) : aBuilder.MakeShell(aShell);
252 NCollection_Map<TopoDS_Shape>& aShapesMap = anIter.ChangeValue();
253 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
254 const TopoDS_Shape& aShape = anExp.Current();
255 if(aShapesMap.Contains(aShape)) {
257 GeomAPI_Shape::COMPSOLID ? aBuilder.Add(aCSolid, aShape) : aBuilder.Add(aShell, aShape);
258 aShapesMap.Remove(aShape);
261 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
262 TopoDS_Shape* aSh = theType == GeomAPI_Shape::COMPSOLID ? new TopoDS_Shape(aCSolid) :
263 new TopoDS_Shape(aShell);
264 aGeomShape->setImpl<TopoDS_Shape>(aSh);
265 theCombinedShapes.push_back(aGeomShape);
268 // Adding free shapes.
269 for(TopExp_Explorer anExp(aShapesComp, aTA); anExp.More(); anExp.Next()) {
270 const TopoDS_Shape& aShape = anExp.Current();
271 if(aFreeShapes.Contains(aShape)) {
272 std::shared_ptr<GeomAPI_Shape> aGeomShape(new GeomAPI_Shape);
273 aGeomShape->setImpl<TopoDS_Shape>(new TopoDS_Shape(aShape));
274 theFreeShapes.push_back(aGeomShape);
278 if(theCombinedShapes.size() == 1 && theFreeShapes.size() == 0) {
279 aResult = theCombinedShapes.front();
280 } else if(theCombinedShapes.size() == 0 && theFreeShapes.size() == 1) {
281 aResult = theFreeShapes.front();
283 TopoDS_Compound aResultComp;
284 TopoDS_Builder aBuilder;
285 aBuilder.MakeCompound(aResultComp);
286 for(ListOfShape::const_iterator anIter = theCombinedShapes.cbegin();
287 anIter != theCombinedShapes.cend(); anIter++) {
288 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
290 for(ListOfShape::const_iterator anIter = theFreeShapes.cbegin();
291 anIter != theFreeShapes.cend(); anIter++) {
292 aBuilder.Add(aResultComp, (*anIter)->impl<TopoDS_Shape>());
294 aResult->setImpl(new TopoDS_Shape(aResultComp));
300 //==================================================================================================
301 static void addSimpleShapeToList(const TopoDS_Shape& theShape,
302 NCollection_List<TopoDS_Shape>& theList)
304 if(theShape.IsNull()) {
308 if(theShape.ShapeType() == TopAbs_COMPOUND) {
309 for(TopoDS_Iterator anIt(theShape); anIt.More(); anIt.Next()) {
310 addSimpleShapeToList(anIt.Value(), theList);
313 theList.Append(theShape);
317 //==================================================================================================
318 static TopoDS_Compound makeCompound(const NCollection_List<TopoDS_Shape> theShapes)
320 TopoDS_Compound aCompound;
322 BRep_Builder aBuilder;
323 aBuilder.MakeCompound(aCompound);
325 for(NCollection_List<TopoDS_Shape>::Iterator anIt(theShapes); anIt.More(); anIt.Next()) {
326 aBuilder.Add(aCompound, anIt.Value());
332 //==================================================================================================
333 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::groupSharedTopology(
334 const std::shared_ptr<GeomAPI_Shape> theCompound)
336 GeomShapePtr aResult = theCompound;
338 if(!theCompound.get()) {
342 TopoDS_Shape anInShape = aResult->impl<TopoDS_Shape>();
343 NCollection_List<TopoDS_Shape> anUngroupedShapes, aStillUngroupedShapes;
344 addSimpleShapeToList(anInShape, anUngroupedShapes);
346 // Iterate over all shapes and find shapes with shared vertices.
347 TopTools_ListOfShape aMapOrder;
348 BOPCol_DataMapOfShapeListOfShape aVertexShapesMap;
349 for(NCollection_List<TopoDS_Shape>::Iterator aShapesIt(anUngroupedShapes);
352 const TopoDS_Shape& aShape = aShapesIt.Value();
353 for(TopExp_Explorer aShapeExp(aShape, TopAbs_VERTEX);
356 const TopoDS_Shape& aVertex = aShapeExp.Current();
357 if (!aVertexShapesMap.IsBound(aVertex)) {
358 NCollection_List<TopoDS_Shape> aList;
359 aList.Append(aShape);
360 aMapOrder.Append(aVertex);
361 aVertexShapesMap.Bind(aVertex, aList);
363 if(!aVertexShapesMap.ChangeFind(aVertex).Contains(aShape)) {
364 aVertexShapesMap.ChangeFind(aVertex).Append(aShape);
370 // Iterate over the map and group shapes.
371 NCollection_Vector<TopTools_ListOfShape> aGroups;
372 while (!aMapOrder.IsEmpty()) {
373 // Get first group of shapes in map, and then unbind it.
374 const TopoDS_Shape& aKey = aMapOrder.First();
375 TopTools_ListOfShape aGroupedShapes = aVertexShapesMap.Find(aKey);
376 aVertexShapesMap.UnBind(aKey);
377 aMapOrder.Remove(aKey);
378 // Iterate over shapes in this group and add to it shapes from groups in map.
379 for(TopTools_ListOfShape::Iterator aGroupIt(aGroupedShapes);
380 aGroupIt.More(); aGroupIt.Next()) {
381 const TopoDS_Shape& aGroupedShape = aGroupIt.Value();
382 TopTools_ListOfShape aKeysToUnbind;
383 for(TopTools_ListOfShape::Iterator aKeysIt(aMapOrder);
386 const TopTools_ListOfShape& aGroupInMap = aVertexShapesMap(aKeysIt.Value());
387 if(!aGroupInMap.Contains(aGroupedShape)) {
388 // Group in map does not containt shape from our group, so go to the next group in map.
391 // Iterate over shape in group in map, and add new shapes into our group.
392 for(TopTools_ListOfShape::Iterator aGroupInMapIt(aGroupInMap);
393 aGroupInMapIt.More();
394 aGroupInMapIt.Next()) {
395 const TopoDS_Shape& aShape = aGroupInMapIt.Value();
396 if (!aGroupedShapes.Contains(aShape)) {
397 aGroupedShapes.Append(aShape);
400 // Save key to unbind from this map.
401 aKeysToUnbind.Append(aKeysIt.Value());
403 // Unbind groups from map that we added to our group.
404 for(TopTools_ListOfShape::Iterator aKeysIt(aKeysToUnbind);
407 aVertexShapesMap.UnBind(aKeysIt.Value());
408 aMapOrder.Remove(aKeysIt.Value());
412 TopTools_ListOfShape aSortedGroup;
413 for(int aST = TopAbs_COMPOUND; aST <= TopAbs_SHAPE; ++aST) {
414 TopTools_ListOfShape::Iterator anIt(aGroupedShapes);
415 while (anIt.More()) {
416 if(anIt.Value().ShapeType() == aST) {
417 aSortedGroup.Append(anIt.Value());
418 aGroupedShapes.Remove(anIt);
424 aGroups.Append(aSortedGroup);
427 TopoDS_Compound aCompound;
428 BRep_Builder aBuilder;
429 aBuilder.MakeCompound(aCompound);
430 ListOfShape aCompSolids, aFreeSolids;
431 for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator
432 anIt(aGroups); anIt.More(); anIt.Next()) {
433 NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
434 GeomShapePtr aGeomShape(new GeomAPI_Shape());
435 if(aGroup.Size() == 1) {
436 aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
438 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
439 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
440 GeomAPI_Shape::COMPSOLID,
444 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
447 if(!aCompound.IsNull()) {
448 aResult->setImpl(new TopoDS_Shape(aCompound));
454 //==================================================================================================
455 std::list<std::shared_ptr<GeomAPI_Pnt> >
456 GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
458 // Bounding box of all objects.
462 for (ListOfShape::const_iterator
463 anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
464 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
465 BRepBndLib::Add(aShape, aBndBox);
468 if(theEnlarge != 0.0) {
469 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
470 aBndBox.Enlarge(theEnlarge);
473 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
474 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
475 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
476 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
478 for(int i = 0; i < 2; i++) {
479 for(int j = 0; j < 2; j++) {
480 for(int k = 0; k < 2; k++) {
481 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
482 aResultPoints.push_back(aPnt);
487 return aResultPoints;
490 //==================================================================================================
491 std::shared_ptr<GeomAPI_Shape>
492 GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
495 return std::shared_ptr<GeomAPI_Shape>();
497 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
498 if (aPlaneFace.IsNull())
499 return std::shared_ptr<GeomAPI_Shape>();
501 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
503 return std::shared_ptr<GeomAPI_Shape>();
505 // make an infinity face on the plane
506 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
508 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
509 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
513 //==================================================================================================
514 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(
515 const std::shared_ptr<GeomAPI_Shape> thePlane,
516 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
518 std::shared_ptr<GeomAPI_Face> aResultFace;
520 if(!thePlane.get()) {
524 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
525 if(aShape.ShapeType() != TopAbs_FACE) {
529 TopoDS_Face aFace = TopoDS::Face(aShape);
530 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
535 GeomLib_IsPlanarSurface isPlanar(aSurf);
536 if(!isPlanar.IsPlanar()) {
540 if(thePoints.size() != 8) {
544 const gp_Pln& aFacePln = isPlanar.Plan();
545 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
546 IntAna_Quadric aQuadric(aFacePln);
547 Standard_Real UMin, UMax, VMin, VMax;
548 UMin = UMax = VMin = VMax = 0;
549 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
550 aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
551 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
552 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
553 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
554 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
555 Standard_Real aPntU(0), aPntV(0);
556 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
557 if(aPntU < UMin) UMin = aPntU;
558 if(aPntU > UMax) UMax = aPntU;
559 if(aPntV < VMin) VMin = aPntV;
560 if(aPntV > VMax) VMax = aPntV;
562 aResultFace.reset(new GeomAPI_Face());
563 aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
568 //==================================================================================================
569 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
570 std::shared_ptr<GeomAPI_Vertex>& theV1,
571 std::shared_ptr<GeomAPI_Vertex>& theV2)
573 if(!theShape.get()) {
574 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
575 aVertex->setImpl(new TopoDS_Vertex());
581 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
582 TopoDS_Vertex aV1, aV2;
583 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
585 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
586 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
587 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
592 //==================================================================================================
593 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
594 const std::shared_ptr<GeomAPI_Dir> theDirection,
595 const ListOfShape& theWires,
596 ListOfShape& theFaces)
598 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
599 theDirection->impl<gp_Dir>()));
600 TopoDS_Face aFace = aMKFace.Face();
602 BRepAlgo_FaceRestrictor aFRestrictor;
603 aFRestrictor.Init(aFace, Standard_False, Standard_True);
604 for(ListOfShape::const_iterator anIt = theWires.cbegin();
605 anIt != theWires.cend();
607 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
608 aFRestrictor.Add(aWire);
611 aFRestrictor.Perform();
613 if(!aFRestrictor.IsDone()) {
617 for(; aFRestrictor.More(); aFRestrictor.Next()) {
618 GeomShapePtr aShape(new GeomAPI_Shape());
619 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
620 theFaces.push_back(aShape);
624 //==================================================================================================
625 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
627 TopoDS_Compound aCompound;
628 BRep_Builder aBuilder;
629 aBuilder.MakeCompound(aCompound);
631 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
632 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
634 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
636 if(aFindPlane.Found() != Standard_True) {
637 return std::shared_ptr<GeomAPI_Pln>();
640 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
641 gp_Pnt aLoc = aPlane->Location();
642 gp_Dir aDir = aPlane->Axis().Direction();
644 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
645 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
647 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
652 //==================================================================================================
653 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(
654 const std::shared_ptr<GeomAPI_Shape> theSubShape,
655 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
657 if(!theSubShape.get() || !theBaseShape.get()) {
661 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
662 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
664 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
665 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
666 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
668 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
671 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
672 if(aBaseShape.ShapeType() == TopAbs_FACE) {
673 // Check that edge is on face surface.
674 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
675 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
676 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
678 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
682 // Check intersections.
683 TopoDS_Vertex aV1, aV2;
684 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
685 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
686 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
687 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
688 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
689 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
691 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
692 // Edge intersect face bound. Check that it is not on edge begin or end.
693 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
694 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
695 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
696 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
703 // No intersections found. Edge is inside or outside face. Check it.
704 BRepAdaptor_Curve aCurveAdaptor(anEdge);
705 gp_Pnt aPointToCheck =
706 aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() +
707 aCurveAdaptor.LastParameter()) / 2.0);
708 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
709 ShapeAnalysis_Surface aSAS(aSurface);
710 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
711 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
712 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
726 //==================================================================================================
727 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
729 if(!theShape.get()) {
733 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
734 return (aChecker.IsValid() == Standard_True);
737 //==================================================================================================
738 std::shared_ptr<GeomAPI_Shape>
739 GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
741 GeomShapePtr anOuterWire;
743 if(!theFace.get() || !theFace->isFace()) {
747 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
748 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
750 anOuterWire.reset(new GeomAPI_Shape());
751 anOuterWire->setImpl(new TopoDS_Shape(aWire));
756 //==================================================================================================
757 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
758 const std::shared_ptr<GeomAPI_Face> theFace)
760 if(!theEdge.get() || !theFace.get()) {
764 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
765 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
767 BRepExtrema_ExtCF anExt(anEdge, aFace);
768 return anExt.IsParallel() == Standard_True;
771 //==================================================================================================
772 std::list<std::shared_ptr<GeomAPI_Vertex> > GeomAlgoAPI_ShapeTools::intersect(
773 const std::shared_ptr<GeomAPI_Edge> theEdge, const std::shared_ptr<GeomAPI_Face> theFace,
774 const bool thePointsOutsideFace)
776 std::list<std::shared_ptr<GeomAPI_Vertex> > aResult;
777 if(!theEdge.get() || !theFace.get()) {
781 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
782 double aFirstOnCurve, aLastOnCurve;
783 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(anEdge, aFirstOnCurve, aLastOnCurve);
785 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
786 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
788 GeomAPI_IntCS anIntAlgo(aCurve, aSurf);
789 if (!anIntAlgo.IsDone())
791 // searching for points-intersection
792 for(int anIntNum = 1; anIntNum <= anIntAlgo.NbPoints() + anIntAlgo.NbSegments(); anIntNum++) {
794 if (anIntNum <= anIntAlgo.NbPoints()) {
795 anInt = anIntAlgo.Point(anIntNum);
796 } else { // take the middle point on the segment of the intersection
797 Handle(Geom_Curve) anIntCurve = anIntAlgo.Segment(anIntNum - anIntAlgo.NbPoints());
798 anIntCurve->D0((anIntCurve->FirstParameter() + anIntCurve->LastParameter()) / 2., anInt);
800 aResult.push_back(std::shared_ptr<GeomAPI_Vertex>(
801 new GeomAPI_Vertex(anInt.X(), anInt.Y(), anInt.Z())));
806 //==================================================================================================
807 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
808 const GeomAlgoAPI_ShapeTools::PointToRefsMap& thePointsInfo,
809 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
811 // to split shape at least one point should be presented in the points container
812 if (thePointsInfo.empty())
815 // General Fuse to split edge by vertices
816 BOPAlgo_Builder aBOP;
817 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
818 // Rebuild closed edge to place vertex to one of split points.
819 // This will prevent edge to be split on same vertex.
820 if (BRep_Tool::IsClosed(aBaseEdge))
822 Standard_Real aFirst, aLast;
823 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
825 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
826 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
827 gp_Pnt aPoint(aPnt->x(), aPnt->y(), aPnt->z());
829 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
830 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
831 aBaseEdge.Orientation(anOrientation);
833 aBOP.AddArgument(aBaseEdge);
835 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
836 for (; aPIt != thePointsInfo.end(); ++aPIt) {
837 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
838 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
839 aBOP.AddArgument(aV);
844 if (aBOP.HasErrors())
847 if (aBOP.ErrorStatus())
852 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
853 TopTools_ListIteratorOfListOfShape anIt(aSplits);
854 for (; anIt.More(); anIt.Next()) {
855 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
856 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
857 theShapes.insert(anEdge);
861 //==================================================================================================
862 void GeomAlgoAPI_ShapeTools::splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
863 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
864 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
866 // General Fuse to split edge by vertices
867 BOPAlgo_Builder aBOP;
868 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
869 // Rebuild closed edge to place vertex to one of split points.
870 // This will prevent edge to be split on seam vertex.
871 if (BRep_Tool::IsClosed(aBaseEdge))
873 Standard_Real aFirst, aLast;
874 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
876 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
877 gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
879 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
880 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
881 aBaseEdge.Orientation(anOrientation);
883 aBOP.AddArgument(aBaseEdge);
885 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
886 for (; aPtIt != thePoints.end(); ++aPtIt) {
887 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
888 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
889 aBOP.AddArgument(aV);
894 if (aBOP.HasErrors())
897 if (aBOP.ErrorStatus())
902 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
903 TopTools_ListIteratorOfListOfShape anIt(aSplits);
904 for (; anIt.More(); anIt.Next()) {
905 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
906 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
907 theShapes.insert(anEdge);
911 //==================================================================================================
912 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
913 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
914 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
916 std::shared_ptr<GeomAPI_Shape> aResultShape;
918 if (thePoints.size() == 2) {
919 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
920 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
922 std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
924 std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
925 aLast = theShapes.end();
926 for (; anIt != aLast; anIt++) {
927 GeomShapePtr aShape = *anIt;
928 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
930 std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
931 std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
932 if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
933 anEdgeLastPoint->isEqual(aLastPoint))
934 aResultShape = aShape;
942 //==================================================================================================
943 std::shared_ptr<GeomAPI_Dir> GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(
944 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
945 const std::shared_ptr<GeomAPI_Ax1> theAxis)
947 gp_Pnt aCentreOfMassPoint =
948 GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape)->impl<gp_Pnt>();
949 Handle(Geom_Line) aLine = new Geom_Line(theAxis->impl<gp_Ax1>());
950 GeomAPI_ProjectPointOnCurve aPrjTool(aCentreOfMassPoint, aLine);
951 gp_Pnt aPoint = aPrjTool.NearestPoint();
953 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(aCentreOfMassPoint.X()-aPoint.X(),
954 aCentreOfMassPoint.Y()-aPoint.Y(),
955 aCentreOfMassPoint.Z()-aPoint.Z()));
959 //==================================================================================================
960 std::shared_ptr<GeomAPI_Edge> GeomAlgoAPI_ShapeTools::wireToEdge(
961 const std::shared_ptr<GeomAPI_Wire>& theWire)
965 const TopoDS_Wire& aWire = theWire->impl<TopoDS_Wire>();
966 TopoDS_Edge aNewEdge = BRepAlgo::ConcatenateWireC0(aWire);
967 anEdge = GeomEdgePtr(new GeomAPI_Edge);
968 anEdge->setImpl(new TopoDS_Edge(aNewEdge));