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>
32 #include <Bnd_Box.hxx>
33 #include <BOPTools.hxx>
34 #include <BRep_Builder.hxx>
35 #include <BRepAdaptor_Curve.hxx>
36 #include <BRepAlgo_FaceRestrictor.hxx>
37 #include <BRepBndLib.hxx>
38 #include <BRepBuilderAPI_FindPlane.hxx>
39 #include <BRepBuilderAPI_MakeEdge.hxx>
40 #include <BRepBuilderAPI_MakeFace.hxx>
41 #include <BRepCheck_Analyzer.hxx>
42 #include <BRepExtrema_DistShapeShape.hxx>
43 #include <BRepExtrema_ExtCF.hxx>
44 #include <BRepGProp.hxx>
45 #include <BRepTools.hxx>
46 #include <BRepTopAdaptor_FClass2d.hxx>
47 #include <Geom2d_Curve.hxx>
48 #include <Geom2d_Curve.hxx>
49 #include <BRepLib_CheckCurveOnSurface.hxx>
50 #include <BRep_Tool.hxx>
51 #include <Geom_Line.hxx>
52 #include <Geom_Plane.hxx>
53 #include <GeomAPI_ProjectPointOnCurve.hxx>
54 #include <GeomLib_IsPlanarSurface.hxx>
55 #include <GeomLib_Tool.hxx>
57 #include <GProp_GProps.hxx>
58 #include <IntAna_IntConicQuad.hxx>
59 #include <IntAna_Quadric.hxx>
60 #include <NCollection_Vector.hxx>
61 #include <ShapeAnalysis.hxx>
62 #include <ShapeAnalysis_Surface.hxx>
63 #include <TopoDS_Builder.hxx>
64 #include <TopoDS_Edge.hxx>
65 #include <TopoDS_Face.hxx>
66 #include <TopoDS_Shape.hxx>
67 #include <TopoDS_Shell.hxx>
68 #include <TopoDS_Vertex.hxx>
70 #include <TopExp_Explorer.hxx>
71 #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 NCollection_Vector<NCollection_List<TopoDS_Shape>> aGroups;
325 while(!anUngroupedShapes.IsEmpty()) {
326 NCollection_List<TopoDS_Shape> aGroupedShapes;
327 aGroupedShapes.Append(anUngroupedShapes.First());
328 anUngroupedShapes.RemoveFirst();
329 for(NCollection_List<TopoDS_Shape>::Iterator aGroupIt(aGroupedShapes);
330 aGroupIt.More(); aGroupIt.Next()) {
331 const TopoDS_Shape& aGroupedShape = aGroupIt.Value();
332 for(TopExp_Explorer aGroupShapeExp(aGroupedShape, TopAbs_VERTEX);
333 aGroupShapeExp.More();
334 aGroupShapeExp.Next()) {
335 // Find all shapes which have same vertex.
336 aStillUngroupedShapes.Clear();
337 const TopoDS_Shape& aVertex1 = aGroupShapeExp.Current();
338 for(NCollection_List<TopoDS_Shape>::Iterator anUngroupedIt(anUngroupedShapes);
339 anUngroupedIt.More();
340 anUngroupedIt.Next()) {
341 const TopoDS_Shape& anUngroupedShape = anUngroupedIt.Value();
342 bool isAdded = false;
343 for(TopExp_Explorer anUgroupedShapeExp(anUngroupedShape, TopAbs_VERTEX);
344 anUgroupedShapeExp.More();
345 anUgroupedShapeExp.Next()) {
346 const TopoDS_Shape& aVertex2 = anUgroupedShapeExp.Current();
347 if(aVertex1.IsSame(aVertex2)) {
348 aGroupedShapes.Append(anUngroupedShape);
354 aStillUngroupedShapes.Append(anUngroupedShape);
357 anUngroupedShapes = aStillUngroupedShapes;
358 if(anUngroupedShapes.IsEmpty()) {
362 if(anUngroupedShapes.IsEmpty()) {
366 aGroups.Append(aGroupedShapes);
369 TopoDS_Compound aCompound;
370 BRep_Builder aBuilder;
371 aBuilder.MakeCompound(aCompound);
372 ListOfShape aCompSolids, aFreeSolids;
373 for(NCollection_Vector<NCollection_List<TopoDS_Shape>>::Iterator
374 anIt(aGroups); anIt.More(); anIt.Next()) {
375 NCollection_List<TopoDS_Shape> aGroup = anIt.Value();
376 GeomShapePtr aGeomShape(new GeomAPI_Shape());
377 if(aGroup.Size() == 1) {
378 aGeomShape->setImpl(new TopoDS_Shape(aGroup.First()));
380 aGeomShape->setImpl(new TopoDS_Shape(makeCompound(aGroup)));
381 aGeomShape = GeomAlgoAPI_ShapeTools::combineShapes(aGeomShape,
382 GeomAPI_Shape::COMPSOLID,
386 aBuilder.Add(aCompound, aGeomShape->impl<TopoDS_Shape>());
389 if(!aCompound.IsNull()) {
390 aResult->setImpl(new TopoDS_Shape(aCompound));
396 //==================================================================================================
397 std::list<std::shared_ptr<GeomAPI_Pnt> >
398 GeomAlgoAPI_ShapeTools::getBoundingBox(const ListOfShape& theShapes, const double theEnlarge)
400 // Bounding box of all objects.
404 for (ListOfShape::const_iterator
405 anObjectsIt = theShapes.begin(); anObjectsIt != theShapes.end(); anObjectsIt++) {
406 const TopoDS_Shape& aShape = (*anObjectsIt)->impl<TopoDS_Shape>();
407 BRepBndLib::Add(aShape, aBndBox);
410 if(theEnlarge != 0.0) {
411 // We enlarge bounding box just to be sure that plane will be large enough to cut all objects.
412 aBndBox.Enlarge(theEnlarge);
415 Standard_Real aXArr[2] = {aBndBox.CornerMin().X(), aBndBox.CornerMax().X()};
416 Standard_Real aYArr[2] = {aBndBox.CornerMin().Y(), aBndBox.CornerMax().Y()};
417 Standard_Real aZArr[2] = {aBndBox.CornerMin().Z(), aBndBox.CornerMax().Z()};
418 std::list<std::shared_ptr<GeomAPI_Pnt> > aResultPoints;
420 for(int i = 0; i < 2; i++) {
421 for(int j = 0; j < 2; j++) {
422 for(int k = 0; k < 2; k++) {
423 std::shared_ptr<GeomAPI_Pnt> aPnt(new GeomAPI_Pnt(aXArr[i], aYArr[j], aZArr[k]));
424 aResultPoints.push_back(aPnt);
429 return aResultPoints;
432 //==================================================================================================
433 std::shared_ptr<GeomAPI_Shape>
434 GeomAlgoAPI_ShapeTools::faceToInfinitePlane(const std::shared_ptr<GeomAPI_Shape> theFace)
437 return std::shared_ptr<GeomAPI_Shape>();
439 TopoDS_Face aPlaneFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
440 if (aPlaneFace.IsNull())
441 return std::shared_ptr<GeomAPI_Shape>();
443 Handle(Geom_Plane) aPlane = Handle(Geom_Plane)::DownCast(BRep_Tool::Surface(aPlaneFace));
445 return std::shared_ptr<GeomAPI_Shape>();
447 // make an infinity face on the plane
448 TopoDS_Shape anInfiniteFace = BRepBuilderAPI_MakeFace(aPlane->Pln()).Shape();
450 std::shared_ptr<GeomAPI_Shape> aResult(new GeomAPI_Shape);
451 aResult->setImpl(new TopoDS_Shape(anInfiniteFace));
455 //==================================================================================================
456 std::shared_ptr<GeomAPI_Face> GeomAlgoAPI_ShapeTools::fitPlaneToBox(
457 const std::shared_ptr<GeomAPI_Shape> thePlane,
458 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints)
460 std::shared_ptr<GeomAPI_Face> aResultFace;
462 if(!thePlane.get()) {
466 const TopoDS_Shape& aShape = thePlane->impl<TopoDS_Shape>();
467 if(aShape.ShapeType() != TopAbs_FACE) {
471 TopoDS_Face aFace = TopoDS::Face(aShape);
472 Handle(Geom_Surface) aSurf = BRep_Tool::Surface(aFace);
477 GeomLib_IsPlanarSurface isPlanar(aSurf);
478 if(!isPlanar.IsPlanar()) {
482 if(thePoints.size() != 8) {
486 const gp_Pln& aFacePln = isPlanar.Plan();
487 Handle(Geom_Plane) aFacePlane = new Geom_Plane(aFacePln);
488 IntAna_Quadric aQuadric(aFacePln);
489 Standard_Real UMin, UMax, VMin, VMax;
490 UMin = UMax = VMin = VMax = 0;
491 for (std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator
492 aPointsIt = thePoints.begin(); aPointsIt != thePoints.end(); aPointsIt++) {
493 const gp_Pnt& aPnt = (*aPointsIt)->impl<gp_Pnt>();
494 gp_Lin aLin(aPnt, aFacePln.Axis().Direction());
495 IntAna_IntConicQuad anIntAna(aLin, aQuadric);
496 const gp_Pnt& aPntOnFace = anIntAna.Point(1);
497 Standard_Real aPntU(0), aPntV(0);
498 GeomLib_Tool::Parameters(aFacePlane, aPntOnFace, Precision::Confusion(), aPntU, aPntV);
499 if(aPntU < UMin) UMin = aPntU;
500 if(aPntU > UMax) UMax = aPntU;
501 if(aPntV < VMin) VMin = aPntV;
502 if(aPntV > VMax) VMax = aPntV;
504 aResultFace.reset(new GeomAPI_Face());
505 aResultFace->setImpl(new TopoDS_Face(BRepLib_MakeFace(aFacePln, UMin, UMax, VMin, VMax).Face()));
510 //==================================================================================================
511 void GeomAlgoAPI_ShapeTools::findBounds(const std::shared_ptr<GeomAPI_Shape> theShape,
512 std::shared_ptr<GeomAPI_Vertex>& theV1,
513 std::shared_ptr<GeomAPI_Vertex>& theV2)
515 if(!theShape.get()) {
516 std::shared_ptr<GeomAPI_Vertex> aVertex(new GeomAPI_Vertex);
517 aVertex->setImpl(new TopoDS_Vertex());
523 const TopoDS_Shape& aShape = theShape->impl<TopoDS_Shape>();
524 TopoDS_Vertex aV1, aV2;
525 ShapeAnalysis::FindBounds(aShape, aV1, aV2);
527 std::shared_ptr<GeomAPI_Vertex> aGeomV1(new GeomAPI_Vertex()), aGeomV2(new GeomAPI_Vertex());
528 aGeomV1->setImpl(new TopoDS_Vertex(aV1));
529 aGeomV2->setImpl(new TopoDS_Vertex(aV2));
534 //==================================================================================================
535 void GeomAlgoAPI_ShapeTools::makeFacesWithHoles(const std::shared_ptr<GeomAPI_Pnt> theOrigin,
536 const std::shared_ptr<GeomAPI_Dir> theDirection,
537 const ListOfShape& theWires,
538 ListOfShape& theFaces)
540 BRepBuilderAPI_MakeFace aMKFace(gp_Pln(theOrigin->impl<gp_Pnt>(),
541 theDirection->impl<gp_Dir>()));
542 TopoDS_Face aFace = aMKFace.Face();
544 BRepAlgo_FaceRestrictor aFRestrictor;
545 aFRestrictor.Init(aFace, Standard_False, Standard_True);
546 for(ListOfShape::const_iterator anIt = theWires.cbegin();
547 anIt != theWires.cend();
549 TopoDS_Wire aWire = TopoDS::Wire((*anIt)->impl<TopoDS_Shape>());
550 aFRestrictor.Add(aWire);
553 aFRestrictor.Perform();
555 if(!aFRestrictor.IsDone()) {
559 for(; aFRestrictor.More(); aFRestrictor.Next()) {
560 GeomShapePtr aShape(new GeomAPI_Shape());
561 aShape->setImpl(new TopoDS_Shape(aFRestrictor.Current()));
562 theFaces.push_back(aShape);
566 //==================================================================================================
567 std::shared_ptr<GeomAPI_Pln> GeomAlgoAPI_ShapeTools::findPlane(const ListOfShape& theShapes)
569 TopoDS_Compound aCompound;
570 BRep_Builder aBuilder;
571 aBuilder.MakeCompound(aCompound);
573 for(ListOfShape::const_iterator anIt = theShapes.cbegin(); anIt != theShapes.cend(); ++anIt) {
574 aBuilder.Add(aCompound, (*anIt)->impl<TopoDS_Shape>());
576 BRepBuilderAPI_FindPlane aFindPlane(aCompound);
578 if(aFindPlane.Found() != Standard_True) {
579 return std::shared_ptr<GeomAPI_Pln>();
582 Handle(Geom_Plane) aPlane = aFindPlane.Plane();
583 gp_Pnt aLoc = aPlane->Location();
584 gp_Dir aDir = aPlane->Axis().Direction();
586 std::shared_ptr<GeomAPI_Pnt> aGeomPnt(new GeomAPI_Pnt(aLoc.X(), aLoc.Y(), aLoc.Z()));
587 std::shared_ptr<GeomAPI_Dir> aGeomDir(new GeomAPI_Dir(aDir.X(), aDir.Y(), aDir.Z()));
589 std::shared_ptr<GeomAPI_Pln> aPln(new GeomAPI_Pln(aGeomPnt, aGeomDir));
594 //==================================================================================================
595 bool GeomAlgoAPI_ShapeTools::isSubShapeInsideShape(
596 const std::shared_ptr<GeomAPI_Shape> theSubShape,
597 const std::shared_ptr<GeomAPI_Shape> theBaseShape)
599 if(!theSubShape.get() || !theBaseShape.get()) {
603 const TopoDS_Shape& aSubShape = theSubShape->impl<TopoDS_Shape>();
604 const TopoDS_Shape& aBaseShape = theBaseShape->impl<TopoDS_Shape>();
606 if(aSubShape.ShapeType() == TopAbs_VERTEX) {
607 // If sub-shape is a vertex check distance to shape. If it is <= Precision::Confusion() then OK.
608 BRepExtrema_DistShapeShape aDist(aBaseShape, aSubShape);
610 if(!aDist.IsDone() || aDist.Value() > Precision::Confusion()) {
613 } else if (aSubShape.ShapeType() == TopAbs_EDGE) {
614 if(aBaseShape.ShapeType() == TopAbs_FACE) {
615 // Check that edge is on face surface.
616 TopoDS_Face aFace = TopoDS::Face(aBaseShape);
617 TopoDS_Edge anEdge = TopoDS::Edge(aSubShape);
618 BRepLib_CheckCurveOnSurface aCheck(anEdge, aFace);
620 if(!aCheck.IsDone() || aCheck.MaxDistance() > Precision::Confusion()) {
624 // Check intersections.
625 TopoDS_Vertex aV1, aV2;
626 ShapeAnalysis::FindBounds(anEdge, aV1, aV2);
627 gp_Pnt aPnt1 = BRep_Tool::Pnt(aV1);
628 gp_Pnt aPnt2 = BRep_Tool::Pnt(aV2);
629 for(TopExp_Explorer anExp(aBaseShape, TopAbs_EDGE); anExp.More(); anExp.Next()) {
630 const TopoDS_Shape& anEdgeOnFace = anExp.Current();
631 BRepExtrema_DistShapeShape aDist(anEdgeOnFace, anEdge);
633 if(aDist.IsDone() && aDist.Value() <= Precision::Confusion()) {
634 // Edge intersect face bound. Check that it is not on edge begin or end.
635 for(Standard_Integer anIndex = 1; anIndex <= aDist.NbSolution(); ++anIndex) {
636 gp_Pnt aPntOnSubShape = aDist.PointOnShape2(anIndex);
637 if(aPntOnSubShape.Distance(aPnt1) > Precision::Confusion()
638 && aPntOnSubShape.Distance(aPnt2) > Precision::Confusion()) {
645 // No intersections found. Edge is inside or outside face. Check it.
646 BRepAdaptor_Curve aCurveAdaptor(anEdge);
647 gp_Pnt aPointToCheck =
648 aCurveAdaptor.Value((aCurveAdaptor.FirstParameter() +
649 aCurveAdaptor.LastParameter()) / 2.0);
650 Handle(Geom_Surface) aSurface = BRep_Tool::Surface(aFace);
651 ShapeAnalysis_Surface aSAS(aSurface);
652 gp_Pnt2d aPointOnFace = aSAS.ValueOfUV(aPointToCheck, Precision::Confusion());
653 BRepTopAdaptor_FClass2d aFClass2d(aFace, Precision::Confusion());
654 if(aFClass2d.Perform(aPointOnFace) == TopAbs_OUT) {
668 //==================================================================================================
669 bool GeomAlgoAPI_ShapeTools::isShapeValid(const std::shared_ptr<GeomAPI_Shape> theShape)
671 if(!theShape.get()) {
675 BRepCheck_Analyzer aChecker(theShape->impl<TopoDS_Shape>());
676 return (aChecker.IsValid() == Standard_True);
679 //==================================================================================================
680 std::shared_ptr<GeomAPI_Shape>
681 GeomAlgoAPI_ShapeTools::getFaceOuterWire(const std::shared_ptr<GeomAPI_Shape> theFace)
683 GeomShapePtr anOuterWire;
685 if(!theFace.get() || !theFace->isFace()) {
689 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
690 TopoDS_Wire aWire = BRepTools::OuterWire(aFace);
692 anOuterWire.reset(new GeomAPI_Shape());
693 anOuterWire->setImpl(new TopoDS_Shape(aWire));
698 //==================================================================================================
699 bool GeomAlgoAPI_ShapeTools::isParallel(const std::shared_ptr<GeomAPI_Edge> theEdge,
700 const std::shared_ptr<GeomAPI_Face> theFace)
702 if(!theEdge.get() || !theFace.get()) {
706 TopoDS_Edge anEdge = TopoDS::Edge(theEdge->impl<TopoDS_Shape>());
707 TopoDS_Face aFace = TopoDS::Face(theFace->impl<TopoDS_Shape>());
709 BRepExtrema_ExtCF anExt(anEdge, aFace);
710 return anExt.IsParallel() == Standard_True;
713 //==================================================================================================
714 void GeomAlgoAPI_ShapeTools::splitShape(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
715 const GeomAlgoAPI_ShapeTools::PointToRefsMap& thePointsInfo,
716 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
718 // to split shape at least one point should be presented in the points container
719 if (thePointsInfo.empty())
722 // General Fuse to split edge by vertices
723 BOPAlgo_Builder aBOP;
724 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
725 // Rebuild closed edge to place vertex to one of split points.
726 // This will prevent edge to be split on same vertex.
727 if (BRep_Tool::IsClosed(aBaseEdge))
729 Standard_Real aFirst, aLast;
730 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
732 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
733 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
734 gp_Pnt aPoint(aPnt->x(), aPnt->y(), aPnt->z());
736 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
737 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
738 aBaseEdge.Orientation(anOrientation);
740 aBOP.AddArgument(aBaseEdge);
742 PointToRefsMap::const_iterator aPIt = thePointsInfo.begin();
743 for (; aPIt != thePointsInfo.end(); ++aPIt) {
744 std::shared_ptr<GeomAPI_Pnt> aPnt = aPIt->first;
745 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
746 aBOP.AddArgument(aV);
750 if (aBOP.ErrorStatus())
754 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
755 TopTools_ListIteratorOfListOfShape anIt(aSplits);
756 for (; anIt.More(); anIt.Next()) {
757 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
758 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
759 theShapes.insert(anEdge);
763 //==================================================================================================
764 void GeomAlgoAPI_ShapeTools::splitShape_p(const std::shared_ptr<GeomAPI_Shape>& theBaseShape,
765 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
766 std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
768 // General Fuse to split edge by vertices
769 BOPAlgo_Builder aBOP;
770 TopoDS_Edge aBaseEdge = theBaseShape->impl<TopoDS_Edge>();
771 // Rebuild closed edge to place vertex to one of split points.
772 // This will prevent edge to be split on seam vertex.
773 if (BRep_Tool::IsClosed(aBaseEdge))
775 Standard_Real aFirst, aLast;
776 Handle(Geom_Curve) aCurve = BRep_Tool::Curve(aBaseEdge, aFirst, aLast);
778 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPIt = thePoints.begin();
779 gp_Pnt aPoint((*aPIt)->x(), (*aPIt)->y(), (*aPIt)->z());
781 TopAbs_Orientation anOrientation = aBaseEdge.Orientation();
782 aBaseEdge = BRepBuilderAPI_MakeEdge(aCurve, aPoint, aPoint).Edge();
783 aBaseEdge.Orientation(anOrientation);
785 aBOP.AddArgument(aBaseEdge);
787 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPtIt = thePoints.begin();
788 for (; aPtIt != thePoints.end(); ++aPtIt) {
789 std::shared_ptr<GeomAPI_Pnt> aPnt = *aPtIt;
790 TopoDS_Vertex aV = BRepBuilderAPI_MakeVertex(gp_Pnt(aPnt->x(), aPnt->y(), aPnt->z()));
791 aBOP.AddArgument(aV);
795 if (aBOP.ErrorStatus())
799 const TopTools_ListOfShape& aSplits = aBOP.Modified(aBaseEdge);
800 TopTools_ListIteratorOfListOfShape anIt(aSplits);
801 for (; anIt.More(); anIt.Next()) {
802 std::shared_ptr<GeomAPI_Shape> anEdge(new GeomAPI_Shape);
803 anEdge->setImpl(new TopoDS_Shape(anIt.Value()));
804 theShapes.insert(anEdge);
808 //==================================================================================================
809 std::shared_ptr<GeomAPI_Shape> GeomAlgoAPI_ShapeTools::findShape(
810 const std::list<std::shared_ptr<GeomAPI_Pnt> >& thePoints,
811 const std::set<std::shared_ptr<GeomAPI_Shape> >& theShapes)
813 std::shared_ptr<GeomAPI_Shape> aResultShape;
815 if (thePoints.size() == 2) {
816 std::list<std::shared_ptr<GeomAPI_Pnt> >::const_iterator aPntIt = thePoints.begin();
817 std::shared_ptr<GeomAPI_Pnt> aFirstPoint = *aPntIt;
819 std::shared_ptr<GeomAPI_Pnt> aLastPoint = *aPntIt;
821 std::set<std::shared_ptr<GeomAPI_Shape> >::const_iterator anIt = theShapes.begin(),
822 aLast = theShapes.end();
823 for (; anIt != aLast; anIt++) {
824 GeomShapePtr aShape = *anIt;
825 std::shared_ptr<GeomAPI_Edge> anEdge(new GeomAPI_Edge(aShape));
827 std::shared_ptr<GeomAPI_Pnt> anEdgeFirstPoint = anEdge->firstPoint();
828 std::shared_ptr<GeomAPI_Pnt> anEdgeLastPoint = anEdge->lastPoint();
829 if (anEdgeFirstPoint->isEqual(aFirstPoint) &&
830 anEdgeLastPoint->isEqual(aLastPoint))
831 aResultShape = aShape;
839 //==================================================================================================
840 std::shared_ptr<GeomAPI_Dir> GeomAlgoAPI_ShapeTools::buildDirFromAxisAndShape(
841 const std::shared_ptr<GeomAPI_Shape> theBaseShape,
842 const std::shared_ptr<GeomAPI_Ax1> theAxis)
844 gp_Pnt aCentreOfMassPoint =
845 GeomAlgoAPI_ShapeTools::centreOfMass(theBaseShape)->impl<gp_Pnt>();
846 Handle(Geom_Line) aLine = new Geom_Line(theAxis->impl<gp_Ax1>());
847 GeomAPI_ProjectPointOnCurve aPrjTool(aCentreOfMassPoint, aLine);
848 gp_Pnt aPoint = aPrjTool.NearestPoint();
850 std::shared_ptr<GeomAPI_Dir> aDir(new GeomAPI_Dir(aCentreOfMassPoint.X()-aPoint.X(),
851 aCentreOfMassPoint.Y()-aPoint.Y(),
852 aCentreOfMassPoint.Z()-aPoint.Z()));