Modeling: a copied body keeps the appearance of its original (#6423) (#6456)

* claim: #6423

* test: a copied body must keep the look of the body it came from

solid.move with copy: true leaves the copy without the appearance, the
material or the face looks of the source body, so it renders with the
default look. The tests fail for that reason: the copy has no entry in
appearances.bodies, in materials or in appearances.faces.

* fix: a copied body keeps the look of the body it came from (#6423)

A move with `copy: true` pushed a new body but left it with the default look, so the copy came out a different colour than the part it came from. The copy now carries the body's appearance and its material, as the component copy already did.

* test: keep the copy regression to the body look and the material (#6423)

* test: each copied body keeps the look and the material of its own source (#6423)

Two bodies with different appearances and different materials, copied in one solid.move: each copy must keep the look and the material of the body it came from.

Scope of the fix: the look of the BODY and its material are carried over to the copy. The per-face looks are NOT carried over: a face look is keyed by the point on the face, so the copy's faces would have to be matched and their points translated, which is a separate feature.
This commit is contained in:
Homero Thompson
2026-10-26 11:12:50 +00:00
committed by GitHub
parent cf7fbeaba1
commit 12fe17d1b7
2 changed files with 107 additions and 4 deletions
+28 -4
View File
@@ -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>> {
@@ -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(&copy), 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(&copy), 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();