SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Hole: the same parameter definition typed for several holes uses that parameter (#12472) (#12473)
Daniel Dimitrov25d7a6b0c30a
diff --git a/crates/doc/src/inline_params.rs b/crates/doc/src/inline_params.rsindex 9d908cfd06..a715015c8b 100644--- a/crates/doc/src/inline_params.rs+++ b/crates/doc/src/inline_params.rs@@ -95,9 +95,17 @@ fn with_unit(rhs: &str, units: &str) -> String {}impl Document {+ /// `name = expression` typed again exactly as an existing user parameter was defined (each+ /// of several holes made one at a time, or one command per hole, given `ScrewDepth = 5`):+ /// the same value in the same unit, so it uses that parameter instead of being refused.+ fn defines_again(&self, d: &Pending) -> bool {+ self.param(&d.name).is_some_and(|p| !p.model && p.expr.trim() == d.expression.trim() && d.unit.as_ref().is_none_or(|u| *u == p.unit))+ }+/// Store every value typed as `name = expression`: the user parameter `name` with that/// expression (a bare number read as the value would read it, in that unit), and `name`- /// where the text was. A name that is taken or reserved is refused, as is a definition where+ /// where the text was. A name that is taken or reserved is refused (unless it is a user+ /// parameter defined exactly so, which the text then names), as is a definition where/// none can be made: in a parameter's own expression or a configuration cell. All or nothing:/// on an error the design is unchanged.pub fn define_inline(&mut self) -> Result<Vec<Defined>> {@@ -182,7 +190,7 @@ impl Document {if let Some(problem) = Self::param_name_problem(&d.name) {return Err(DocError::Invalid(problem));}- if self.name_taken(&d.name) {+ if self.name_taken(&d.name) && !self.defines_again(d) {return Err(DocError::Invalid(format!("there is already a parameter `{}`: type `{}` to use it, or change it in Parameters",d.name, d.name@@ -191,6 +199,10 @@ impl Document {}let mut out = Vec::with_capacity(found.defs.len());for d in found.defs {+ // Typed again as it was defined, the text just names the parameter already there.+ if self.defines_again(&d) {+ continue;+ }// A value of no fixed kind (a joint's) is evaluated without the design's units, so// its bare number stays a millimetre length whatever they are (#8865): `w = 5` is// what `5` is there.diff --git a/crates/engine/src/cmd/parameters_tests.rs b/crates/engine/src/cmd/parameters_tests.rsindex cdde63712b..c711ef5c5b 100644--- a/crates/engine/src/cmd/parameters_tests.rs+++ b/crates/engine/src/cmd/parameters_tests.rs@@ -1071,6 +1071,38 @@ fn a_refused_definition_changes_nothing() {assert!(r.get("defined").is_none(), "{r}");}+/// Several holes made one command at a time, each with its depth typed as `ScrewDepth = 5`: the+/// first defines `ScrewDepth`, the later ones name it instead of being refused because it+/// already exists. Typed again with another value, it is still refused and changes nothing.+#[test]+fn the_same_definition_typed_again_uses_the_parameter() {+ let mut s = Session::default();+ run(&mut s, "solid.box", json!({"length": 60, "width": 40, "height": 12}));+ let r = run(&mut s, "solid.hole", json!({"position": [15, 20, 12], "diameter": 5, "depth": "ScrewDepth = 5"}));+ assert_eq!(r["definitions"].as_array().map(Vec::len), Some(1), "{r}");+ let r = run(&mut s, "solid.hole", json!({"position": [45, 20, 12], "diameter": 5, "depth": "ScrewDepth = 5"}));+ assert!(r.get("definitions").is_none_or(|d| d.as_array().is_some_and(Vec::is_empty)), "nothing new is defined: {r}");+ assert_eq!(s.doc.params.iter().filter(|p| p.name == "ScrewDepth").count(), 1);+ assert_eq!(expr_of(&mut s, "ScrewDepth"), "5");+ let depths: Vec<&str> = s+ .doc+ .features+ .iter()+ .filter_map(|f| match &f.kind {+ solidcraft_doc::FeatureKind::Hole { depth, .. } => depth.as_ref().map(|d| d.as_str()),+ _ => None,+ })+ .collect();+ assert_eq!(depths, ["ScrewDepth", "ScrewDepth"]);+ run(&mut s, "parameters.change", json!({"changes": {"ScrewDepth": "8"}}));+ // Another value for the name is still a parameter that already exists.+ let undo = s.undo.len();+ let e = err(&mut s, "solid.hole", json!({"position": [30, 30, 12], "diameter": 5, "depth": "ScrewDepth = 6"}));+ assert!(e.contains("there is already a parameter `ScrewDepth`"), "{e}");+ assert_eq!(s.undo.len(), undo);+ assert_eq!(expr_of(&mut s, "ScrewDepth"), "8");+}+/// Changing a parameter to `name = expression` names it (#4071): the dimension's or feature/// input's own parameter is renamed, references follow, and it takes the expression; no other/// parameter is made. One undo takes both back.diff --git a/crates/engine/src/lib.rs b/crates/engine/src/lib.rsindex 3d949d0394..2fe4d78a76 100644--- a/crates/engine/src/lib.rs+++ b/crates/engine/src/lib.rs@@ -1814,7 +1814,14 @@ impl Session {fn define_inline(&mut self, id: &str, mut v: Value) -> Result<Value> {let mut doc = (*self.doc).clone();let defined = doc.define_inline().map_err(|e| EngineError::BadParams { cmd: id.to_string(), msg: e.to_string() })?;- let Some(first) = defined.first() else { return Ok(v) };+ let Some(first) = defined.first() else {+ // Only definitions of parameters that already exist, typed again as they were made:+ // nothing new to report, but the text now names the parameter.+ if doc != *self.doc {+ self.doc = Arc::new(doc);+ }+ return Ok(v);+ };if let Some(out) = v.as_object_mut() {out.insert("defined".into(), serde_json::json!(first.name));out.insert("definitions".into(), serde_json::json!(defined));diff --git a/crates/ui-egui/src/hole_place_ui_tests.rs b/crates/ui-egui/src/hole_place_ui_tests.rsindex 890728a3c5..a3cb48b826 100644--- a/crates/ui-egui/src/hole_place_ui_tests.rs+++ b/crates/ui-egui/src/hole_place_ui_tests.rs@@ -466,3 +466,38 @@ fn editing_a_hole_replaces_its_face_instead_of_adding_one() {assert_eq!(again, hole, "the same feature");assert!(position.dist(on_top) < 1e-3, "OK uses the new face: {position:?}");}++/// Single holes on two faces with the depth typed as `ScrewDepth = 5`: OK runs one command per+/// hole, the first defines `ScrewDepth` and both holes use it, instead of the second hole being+/// refused because the parameter already exists.+#[test]+fn single_holes_on_two_faces_share_a_parameter_typed_in_the_dialog() {+ let mut h = block_as_installed();+ h.step(&json!({"call": "ui.view", "params": {"view": "iso"}})).unwrap();+ h.settle();+ let before = h.app.session.doc.features.len();+ h.step(&json!({"start": "solid.hole"})).unwrap();+ for p in [Vec3::new(30.0, 20.0, 20.0), Vec3::new(25.0, 0.0, 10.0)] {+ let at = screen(&mut h, p);+ h.step(&json!({"click": at})).unwrap();+ }+ assert_eq!(places(&h).len(), 2, "a place on the top and one on the front: {:?}", places(&h));+ match h.app.dialog.as_mut().map(|d| &mut d.kind) {+ Some(Kind::Hole { depth, .. }) => *depth = "ScrewDepth = 5".into(),+ _ => panic!("the Hole dialog is open"),+ }+ h.step(&json!({"click": {"handle": "dialog:ok"}})).unwrap();+ assert!(h.app.dialog.is_none(), "{:?}", h.app.dialog.as_ref().map(|d| &d.error));+ let param = h.app.session.doc.param("ScrewDepth").expect("the first hole defines ScrewDepth");+ assert_eq!(param.unit, "mm");+ let depths: Vec<String> = h.app.session.doc.features[before..]+ .iter()+ .filter_map(|f| match &f.kind {+ FeatureKind::Hole { depth, .. } => depth.as_ref().map(|d| d.as_str().to_string()),+ _ => None,+ })+ .collect();+ assert_eq!(depths, ["ScrewDepth", "ScrewDepth"]);+ assert!(!solid_at(&h, Vec3::new(30.0, 20.0, 17.0)) && !solid_at(&h, Vec3::new(25.0, 3.0, 10.0)), "both holes are drilled");+ assert!(solid_at(&h, Vec3::new(30.0, 20.0, 8.0)), "5 mm deep (and its drill point), not through");+}