A sketch line end on a curve within the axis snap lands where the preview shows (#1122)

A line end snapped onto a curve within the horizontal/vertical snap slides along the curve to where the axis crosses it; the preview, value boxes and click share one placement (#1121).
This commit is contained in:
Tobias Breckle authored and GitHub committed 2026-10-14 13:24:19 -04:00
1 parent aa2ace81af
commit ed943a74e6
3 files changed
+120 -8

No files matched your search

+49 -2
View File
@@ -107,6 +107,54 @@ pub fn effective(app: &SolveApp, t: &Tool, h: Vec2) -> Vec2 {
}
}
/// The point a click places for the hover `p` (the preview draws this one): typed values win
/// over the cursor and drop its snap; a line's free end snaps to horizontal/vertical, one on
/// sketch geometry stays there, and one on a curve slides along it to where the axis crosses.
pub fn placed(app: &SolveApp, t: &Tool, p: &(Vec2, Option<String>)) -> (Vec2, Option<String>) {
let line = t.cmd == "sketch.line";
if t.dims.iter().any(|b| b.locked) || (line && p.1.is_none()) {
return (effective(app, t, p.0), None);
}
if line && let Some(q) = on_curve_axis(app, t, p) {
return q;
}
p.clone()
}
/// Where the preview and the value boxes put the tool's next point for the hover `p`: for a
/// line, exactly what a click places.
pub fn shown(app: &SolveApp, t: &Tool, p: &(Vec2, Option<String>)) -> Vec2 {
if t.cmd == "sketch.line" { placed(app, t, p).0 } else { effective(app, t, p.0) }
}
/// A line end snapped onto a curve (`on:<curve>:x,y`) within the axis snap: the point where the
/// horizontal/vertical line from the start crosses that curve near the cursor (from a circle's
/// centre, its quadrant point), still on the curve. None: no axis snap there, or the axis misses
/// the curve close by (the curve snap wins as it is).
fn on_curve_axis(app: &SolveApp, t: &Tool, p: &(Vec2, Option<String>)) -> Option<(Vec2, Option<String>)> {
let a = t.pts.last()?.0;
let (curve, _) = p.1.as_deref()?.strip_prefix("on:")?.rsplit_once(':')?;
let v = p.0 - a;
let ang = snap_axis(v.angle());
if ang == v.angle() {
return None;
}
let st = app.session.world_state();
let sk = &st.sketch(app.session.active_sketch?)?.sketch;
let shape = sk.shape(sk.curve_index(curve)?)?;
let dir = Vec2::from_angle(ang);
let horizontal = dir.y.abs() < dir.x.abs();
let axis = solidcraft_engine::sketch::Shape::Line { a, b: a + dir };
let tol = 1e-9 * (1.0 + v.len());
let q = solidcraft_engine::sketch::intersections(&axis, &shape)
.into_iter()
// Exactly on the axis, so the command infers the horizontal/vertical constraint.
.map(|q| if horizontal { Vec2::new(q.x, a.y) } else { Vec2::new(a.x, q.y) })
.filter(|q| (*q - a).dot(dir) > tol && shape.dist(*q) <= tol && q.dist(p.0) <= v.len() * 0.1)
.min_by(|x, y| x.dist(p.0).total_cmp(&y.dist(p.0)))?;
Some((q, Some(format!("on:{curve}:{},{}", q.x, q.y))))
}
/// Angles within a few degrees of horizontal or vertical snap to it (the line is drawn
/// horizontal or vertical, and the command infers the constraint).
pub fn snap_axis(ang: f64) -> f64 {
@@ -325,8 +373,7 @@ fn boxes(app: &SolveApp, ui: &mut egui::Ui, proj: &Proj, t: &mut Tool) -> Option
let sid = app.session.active_sketch?;
let st = app.session.world_state();
let plane = st.sketch(sid)?.plane;
let h = t.hover.as_ref().map(|x| x.0)?;
let e = effective(app, t, h);
let e = shown(app, t, t.hover.as_ref()?);
let px = 2.0 * app.cam.half_height() / f64::from(proj.rect.height().max(1.0));
let lv = live(app, t, e, px);
let focus = std::mem::take(&mut t.dims_focus);