Replace Face keeps its body's faces attachable (#1925)
Replace Face rebuilt its body with ModelBody::new and dropped the body's lineage, so no face of the result was named exactly: a sketch on any face of it, even one the replacement didn't touch, failed with "this face can't keep an attachment". Each face it moves is now a lineage step, as in Offset Face (which does the same geometry), and the result keeps the lineage. The test of a body without history used Replace Face as its example of an operation that reports none; it now lifts the block with Align instead, the same geometry. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: bherbruck <brennen.herbruck@gmail.com>
This commit is contained in:
@@ -97,20 +97,22 @@ pub(super) fn eval(doc: &Document, vals: &BTreeMap<String, Value>, f: &Feature,
|
||||
let tn = tp.normal();
|
||||
let i = body_at(st, body, &crate::plain(faces))?;
|
||||
let Some(mb) = st.bodies.get(i).cloned() else { return Err(DocError::Invalid("body".into())) };
|
||||
let mut b = mb.body.clone();
|
||||
// Each face moved is a step of the body's lineage, as Offset Face's is: without it no face of
|
||||
// the result could take a sketch's attachment.
|
||||
let mut b = mb.traced();
|
||||
for p in faces {
|
||||
let n =
|
||||
planar_normal_at(&b, p.0).ok_or_else(|| DocError::Invalid("not supported yet: replacing a face that is not planar".into()))?;
|
||||
let n = planar_normal_at(&b.body, p.0)
|
||||
.ok_or_else(|| DocError::Invalid("not supported yet: replacing a face that is not planar".into()))?;
|
||||
if n.dot(tn).abs() < 1.0 - 1e-9 {
|
||||
return Err(DocError::Invalid("not supported yet: a target that is not parallel to the face".into()));
|
||||
}
|
||||
let d = (tp.origin - p.0).dot(n);
|
||||
if d.abs() > 1e-9 {
|
||||
b = kernel::offset_faces(&b, &[p.0], d)?;
|
||||
b = Traced::step(kernel::offset_faces(&b.body, &[p.0], d)?, &[&b]);
|
||||
}
|
||||
}
|
||||
if let Some(slot) = st.bodies.get_mut(i) {
|
||||
*slot = ModelBody::new(mb.name, b, mb.feature);
|
||||
*slot = ModelBody::traced_new(mb.name, b, mb.feature);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -226,8 +226,9 @@ fn a_name_of_a_body_without_history_is_found_by_geometry_and_says_so_once_it_mov
|
||||
run(&mut s, "sketch.dimension", json!({"entities": ["l1"], "value": "len"}));
|
||||
run(&mut s, "sketch.finish", json!({}));
|
||||
run(&mut s, "solid.extrude", json!({"distance": 10, "name": "Block"}));
|
||||
// Replacing the top by a plane: an operation that reports no history.
|
||||
run(&mut s, "solid.replace_face", json!({"faces": [[20, 15, 10]], "target": {"origin": [0, 0, 12], "normal": [0, 0, 1]}, "name": "Lift"}));
|
||||
// Lifting it 2 mm with Align: an operation that reports no history (Replace Face, used here
|
||||
// before, keeps it now). The top ends at z = 12 as it did.
|
||||
run(&mut s, "solid.align", json!({"bodies": ["Body1"], "from": [0, 0, 0], "to": [0, 0, 2], "name": "Lift"}));
|
||||
let state = s.model.state();
|
||||
let body = state.bodies.first().unwrap();
|
||||
assert!((0..solidcraft_doc::naming::face_names(body).len()).all(|i| !solidcraft_doc::naming::face_name_exact(body, i)));
|
||||
@@ -242,6 +243,36 @@ fn a_name_of_a_body_without_history_is_found_by_geometry_and_says_so_once_it_mov
|
||||
assert!(!solid_at(&s, [49.9, 15.0, 11.9]));
|
||||
}
|
||||
|
||||
/// Replace Face keeps the body's history, as Offset Face does: every face of the result is named
|
||||
/// exactly, and a sketch takes an attachment on the replaced face (before, no face could: "this
|
||||
/// face can't keep an attachment") and follows it when the target moves.
|
||||
#[test]
|
||||
fn a_sketch_on_a_replaced_face_follows_it() {
|
||||
let mut s = Session::default();
|
||||
run(&mut s, "parameters.add", json!({"name": "top", "expression": "15 mm"}));
|
||||
block(&mut s);
|
||||
run(&mut s, "solid.replace_face", json!({"faces": [[20, 15, 10]], "target": {"origin": [0, 0, "top"], "normal": [0, 0, 1]}, "name": "Lift"}));
|
||||
assert!(names(&s, "Body1").iter().all(|(_, exact)| *exact), "{:?}", names(&s, "Body1"));
|
||||
// A side the replacement only stretched takes an attachment too.
|
||||
run(&mut s, "sketch.create", json!({"plane": {"face": [0, 15, 5]}, "name": "Side"}));
|
||||
run(&mut s, "sketch.finish", json!({}));
|
||||
run(&mut s, "sketch.create", json!({"plane": {"face": [20, 15, 15]}, "name": "OnTop"}));
|
||||
run(&mut s, "sketch.circle.center", json!({"center": [0, 0], "radius": 3}));
|
||||
run(&mut s, "sketch.finish", json!({}));
|
||||
run(&mut s, "solid.extrude", json!({"sketch": "OnTop", "distance": 5, "operation": "join", "name": "Boss"}));
|
||||
// The boss stands on the replaced top wherever the circle's centre lands on it.
|
||||
let (lo, hi) = {
|
||||
let state = s.model.state();
|
||||
let b = state.body("Body1").unwrap().mesh().bounds();
|
||||
(b.min.z, b.max.z)
|
||||
};
|
||||
assert!((lo - 0.0).abs() < 1e-6 && (hi - 20.0).abs() < 1e-6, "{lo} {hi}");
|
||||
change(&mut s, "top", "18 mm");
|
||||
assert_eq!(error(&s, "Boss"), None);
|
||||
let hi = s.model.state().body("Body1").unwrap().mesh().bounds().max.z;
|
||||
assert!((hi - 23.0).abs() < 1e-6, "the boss follows the top to z = 18: {hi}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_sketch_on_a_certified_face_survives_a_fillet_and_a_cut_upstream_of_it() {
|
||||
let mut s = Session::default();
|
||||
|
||||
Reference in New Issue
Block a user