SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch: a text rebuilt with other words keeps the far-move walk (#10357)
NOWN-DEV68105938e236
diff --git a/crates/doc/src/document.rs b/crates/doc/src/document.rsindex e41ceaeb17..160cc4b160 100644--- a/crates/doc/src/document.rs+++ b/crates/doc/src/document.rs@@ -4173,6 +4173,11 @@ impl Document {analysis: solidcraft_sketch::Analysis,) -> Result<solidcraft_sketch::SolveReport> {self.apply_dimension_values(vals, sk)?;+ // A text built anew since it was stored (an expression gave it other words) has other+ // outline entities: the stored sketch is taken with this sketch's, so the points still+ // pair up for the side checks and the walk below.+ let paired = moved_from.and_then(|from| sk.stored_with_rebuilt_texts(from));+ let moved_from = paired.as_ref().or(moved_from);let was = |sk: &Sketch| moved_from.and_then(|from| sk.positions_in(from));let offsets = !sk.offsets.is_empty() || !sk.freeform_offsets.is_empty();// Offsets always require another solve after refresh. Only that last report isdiff --git a/crates/engine/src/cmd/sketch_project_side_tests.rs b/crates/engine/src/cmd/sketch_project_side_tests.rsindex 9b43f1a21e..83295250a5 100644--- a/crates/engine/src/cmd/sketch_project_side_tests.rs+++ b/crates/engine/src/cmd/sketch_project_side_tests.rs@@ -86,6 +86,77 @@ fn a_distance_from_a_projected_line_keeps_its_side_when_the_source_moves_past()assert_p(&s, 15.0, "after an edit");}+/// A label's sketch: three points brought in from sketch `A` (a 20 long vertical line whose start+/// the parameters `x` and `y` place, and its middle), a construction rectangle 5 wide on them+/// (its near side on the line's ends, its far side 5 from the middle) and a centred text on the+/// far side whose words the text parameter `Label` gives. Other words are other outlines: the text+/// is built anew in the evaluated sketch, so its entities are not the stored sketch's. Moved 280+/// away afterwards (more than one jump can take: the solve walks there from the stored sketch), the+/// rectangle follows and keeps its side.+#[test]+fn a_label_follows_its_source_far_after_a_text_parameter_gave_it_other_words() {+ let mut s = Session::default();+ run(&mut s, "parameters.add", json!({"name": "x", "expression": "100 mm"}));+ run(&mut s, "parameters.add", json!({"name": "y", "expression": "200 mm"}));+ run(&mut s, "parameters.add", json!({"name": "Label", "expression": "A1", "unit": "text"}));+ run(&mut s, "sketch.create", json!({"plane": "XY", "name": "A"}));+ run(&mut s, "sketch.line", json!({"points": [[100, 200], [100, 180]], "ids": ["rail"]}));+ run(&mut s, "sketch.constraint.horizontal_vertical", json!({"line": "rail", "mode": "vertical"}));+ run(&mut s, "sketch.dimension", json!({"entities": ["origin", "rail.start"], "type": "horizontal", "value": "x"}));+ run(&mut s, "sketch.dimension", json!({"entities": ["origin", "rail.start"], "type": "vertical", "value": "y"}));+ run(&mut s, "sketch.dimension", json!({"entities": ["rail"], "value": "20 mm"}));+ run(&mut s, "sketch.point", json!({"point": [100, 190], "id": "mid"}));+ run(&mut s, "sketch.constraint.midpoint", json!({"point": "mid", "line": "rail"}));+ run(&mut s, "sketch.finish", json!({}));+ run(&mut s, "sketch.create", json!({"plane": "XY", "name": "S"}));+ let refs = ["rail.start", "rail.end", "mid"].map(|p| json!({"sketch": "A", "point": p}));+ let r = run(&mut s, "sketch.project", json!({"refs": refs}));+ let brought: Vec<String> = (0..3).map(|i| r["links"][i]["points"][0].as_str().unwrap().to_string()).collect();+ let near = 100.0 - 5.0;+ for (id, a, b) in [+ ("l1", [100.0, 200.0], [100.0, 180.0]),+ ("l2", [near, 200.0], [near, 180.0]),+ ("l3", [100.0, 200.0], [near, 200.0]),+ ("l4", [100.0, 180.0], [near, 180.0]),+ ] {+ run(&mut s, "sketch.line", json!({"points": [a, b], "ids": [id], "construction": true, "connect": false}));+ }+ for (a, b) in [+ ("l1.start", brought[0].as_str()),+ ("l1.end", &brought[1]),+ ("l3.start", "l1.start"),+ ("l3.end", "l2.start"),+ ("l4.start", "l1.end"),+ ("l4.end", "l2.end"),+ ] {+ run(&mut s, "sketch.constraint.coincident", json!({"a": a, "b": b}));+ }+ run(&mut s, "sketch.constraint.parallel", json!({"a": "l2", "b": "l1"}));+ run(&mut s, "sketch.dimension", json!({"entities": [brought[2], "l2"], "type": "distance", "value": "5 mm"}));+ run(&mut s, "sketch.constraint.perpendicular", json!({"a": "l3", "b": "l1"}));+ run(&mut s, "sketch.constraint.perpendicular", json!({"a": "l4", "b": "l1"}));+ run(&mut s, "sketch.text", json!({"text_expression": "Label + '-1'", "at": [0, 0], "height": 3, "align": "center", "path": "l2"}));+ run(&mut s, "sketch.finish", json!({}));+ let far_side = |s: &Session| {+ let st = s.model.state();+ let sk = &st.sketch(s.doc.find_feature("S").unwrap().id).unwrap().sketch;+ sk.point(sk.resolve_point("l2.start").unwrap()).unwrap()+ };+ #[track_caller]+ fn quiet(s: &Session, when: &str) {+ let feature = s.model.result(s.doc.find_feature("S").unwrap().id).unwrap();+ assert_eq!((&feature.error, &feature.warning), (&None, &None), "{when}");+ }+ quiet(&s, "drawn");+ run(&mut s, "parameters.change", json!({"name": "Label", "expression": "B2"}));+ quiet(&s, "relabelled");+ for (x, y) in [(300.0, 400.0), (100.0, 200.0)] {+ run(&mut s, "parameters.change", json!({"changes": {"x": format!("{x} mm"), "y": format!("{y} mm")}}));+ quiet(&s, &format!("moved to ({x}, {y})"));+ assert!(far_side(&s).dist(Vec2::new(x - 5.0, y)) < 1e-6, "({x}, {y}): {:?}", far_side(&s));+ }+}+/// The same from the other side: the point above the line, the source rising past it and/// coming back; and with the line brought in by Include 3D Geometry.#[test]diff --git a/crates/sketch/src/link.rs b/crates/sketch/src/link.rsindex 54bedf4840..0c3cd518ed 100644--- a/crates/sketch/src/link.rs+++ b/crates/sketch/src/link.rs@@ -1128,6 +1128,59 @@ impl Sketch {self.points.iter().zip(&was).any(|(p, w)| p.pos != *w).then_some(was)}+ /// `stored` as this sketch has its entities, when they differ only by texts built anew+ /// since (other words, from an expression, are other outlines with new points and curves):+ /// those texts' entities as this sketch has them, every other point where `stored` has it,+ /// every other circle and ellipse with its stored radius, arcs with their stored turn and+ /// constraints with their stored values. Paired so entity by entity, a solve after the links+ /// moved can walk from the stored state as it does when no text changed. `None` when the+ /// two are the same entities or differ in anything else.+ pub fn stored_with_rebuilt_texts(&self, stored: &Sketch) -> Option<Sketch> {+ let texts: Vec<&str> = self.links.iter().filter(|l| matches!(l.source, LinkSource::Text { .. })).map(|l| l.id.as_str()).collect();+ let in_text = |link: &Option<String>| link.as_deref().is_some_and(|l| texts.contains(&l));+ let same = self.points.len() == stored.points.len()+ && self.curves.len() == stored.curves.len()+ && self.points.iter().zip(&stored.points).all(|(p, q)| p.id == q.id)+ && self.curves.iter().zip(&stored.curves).all(|(c, d)| c.id == d.id);+ let ids = |sk: &Sketch| sk.links.iter().map(|l| l.id.clone()).collect::<Vec<_>>();+ if same+ || texts.is_empty()+ || ids(self) != ids(stored)+ || !self.points.iter().filter(|p| !in_text(&p.link)).map(|p| &p.id).eq(stored.points.iter().filter(|p| !in_text(&p.link)).map(|p| &p.id))+ || !self.curves.iter().filter(|c| !in_text(&c.link)).map(|c| &c.id).eq(stored.curves.iter().filter(|c| !in_text(&c.link)).map(|c| &c.id))+ || !self.constraints.iter().map(|c| &c.id).eq(stored.constraints.iter().map(|c| &c.id))+ {+ return None;+ }+ let mut out = self.clone();+ let points: std::collections::BTreeMap<&str, Vec2> =+ stored.points.iter().filter(|p| !in_text(&p.link)).map(|p| (p.id.as_str(), p.pos)).collect();+ for p in out.points.iter_mut().filter(|p| !in_text(&p.link)) {+ if let Some(pos) = points.get(p.id.as_str()) {+ p.pos = *pos;+ }+ }+ let curves: std::collections::BTreeMap<&str, &Curve> =+ stored.curves.iter().filter(|c| !in_text(&c.link)).map(|c| (c.id.as_str(), c)).collect();+ for c in out.curves.iter_mut().filter(|c| !in_text(&c.link)) {+ let Some(d) = curves.get(c.id.as_str()) else { continue };+ match (&mut c.kind, &d.kind) {+ (CurveKind::Circle { r, .. }, CurveKind::Circle { r: s, .. }) | (CurveKind::Ellipse { r, .. }, CurveKind::Ellipse { r: s, .. }) => {+ *r = *s+ }+ (CurveKind::Conic { rho, .. }, CurveKind::Conic { rho: s, .. }) => *rho = *s,+ _ => {}+ }+ c.reversed = d.reversed;+ }+ for (c, d) in out.constraints.iter_mut().zip(&stored.constraints) {+ if let Some(v) = d.kind.value() {+ c.kind.set_value(v);+ }+ }+ Some(out)+ }+fn drop_link_entities(&mut self, id: &str) {let curves = self.link_curves(id);let keep = self.links.clone();