Skip to content

Commit

Permalink
PD: [skip ci] refactor Hole feature
Browse files Browse the repository at this point in the history
  • Loading branch information
wwmayer committed Feb 26, 2022
1 parent 3ce87cb commit b05fb73
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 40 deletions.
82 changes: 42 additions & 40 deletions src/Mod/PartDesign/App/FeatureHole.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1831,17 +1831,13 @@ App::DocumentObjectExecReturn* Hole::execute(void)

double angle = Base::toRadians<double>(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()) {
Expand All @@ -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<BRepAlgoAPI_BooleanOperation> 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)
Expand Down Expand Up @@ -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();
Expand Down
1 change: 1 addition & 0 deletions src/Mod/PartDesign/App/FeatureHole.h
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class PartDesignExport Hole : public ProfileBased
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);
Expand Down

0 comments on commit b05fb73

Please sign in to comment.