SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Prevent angular drags from collapsing construction geometry (#8148) (#8160)
ermanyop-stacka5f472c23ec0
diff --git a/crates/engine/tests/sketch_drag_target.rs b/crates/engine/tests/sketch_drag_target.rsindex 0daef68b00..f5a0212d6a 100644--- a/crates/engine/tests/sketch_drag_target.rs+++ b/crates/engine/tests/sketch_drag_target.rs@@ -5,6 +5,30 @@ mod four_bar;use serde_json::json;use solidcraft_geom::Vec2;+#[test]+fn angular_drag_keeps_the_horizontal_construction_line() {+ for live in [false, true] {+ let mut s = solidcraft_engine::Session::default();+ for (id, params) in [+ ("sketch.create", json!({"plane":"XY"})),+ ("sketch.line", json!({"points":[[0,0],[20,0]],"ids":["reference"],"construction":true})),+ ("sketch.constraint.horizontal_vertical", json!({"line":"reference"})),+ ("sketch.line", json!({"points":["reference.start",[10,10]],"ids":["arm"],"infer":false})),+ ("sketch.dimension", json!({"entities":["reference","arm"],"value":45})),+ ("sketch.drag", json!({"entity":"arm.end","from":[10,10],"to":[10,10.5],"live":live})),+ ] {+ s.execute(id, ¶ms).unwrap();+ }+ if live {+ s.execute("sketch.drag_end", &json!({})).unwrap();+ }+ let point = |id| four_bar::point(&s, id).unwrap();+ assert!(point("reference.start").dist(point("reference.end")) > 1.0, "the construction reference must remain visible");+ let end = point("arm.end");+ assert!((end.x - end.y).abs() < 1e-5, "the arm must retain its 45 degree angle: {end:?}");+ }+}+fn drag(s: &mut solidcraft_engine::Session, target: Vec2) {let from = four_bar::point(s, "crank.end").unwrap();s.execute("sketch.drag", &json!({"entity":"crank.end","from":[from.x,from.y],"to":[target.x,target.y],"live":true})).unwrap();diff --git a/crates/sketch/src/least_motion_tests.rs b/crates/sketch/src/least_motion_tests.rsindex f009300a76..875c7cd712 100644--- a/crates/sketch/src/least_motion_tests.rs+++ b/crates/sketch/src/least_motion_tests.rs@@ -8,6 +8,44 @@ fn v(x: f64, y: f64) -> Vec2 {Vec2::new(x, y)}+#[test]+fn an_existing_short_angular_reference_still_solves() {+ let mut sk = Sketch::new();+ let reference = sk.add_line(v(0.0, 0.0), v(1e-7, 0.0), Some(0), None, None).unwrap();+ let line = sk.add_line(v(0.0, 0.0), v(10.0, 10.0), Some(0), None, None).unwrap();+ sk.curves[reference].construction = true;+ sk.add_constraint(ConstraintKind::Horizontal { l: reference }, None).unwrap();+ sk.add_constraint(ConstraintKind::Angle { a: reference, b: line, value: std::f64::consts::FRAC_PI_4, flip: false }, None).unwrap();+ let report = crate::solve(&mut sk);+ assert!(report.ok(), "an unchanged short reference is not a new collapse: {report:?}");+}++#[test]+fn dragging_an_angular_constrained_line_keeps_its_construction_reference() {+ for target in [v(11.0, 11.0), v(10.0, 20.0), v(-10.0, 20.0), v(-10.0, -10.0), v(10.0, -10.0)] {+ let mut sk = Sketch::new();+ let reference = sk.add_line(v(0.0, 0.0), v(20.0, 0.0), Some(0), None, None).unwrap();+ sk.curves[reference].construction = true;+ let line = sk.add_line(v(0.0, 0.0), v(10.0, 10.0), Some(0), None, None).unwrap();+ sk.add_constraint(ConstraintKind::Horizontal { l: reference }, None).unwrap();+ sk.add_constraint(ConstraintKind::Angle { a: reference, b: line, value: std::f64::consts::FRAC_PI_4, flip: false }, None).unwrap();+ assert!(crate::solve(&mut sk).ok());+ let crate::CurveKind::Line { b: endpoint, .. } = sk.curves[line].kind else { panic!("line") };+ for step in 1..=20 {+ let before: Vec<_> = sk.points.iter().map(|p| p.pos).collect();+ sk.points[endpoint].pos = v(10.0, 10.0) + (target - v(10.0, 10.0)) * (step as f64 / 20.0);+ let report = solve_with(&mut sk, &Hold { points: vec![endpoint], radii: vec![] }, Some(&before), Analysis::StatusOnly, None);+ if target == v(11.0, 11.0) {+ assert!(report.ok(), "a compatible drag must still solve: {report:?}");+ }+ if report.ok() {+ let crate::CurveKind::Line { a, b } = sk.curves[reference].kind else { panic!("line") };+ assert!(sk.points[a].pos.dist(sk.points[b].pos) > 1e-4, "construction line collapsed toward {target:?} at step {step}: {report:?}");+ }+ }+ }+}+/// A rectangle with a divider whose ends are held on the bottom and top sides (point on curve)./// Dragging the top-left corner tilts the top side: the divider's top end slides along with it,/// the far corner stays.diff --git a/crates/sketch/src/solver.rs b/crates/sketch/src/solver.rsindex df858ea427..3e3ab93540 100644--- a/crates/sketch/src/solver.rs+++ b/crates/sketch/src/solver.rs@@ -2901,6 +2901,32 @@ fn solve_inner(let now = State { sk, lay: &lay, x: &x };for (b, _) in &sub {let (Block::User(ci, k) | Block::TangentAt(ci, k, _)) = b else { continue };+ // An angle requires two directions. A nearly collapsed line can satisfy its+ // horizontal/vertical residual within tolerance while pointing anywhere, so+ // the angle alone must not turn that degenerate pose into a solved sketch.+ // Compare against the input length, so an already short reference still solves.+ let angular_lines = match **k {+ ConstraintKind::Angle { a, b, .. } => Some([a, b]),+ ConstraintKind::LineAngle { l, .. } => Some([l, l]),+ _ => None,+ };+ if angular_lines.is_some_and(|lines| {+ lines.iter().any(|&l| {+ let (a, b) = now.line(l);+ let original = match sk.curves.get(l).map(|c| &c.kind) {+ Some(CurveKind::Line { a, b }) => sk.point(*a).zip(sk.point(*b)).map_or(1.0, |(a, b)| a.dist(b)),+ _ => 1.0,+ };+ a.dist(b) < ACCEPT * original+ })+ }) {+ if let Some(c) = sk.constraints.get(*ci)+ && !failing.contains(&c.id)+ {+ failing.push(c.id.clone());+ }+ degenerate = true;+ }let ConstraintKind::Tangent { a: ta, b: tb } = **k else { continue };let Some(c) = sk.constraints.get(*ci) else { continue };let mut touch_free = Vec::new();