From ea9ea7bba85cdf8a0bf46b1d02c1a71708967cd8 Mon Sep 17 00:00:00 2001 From: austin62jake Date: Sat, 31 Oct 2026 11:53:42 +0000 Subject: [PATCH] Sketch: a driven dimension nothing fixes any more takes a value again (#10018) (#10023) * Sketch: a driven dimension nothing fixes any more takes a value again (#10018) Co-Authored-By: Claude Opus 5.5 * Sketch: a driven dimension nothing fixes any more takes a value again (#10018) A dimension placed while a constraint fixed what it measures goes in driven, and stayed driven once that constraint was deleted: a double-click only said it has no value to edit. The double-click now tries making it driving on a scratch copy; when nothing else fixes it, the value box opens at the measured value, and Enter makes it driving (sketch.toggle_driven) at the typed value (parameters.change) in one undo step. A refused value goes back to a checkpoint, so it stays driven. While something still fixes it, it stays read-only as before. Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude --- crates/ui-egui/src/dim_view.rs | 120 +++++++++++++++--- .../ui-egui/src/dimension_create_ui_tests.rs | 76 +++++++++++ 2 files changed, 177 insertions(+), 19 deletions(-) diff --git a/crates/ui-egui/src/dim_view.rs b/crates/ui-egui/src/dim_view.rs index 93678d020..7494c1380 100644 --- a/crates/ui-egui/src/dim_view.rs +++ b/crates/ui-egui/src/dim_view.rs @@ -8,9 +8,10 @@ //! its dimension line), a double-click (or one click with the Dimension tool) edits its value //! in place with parameter completion. A finished sketch's shown dimensions (Show Dimensions, //! drawn by `crate::browser::dims_overlay`) take the same double-click from the model view: the -//! value is edited without editing the sketch. +//! value is edited without editing the sketch. A driven dimension that nothing else fixes any +//! more opens at its measured value too, and the value makes it driving. //! Everything acts through commands: `sketch.dimension_text`, `sketch.delete`, -//! `parameters.change`. +//! `sketch.toggle_driven`, `parameters.change`. use crate::ui_kit::{Container, Surface}; use std::cell::RefCell; @@ -62,6 +63,9 @@ struct Edit { /// A shown dimension of a finished sketch, edited from the model view without editing the /// sketch (its id). None: a dimension of the sketch being edited. sketch: Option, + /// A driven dimension that nothing else fixes any more, which the value makes driving (its + /// constraint id; `param` names the parameter it gets). + driven: Option, } #[derive(Clone, Copy, Debug)] @@ -154,8 +158,19 @@ pub fn begin_dimension(app: &mut SolveApp, entities: Vec, at: Vec2) -> R return Ok(()); } EDIT.with(|edit| { - *edit.borrow_mut() = - Some(Edit { param, expr, at: None, fresh: FRESH, wait: 10, draft: Some(draft), live: None, selection: None, picked: None, sketch: None }) + *edit.borrow_mut() = Some(Edit { + param, + expr, + at: None, + fresh: FRESH, + wait: 10, + draft: Some(draft), + live: None, + selection: None, + picked: None, + sketch: None, + driven: None, + }) }); PLACEMENT.with(|preview| *preview.borrow_mut() = None); Ok(()) @@ -175,8 +190,19 @@ pub fn begin_live(app: &mut SolveApp, entities: Vec, pointer: Option bool { selection: None, picked: None, sketch: Some(d.sketch), + driven: None, }) }); true @@ -477,11 +504,19 @@ fn open_editor(app: &mut SolveApp, id: Option) -> bool { let Some(d) = id.and_then(|id| DRAWN.with(|d| d.borrow().iter().find(|x| x.id == id).cloned())) else { return false; }; - let Some(param) = d.param else { - app.set_status("A driven dimension measures the sketch; it has no value to edit", false); - return true; + let (param, expr, driven) = match d.param { + Some(param) => { + let expr = app.session.doc.param(¶m).map(|p| p.expr.clone()).unwrap_or_default(); + (param, expr, None) + } + None => match freed(app, &d.id) { + Some((param, expr)) => (param, expr, Some(d.id.clone())), + None => { + app.set_status("A driven dimension measures the sketch; it has no value to edit", false); + return true; + } + }, }; - let expr = app.session.doc.param(¶m).map(|p| p.expr.clone()).unwrap_or_default(); EDIT.with(|e| { *e.borrow_mut() = Some(Edit { param, @@ -494,11 +529,63 @@ fn open_editor(app: &mut SolveApp, id: Option) -> bool { selection: None, picked: None, sketch: None, + driven, }) }); true } +/// A driven dimension that nothing else fixes any more, such as one placed while a Tangent +/// held its circle and left driven when the Tangent was deleted (#10018): the parameter it gets +/// as a driving one and its measured value, from making it driving on a scratch copy. None +/// while something still fixes what it measures (it can only measure). +fn freed(app: &SolveApp, constraint: &str) -> Option<(String, String)> { + let sketch = app.session.active_sketch?; + let mut scratch = app.session.scratch(); + scratch.execute("sketch.toggle_driven", &json!({"constraint": constraint, "driven": false})).ok()?; + let param = scratch.doc.sketch(sketch).ok()?.constraints.iter().find(|c| c.id == constraint)?.param.clone()?; + let expr = scratch.doc.param(¶m)?.expr.clone(); + Some((param, expr)) +} + +/// The value typed for a driven dimension (`freed`): it becomes driving at its measured value +/// (`sketch.toggle_driven`), then takes the value (`parameters.change`), in one undo step. When +/// the value is refused the design goes back to the checkpoint taken first, so it stays driven. +fn make_driving(app: &mut SolveApp, constraint: &str, typed: &str) -> Result { + let depth = app.session.undo.len(); + let checkpoint = app.run("edit.checkpoint", json!({}))?["checkpoint"].clone(); + let result = app + .run("sketch.toggle_driven", json!({"constraint": constraint, "driven": false})) + .map_err(|err| shown_error("sketch.toggle_driven", &err)) + .and_then(|_| { + let param = app + .session + .active_sketch + .and_then(|sketch| app.session.doc.sketch(sketch).ok()?.constraints.iter().find(|c| c.id == constraint)?.param.clone()) + .ok_or("The dimension value is unavailable")?; + change_value(app, ¶m, typed) + }); + let forget = result.is_ok(); + let _ = app.run("edit.restore_checkpoint", json!({"checkpoint": checkpoint, "forget": forget})); + if forget { + app.session.undo.truncate(depth + 1); + } + result +} + +/// Set dimension parameter `param` to the expression typed for it, in the parameter's unit. +fn change_value(app: &mut SolveApp, param: &str, typed: &str) -> Result { + let kind = app + .session + .doc + .param(param) + .and_then(|p| solidcraft_engine::doc::expr::unit_info(&p.unit)) + .map(|(k, _)| k) + .unwrap_or(solidcraft_engine::doc::expr::Kind::Unitless); + let expression = crate::length_input::expression(&app.session.doc, typed, kind); + app.run("parameters.change", json!({"name": param, "expression": expression})).map_err(|err| shown_error("parameters.change", &err)) +} + /// Start dragging the text of the dimension under `press` (true: a dimension took the drag). /// Its lines take it only off sketch geometry (`on_geometry`: a point or curve is under `press`). pub fn drag_start(app: &SolveApp, press: Pos2, press_sketch: Vec2, on_geometry: bool) -> bool { @@ -778,6 +865,7 @@ pub fn edit_param(app: &SolveApp, param: &str) { selection: None, picked: None, sketch: None, + driven: None, }) }); } @@ -946,16 +1034,10 @@ fn edit_box(app: &mut SolveApp, ctx: &egui::Context) { } else { Err("The sketch changed; place the dimension again".into()) } + } else if let Some(constraint) = &e.driven { + make_driving(app, constraint, &e.expr) } else { - let kind = app - .session - .doc - .param(&e.param) - .and_then(|p| solidcraft_engine::doc::expr::unit_info(&p.unit)) - .map(|(k, _)| k) - .unwrap_or(solidcraft_engine::doc::expr::Kind::Unitless); - let expression = crate::length_input::expression(&app.session.doc, &e.expr, kind); - app.run("parameters.change", json!({"name": e.param, "expression": expression})).map_err(|err| shown_error("parameters.change", &err)) + change_value(app, &e.param, &e.expr) }; match result { Err(err) => { diff --git a/crates/ui-egui/src/dimension_create_ui_tests.rs b/crates/ui-egui/src/dimension_create_ui_tests.rs index 7484dd234..310420f97 100644 --- a/crates/ui-egui/src/dimension_create_ui_tests.rs +++ b/crates/ui-egui/src/dimension_create_ui_tests.rs @@ -574,6 +574,82 @@ fn an_over_constraining_dimension_is_placed_as_a_driven_one() { assert_eq!(h.app.session.doc.to_json(), original); } +/// A dimension placed while a constraint fixed what it measures is driven; once that constraint +/// is deleted, a double-click opens its value, and the value makes it driving, in one undo step +/// (#10018). While the constraint holds it stays read-only, and a value refused leaves it driven. +#[test] +fn a_driven_dimension_nothing_fixes_any_more_takes_a_value() { + let mut h = line([20., 0.]); + h.app.run("sketch.dimension", json!({"entities":["l1"],"value":"20 mm","text_at":[10,6]})).unwrap(); + h.app.run("sketch.circle.center", json!({"center":[0,0],"radius":15})).unwrap(); + // The circle's centre is on the origin and the line's end on the circle: its size is fixed. + let coincident = h.app.run("sketch.constraint.coincident", json!({"a":"l1.end","b":"c1"})).unwrap()["constraint"].as_str().unwrap().to_owned(); + h.app.run("sketch.dimension", json!({"entities":["c1"],"driven":true,"text_at":[-14,14]})).unwrap(); + h.settle(); + let sketch = h.app.session.active_sketch.unwrap(); + let dim = |h: &Harness| { + let state = h.app.session.world_state(); + let sk = &state.sketch(sketch).unwrap().sketch; + sk.constraints + .iter() + .find(|c| { + matches!( + c.kind, + solidcraft_engine::sketch::ConstraintKind::Diameter { .. } | solidcraft_engine::sketch::ConstraintKind::Radius { .. } + ) + }) + .cloned() + .unwrap() + }; + let id = dim(&h).id; + assert!(dim(&h).driven && dim(&h).param.is_none(), "{:?}", dim(&h)); + let double_click = |h: &mut Harness| { + h.frames(40); + h.step(&json!({"click":{"dimension":id},"double":true})).unwrap(); + }; + + // The Coincident still fixes the circle: the dimension only measures. + let fixed = h.app.session.doc.to_json(); + double_click(&mut h); + assert!(!crate::dim_view::editing(), "a dimension the sketch determines has no value to edit"); + assert!(h.app.status.as_ref().is_some_and(|s| s.0.contains("driven")), "{:?}", h.app.status); + assert_eq!(h.app.session.doc.to_json(), fixed); + + // Without it, the value opens at what the dimension measures, named as the parameter it gets. + h.app.run("sketch.delete", json!({"entities":[coincident]})).unwrap(); + h.settle(); + let before = h.app.session.doc.to_json(); + let undo = h.app.session.undo.len(); + let name = h.app.session.doc.next_d_name(); + let measured = dim(&h).kind.value().unwrap(); + double_click(&mut h); + assert!(crate::dim_view::editing(), "{:?}", h.app.status); + assert!(h.widget(&format!("Dimension: {name}")).is_some(), "{}", h.call("ui.inspect", json!({}))); + + // A value the design refuses leaves it driven, and the design and its history as they were. + type_value(&mut h, "nosuch * 2"); + h.step(&json!({"key":"Enter"})).unwrap(); + assert!(crate::dim_view::editing(), "a refused value keeps the box open"); + assert_eq!(h.app.session.doc.to_json(), before); + assert_eq!(h.app.session.undo.len(), undo); + + // A value it takes makes it driving at that value. + type_value(&mut h, "30"); + h.step(&json!({"key":"Enter"})).unwrap(); + assert!(!crate::dim_view::editing(), "{:?}", h.app.status); + let driving = dim(&h); + assert!(!driving.driven && driving.param.as_deref() == Some(name.as_str()), "{driving:?}"); + let value = driving.kind.value().unwrap(); + assert!((value - 30.0).abs() < 1e-6 && (measured - 30.0).abs() > 1.0, "{measured} → {value}"); + assert!(h.app.session.world_state().sketch(sketch).unwrap().report.ok()); + + // One undo step puts the driven dimension back. + assert_eq!(h.app.session.undo.len(), undo + 1); + h.app.run("edit.undo", json!({})).unwrap(); + assert_eq!(h.app.session.doc.to_json(), before); + crate::dim_view::cancel(); +} + // ---- Type-first: D, click, type, Enter ---- /// A sketch with `setup` drawn, the Top view centred on `centre`, and the Dimension tool on.