Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Extrude: a tapered side drawn in collinear pieces is one face too (#1788) (#2583)

DuBearte33d7a86d500b
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index e65bd328d8..9d99288cdb 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -3778,3 +3778,15 @@ fn a_sketch_on_one_piece_stays_attached_when_the_side_joins() {
let sketch = run(&mut s, "timeline.inspect", json!({"feature": "Sketch2"}));
assert!(sketch["error"].is_null() && sketch["warning"].is_null(), "{sketch}");
}
+
+/// With a taper too, a side drawn in two collinear pieces extrudes into one face (#1788).
+#[test]
+fn a_tapered_side_drawn_in_two_collinear_pieces_is_one_face() {
+ let mut s = Session::default();
+ run(&mut s, "sketch.create", json!({"plane": "XY"}));
+ run(&mut s, "sketch.line", json!({"points": [[0, 0], [10, 0], [20, 0], [20, 10], [0, 10], [0, 0]]}));
+ run(&mut s, "sketch.finish", json!({}));
+ run(&mut s, "solid.extrude", json!({"distance": 5, "taper": "5 deg"}));
+ let m = run(&mut s, "inspect.measure", json!({}));
+ assert_eq!(m["bodies"][0]["kernel"]["faces"], 6, "{m}");
+}
diff --git a/crates/kernel/src/build.rs b/crates/kernel/src/build.rs
index d4ff0dcd66..7d306b371a 100644
--- a/crates/kernel/src/build.rs
+++ b/crates/kernel/src/build.rs
@@ -1328,7 +1328,9 @@ pub fn extrude_tapered_between(plane: &Plane, region: &Region2, from: f64, to: f
region_ok(region)?;
let (t, sign) = (taper.tan(), dir_sign.signum());
let (bottom_plane, top_plane) = (plane.offset(from * sign), plane.offset(to * sign));
- let loops0 = oriented(region)?;
+ // A side drawn in collinear pieces slopes as one face over all of them, as a straight
+ // extrude's does (#1788).
+ let loops0 = oriented(region)?.map_loops(|l| l.merged_lines(continues_straight));
let split: Vec<(Loop2, Vec<usize>)> = loops0.loops().map(|l| offset_splits(&l.lp, from.abs().max(to.abs()) * t)).collect();
let ends = |h: f64| split.iter().map(|(lp, _)| offset_loop(lp, h * t)).collect::<Result<Vec<_>>>();
let (bottoms, tops) = (ends(from)?, ends(to)?);
diff --git a/crates/kernel/src/history_tests.rs b/crates/kernel/src/history_tests.rs
index 25e2a58f01..7ce1c10ce7 100644
--- a/crates/kernel/src/history_tests.rs
+++ b/crates/kernel/src/history_tests.rs
@@ -277,3 +277,28 @@ fn collinear_profile_lines_extrude_into_one_side_face() {
let body = crate::extrude_whole_sides(&Plane::XY, &[Region2 { outer: Loop2::polygon(&step), holes: vec![] }], 0.0, 5.0).unwrap().pop().unwrap();
assert_eq!(body.face_count(), 8);
}
+
+/// A tapered extrude joins a side drawn in collinear pieces as the straight extrude does
+/// (#1788): one sloped face generated by both pieces, and the same solid as a side in one piece.
+#[test]
+fn collinear_profile_lines_taper_into_one_side_face() {
+ use solidcraft_geom::{Loop2, Plane, Region2, Vec2};
+ let polygon =
+ |pts: &[(f64, f64)]| Region2 { outer: Loop2::polygon(&pts.iter().map(|&(x, y)| Vec2::new(x, y)).collect::<Vec<_>>()), holes: vec![] };
+ let split = polygon(&[(0.0, 0.0), (10.0, 0.0), (20.0, 0.0), (20.0, 10.0), (0.0, 10.0)]);
+ let whole = polygon(&[(0.0, 0.0), (20.0, 0.0), (20.0, 10.0), (0.0, 10.0)]);
+ let taper = 5f64.to_radians();
+ for (from, to, foot) in [(0.0, 5.0, 0.0), (-1.0, 5.0, 1.0)] {
+ let body = crate::extrude_tapered_between(&Plane::XY, &split, from, to, foot, 1.0, taper).unwrap();
+ let reference = crate::extrude_tapered_between(&Plane::XY, &whole, from, to, foot, 1.0, taper).unwrap();
+ assert_eq!(body.face_count(), reference.face_count(), "{from}..{to} foot {foot}: as many faces as one piece");
+ let (v, w) = (crate::measure(&body).unwrap().volume, crate::measure(&reference).unwrap().volume);
+ assert!((v - w).abs() < 1e-9 * w, "{v} vs {w}");
+ let h = body.history().unwrap();
+ h.check(&body, &[]).unwrap();
+ assert!(
+ h.faces.iter().any(|f| f.origins == vec![FaceOrigin::Profile { segment: 0 }, FaceOrigin::Profile { segment: 1 }]),
+ "the bottom side is generated by both its pieces"
+ );
+ }
+}