From b05fb731962b73b63204992d4a92f39a91b5da9b Mon Sep 17 00:00:00 2001 From: wmayer Date: Sat, 26 Feb 2022 17:06:38 +0100 Subject: [PATCH] PD: [skip ci] refactor Hole feature --- src/Mod/PartDesign/App/FeatureHole.cpp | 82 +++++++++++++------------- src/Mod/PartDesign/App/FeatureHole.h | 1 + 2 files changed, 43 insertions(+), 40 deletions(-) diff --git a/src/Mod/PartDesign/App/FeatureHole.cpp b/src/Mod/PartDesign/App/FeatureHole.cpp index 0a917884ad..ebbdb6555b 100644 --- a/src/Mod/PartDesign/App/FeatureHole.cpp +++ b/src/Mod/PartDesign/App/FeatureHole.cpp @@ -1831,17 +1831,13 @@ App::DocumentObjectExecReturn* Hole::execute(void) double angle = Base::toRadians(360.0); BRepPrimAPI_MakeRevol RevolMaker(face, gp_Ax1(firstPoint, zDir), angle); - - TopoDS_Shape protoHole; - if (RevolMaker.IsDone()) { - protoHole = RevolMaker.Shape(); - - if (protoHole.IsNull()) - return new App::DocumentObjectExecReturn("Hole error: Resulting shape is empty"); - } - else + if (!RevolMaker.IsDone()) return new App::DocumentObjectExecReturn("Hole error: Could not revolve sketch"); + TopoDS_Shape protoHole = RevolMaker.Shape(); + if (protoHole.IsNull()) + return new App::DocumentObjectExecReturn("Hole error: Resulting shape is empty"); + // Make thread if (Threaded.getValue() && ModelThread.getValue()) { @@ -1856,44 +1852,17 @@ App::DocumentObjectExecReturn* Hole::execute(void) protoHole = mkFuse.Shape(); } - BRep_Builder builder; - TopoDS_Compound holes; - builder.MakeCompound(holes); - TopTools_IndexedMapOfShape edgeMap; - TopExp::MapShapes(profileshape, TopAbs_EDGE, edgeMap); - for (int i = 1; i <= edgeMap.Extent(); i++) { - Standard_Real c_start; - Standard_Real c_end; - TopoDS_Edge edge = TopoDS::Edge(edgeMap(i)); - Handle(Geom_Curve) c = BRep_Tool::Curve(edge, c_start, c_end); - - // Circle? - if (c->DynamicType() != STANDARD_TYPE(Geom_Circle)) - continue; - - Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(c); - gp_Pnt loc = circle->Axis().Location(); - - - gp_Trsf localSketchTransformation; - localSketchTransformation.SetTranslation(gp_Pnt(0, 0, 0), - gp_Pnt(loc.X(), loc.Y(), loc.Z())); - - TopoDS_Shape copy = protoHole; - copy.Move(localSketchTransformation); - builder.Add(holes, copy); - } - + TopoDS_Compound holes = findHoles(profileshape, protoHole); this->AddSubShape.setValue(holes); // For some reason it is faster to do the cut through a BooleanOperation. - std::unique_ptr mkBool(new BRepAlgoAPI_Cut(base, holes)); - if (!mkBool->IsDone()) { + BRepAlgoAPI_Cut mkBool(base, holes); + if (!mkBool.IsDone()) { std::stringstream error; error << "Boolean operation failed"; return new App::DocumentObjectExecReturn(error.str()); } - TopoDS_Shape result = mkBool->Shape(); + TopoDS_Shape result = mkBool.Shape(); // We have to get the solids (fuse sometimes creates compounds) @@ -1980,6 +1949,39 @@ gp_Vec Hole::computePerpendicular(const gp_Vec& zDir) const return xDir; } +TopoDS_Compound Hole::findHoles(const TopoDS_Shape& profileshape, const TopoDS_Shape& protohole) const +{ + BRep_Builder builder; + TopoDS_Compound holes; + builder.MakeCompound(holes); + TopTools_IndexedMapOfShape edgeMap; + TopExp::MapShapes(profileshape, TopAbs_EDGE, edgeMap); + for (int i = 1; i <= edgeMap.Extent(); i++) { + Standard_Real c_start; + Standard_Real c_end; + TopoDS_Edge edge = TopoDS::Edge(edgeMap(i)); + Handle(Geom_Curve) c = BRep_Tool::Curve(edge, c_start, c_end); + + // Circle? + if (c->DynamicType() != STANDARD_TYPE(Geom_Circle)) + continue; + + Handle(Geom_Circle) circle = Handle(Geom_Circle)::DownCast(c); + gp_Pnt loc = circle->Axis().Location(); + + + gp_Trsf localSketchTransformation; + localSketchTransformation.SetTranslation(gp_Pnt(0, 0, 0), + gp_Pnt(loc.X(), loc.Y(), loc.Z())); + + TopoDS_Shape copy = protohole; + copy.Move(localSketchTransformation); + builder.Add(holes, copy); + } + + return holes; +} + TopoDS_Shape Hole::makeThread(const gp_Vec& xDir, const gp_Vec& zDir, double length) { bool leftHanded = (bool)ThreadDirection.getValue(); diff --git a/src/Mod/PartDesign/App/FeatureHole.h b/src/Mod/PartDesign/App/FeatureHole.h index 1477aa275d..645a388ee9 100644 --- a/src/Mod/PartDesign/App/FeatureHole.h +++ b/src/Mod/PartDesign/App/FeatureHole.h @@ -222,6 +222,7 @@ private: void rotateToNormal(const gp_Dir& helixAxis, const gp_Dir& normalAxis, TopoDS_Shape& helixShape) const; gp_Vec computePerpendicular(const gp_Vec&) const; TopoDS_Shape makeThread(const gp_Vec&, const gp_Vec&, double); + TopoDS_Compound findHoles(const TopoDS_Shape& profileshape, const TopoDS_Shape& protohole) const; // helpers for nlohmann json friend void from_json(const nlohmann::json &j, CounterBoreDimension &t);