SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Edit Feature: a primitive in a moved component keeps its placement (#2926) (#5989)
sascha2101a1b9ca13e3b9
diff --git a/crates/engine/src/frames.rs b/crates/engine/src/frames.rsindex 599eb6b507..ee7eef0cff 100644--- a/crates/engine/src/frames.rs+++ b/crates/engine/src/frames.rs@@ -406,6 +406,14 @@ pub fn to_local(s: &Session, id: &str, params: &Value) -> Option<Value> {}}}+ // Edit Feature passes a primitive's placement through as saved (it can hold+ // expressions), as `timeline.inspect` reports it: mapping it again would move a+ // primitive in a moved component (#2926).+ for (key, saved) in primitive_placement(&feature.kind, cmd) {+ if params.get("params").and_then(|p| p.get(key)).is_some_and(|given| same_coordinates(given, &saved)) {+ inner[key] = saved;+ }+ }let mut out = params.clone();out["params"] = inner;return Some(out);@@ -489,6 +497,42 @@ pub fn to_local(s: &Session, id: &str, params: &Value) -> Option<Value> {m.changed.then_some(Value::Object(out))}+/// A primitive's saved placement inputs, in its component's frame, for redefining it with `cmd`.+fn primitive_placement(kind: &solidcraft_doc::FeatureKind, cmd: &str) -> Vec<(&'static str, Value)> {+ use solidcraft_doc::FeatureKind as Feature;+ let point = |p: &solidcraft_doc::PointExpr| {+ Value::Array(+ p.iter()+ .map(|e| e.as_str().trim().parse::<f64>().ok().filter(|x| x.is_finite()).map_or_else(|| Value::from(e.as_str()), Value::from))+ .collect(),+ )+ };+ match (kind, cmd) {+ (Feature::Box { corner, .. }, "solid.box") => vec![("corner", point(corner))],+ (Feature::Sphere { center, .. }, "solid.sphere") | (Feature::Torus { center, .. }, "solid.torus") => vec![("center", point(center))],+ (Feature::Cylinder { base, axis, .. }, "solid.cylinder") | (Feature::Coil { base, axis, .. }, "solid.coil") => {+ vec![("base", point(base)), ("axis", pt(axis.0))]+ }+ _ => Vec::new(),+ }+}++/// Do two coordinate lists hold the same values (`0` and `0.0` and `"0"` alike, expressions+/// by their text)?+fn same_coordinates(a: &Value, b: &Value) -> bool {+ let number = |v: &Value| v.as_f64().or_else(|| v.as_str().and_then(|s| s.trim().parse::<f64>().ok()));+ match (a.as_array(), b.as_array()) {+ (Some(a), Some(b)) => {+ a.len() == b.len()+ && a.iter().zip(b).all(|(x, y)| match (number(x), number(y)) {+ (Some(x), Some(y)) => x == y,+ _ => x.as_str().map(str::trim).is_some_and(|x| Some(x) == y.as_str().map(str::trim)),+ })+ }+ _ => false,+ }+}+#[cfg(test)]#[path = "frames_tests.rs"]mod tests;diff --git a/crates/engine/src/frames_tests.rs b/crates/engine/src/frames_tests.rsindex ff6ee83f3a..fcd11c4575 100644--- a/crates/engine/src/frames_tests.rs+++ b/crates/engine/src/frames_tests.rs@@ -217,6 +217,51 @@ fn block_in_component() -> Session {s}+/// Edit Feature passes a primitive's placement through as saved, so changing the radius of+/// Leaf B's rod in the Hinge Assembly leaves the leaf where it was (#2926).+#[test]+fn editing_a_primitive_in_a_turned_component_keeps_its_placement() {+ let sample = crate::sample::samples().into_iter().find(|x| x.name == "Hinge Assembly").unwrap();+ let mut s = Session::default();+ s.run_script(&(sample.script)()).unwrap();+ let (plate_lo, plate_hi) = world_bbox(&s, "Body3");+ let (rod_lo, rod_hi) = world_bbox(&s, "Body4");+ // What the dialog sends: the saved definition, with the new radius.+ let definition = run(&mut s, "timeline.inspect", json!({"feature": "Cylinder2"}))["definition"].clone();+ let params = json!({"base": definition["base"], "axis": definition["axis"], "radius": "6", "height": "40", "operation": "new_body"});+ run(&mut s, "timeline.redefine", json!({"feature": "Cylinder2", "command": "solid.cylinder", "params": params}));+ let after = run(&mut s, "timeline.inspect", json!({"feature": "Cylinder2"}))["definition"].clone();+ assert_eq!((&after["base"], &after["axis"]), (&definition["base"], &definition["axis"]));+ let (lo, hi) = world_bbox(&s, "Body3");+ assert!(near(lo, plate_lo) && near(hi, plate_hi), "the leaf moved: {lo:?} {hi:?}");+ // The rod grows about its own axis (its mesh is faceted, so its size is close, not exact).+ let (lo, hi) = world_bbox(&s, "Body4");+ assert!(((lo + hi) * 0.5).dist((rod_lo + rod_hi) * 0.5) < 1e-3, "the rod moved: {lo:?} {hi:?}");+ let (size, before) = (hi - lo, rod_hi - rod_lo);+ assert!((size.x - before.x).abs() < 1e-6 && (size.y - 12.0).abs() < 0.05 && (size.z - 12.0).abs() < 0.05, "{size:?}");+}++/// A new placement given to `timeline.redefine` is still a world point.+#[test]+fn a_new_primitive_placement_is_still_mapped_from_the_world() {+ let mut s = moved_part();+ run(&mut s, "component.activate", json!({"component": "Part"}));+ run(&mut s, "solid.cylinder", json!({"base": [60, 10, 10], "radius": 2, "height": 5, "operation": "new", "name": "Peg"}));+ let base = |s: &mut Session| run(s, "timeline.inspect", json!({"feature": "Peg"}))["definition"]["base"].clone();+ assert_eq!(base(&mut s), json!([10.0, 10.0, 10.0]));+ let redefine = |s: &mut Session, at: Value| {+ run(+ s,+ "timeline.redefine",+ json!({"feature": "Peg", "command": "solid.cylinder", "params": {"base": at, "radius": 3, "height": 5, "operation": "new"}}),+ );+ };+ redefine(&mut s, json!([10, 10, 10]));+ assert_eq!(base(&mut s), json!([10.0, 10.0, 10.0]));+ redefine(&mut s, json!([70, 10, 10]));+ assert_eq!(base(&mut s), json!([20.0, 10.0, 10.0]));+}+/// The user's "big square": a sketch made in the root on a moved component's face lies on the/// face where it is shown, its projected loop on the face's edges (moved and turned), and it/// follows when the occurrence moves on.