Sketch: the Midpoint Line tool shows the length and angle while drawing (#6198) (#6401)

* claim: #6198

* Sketch: show the Length/Angle readout while drawing a Midpoint Line (#6198)
This commit is contained in:
Homero Thompson
2026-10-26 11:14:22 +00:00
committed by GitHub
parent 19c8eb846e
commit bd38bc40be
4 changed files with 92 additions and 10 deletions
+3 -3
View File
@@ -11,7 +11,7 @@ pub(super) fn draw(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "sketch.draw.dimensioned";
let create = str_(p, "command").ok_or_else(|| bad(cmd, "`command` must name a supported sketch creator"))?;
let roles: &[&str] = match create {
"sketch.line" => &["length", "angle"],
"sketch.line" | "sketch.line.midpoint" => &["length", "angle"],
"sketch.rectangle.two_point" | "sketch.rectangle.center" | "sketch.rectangle.three_point" | "sketch.rectangle.rounded" => {
&["width", "height"]
}
@@ -87,8 +87,8 @@ pub(super) fn draw(s: &mut Session, p: &Value) -> Result<Value> {
let mut added = serde_json::Map::new();
for (role, value) in dimensions {
let (entities, ty) = match (create, role.as_str()) {
("sketch.line", "length") => (vec![curve(0)?], "length"),
("sketch.line", "angle") => (vec![curve(0)?], "angle"),
("sketch.line" | "sketch.line.midpoint", "length") => (vec![curve(0)?], "length"),
("sketch.line" | "sketch.line.midpoint", "angle") => (vec![curve(0)?], "angle"),
// A rounded rectangle's sides stop at the arcs: its size is between opposite sides.
("sketch.rectangle.rounded", "width" | "height") => {
let [a, b] = if role == "width" { ["left", "right"] } else { ["bottom", "top"] };
@@ -182,6 +182,26 @@ fn invalid_dimensioned_shapes_are_atomic_including_late_constraint_failure() {
assert!(s.undo.len() >= history);
}
#[test]
fn a_dimensioned_midpoint_line_carries_its_length_and_angle() {
let mut s = session();
let out = draw(&mut s, "sketch.line.midpoint", json!({"mid":[0,0],"end":[10,0]}), json!({"length":20,"angle":0}));
assert_eq!(out["curves"].as_array().map(|c| c.len()), Some(1), "{out}");
close(measure(&s, |k| matches!(k, ConstraintKind::Length { .. })), 20.0);
close(measure(&s, |k| matches!(k, ConstraintKind::LineAngle { .. })), 0.0);
let sk = sketch(&s);
let line = sk.curve_index(out["curves"][0].as_str().unwrap()).unwrap();
let solidcraft_sketch::CurveKind::Line { a, b } = sk.curves[line].kind else { panic!("a line") };
// The clicked middle is the line's middle.
close((sk.points[a].pos.x + sk.points[b].pos.x) * 0.5, 0.0);
close((sk.points[a].pos.y + sk.points[b].pos.y) * 0.5, 0.0);
assert!(
sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::Midpoint { l, .. } if l == line)),
"the middle is constrained: {:?}",
sk.constraints
);
}
#[test]
fn dimensioned_authoring_uses_selected_occurrence_and_keeps_local_geometry() {
let mut s = Session::default();
+22 -7
View File
@@ -37,7 +37,7 @@ pub fn boxes_for(cmd: &str, n: usize) -> Vec<DimBox> {
use ValueKind::*;
let b = DimBox::new;
match (cmd, n) {
("sketch.line", n) if n >= 1 => vec![b("Length", Length), b("Angle", Angle)],
("sketch.line" | "sketch.line.midpoint", n) if n >= 1 => vec![b("Length", Length), b("Angle", Angle)],
("sketch.rectangle.two_point" | "sketch.rectangle.center", 1) => vec![b("Width", Length), b("Height", Length)],
("sketch.rectangle.rounded", 1) => vec![b("Width", Length), b("Height", Length), b("Radius", Length)],
("sketch.circle.center", 1) => vec![b("Diameter", Length)],
@@ -79,6 +79,14 @@ pub fn effective(app: &SolveApp, t: &Tool, h: Vec2) -> Vec2 {
let ang = lk("Angle").unwrap_or_else(|| snap_axis(v.angle()));
a + Vec2::from_angle(ang) * len
}
// A midpoint line runs from the end's mirror image through the midpoint to the end, so
// the midpoint is the middle of the line: a typed length is half of it on each side.
("sketch.line.midpoint", 1) => {
let v = h - a;
let half = lk("Length").map_or(v.len(), |length| length * 0.5);
let ang = lk("Angle").unwrap_or_else(|| v.angle());
a + Vec2::from_angle(ang) * half
}
("sketch.rectangle.two_point" | "sketch.rectangle.rounded", 1) => {
let d = h - a;
Vec2::new(a.x + lk("Width").map_or(d.x, |w| w * sign(d.x)), a.y + lk("Height").map_or(d.y, |hh| hh * sign(d.y)))
@@ -203,6 +211,15 @@ pub fn axis_aligned(a: Vec2, b: Vec2) -> Option<bool> {
}
}
/// The Length and Angle boxes of a line from `a` to `e`: the length at the segment's middle,
/// the angle along it, both a little off the line so they don't cover it.
fn line_boxes(a: Vec2, e: Vec2, px: f64, off: f64) -> Vec<(f64, Vec2)> {
let v = e - a;
let d = dir_or_x(v);
let n = d.perp();
vec![(v.len(), (a + e) * 0.5 + n * off), (v.angle(), a + d * (v.len() * 0.3).min(60.0 * px) - n * off)]
}
/// The value each box shows while it follows the cursor, and where it sits (sketch coordinates).
/// `px` is the size of a screen pixel in sketch units: boxes sit a little off the segment they
/// measure so they don't cover it.
@@ -211,12 +228,10 @@ fn live(app: &SolveApp, t: &Tool, e: Vec2, px: f64) -> Vec<(f64, Vec2)> {
let first = t.pts.first().map(|p| p.0).unwrap_or(a);
let off = 20.0 * px;
match (t.cmd, t.pts.len()) {
("sketch.line", _) | ("sketch.slot.center_to_center" | "sketch.slot.overall", 1) => {
let v = e - a;
let d = dir_or_x(v);
let n = d.perp();
vec![(v.len(), (a + e) * 0.5 + n * off), (v.angle(), a + d * (v.len() * 0.3).min(60.0 * px) - n * off)]
}
("sketch.line", _) | ("sketch.slot.center_to_center" | "sketch.slot.overall", 1) => line_boxes(a, e, px, off),
// The midpoint line runs from the end's mirror image through the midpoint to the end,
// so its length is the whole line: twice the distance from the midpoint to the cursor.
("sketch.line.midpoint", 1) => line_boxes(a * 2.0 - e, e, px, off),
("sketch.slot.center_point", 1) => vec![(2.0 * (e - a).len(), (a + e) * 0.5 + dir_or_x(e - a).perp() * off)],
("sketch.rectangle.two_point", 1) => {
let (sx, sy) = (sign(e.x - a.x), sign(e.y - a.y));
+47
View File
@@ -365,6 +365,53 @@ fn a_line_end_on_a_circle_outside_the_axis_snap_stays_at_the_cursor() {
assert!((end.len() - 30.0).abs() < 1e-9 && end.dist(at) < 0.5, "{end:?}");
}
/// #6198: the Midpoint Line tool shows the same Length/Angle readout as the Line tool while
/// the line is being drawn.
#[test]
fn a_midpoint_line_shows_its_length_and_angle_while_it_is_drawn() {
let mut h = Harness::new();
h.app.run("sketch.create", json!({"plane": "XY"})).unwrap();
h.step(&json!({"start": "sketch.line.midpoint"})).unwrap();
h.step(&json!({"click": {"world": [0, 0, 0]}})).unwrap();
h.step(&json!({"move": {"world": [10, 0, 0]}})).unwrap();
let tool = h.app.tool.as_ref().unwrap();
assert_eq!(tool.dims.iter().map(|d| d.label).collect::<Vec<_>>(), ["Length", "Angle"], "the midpoint line shows the line tool's boxes");
// The line runs from the cursor's mirror image through the clicked middle to the
// cursor: its length is twice the middle-to-cursor distance.
let length: f64 = tool.dims[0].value.parse().unwrap_or_default();
assert!((length - 20.0).abs() < 0.01, "the whole line's length: {}", tool.dims[0].value);
let angle: f64 = tool.dims[1].value.trim_end_matches(" deg").parse().unwrap_or_default();
assert!(angle.abs() < 0.1, "the line's angle: {}", tool.dims[1].value);
}
/// #6198: a typed length and angle drive the midpoint line that is drawn.
#[test]
fn a_midpoint_line_takes_a_typed_length_and_angle() {
use solidcraft_engine::sketch::ConstraintKind;
let mut h = Harness::new();
h.app.run("sketch.create", json!({"plane": "XY"})).unwrap();
h.step(&json!({"start": "sketch.line.midpoint"})).unwrap();
h.step(&json!({"click": {"world": [0, 0, 0]}})).unwrap();
h.step(&json!({"move": {"world": [10, 0, 0]}})).unwrap();
type_value(&mut h, "20");
tab(&mut h, false);
type_value(&mut h, "45 deg");
h.step(&json!({"key": "Enter"})).unwrap();
let state = h.app.session.world_state();
let sk = &state.sketch(h.app.session.active_sketch.unwrap()).unwrap().sketch;
assert_eq!(sk.curves.len(), 1, "{:?}", h.app.status);
assert!(
sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::Length { value, .. } if (value - 20.0).abs() < 1e-9)),
"the typed length is a driving dimension: {:?}",
sk.constraints
);
assert!(
sk.constraints.iter().any(|c| matches!(c.kind, ConstraintKind::LineAngle { value, .. } if (value - 45f64.to_radians()).abs() < 1e-9)),
"the typed angle is a driving dimension: {:?}",
sk.constraints
);
}
/// A value box set off the cursor stays in the drawing area (#2201): past an edge it goes to the
/// cursor's other side, still clear of it; with no room either side it is pushed inside.
#[test]