Sketch text: a text on a path follows the path in the same evaluation (#6215)
* Test: a text on a path follows the path when a parameter moves it A construction line placed by a dimension that reads a parameter, and a text laid along it: after the parameter changes the line has moved and the text has not. It follows one evaluation late. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PsiqyXWBFGTCX9J51bhysc * Sketch text: a text on a path follows the path in the same evaluation Problem: texts are laid out with the sketch's links, before the sketch is solved. A solve that moves the curve a text runs along (a path placed by a parameter) left the text where the path had been before the change, so it followed one evaluation late. Solution: after a solve that keeps the sketch's orientation, every text that runs along a curve of its sketch is laid out again along that curve as the solve left it; a text that no longer lays out adds its warning. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PsiqyXWBFGTCX9J51bhysc --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -6714,6 +6714,9 @@ fn eval_feature(doc: &Document, vals: &BTreeMap<String, Value>, 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("; "));
|
||||
|
||||
@@ -572,6 +572,32 @@ pub fn refresh_links(doc: &Document, vals: &BTreeMap<String, Value>, 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<String> {
|
||||
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<String, Value>,
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user