diff --git a/crates/doc/src/eval.rs b/crates/doc/src/eval.rs index 783c22fa2..bcfdea4d2 100644 --- a/crates/doc/src/eval.rs +++ b/crates/doc/src/eval.rs @@ -6714,6 +6714,9 @@ fn eval_feature(doc: &Document, vals: &BTreeMap, f: &Feature, st: let report = doc.solve_sketch(vals, &mut sk, &stored, Some(sketch))?; if !report.keeps_orientation() { warns.insert(0, report.failure(|f| format!("the sketch constraints conflict ({f})"))); + } else { + // The solve may have moved the curve a text runs along. + warns.extend(crate::project::relay_path_texts(&mut sk)); } if !warns.is_empty() { *warning = Some(warns.join("; ")); diff --git a/crates/doc/src/project.rs b/crates/doc/src/project.rs index a71f4a4d5..02d258690 100644 --- a/crates/doc/src/project.rs +++ b/crates/doc/src/project.rs @@ -572,6 +572,32 @@ pub fn refresh_links(doc: &Document, vals: &BTreeMap, st: &ModelS refresh_links_in(doc, vals, st, plane, sk, None) } +/// Lay every text that runs along a curve of its sketch along that curve as the solve left it. +/// The texts are laid out with the other links, before the solve, which can move their path (a +/// path dimensioned from a parameter): without this a text followed its path one evaluation +/// late. The warnings for texts that no longer lay out. +pub(crate) fn relay_path_texts(sk: &mut Sketch) -> Vec { + let texts: Vec<(String, LinkSource)> = sk + .links + .iter() + .filter(|l| !l.lost && matches!(&l.source, LinkSource::Text { style, .. } if style.path.is_some())) + .map(|l| (l.id.clone(), l.source.clone())) + .collect(); + let mut warn = Vec::new(); + for (id, source) in texts { + match sk.text_source_geometry(&source) { + Ok(geom) if geom.is_empty() => warn.push(format!("the {} makes no outline", source.describe())), + Ok(geom) => { + if let Err(e) = sk.update_link(&id, &geom) { + warn.push(format!("{}: {e}", source.describe())); + } + } + Err(e) => warn.push(format!("{}: {e}", source.describe())), + } + } + warn +} + fn refresh_links_in( doc: &Document, vals: &BTreeMap, diff --git a/crates/engine/src/cmd/sketch_text_move_tests.rs b/crates/engine/src/cmd/sketch_text_move_tests.rs index 2fa47a9df..188cb73fa 100644 --- a/crates/engine/src/cmd/sketch_text_move_tests.rs +++ b/crates/engine/src/cmd/sketch_text_move_tests.rs @@ -189,6 +189,36 @@ fn text_on_a_path_follows_its_curve_when_the_curve_moves() { assert_eq!(solidcraft_doc::Document::from_json(&saved).unwrap().to_json(), saved); } +/// A path placed by a parameter: the text on it follows in the same evaluation. (Texts are laid +/// out with the sketch's links, before the solve that moves the path: the text came one +/// evaluation late, where the path had been before the change.) +#[test] +fn text_on_a_path_follows_the_path_when_a_parameter_moves_it() { + let mut s = Session::default(); + run(&mut s, "parameters.add", json!({"name":"W","expression":"20 mm"})); + run(&mut s, "sketch.create", json!({"plane":"XY"})); + let line = run(&mut s, "sketch.line", json!({"points":[[20,15],[80,15]],"construction":true}))["curves"][0].as_str().unwrap().to_string(); + let start = format!("{line}.start"); + run(&mut s, "sketch.constraint.horizontal_vertical", json!({"line":line,"mode":"horizontal"})); + run(&mut s, "sketch.dimension", json!({"entities":[start,"origin"],"type":"horizontal","value":"W"})); + run(&mut s, "sketch.dimension", json!({"entities":[start,"origin"],"type":"vertical","value":"15 mm"})); + run(&mut s, "sketch.dimension", json!({"entities":[line],"type":"length","value":"60 mm"})); + let link = + run(&mut s, "sketch.text", json!({"text":"AB-12","at":[0,0],"height":8,"path":line,"align":"center"}))["link"].as_str().unwrap().to_string(); + run(&mut s, "sketch.finish", json!({})); + let middle = |s: &Session| { + let (lo, hi) = text_box(s, &link); + (lo.x + hi.x) / 2.0 + }; + let drawn = middle(&s); + assert!((drawn - 50.0).abs() < 1.0, "{drawn}"); + for w in [300.0, 20.0, 140.0] { + run(&mut s, "parameters.change", json!({"name":"W","expression":format!("{w} mm")})); + let moved = middle(&s) - drawn; + assert!((moved - (w - 20.0)).abs() < 1e-6, "W {w}: the text moved {moved}"); + } +} + #[test] fn text_style_parameters_are_checked() { let (mut s, link, _) = setup();