diff --git a/src/libslic3r/CAD/CadDocument.cpp b/src/libslic3r/CAD/CadDocument.cpp index 4f7d2d4421..1699aa07d6 100644 --- a/src/libslic3r/CAD/CadDocument.cpp +++ b/src/libslic3r/CAD/CadDocument.cpp @@ -116,19 +116,30 @@ namespace Slic3r { // ---- helical-thread construction helpers (file-local) ---------------------- -// Helix spine on a cylinder (radius/pitch/height) about `axis`, as a wire. +// Helix spine on a cylinder (radius/pitch/height) about `axis`, as a wire of edges of at most one +// turn each, all the same length. A thread swept along it has a face per edge, and a face that +// winds round several turns is what OCCT's face/face intersection cannot be trusted with: cutting a +// thread swept along a single many-turn edge from a bore intermittently loses a piece of the +// helical curve where the groove meets the wall, and the cut then reports success having removed +// nothing. Which placements fail depends only on round-off, so a bore that threads at one spot on +// the bed would fail a few millimetres away. Faces of under one turn intersect reliably. static TopoDS_Wire make_helix_wire(const gp_Ax3& axis, double radius, double pitch, double height) { Handle(Geom_CylindricalSurface) cyl = new Geom_CylindricalSurface(axis, radius); double turns = (pitch > 1e-6) ? (height / pitch) : 1.0; // In the surface (u,v) parametrization u is the angle, v the axial height. - gp_Pnt2d p0(0.0, 0.0); - gp_Pnt2d p1(2.0 * M_PI * turns, height); - Handle(Geom2d_TrimmedCurve) seg = GC_MakeSegment2d(p0, p1); - TopoDS_Edge e = BRepBuilderAPI_MakeEdge(seg, cyl).Edge(); - BRepLib::BuildCurves3d(e); - return BRepBuilderAPI_MakeWire(e).Wire(); + const int n = std::max(1, int(std::ceil(turns - 1e-9))); + BRepBuilderAPI_MakeWire wire; + for (int i = 0; i < n; ++i) { + const double t0 = double(i) / n, t1 = double(i + 1) / n; + Handle(Geom2d_TrimmedCurve) seg = GC_MakeSegment2d(gp_Pnt2d(2.0 * M_PI * turns * t0, height * t0), + gp_Pnt2d(2.0 * M_PI * turns * t1, height * t1)); + TopoDS_Edge e = BRepBuilderAPI_MakeEdge(seg, cyl).Edge(); + BRepLib::BuildCurves3d(e); + wire.Add(e); // joins it to the previous edge's end, the same point on the cylinder + } + return wire.Wire(); } // Helix spine from a CadFeature's helix params. Supports cylindrical (taper==0) diff --git a/tests/libslic3r/test_caddocument.cpp b/tests/libslic3r/test_caddocument.cpp index ac6fe634c9..d6bdbca520 100644 --- a/tests/libslic3r/test_caddocument.cpp +++ b/tests/libslic3r/test_caddocument.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -9140,6 +9141,82 @@ TEST_CASE("A new thread reads its radius as the nominal (major) radius", "[CadDo CHECK(doc.error.find("thread") != std::string::npos); } +// Taps the bore of body 0 with the standard `designation` along the bore's whole length, as the Thread card and the +// thread command do for a picked bore, and returns the volume the thread removes. +static double tap_the_bore(CadDocument& doc, const char* designation) +{ + REQUIRE(doc.recompute()); + const double before = doc.body_mass_properties(0).volume; + const std::vector faces = GeometryEngine::faces_of(doc.bodies[0].shape); + int bore = -1; + for (int i = 0; i < int(faces.size()); ++i) + if (GeometryEngine::cylinder_of_face(faces[i]).ok) bore = i; + REQUIRE(bore >= 0); + SketchPlane plane; + GeometryEngine::CylinderFace cyl; + REQUIRE(doc.thread_target(0, bore, -1, plane, cyl)); + REQUIRE(cyl.internal); + const ThreadSpec* s = find_thread_standard(designation); + REQUIRE(s != nullptr); + doc.add_thread(0.5 * s->major_diameter_mm, s->pitch_mm, cyl.height, s->internal_depth_mm(), true, 0, 0, plane, "Thread"); + const bool rebuilt = doc.recompute(); + INFO(doc.error); + REQUIRE(rebuilt); + return before - doc.body_mass_properties(0).volume; +} + +// The volume of the ring between a thread's minor and major diameters over `length`. A tapped thread's groove is a V +// from the one out to the other, so it takes part of that ring, never all of it. +static double thread_ring(const char* designation, double length) +{ + const ThreadSpec* s = find_thread_standard(designation); + return 0.25 * M_PI * (std::pow(s->major_diameter_mm, 2) - std::pow(s->minor_diameter_mm(), 2)) * length; +} + +TEST_CASE("A bore takes the same thread wherever the part sits on the bed", "[CadDocument][thread]") +{ + using Catch::Matchers::WithinRel; + // The M5 hex standoff of the cad_features replay: 8 mm across flats, 15 long, bored with M5's tap drill, with the + // design's origin, which the Design tab puts at the bed's centre, at `origin`. + auto tapped_standoff = [](const Vec3d& origin) { + CadDocument doc; + doc.modeling_origin = origin; + SketchPlane xy = SketchPlane::XY(); + xy.origin += origin; + SketchProfile hex; + hex.points = {{4.618802, 0}, {2.309401, 4}, {-2.309401, 4}, {-4.618802, 0}, {-2.309401, -4}, {2.309401, -4}}; + hex.closed = true; + doc.add_extrude(doc.add_sketch_profile(hex, xy, "Sketch"), 15.0, false, BooleanMode::New, "Extrude"); + doc.add_hole(find_thread_standard("M5")->minor_diameter_mm(), 15.0, true, 0, 0, xy, "Hole"); + return tap_the_bore(doc, "M5"); + }; + const double at_origin = tapped_standoff(Vec3d::Zero()); + CHECK(at_origin > 0.1 * thread_ring("M5", 15.0)); + CHECK(at_origin < 0.5 * thread_ring("M5", 15.0)); + // Centres where OCCT, cutting a groove whose faces wind round several turns of the thread, loses part of the + // groove's intersection with the bore wall, and the cut removes nothing. (125, 125) is the middle of a 250 mm bed. + const Vec3d centre = GENERATE(Vec3d(125, 125, 0), Vec3d(118, 118, 0), Vec3d(122.5, 122.5, 0), Vec3d(130.5, 130.5, 0), + Vec3d(131.51, 119.07, 0)); + CHECK_THAT(tapped_standoff(centre), WithinRel(at_origin, 1e-3)); +} + +TEST_CASE("A blind hole takes a thread along its whole depth", "[CadDocument][thread]") +{ + // M3's tap drill 8 deep into the top of a 20 x 20 x 10 block, drilled from the top face down. + CadDocument doc; + doc.add_extrude(doc.add_sketch(SketchShape::Rectangle, SketchPlane::XY(), 20, 20, 0, "Sketch"), 10.0, false, + BooleanMode::New, "Block"); + SketchPlane top; + top.origin = Vec3d(0, 0, 10); + top.normal = Vec3d(0, 0, -1); + top.x_axis = Vec3d(1, 0, 0); + top.y_axis = top.normal.cross(top.x_axis); + doc.add_hole(find_thread_standard("M3")->minor_diameter_mm(), 8.0, false, 0, 0, top, "Hole"); + const double removed = tap_the_bore(doc, "M3"); + CHECK(removed > 0.1 * thread_ring("M3", 8.0)); + CHECK(removed < 0.5 * thread_ring("M3", 8.0)); +} + TEST_CASE("CadDocument: a profile on a body face touches it, one in free space does not", "[CadDocument]") { // The Extrude default reads this: a profile drawn on a body joins it (charter L6). diff --git a/tools/mcp/test-entries/design.md b/tools/mcp/test-entries/design.md index 23a653841d..f140baf174 100644 --- a/tools/mcp/test-entries/design.md +++ b/tools/mcp/test-entries/design.md @@ -38,7 +38,7 @@ The replay runs in a new project in the same instance. ## `test_cad_slow_build` `test_cad_slow_build.py` ยท Python, 7 checks (5 on Windows) -**Tests:** That the app stays inspectable while a long Design rebuild runs off the UI thread behind the progress dialog. The rebuild is an M8 thread tapped 60 mm deep. While it runs, `app_state` and a main-window screenshot answer, the progress dialog is listed and can be captured, and another Design command is refused as busy. The thread then lands as one feature. +**Tests:** That the app stays inspectable while a long Design rebuild runs off the UI thread behind the progress dialog. The rebuild is an M8 thread tapped 250 mm deep, 200 turns, which takes several seconds. While it runs, `app_state` and a main-window screenshot answer, the progress dialog is listed and can be captured, and another Design command is refused as busy. The thread then lands as one feature. **Decides by:** Exits 1 when a named check prints FAIL, or when an `app_state` or screenshot probe raises. - Result: "the thread call succeeds" (the awaited result has `ok` true) and "one thread feature and one body". diff --git a/tools/mcp/test_cad_slow_build.py b/tools/mcp/test_cad_slow_build.py index 2be4d14006..44050f3761 100755 --- a/tools/mcp/test_cad_slow_build.py +++ b/tools/mcp/test_cad_slow_build.py @@ -21,10 +21,11 @@ import time sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import orca_mcp # noqa: E402 -# An M8 thread tapped 60 mm deep into a 30 mm square block: 48 turns, several seconds of OCCT pipe and -# boolean work (about 8 s on an M2 Mac). -BLOCK = {"width": 30, "height": 30, "distance": 60} -THREAD = {"plane": "XY", "internal": True, "designation": "M8", "length": 60} +# An M8 thread tapped 250 mm deep into a 30 mm square block: 200 turns, several seconds of OCCT pipe and +# boolean work (about 5.5 s on a desktop PC running nothing else). The Windows probe starts 2 s in and +# needs the build still running after it. +BLOCK = {"width": 30, "height": 30, "distance": 250} +THREAD = {"plane": "XY", "internal": True, "designation": "M8", "length": 250} def progress_dialog_ref(client):