SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Modeling: a copied body keeps the appearance of its original (#6423) (#6456)
Homero Thompson12fe17d1b76b
diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rsindex b129091dab..dd1ea52f9e 100644--- a/crates/engine/src/cmd/features.rs+++ b/crates/engine/src/cmd/features.rs@@ -1599,17 +1599,41 @@ fn move_body_feature(s: &mut Session, p: &Value) -> Result<Value> {if let Some(a) = &angle {check_expr(s, a, Kind::Angle, cmd, "angle")?;}- add_feature(+ let copy = bool_(p, "copy").unwrap_or(false);+ let reply = add_feature(s,p,FeatureKind::Move {- bodies: bodies.into_iter().map(BodyName).collect(),+ bodies: bodies.iter().cloned().map(BodyName).collect(),translate: t,rotate_axis: p.get("axis").and_then(vec3).map(Dir),angle,- copy: bool_(p, "copy").unwrap_or(false),+ copy,},- )+ )?;+ if copy {+ copy_keeps_appearance(s, &reply, &bodies, cmd)?;+ }+ Ok(reply)+}++/// A copy keeps the look and the material of the body it came from (#6423): the copy is the same+/// part in another place, so it looks the same.+fn copy_keeps_appearance(s: &mut Session, reply: &Value, sources: &[String], cmd: &str) -> Result<()> {+ let id = reply.get("feature").and_then(Value::as_u64).ok_or_else(|| bad(cmd, "the copy has no feature id"))?;+ let copies: Vec<String> = s.model.state().bodies.iter().filter(|b| b.feature == id).map(|b| b.name.clone()).collect();+ if copies.len() != sources.len() {+ return Err(bad(cmd, "the copy did not make one body per body it copied"));+ }+ for (source, copy) in sources.iter().zip(copies.iter()) {+ if let Some(look) = s.doc.appearances.bodies.get(source).cloned() {+ s.doc_mut().appearances.bodies.insert(copy.clone(), look);+ }+ if let Some(material) = s.doc.materials.get(source).cloned() {+ s.doc_mut().materials.insert(copy.clone(), material);+ }+ }+ Ok(())}fn source_features(s: &Session, p: &Value, cmd: &str) -> Result<Vec<String>> {diff --git a/crates/engine/src/cmd/solid_move_copy_tests.rs b/crates/engine/src/cmd/solid_move_copy_tests.rsindex 4095bfcc4f..7c057c386f 100644--- a/crates/engine/src/cmd/solid_move_copy_tests.rs+++ b/crates/engine/src/cmd/solid_move_copy_tests.rs@@ -14,6 +14,30 @@ fn volume(body: &solidcraft_doc::ModelBody) -> f64 {solidcraft_kernel::measure(&body.body).unwrap().volume}+/// A box of 20 mm with its corner at `corner`, as a body of its own.+fn cube20_at(s: &mut Session, name: &str, corner: [i32; 3]) {+ run(s, "solid.box", json!({"length": 20, "width": 20, "height": 20, "body_name": name, "corner": corner}));+}++/// The copy of `source`: the body that sits where the source landed after the move. Which+/// copy is which is read from the geometry, not from the order the bodies are listed in.+fn copy_of(s: &Session, source: &str, translate: Vec3) -> String {+ let state = s.model.state();+ let from = state.body(source).expect("the source body is in the model").mesh().bounds().min;+ let want = Vec3::new(from.x + translate.x, from.y + translate.y, from.z + translate.z);+ state+ .bodies+ .iter()+ .find(|b| b.name != source && b.mesh().bounds().min.dist(want) < 1e-6)+ .map(|b| b.name.clone())+ .expect("the move must have copied the body")+}++/// The name of the body a command made, as the command reported it.+fn created(reply: &Value) -> String {+ reply["created_bodies"].as_array().and_then(|a| a.first()).and_then(Value::as_str).expect("the command must report the body it made").to_string()+}+#[test]fn copy_keeps_the_original_and_adds_a_transformed_body() {let mut s = Session::default();@@ -54,6 +78,61 @@ fn without_copy_the_move_still_replaces_the_body_in_place() {assert!(bounds.min.dist(Vec3::new(30.0, 0.0, 0.0)) < 1e-6, "{bounds:?}");}+#[test]+fn a_copied_body_keeps_the_appearance_of_the_body_it_came_from() {+ let mut s = Session::default();+ cube20(&mut s, "Cube");+ run(&mut s, "appearance.assign", json!({"bodies": ["Cube"], "appearance": "ABS - Blue"}));+ let reply = run(&mut s, "solid.move", json!({"bodies": ["Cube"], "translate": [30, 0, 0], "copy": true}));+ let copy = created(&reply);+ let source = s.doc.appearances.bodies.get("Cube").expect("the source body keeps its look").clone();+ assert_eq!(s.doc.appearances.bodies.get(©), Some(&source), "the copy must keep the appearance of the body it came from");+}++#[test]+fn a_copied_body_keeps_the_material_of_the_body_it_came_from() {+ let mut s = Session::default();+ cube20(&mut s, "Cube");+ run(&mut s, "material.assign", json!({"bodies": ["Cube"], "material": "Aluminum"}));+ let reply = run(&mut s, "solid.move", json!({"bodies": ["Cube"], "translate": [30, 0, 0], "copy": true}));+ let copy = created(&reply);+ let source = s.doc.materials.get("Cube").expect("the source body keeps its material").clone();+ assert_eq!(s.doc.materials.get(©), Some(&source), "the copy must keep the material of the body it came from");+}++#[test]+fn each_copied_body_keeps_the_look_and_the_material_of_the_body_it_came_from() {+ let mut s = Session::default();+ cube20(&mut s, "Alpha");+ cube20_at(&mut s, "Beta", [50, 0, 0]);+ run(&mut s, "appearance.assign", json!({"bodies": ["Alpha"], "appearance": "ABS - Blue"}));+ run(&mut s, "appearance.assign", json!({"bodies": ["Beta"], "appearance": "ABS - Red"}));+ run(&mut s, "material.assign", json!({"bodies": ["Alpha"], "material": "Aluminum"}));+ run(&mut s, "material.assign", json!({"bodies": ["Beta"], "material": "Steel"}));++ // One move, two bodies: each copy keeps the look of the body it came from, not the look of+ // the body copied before it.+ run(&mut s, "solid.move", json!({"bodies": ["Beta", "Alpha"], "translate": [30, 0, 0], "copy": true}));+ assert_eq!(s.model.state().bodies.len(), 4, "two sources and their two copies");++ let translate = Vec3::new(30.0, 0.0, 0.0);+ let alpha = copy_of(&s, "Alpha", translate);+ let beta = copy_of(&s, "Beta", translate);+ assert_ne!(alpha, beta, "the two bodies must have two different copies");++ let alpha_look = s.doc.appearances.bodies.get("Alpha").expect("the source body keeps its look").name.clone();+ let beta_look = s.doc.appearances.bodies.get("Beta").expect("the source body keeps its look").name.clone();+ assert_ne!(alpha_look, beta_look, "the two bodies must have different looks for this to mean anything");+ assert_eq!(+ s.doc.appearances.bodies.get(&alpha).map(|l| l.name.clone()),+ Some(alpha_look.clone()),+ "the copy of Alpha must keep the look of Alpha"+ );+ assert_eq!(s.doc.appearances.bodies.get(&beta).map(|l| l.name.clone()), Some(beta_look.clone()), "the copy of Beta must keep the look of Beta");+ assert_eq!(s.doc.materials.get(&alpha).map(String::as_str), Some("Aluminum"), "the copy of Alpha must keep the material of Alpha");+ assert_eq!(s.doc.materials.get(&beta).map(String::as_str), Some("Steel"), "the copy of Beta must keep the material of Beta");+}+#[test]fn an_older_design_with_no_copy_key_at_all_still_loads_and_moves_in_place() {let mut s = Session::default();