SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
sketch.edit_text: batch several text edits into one rebuild (#2793)
NOWN-DEV68c72082030a
diff --git a/crates/engine/src/cmd/sketch_create.rs b/crates/engine/src/cmd/sketch_create.rsindex 0fb17162bb..0a080a7f25 100644--- a/crates/engine/src/cmd/sketch_create.rs+++ b/crates/engine/src/cmd/sketch_create.rs@@ -112,7 +112,8 @@ pub static COMMANDS: &[CommandSpec] = &[.params(concat!("link: the text's link id (or entity: one of its curves), text?, at?, height?, angle?: deg (numbers or expressions as in sketch.text; a number replaces an expression, and so does moving the text), font?: family or file path (empty: the built-in font), ",text_style_doc!(),- " (each kept when left out); live?: bool: one frame of an interactive change (the sketch is solved, the features after it stay as they were until `sketch.drag_end`)",+ " (each kept when left out); or edits: [{link or entity, text?, at?, …}]: several texts changed together, the design rebuilt once \+ (all or nothing; returns edits: each one's result); live?: bool: one frame of an interactive change (the sketch is solved, the features after it stay as they were until `sketch.drag_end`)",created_doc!())),];@@ -344,6 +345,9 @@ pub(super) fn move_text(sk: &mut Sketch, link: &str, translate: Vec2, rotation:Ok(sk.link_curves(&link))}+/// The most texts one `sketch.edit_text {edits}` changes.+const MAX_TEXT_EDITS: usize = 1000;+fn edit_text(s: &mut Session, p: &Value) -> Result<Value> {let cmd = "sketch.edit_text";let id = s.active_sketch.ok_or_else(|| bad(cmd, "no sketch is being edited"))?;@@ -352,7 +356,7 @@ fn edit_text(s: &mut Session, p: &Value) -> Result<Value> {// The numbers its expressions give now (a parameter may have changed since the sketch was// last stored); one that cannot be evaluated is left as it was, to be replaced by this edit.let _ = doc.apply_text_values(&doc.param_values().0, &mut sk);- let out = apply_text_edit(&mut sk, &doc, p)?;+ let out = apply_text_edits(&mut sk, &doc, p)?;*doc.sketch_mut(id)? = sk;*s.doc_mut() = doc;Ok(out)@@ -363,10 +367,27 @@ fn edit_text(s: &mut Session, p: &Value) -> Result<Value> {fn edit_text_live(s: &mut Session, p: &Value) -> Result<(Value, Option<crate::LiveEdit>)> {let cmd = "sketch.edit_text";let id = s.active_sketch.ok_or_else(|| bad(cmd, "no sketch is being edited"))?;- let (out, report) = super::sketch::edit_solved(s, id, false, |sk, doc| Ok((apply_text_edit(sk, doc, p)?, super::sketch::Left::Unsolved)))?;+ let (out, report) = super::sketch::edit_solved(s, id, false, |sk, doc| Ok((apply_text_edits(sk, doc, p)?, super::sketch::Left::Unsolved)))?;Ok((out, report.map(|report| crate::LiveEdit { sketch: id, report })))}+/// `sketch.edit_text`'s change: one text's (`link` or `entity`), or each of `edits` in turn.+/// One command, so the features after the sketch are rebuilt once for all of them.+fn apply_text_edits(sk: &mut Sketch, doc: &solidcraft_doc::Document, p: &Value) -> Result<Value> {+ let cmd = "sketch.edit_text";+ match p.get("edits") {+ None | Some(Value::Null) => apply_text_edit(sk, doc, p),+ Some(Value::Array(edits)) => {+ if edits.is_empty() || edits.len() > MAX_TEXT_EDITS {+ return Err(bad(cmd, format!("`edits` must hold 1 to {MAX_TEXT_EDITS} edits")));+ }+ let outs = edits.iter().map(|e| apply_text_edit(sk, doc, e)).collect::<Result<Vec<_>>>()?;+ Ok(json!({"edits": outs}))+ }+ Some(_) => Err(bad(cmd, "`edits` must be a list of edits")),+ }+}+/// `sketch.edit_text`'s change to its sketch: the text source's new values and its outlines/// regenerated from them.fn apply_text_edit(sk: &mut Sketch, doc: &solidcraft_doc::Document, p: &Value) -> Result<Value> {diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rsindex a560d7f0c5..b8a1f21b38 100644--- a/crates/engine/src/cmd/sketch_modify_tests.rs+++ b/crates/engine/src/cmd/sketch_modify_tests.rs@@ -1586,6 +1586,35 @@ fn offset_driver_format18_offsets_keep_fixed_distances_on_upgrade() {assert_eq!(ids(&result["curves"]).len(), sk.offsets[0].outputs.len());}+#[test]+fn several_texts_edit_in_one_rebuild() {+ let mut s = Session::default();+ run(&mut s, "sketch.create", json!({"plane": "XY", "name": "Labels"}));+ let a = run(&mut s, "sketch.text", json!({"text": "I", "at": [0, 0], "height": 10}))["link"].as_str().unwrap().to_string();+ let b = run(&mut s, "sketch.text", json!({"text": "I", "at": [30, 0], "height": 10}))["link"].as_str().unwrap().to_string();+ run(&mut s, "sketch.finish", json!({}));+ run(&mut s, "solid.extrude", json!({"sketch": "Labels", "distance": 2}));+ let volume = |s: &mut Session| run(s, "inspect.measure", json!({}))["total"]["volume_mm3"].as_f64().unwrap();+ let v0 = volume(&mut s);+ run(&mut s, "sketch.edit", json!({"sketch": "Labels"}));+ let (before, steps) = (s.evaluations, s.undo.len());+ let r = run(&mut s, "sketch.edit_text", json!({"edits": [{"link": a, "text": "II"}, {"link": b, "text": "II"}]}));+ assert_eq!(s.evaluations, before + 1, "one rebuild for both texts");+ assert_eq!(s.undo.len(), steps + 1, "one undo step for both texts");+ assert_eq!(r["edits"][1]["link"], json!(b), "{r}");+ // All or nothing: a bad second edit leaves the first text as it was.+ assert!(s.execute("sketch.edit_text", &json!({"edits": [{"link": a, "text": "III"}, {"link": "nope", "text": "I"}]})).is_err());+ assert!(s.execute("sketch.edit_text", &json!({"edits": []})).is_err());+ // A live frame takes the same list.+ let r = run(&mut s, "sketch.edit_text", json!({"edits": [{"link": a, "height": 12}, {"link": b, "height": 12}], "live": true}));+ assert_eq!(r["edits"][0]["link"], json!(a), "{r}");+ run(&mut s, "sketch.edit_text", json!({"edits": [{"link": a, "height": 10}, {"link": b, "height": 10}], "live": true}));+ run(&mut s, "sketch.drag_end", json!({}));+ run(&mut s, "sketch.finish", json!({}));+ let v1 = volume(&mut s);+ assert!((v1 / v0 - 2.0).abs() < 0.05, "two bars per label now: {v0} -> {v1}");+}+#[test]fn trim_keeps_the_rest_of_a_dimensioned_line_where_it_was() {// A 40 x 40 square, its right side dimensioned with the Dimension tool (a line length).