Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Hole: drill only the shown bodies when no target is picked (#7520) (#7562)

sascha210107ef94e10232
diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs
index f5d5f31906..68d44dc175 100644
--- a/crates/engine/src/cmd/features.rs
+++ b/crates/engine/src/cmd/features.rs
@@ -744,6 +744,8 @@ fn add_feature_in(s: &mut Session, p: &Value, kind: FeatureKind, world: bool) ->
names
} else if pts.is_empty() {
Vec::new()
+ } else if matches!(kind, FeatureKind::Hole { .. }) {
+ hole_face_names(s, p, &kind)?
} else {
solidcraft_doc::face_point_names(&s.doc, &s.model.state(), &kind, s.active_component)?
}
@@ -1082,6 +1084,22 @@ fn face_body_reached(s: &Session, sketch: u64) -> Result<Option<String>> {
Ok(Some(body.name.clone()))
}
+/// A hole's face points, named on the bodies it drills where one of theirs holds the point
+/// (#7520): a face of another body in the same plane, a hidden one the hole leaves alone, would
+/// otherwise take the name of the face that was picked. Over every body, as before, elsewhere.
+fn hole_face_names(s: &Session, p: &Value, kind: &FeatureKind) -> Result<Vec<String>> {
+ let st = s.model.state();
+ let all = solidcraft_doc::face_point_names(&s.doc, &st, kind, s.active_component)?;
+ let drilled = string_list_or_default(p, "participants");
+ if drilled.is_empty() || drilled.iter().any(|b| b == "*") {
+ return Ok(all);
+ }
+ let mut scoped = solidcraft_doc::ModelState::default();
+ scoped.bodies = st.bodies.iter().filter(|b| drilled.contains(&b.name)).cloned().collect();
+ let own = solidcraft_doc::face_point_names(&s.doc, &scoped, kind, s.active_component)?;
+ Ok(own.into_iter().zip(all).map(|(own, any)| if own.is_empty() { any } else { own }).collect())
+}
+
/// The body whose face `sketch` is attached to (on the face itself, not offset from it), when
/// it is one the active component works on ([`active_scope`]).
fn sketch_face_body(s: &Session, sketch: u64) -> Option<String> {
@@ -2915,6 +2933,20 @@ fn hole(s: &mut Session, p: &Value) -> Result<Value> {
// placed) cuts that body: the pick is already in that body's frame, and the
// component's own bodies alone would leave nothing to cut.
p["participants"] = json!([body]);
+ } else if let Some(mut shown) = shown_scope(s) {
+ // Like a cut (#3961), a hole leaves hidden bodies alone (#7520): while some are
+ // hidden, it drills the shown ones, stored so showing the others later changes
+ // nothing. The body whose face was picked is drilled though hidden (picked through
+ // Select Other, or hidden with the dialog open): it is the one the hole is for.
+ if !shown.is_empty()
+ && points.is_none()
+ && let Some(body) = hidden_body_picked_at(s, position, &shown)
+ {
+ shown.push(body);
+ }
+ if !shown.is_empty() {
+ p["participants"] = json!(shown);
+ }
}
}
add_feature(
@@ -3000,16 +3032,36 @@ fn hole_reference_params(s: &Session, p: &Value, cmd: &str) -> Result<Vec<solidc
/// component than the active one.
fn body_of_other_component_at(s: &Session, p: Vec3) -> Option<String> {
let st = s.model.state();
- let (_, body) = st
- .bodies
+ let body = body_with_face_at(&st, p, |_| true)?;
+ (s.doc.model_body_component(body) != s.active_component).then(|| body.name.clone())
+}
+
+/// The hidden body whose face holds `p`, when no shown body has a face there: a face shared by
+/// a shown and a hidden body is the shown one's, as a pick in the view would take it (#7520).
+fn hidden_body_picked_at(s: &Session, p: Vec3, shown: &[String]) -> Option<String> {
+ let st = s.model.state();
+ if body_with_face_at(&st, p, |b| shown.contains(&b.name)).is_some() {
+ return None;
+ }
+ let scope: Vec<&str> = active_scope(s, &st).into_iter().map(|b| b.name.as_str()).collect();
+ body_with_face_at(&st, p, |b| scope.contains(&b.name.as_str())).map(|b| b.name.clone())
+}
+
+/// The body, among those `keep` takes, with the face nearest `p` within its pick tolerance.
+fn body_with_face_at(
+ st: &solidcraft_doc::ModelState,
+ p: Vec3,
+ keep: impl Fn(&solidcraft_doc::ModelBody) -> bool,
+) -> Option<&solidcraft_doc::ModelBody> {
+ st.bodies
.iter()
- .filter(|b| !b.body.is_mesh())
+ .filter(|b| !b.body.is_mesh() && keep(b))
.filter_map(|b| {
let (d, _) = solidcraft_doc::appearance::nearest_face(b, p)?;
(d <= solidcraft_doc::appearance::pick_tolerance(b)).then_some((d, b))
})
- .min_by(|a, b| a.0.total_cmp(&b.0))?;
- (s.doc.model_body_component(body) != s.active_component).then(|| body.name.clone())
+ .min_by(|a, b| a.0.total_cmp(&b.0))
+ .map(|(_, b)| b)
}
/// The points of a sketch a hole drills at when only the sketch is given.
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index 1d9e6b8eba..c073a58e4b 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -3432,6 +3432,169 @@ fn hole_as_deep_as_the_plate() {
}
}
+/// #7520: a hole that names no bodies leaves hidden ones alone, as a cut does (#3961), and keeps
+/// doing so once they are shown again. Its face is named on the body it drills, not on a hidden
+/// one whose face lies in the same plane; with nothing hidden, it still drills every body.
+#[test]
+fn a_hole_leaves_hidden_bodies_alone() {
+ let setup = |hide: bool| {
+ let mut s = Session::default();
+ // Two bodies whose bottom faces share the plane z = 0, the hidden one made first.
+ run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 20, "body_name": "Block"}));
+ run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 5, "operation": "new", "body_name": "Plate"}));
+ if hide {
+ run(&mut s, "browser.visibility", json!({"items": ["Block"], "visible": false}));
+ }
+ // The name of the plate's face that is picked, before the hole takes the point away.
+ let picked =
+ solidcraft_doc::naming::face_name_at_body(s.world_state().unwrap().body("Plate").unwrap(), solidcore_record::Vec3::new(20.0, 20.0, 0.0));
+ let r = s.execute("solid.hole", &json!({"position": [20, 20, 0], "diameter": 5, "depth": 10, "name": "Hole1"})).unwrap();
+ (s, r, picked)
+ };
+ let vol = |s: &Session, name: &str| solidcraft_kernel::measure(&s.world_state().unwrap().body(name).unwrap().body).unwrap().volume;
+ let (block, plate) = (40.0 * 40.0 * 20.0, 40.0 * 40.0 * 5.0);
+ let (mut hidden, r, picked) = setup(true);
+ assert_eq!(r["changed_bodies"], json!(["Plate"]));
+ assert!((vol(&hidden, "Block") - block).abs() < 1e-6, "the hidden body is not drilled");
+ assert!(vol(&hidden, "Plate") < plate - 1.0, "the shown one is");
+ let hole = hidden.doc.find_feature("Hole1").unwrap();
+ assert!(picked.is_some());
+ assert_eq!((hole.participants.clone(), hole.face_names.first().cloned()), (vec!["Plate".to_string()], picked));
+ run(&mut hidden, "browser.visibility", json!({"items": ["Block"], "visible": true}));
+ assert!((vol(&hidden, "Block") - block).abs() < 1e-6, "shown again, it stays as it was");
+ let (shown, r, _) = setup(false);
+ assert_eq!(r["changed_bodies"], json!(["Block", "Plate"]), "with nothing hidden, both are drilled");
+ assert!(vol(&shown, "Block") < block - 1.0 && vol(&shown, "Plate") < plate - 1.0);
+}
+
+/// #7520 (review): the body whose face is picked is drilled even while it is hidden (picked
+/// through Select Other, or hidden with the dialog open), and editing a hole made with nothing
+/// hidden keeps drilling every body, though one is hidden at the time of the edit.
+#[test]
+fn a_hole_drills_its_picked_body_and_an_edit_keeps_its_scope() {
+ let stack = || {
+ let mut s = Session::default();
+ // Two plates stacked: Lower z 0..5, Upper z 5..10.
+ run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 5, "body_name": "Lower"}));
+ run(&mut s, "solid.box", json!({"corner": [0, 0, 5], "length": 40, "width": 40, "height": 5, "operation": "new", "body_name": "Upper"}));
+ s
+ };
+ let vol = |s: &Session, name: &str| solidcraft_kernel::measure(&s.world_state().unwrap().body(name).unwrap().body).unwrap().volume;
+ let plate = 40.0 * 40.0 * 5.0;
+ let hole = |d: f64| json!({"position": [20, 20, 10], "direction": [0, 0, -1], "diameter": d, "depth": 20, "name": "Hole1"});
+
+ // The picked face is on the hidden Upper: it is drilled, and so is the shown Lower below.
+ let mut s = stack();
+ run(&mut s, "browser.visibility", json!({"items": ["Upper"], "visible": false}));
+ let r = s.execute("solid.hole", &hole(5.0)).unwrap();
+ assert_eq!(r["changed_bodies"], json!(["Lower", "Upper"]), "the picked body is drilled though hidden");
+ assert!(vol(&s, "Upper") < plate - 1.0 && vol(&s, "Lower") < plate - 1.0);
+
+ // Made with nothing hidden, then edited with Lower hidden: both stay drilled, at the new size.
+ let mut s = stack();
+ run(&mut s, "solid.hole", hole(5.0));
+ let small = vol(&s, "Lower");
+ assert!(small < plate - 1.0);
+ run(&mut s, "browser.visibility", json!({"items": ["Lower"], "visible": false}));
+ run(
+ &mut s,
+ "timeline.redefine",
+ json!({"feature": "Hole1", "command": "solid.hole", "params": {"position": [20, 20, 10], "direction": [0, 0, -1], "diameter": 6, "depth": 20}}),
+ );
+ assert!(s.doc.find_feature("Hole1").unwrap().participants.is_empty(), "the edit keeps the hole's scope");
+ assert!(vol(&s, "Lower") < small - 1.0, "the hidden Lower keeps its hole, now wider");
+ assert!(vol(&s, "Upper") < small - 1.0);
+}
+
+/// #7520 (history): a hole made with a body hidden stores the bodies it drills, so it replays
+/// the same way: volumes match exactly after a save and reopen, a rebuild from scratch, undo and
+/// redo, showing the hidden body, renaming bodies and editing an earlier feature.
+#[test]
+fn a_hole_made_with_a_hidden_body_replays_the_same() {
+ let build = |plate_height: f64| {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 20, "body_name": "Block"}));
+ run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": plate_height, "operation": "new", "body_name": "Plate"}));
+ run(&mut s, "browser.visibility", json!({"items": ["Block"], "visible": false}));
+ run(&mut s, "solid.hole", json!({"position": [20, 20, 0], "diameter": 5, "depth": 3, "name": "Hole1"}));
+ s
+ };
+ let vol = |s: &Session, name: &str| solidcraft_kernel::measure(&s.world_state().unwrap().body(name).unwrap().body).unwrap().volume;
+ // A new session evaluated from the saved text alone, as a rebuild from scratch does.
+ let scratch = |s: &Session| Session::new(solidcraft_doc::Document::from_json(&s.doc.to_json()).unwrap());
+ let s = build(5.0);
+ let (block, plate) = (vol(&s, "Block"), vol(&s, "Plate"));
+ assert_eq!(block, 40.0 * 40.0 * 20.0, "the hidden body is not drilled");
+ assert!(plate < 40.0 * 40.0 * 5.0 - 1.0, "the shown one is");
+ let same = |s: &Session, what: &str| {
+ assert_eq!(vol(s, "Block"), block, "{what}: Block");
+ assert_eq!(vol(s, "Plate"), plate, "{what}: Plate");
+ };
+ // (1) Save and reopen.
+ let dir = std::env::temp_dir().join(format!("solidcraft-hole-history-{}", std::process::id()));
+ std::fs::create_dir_all(&dir).unwrap();
+ let file = dir.join("hole.solidcraft");
+ let mut saved = build(5.0);
+ run(&mut saved, "file.save_as", json!({"path": file.to_string_lossy()}));
+ let mut reopened = Session::default();
+ run(&mut reopened, "doc.open", json!({"path": file.to_string_lossy()}));
+ same(&reopened, "reopened");
+ let _ = std::fs::remove_dir_all(&dir);
+ // (2) Rebuild from scratch.
+ same(&scratch(&s), "rebuilt");
+ // (3) Undo and redo.
+ let mut u = build(5.0);
+ run(&mut u, "edit.undo", json!({}));
+ assert_eq!(vol(&u, "Plate"), 40.0 * 40.0 * 5.0, "undone: no hole");
+ run(&mut u, "edit.redo", json!({}));
+ same(&u, "redone");
+ same(&scratch(&u), "redone and rebuilt");
+ // (4) Show the hidden body, then rebuild.
+ let mut shown = build(5.0);
+ run(&mut shown, "browser.visibility", json!({"items": ["Block"], "visible": true}));
+ same(&shown, "shown");
+ same(&scratch(&shown), "shown and rebuilt");
+ // (5) Rename the drilled body (and the hidden one), then rebuild.
+ let mut renamed = build(5.0);
+ run(&mut renamed, "body.rename", json!({"body": "Plate", "name": "Slab"}));
+ run(&mut renamed, "body.rename", json!({"body": "Block", "name": "Core"}));
+ for r in [&renamed, &scratch(&renamed)] {
+ assert_eq!(vol(r, "Slab"), plate, "renamed: Slab");
+ assert_eq!(vol(r, "Core"), block, "renamed: Core");
+ }
+ // (6) Edit an earlier feature (the plate's own box, then the hidden block's), then rebuild:
+ // the same as a design made with the new size from the start.
+ let feature = |s: &Session, body: &str| {
+ let id = s.world_state().unwrap().body(body).unwrap().feature;
+ s.doc.features.iter().find(|f| f.id == id).unwrap().name.clone()
+ };
+ let mut edited = build(5.0);
+ let (plate_box, block_box) = (feature(&edited, "Plate"), feature(&edited, "Block"));
+ run(&mut edited, "timeline.edit", json!({"feature": plate_box, "set": {"height": "8"}}));
+ let fresh = build(8.0);
+ for r in [&edited, &scratch(&edited)] {
+ assert_eq!(vol(r, "Plate"), vol(&fresh, "Plate"), "plate edited");
+ assert_eq!(vol(r, "Block"), block, "plate edited: Block");
+ }
+ run(&mut edited, "timeline.edit", json!({"feature": block_box, "set": {"height": "30"}}));
+ for r in [&edited, &scratch(&edited)] {
+ assert_eq!(vol(r, "Block"), 40.0 * 40.0 * 30.0, "block edited stays undrilled");
+ assert_eq!(vol(r, "Plate"), vol(&fresh, "Plate"), "block edited: Plate");
+ }
+}
+
+/// #7520 (review): with every body hidden, a hole still drills all of them, as before.
+#[test]
+fn a_hole_with_every_body_hidden_drills_them_all() {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 20, "body_name": "Block"}));
+ run(&mut s, "solid.box", json!({"length": 40, "width": 40, "height": 5, "operation": "new", "body_name": "Plate"}));
+ run(&mut s, "browser.visibility", json!({"items": ["Block", "Plate"], "visible": false}));
+ let r = s.execute("solid.hole", &json!({"position": [20, 20, 0], "diameter": 5, "depth": 3, "name": "Hole1"})).unwrap();
+ assert_eq!(r["changed_bodies"], json!(["Block", "Plate"]));
+ assert!(s.doc.find_feature("Hole1").unwrap().participants.is_empty());
+}
+
/// Appearances outlive later features: a coloured face keeps its colour after a fillet on a
/// neighbouring edge; an imported STEP's body and face colours survive a hole.
#[test]
diff --git a/crates/ui-egui/src/preview.rs b/crates/ui-egui/src/preview.rs
index 3ad018e979..2b9a43b4ba 100644
--- a/crates/ui-egui/src/preview.rs
+++ b/crates/ui-egui/src/preview.rs
@@ -1262,6 +1262,21 @@ mod tests {
}
}
+ /// #7520: a Hole's preview leaves a hidden body out, drawn nor drilled, as OK does; before,
+ /// it replaced the hidden body with a drilled one while the dialog was open.
+ #[test]
+ fn a_hole_preview_leaves_hidden_bodies_out() {
+ let mut s = Session::default();
+ s.execute("solid.box", &json!({"length": 40, "width": 40, "height": 5, "body_name": "Plate"})).unwrap();
+ s.execute("solid.box", &json!({"corner": [0, 0, 5], "length": 40, "width": 40, "height": 20, "operation": "new", "body_name": "Block"}))
+ .unwrap();
+ s.execute("browser.visibility", &json!({"items": ["Block"], "visible": false})).unwrap();
+ let cmd = ("solid.hole".to_string(), json!({"position": [20, 20, 0], "diameter": 5, "depth": 10}));
+ let built = compute(&s, &[cmd], colors()).unwrap();
+ assert!(built.error.is_none(), "{:?}", built.error);
+ assert_eq!(built.replaced, vec!["Plate".to_string()]);
+ }
+
/// A plate in its own component, stood up by its placement (an imported part set upright)
/// and edited with that component active: the extrusion's tool is drawn where the plate is
/// shown, on its top face, not at right angles to it in the component's own frame.