SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch Fillet and Chamfer: two lines that share no end are brought to where they meet (#6912, #7759) (#8426)
semmelrinde3292cba4ad8b
diff --git a/crates/engine/src/cmd/sketch_corner_tests.rs b/crates/engine/src/cmd/sketch_corner_tests.rsindex f49402bd5a..2a78bf5b00 100644--- a/crates/engine/src/cmd/sketch_corner_tests.rs+++ b/crates/engine/src/cmd/sketch_corner_tests.rs@@ -297,3 +297,63 @@ fn a_fillet_of_a_nearly_straight_or_folded_corner_with_dimensions_does_not_panicassert!(sk(&s).points.iter().all(|p| p.pos.is_finite()));}}++/// Two lines that share no end are brought to where they meet before the corner is rounded or+/// cut (#6912, #7759, #7985). A line that stops short is extended, and one that runs past the+/// other is cut there, losing its shorter piece; the far ends stay. Parallel lines, an end held+/// by other geometry and a dimensioned length are refused, and change nothing.+#[test]+fn a_fillet_or_chamfer_brings_two_separate_lines_to_where_they_meet() {+ use solidcraft_geom::Vec2;+ use solidcraft_sketch::CurveKind;+ let lines = |s: &mut Session, a: [[f64; 2]; 2], b: [[f64; 2]; 2]| {+ let la = curves(&run(s, "sketch.line", json!({"points": a, "connect": false})))[0].clone();+ let lb = curves(&run(s, "sketch.line", json!({"points": b, "connect": false})))[0].clone();+ (la, lb)+ };+ let ends = |s: &Session, id: &str| {+ let k = sk(s);+ let CurveKind::Line { a, b } = k.curves[k.curve_index(id).unwrap()].kind else { panic!("{id} is a line") };+ (k.point(a).unwrap(), k.point(b).unwrap())+ };+ let near = |q: Vec2, x: f64, y: f64| q.dist(Vec2::new(x, y)) < 1e-6;+ // Stopping short of each other (a gap), and crossing with a short piece past the corner.+ for (a, b) in [([[0.0, 0.0], [10.0, 0.0]], [[20.0, 5.0], [20.0, 20.0]]), ([[0.0, 0.0], [24.0, 0.0]], [[20.0, -4.0], [20.0, 20.0]])] {+ let mut s = new_sketch();+ let (la, lb) = lines(&mut s, a, b);+ let r = run(&mut s, "sketch.fillet", json!({"a": la, "b": lb, "radius": 3}));+ assert!(solved(&s), "{r}");+ let k = sk(&s);+ let arc = k.curve_index(&curves(&r)[0]).unwrap();+ let CurveKind::Arc { c, .. } = k.curves[arc].kind else { panic!("an arc") };+ assert!(near(k.point(c).unwrap(), 17.0, 3.0), "{:?}", k.point(c));+ let ((a0, a1), (b0, b1)) = (ends(&s, &la), ends(&s, &lb));+ assert!(near(a0, 0.0, 0.0) && near(a1, 17.0, 0.0), "{la}: {a0:?} {a1:?}");+ assert!(near(b0, 20.0, 3.0) && near(b1, 20.0, 20.0), "{lb}: {b0:?} {b1:?}");+ assert_eq!(k.curves.len(), 3, "the two lines and the arc, nothing left past the corner");+ assert!(k.points.iter().all(|q| !near(q.pos, 24.0, 0.0) && !near(q.pos, 20.0, -4.0) && !near(q.pos, 20.0, 0.0)), "{:?}", k.points);+ }+ // A chamfer the same way.+ let mut s = new_sketch();+ let (la, lb) = lines(&mut s, [[0.0, 0.0], [10.0, 0.0]], [[20.0, 5.0], [20.0, 20.0]]);+ run(&mut s, "sketch.chamfer.equal_distance", json!({"a": la, "b": lb, "distance": 4}));+ assert!(solved(&s));+ let ((_, a1), (b0, _)) = (ends(&s, &la), ends(&s, &lb));+ assert!(near(a1, 16.0, 0.0) && near(b0, 20.0, 4.0), "{a1:?} {b0:?}");+ run(&mut s, "edit.undo", json!({}));+ assert_eq!(sk(&s).curves.len(), 2, "undo brings back the two separate lines");++ // Refused: parallel lines, an end joined to a third line, a dimensioned length.+ let mut s = new_sketch();+ let (pa, pb) = lines(&mut s, [[0.0, 0.0], [10.0, 0.0]], [[0.0, 5.0], [10.0, 5.0]]);+ let (la, lb) = lines(&mut s, [[0.0, 20.0], [10.0, 20.0]], [[20.0, 25.0], [20.0, 40.0]]);+ run(&mut s, "sketch.line", json!({"points": [format!("{la}.end"), [10, 30]], "connect": false}));+ let (da, db) = lines(&mut s, [[0.0, 50.0], [10.0, 50.0]], [[20.0, 55.0], [20.0, 70.0]]);+ run(&mut s, "sketch.dimension", json!({"entities": [da], "value": 10}));+ let before = sk(&s);+ for (a, b, why) in [(&pa, &pb, "parallel"), (&la, &lb, "held by other geometry"), (&da, &db, "length is held")] {+ let e = s.execute("sketch.fillet", &json!({"a": a, "b": b, "radius": 2})).unwrap_err().to_string();+ assert!(e.contains(why), "{a}, {b}: {e}");+ }+ assert_eq!(sk(&s), before, "refusals change nothing");+}diff --git a/crates/engine/src/cmd/sketch_modify.rs b/crates/engine/src/cmd/sketch_modify.rsindex 77e7a6a72a..b041cb8681 100644--- a/crates/engine/src/cmd/sketch_modify.rs+++ b/crates/engine/src/cmd/sketch_modify.rs@@ -24,22 +24,22 @@ pub static COMMANDS: &[CommandSpec] = &[.at("SKETCH", "MODIFY").icon("sketch_fillet").enabled(in_sketch)- .params(concat!("point: corner shared by two curves (\"l1.end\") | points: [corners] (all rounded alike: one radius, the rest equal; a corner is a point reference or {a, b}, the two curves meeting there) | a, b: two lines or arcs meeting at a corner; radius: expr. A corner that dimensions or constraints measure to stays as a virtual sharp corner on both curves' extensions (`virtual_corners`; the moved ones in `moved_to_virtual_corner`)", created_doc!())),+ .params(concat!("point: corner shared by two curves (\"l1.end\") | points: [corners] (all rounded alike: one radius, the rest equal; a corner is a point reference or {a, b}, the two curves meeting there) | a, b: two lines or arcs meeting at a corner (two lines that share no end are first brought to where they meet: the nearer end of a line that stops short moves there, and a line that runs past is cut there, losing its shorter piece); radius: expr. A corner that dimensions or constraints measure to stays as a virtual sharp corner on both curves' extensions (`virtual_corners`; the moved ones in `moved_to_virtual_corner`)", created_doc!())),CommandSpec::new("sketch.chamfer.equal_distance", "Equal Distance Chamfer", chamfer_equal).at("SKETCH", "MODIFY").icon("sketch_chamfer").enabled(in_sketch)- .params(concat!("point | a, b: a corner of two lines; distance", created_doc!())),+ .params(concat!("point | a, b: a corner of two lines (two that share no end are first brought to where they meet, as in sketch.fillet); distance", created_doc!())),CommandSpec::new("sketch.chamfer.distance_angle", "Distance and Angle Chamfer", chamfer_dist_angle).at("SKETCH", "MODIFY").icon("sketch_chamfer_angle").enabled(in_sketch)- .params(concat!("point | a, b: a corner of two lines; distance: along a, angle: deg from a", created_doc!())),+ .params(concat!("point | a, b: a corner of two lines (two that share no end are first brought to where they meet, as in sketch.fillet); distance: along a, angle: deg from a", created_doc!())),CommandSpec::new("sketch.chamfer.two_distance", "Two Distance Chamfer", chamfer_two).at("SKETCH", "MODIFY").icon("sketch_chamfer_two_distances").enabled(in_sketch)- .params(concat!("point | a, b: a corner of two lines; distance: along a, distance2: along b", created_doc!())),+ .params(concat!("point | a, b: a corner of two lines (two that share no end are first brought to where they meet, as in sketch.fillet); distance: along a, distance2: along b", created_doc!())),CommandSpec::new("sketch.offset", "Offset", offset).at("SKETCH", "MODIFY").icon("offset")@@ -1469,6 +1469,70 @@ fn refuse_offset_corner(sk: &Sketch, p: &Value, cmd: &str) -> Result<()> {}}+/// Two lines given as `a` and `b` that share no end are brought to where they meet, so they+/// make a corner to round or cut (#6912, #7759, #7985). Each line's end nearer that point moves+/// onto it: a line that stops short is extended, and one that runs past the other is cut there,+/// losing its shorter piece. Refused when the lines are parallel or meet too far away, and when+/// an end that would move is held by anything else (another curve, a constraint or dimension on+/// it, a fixed or driven position) or its line has a size of its own (a length, an equal or a+/// midpoint), which moving the end would change.+fn meet_lines(sk: &mut Sketch, p: &Value, cmd: &str) -> Result<()> {+ if str_(p, "point").is_some() {+ return Ok(());+ }+ let line = |k: &str| str_(p, k).and_then(|i| sk.curve_index(i));+ let (Some(la), Some(lb)) = (line("a"), line("b")) else { return Ok(()) };+ let ends = |l: usize| match sk.curves.get(l).map(|c| &c.kind) {+ Some(CurveKind::Line { a, b }) => Some((*a, *b)),+ _ => None,+ };+ let (Some((a0, a1)), Some((b0, b1))) = (ends(la), ends(lb)) else { return Ok(()) };+ if la == lb || [a0, a1].iter().any(|x| *x == b0 || *x == b1) {+ return Ok(());+ }+ let (Some(p0), Some(p1), Some(q0), Some(q1)) = (sk.point(a0), sk.point(a1), sk.point(b0), sk.point(b1)) else { return Ok(()) };+ let (da, db) = (p1 - p0, q1 - q0);+ let den = da.cross(db);+ if den.abs() <= 1e-9 * da.len() * db.len() {+ return Err(bad(cmd, "the lines are parallel"));+ }+ // Where they meet, as a fraction along each line from its first end.+ let (t, u) = ((q0 - p0).cross(db) / den, (q0 - p0).cross(da) / den);+ let meet = p0 + da * t;+ if !(meet.is_finite() && meet.len() < 1e7) {+ return Err(bad(cmd, "the lines meet too far away"));+ }+ // The end on the shorter side of the meeting point moves (the nearer end, for a line that+ // stops short of it).+ let (na, nb) = (if t >= 0.5 { a1 } else { a0 }, if u >= 0.5 { b1 } else { b0 });+ for (l, e) in [(la, na), (lb, nb)] {+ let id = sk.curves.get(l).map(|c| c.id.clone()).unwrap_or_default();+ let other_curve = sk.curves.iter().enumerate().any(|(i, c)| i != l && c.kind.uses(e));+ let positioned = sk.points.get(e).is_some_and(|q| q.fixed || q.exprs.is_some() || q.link.is_some());+ if e == 0 || other_curve || positioned || sk.is_linked_curve(l) || sk.constraints.iter().any(|k| k.kind.refers_to_point(e)) {+ return Err(bad(cmd, format!("`{id}` doesn't reach the other line, and its end that would move there is held by other geometry")));+ }+ let sized = sk.constraints.iter().any(|k| {+ matches!(k.kind, ConstraintKind::Length { l: x, .. } | ConstraintKind::Midpoint { l: x, .. } if x == l)+ || matches!(k.kind, ConstraintKind::Equal { a, b } if a == l || b == l)+ });+ if sized {+ return Err(bad(cmd, format!("`{id}` doesn't reach the other line, and its length is held: extending or cutting it would change that")));+ }+ }+ for e in [na, nb] {+ if let Some(q) = sk.points.get_mut(e) {+ q.pos = meet;+ }+ }+ // One corner point: b's moved end becomes a's.+ for c in sk.curves.iter_mut() {+ c.kind.map_points(&|x| if x == nb { na } else { x });+ }+ sk.remove_points(&[nb]);+ Ok(())+}+/// A corner of two lines: (line a, line b, corner point, far end of a, far end of b).fn corner(sk: &Sketch, p: &Value, cmd: &str) -> Result<(usize, usize, usize, usize, usize)> {let ends = |l: usize| match sk.curves.get(l).map(|c| &c.kind) {@@ -1700,6 +1764,7 @@ fn fillet_corner(sk: &mut Sketch, p: &Value, r: f64, cmd: &str) -> Result<(usizerefuse_infinite_corner(sk, p, cmd)?;refuse_offset_corner(sk, p, cmd)?;weld_corner(sk, p);+ meet_lines(sk, p, cmd)?;let (la, lb, c, fa, fb) = match corner(sk, p, cmd) {Ok(x) => x,// Not two lines: a corner with an arc.@@ -1831,6 +1896,7 @@ fn chamfer(s: &mut Session, p: &Value, cmd: &str, dists: impl FnOnce(f64) -> Resrefuse_infinite_corner(sk, p, cmd)?;refuse_offset_corner(sk, p, cmd)?;weld_corner(sk, p);+ meet_lines(sk, p, cmd)?;let (la, lb, c, fa, fb) = corner(sk, p, cmd)?;let (pc, pa, pb) = (sk.point(c).unwrap_or_default(), sk.point(fa).unwrap_or_default(), sk.point(fb).unwrap_or_default());let (u, v) = ((pa - pc).normalized().unwrap_or(Vec2::X), (pb - pc).normalized().unwrap_or(Vec2::Y));