PD: fix error 'gp_Dir::Crossed() - result vector has zero norm'

As reported here: https://forum.freecadweb.org/viewtopic.php?f=3&t=65826
This commit is contained in:
wmayer
2022-02-26 16:07:35 +01:00
parent 96045f3e58
commit 08a3b60031
2 changed files with 37 additions and 7 deletions

View File

@@ -1938,13 +1938,7 @@ App::DocumentObjectExecReturn* Hole::execute(void)
helix.Move(loc1);
// rotate the helix so that it is pointing in the zdir.
double angle = acos(dir_axis1 * zDir);
if (abs(angle) > Precision::Confusion()) {
gp_Dir rotAxis = dir_axis1 ^ zDir;
mov.SetRotation(gp_Ax1(origo, rotAxis), angle);
TopLoc_Location loc2(mov);
helix.Move(loc2);
}
rotateToNormal(dir_axis1, zDir, helix);
// create the pipe shell
BRepOffsetAPI_MakePipeShell mkPS(TopoDS::Wire(helix));
@@ -2071,6 +2065,41 @@ App::DocumentObjectExecReturn* Hole::execute(void)
}
}
void Hole::rotateToNormal(const gp_Dir& helixAxis, const gp_Dir& normalAxis, TopoDS_Shape& helixShape)
{
auto getRotationAxis = [](const gp_Dir& dir1, const gp_Dir& dir2, gp_Dir& dir3, double& angle) {
if (dir1.IsEqual(dir2, Precision::Angular()))
return false;
angle = acos(dir1 * dir2);
if (dir1.IsOpposite(dir2, Precision::Angular())) {
// Create a vector that is not parallel to dir1
gp_XYZ xyz(dir1.XYZ());
if (fabs(xyz.X()) <= fabs(xyz.Y()) && fabs(xyz.X()) <= fabs(xyz.Z()))
xyz.SetX(1.0);
else if (fabs(xyz.Y()) <= fabs(xyz.X()) && fabs(xyz.Y()) <= fabs(xyz.Z()))
xyz.SetY(1.0);
else
xyz.SetZ(1.0);
dir3 = dir1.Crossed(gp_Dir(xyz));
}
else {
dir3 = dir1.Crossed(dir2);
}
return true;
};
// rotate the helixAxis so that it is pointing in the normalAxis.
double angle;
gp_Dir rotAxis;
if (getRotationAxis(helixAxis, normalAxis, rotAxis, angle)) {
gp_Pnt origo(0.0, 0.0, 0.0);
gp_Trsf mov = helixShape.Location().Transformation();
mov.SetRotation(gp_Ax1(origo, rotAxis), angle);
TopLoc_Location loc2(mov);
helixShape.Move(loc2);
}
}
void Hole::addCutType(const CutDimensionSet& dimensions)
{
const CutDimensionSet::ThreadType thread = dimensions.thread_type;

View File

@@ -219,6 +219,7 @@ private:
double getThreadClassClearance();
double getThreadRunout(int mode = 1);
double getThreadPitch();
void rotateToNormal(const gp_Dir& helixAxis, const gp_Dir& normalAxis, TopoDS_Shape& helixShape);
// helpers for nlohmann json
friend void from_json(const nlohmann::json &j, CounterBoreDimension &t);