Skip to content

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

Isolate a body: other components are hidden as components, in one step (#7896) (#7951)

leValdemar196d83f95fba6e
diff --git a/crates/engine/src/cmd/browser.rs b/crates/engine/src/cmd/browser.rs
index 6f3d097501..c75286f6ec 100644
--- a/crates/engine/src/cmd/browser.rs
+++ b/crates/engine/src/cmd/browser.rs
@@ -64,7 +64,7 @@ pub static COMMANDS: &[CommandSpec] = &[
or features?: [feature id or name] (the bodies they made; path?: [occurrence ids] for only that placement's); \
or folder?: bodies|sketches|construction|origin|joints|all (all of it; showing also shows its browser folders and groups, and `all` takes in the occurrence hides too); \
or tree_folders?: [browser folder or group key: c0/bodies, o:4/sketches, c0/g7, c0/joints, c0/canvases, c0/point_clouds] (the folder or group itself: its items keep their own state); \
- or paths?: [[occurrence ids]] (exact placements and descendants), isolate?: bool (requires paths and visible:true). Saved with the design and undone like any other change (the camera is not)",
+ or paths?: [[occurrence ids]] (exact placements and descendants), isolate?: bool (with paths or items of bodies, and visible:true: everything else is hidden, other placements as placements). Saved with the design and undone like any other change (the camera is not)",
),
CommandSpec::new("browser.expand_all", "Expand All", expand_all).icon("folder").noundo().params(
"path?: [occurrence ids] (that placement and its descendants); component?: id (that component's own folders); \
@@ -436,6 +436,89 @@ fn tree_folder_key(k: &str) -> bool {
k.len() <= 2048 && scope_ok && last_ok
}
+/// Isolate bodies (#7896): the picked bodies stay, and so do the placements that hold them, down
+/// to their own; those placements' other bodies and the root's other bodies are hidden one by
+/// one, and every other placement is hidden as a placement, so its Browser row shows it is off and
+/// its eye brings it back.
+fn isolate_bodies(s: &mut Session, p: &Value, visible: bool) -> Result<Value> {
+ let cmd = "browser.visibility";
+ if !visible {
+ return Err(bad(cmd, "isolate requires visible:true"));
+ }
+ if ["folder", "features", "joints"].iter().any(|k| p.get(*k).is_some()) {
+ return Err(bad(cmd, "isolate takes `paths` or `items` (bodies)"));
+ }
+ let keys = keys(p, "items", cmd)?;
+ if keys.is_empty() {
+ return Err(bad(cmd, "isolate needs `paths` or `items` (bodies)"));
+ }
+ let world = s.world_state()?;
+ let keep: Vec<String> = keys
+ .iter()
+ .map(|k| match shown_item(s, &world, k) {
+ Some(Shown::Body(name)) => Ok(name),
+ _ => Err(bad(cmd, format!("no body `{k}`"))),
+ })
+ .collect::<Result<_>>()?;
+ let mut hidden_bodies: Vec<String> =
+ s.model.state().bodies.iter().filter(|b| s.doc.model_body_component(b) == 0 && !keep.contains(&b.name)).map(|b| b.name.clone()).collect();
+ let mut hidden = Vec::new();
+ let mut stack = vec![Vec::<u64>::new()];
+ let mut count = 0;
+ while let Some(parent) = stack.pop() {
+ for child in s.occurrence_children(&parent)? {
+ count += 1;
+ if count > 100_000 {
+ return Err(bad(cmd, "isolate exceeds 100000 placements"));
+ }
+ if s.occurrence_bodies_shared(&child.path, true)?.iter().any(|b| keep.contains(b)) {
+ hidden_bodies.extend(child.bodies.into_iter().map(|b| b.world).filter(|b| !keep.contains(b)));
+ stack.push(child.path);
+ } else {
+ hidden.push(child.path);
+ }
+ }
+ }
+ // A hidden Bodies folder or group a picked body is in would keep it hidden: those show
+ // again. Their other bodies are hidden one by one above, as the root's are.
+ let shown_folders = folders_holding(s, &keep);
+ s.visibility.hidden_folders.retain(|k| !shown_folders.contains(k));
+ s.visibility.hidden_occurrences = hidden;
+ s.visibility.hidden_bodies = hidden_bodies;
+ s.refresh_display();
+ Ok(json!({"visible":true,"isolated":keep,"visibility":s.visibility}))
+}
+
+/// The tree keys of the Bodies folders and groups (with the groups they are in) that hold the
+/// bodies named `world` in some placement.
+fn folders_holding(s: &Session, world: &[String]) -> std::collections::BTreeSet<String> {
+ let mut keys = std::collections::BTreeSet::new();
+ if s.visibility.hidden_folders.is_empty() {
+ return keys;
+ }
+ let st = s.model.state();
+ let names = solidcraft_doc::WorldNames::new(&s.doc, &st);
+ // Each body's component, found once rather than once per placement.
+ let owned: Vec<(u64, &str)> = s.doc.model_body_components(&st).into_iter().zip(st.bodies.iter().map(|b| b.name.as_str())).collect();
+ for (component, path, _) in s.doc.placements() {
+ let scope = Session::folder_scope(&path);
+ for b in owned.iter().filter(|(c, _)| *c == component).map(|(_, name)| *name) {
+ if !world.contains(&names.name(b, component, &path)) {
+ continue;
+ }
+ keys.insert(format!("{scope}/bodies"));
+ for g in s.doc.browser_groups.iter().filter(|g| g.component == component && g.folder == "bodies" && g.has_item(b)) {
+ for id in std::iter::successors(Some(g.id), |id| s.doc.browser_group(*id).and_then(|g| g.parent))
+ .take(solidcraft_doc::MAX_GROUP_NESTING + 1)
+ {
+ keys.insert(format!("{scope}/g{id}"));
+ }
+ }
+ }
+ }
+ keys
+}
+
fn visibility(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "browser.visibility";
let visible = bool_(p, "visible").ok_or_else(|| bad(cmd, "`visible` must be true or false"))?;
@@ -525,6 +608,9 @@ fn visibility(s: &mut Session, p: &Value) -> Result<Value> {
s.refresh_display();
return Ok(json!({"visible":visible,"changed":paths.len(),"visibility":s.visibility}));
}
+ if bool_(p, "isolate").unwrap_or(false) {
+ return isolate_bodies(s, p, visible);
+ }
let items: Vec<Shown> = match (str_(p, "folder"), p.get("items")) {
_ if p.get("features").is_some() => {
if ["items", "folder", "joints"].iter().any(|k| p.get(*k).is_some()) {
diff --git a/crates/engine/src/cmd/occurrence_tests.rs b/crates/engine/src/cmd/occurrence_tests.rs
index 0cfc771d14..b2c8552f90 100644
--- a/crates/engine/src/cmd/occurrence_tests.rs
+++ b/crates/engine/src/cmd/occurrence_tests.rs
@@ -541,6 +541,84 @@ fn isolate_nested_child_hides_ancestor_material_and_sibling() {
}
}
+/// #7896: isolating a body hides its own component's other bodies one by one and every other
+/// placement as a placement, so the Browser row of a component shows it is off and its eye
+/// brings it back. A second placement of the body's own component is another placement.
+/// #7896: isolating a body inside a hidden Bodies folder or group shows that folder and group
+/// again, so the body is visible; the folder's other bodies stay hidden.
+#[test]
+fn isolate_shows_the_hidden_folder_and_group_a_picked_body_is_in() {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10,"body_name":"Kept"}));
+ run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10,"corner":[20,0,0],"body_name":"Other","operation":"new"}));
+ let group = run(&mut s, "browser.group", json!({"folder": "bodies", "items": ["Kept"], "name": "Held"}));
+ let group = group["group"].as_u64().or_else(|| group["id"].as_u64()).expect("the group's id");
+ run(&mut s, "browser.visibility", json!({"tree_folders": ["c0/bodies", format!("c0/g{group}")], "visible": false}));
+ assert!(!s.body_visible("Kept") && !s.body_visible("Other"));
+ run(&mut s, "browser.visibility", json!({"items": ["Kept"], "visible": true, "isolate": true}));
+ assert!(s.body_visible("Kept"), "the picked body shows: {:?}", s.visibility.hidden_folders);
+ assert!(!s.body_visible("Other"), "the folder's other body stays hidden");
+ assert!(s.visibility.hidden_folders.is_empty(), "{:?}", s.visibility.hidden_folders);
+}
+
+#[test]
+fn isolate_bodies_hides_other_components_as_components() {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10,"corner":[-20,0,0],"body_name":"Root"}));
+ let comp = |s: &mut Session, name: &str| {
+ let c = run(s, "component.create", json!({"name": name, "parent": "root", "grounded": false}));
+ c["occurrence"].as_u64().unwrap()
+ };
+ let a = comp(&mut s, "CompA");
+ run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10,"body_name":"BoxA"}));
+ run(&mut s, "solid.box", json!({"length":2,"width":2,"height":2,"corner":[0,0,20],"body_name":"BoxA2"}));
+ let b = comp(&mut s, "CompB");
+ run(&mut s, "solid.box", json!({"length":10,"width":10,"height":10,"corner":[20,0,0],"body_name":"BoxB1"}));
+ run(&mut s, "solid.box", json!({"length":5,"width":5,"height":5,"corner":[40,0,0],"body_name":"BoxB2"}));
+ let a2 = run(&mut s, "occurrence.copy", json!({"occurrence":a,"translate":[0,40,0]}))["occurrence"].as_u64().unwrap();
+ let in_a = s.occurrence_bodies(&[a], false).unwrap();
+ let (box_a, box_a2) = (in_a.iter().find(|n| !n.contains("BoxA2")).unwrap().clone(), in_a.iter().find(|n| n.contains("BoxA2")).unwrap().clone());
+
+ let out = run(&mut s, "browser.visibility", json!({"visible":true,"items":[box_a],"isolate":true}));
+ assert_eq!(out["isolated"], json!([box_a]));
+ let mut occurrences = s.visibility.hidden_occurrences.clone();
+ occurrences.sort();
+ let mut others = vec![vec![b], vec![a2]];
+ others.sort();
+ assert_eq!(occurrences, others, "CompB and CompA's other placement go as placements");
+ let mut bodies = s.visibility.hidden_bodies.clone();
+ bodies.sort();
+ assert_eq!(bodies, vec![box_a2.clone(), "Root".to_string()], "only bodies beside BoxA go one by one");
+ assert!(s.body_visible(&box_a) && !s.body_visible(&box_a2) && !s.body_visible("Root"));
+ for name in s.occurrence_bodies(&[b], false).unwrap().iter().chain(&s.occurrence_bodies(&[a2], false).unwrap()) {
+ assert!(!s.body_visible(name), "{name}");
+ }
+ // CompB's eye brings it back whole.
+ run(&mut s, "browser.visibility", json!({"visible":true,"paths":[[b]]}));
+ assert!(s.occurrence_bodies(&[b], false).unwrap().iter().all(|n| s.body_visible(n)));
+
+ // Isolating a root body hides every placement as a placement and no body one by one.
+ run(&mut s, "browser.visibility", json!({"visible":true,"items":["Root"],"isolate":true}));
+ let mut occurrences = s.visibility.hidden_occurrences.clone();
+ occurrences.sort();
+ let mut all = vec![vec![a], vec![b], vec![a2]];
+ all.sort();
+ assert_eq!(occurrences, all);
+ assert!(s.visibility.hidden_bodies.is_empty() && s.body_visible("Root"), "{:?}", s.visibility.hidden_bodies);
+
+ // Only bodies isolate, and only shown; a refusal changes nothing.
+ let before = s.visibility.clone();
+ for p in [
+ json!({"visible":false,"items":["Root"],"isolate":true}),
+ json!({"visible":true,"items":["XY"],"isolate":true}),
+ json!({"visible":true,"items":["Nope"],"isolate":true}),
+ json!({"visible":true,"folder":"bodies","isolate":true}),
+ ] {
+ assert!(s.execute("browser.visibility", &p).is_err(), "{p}");
+ }
+ assert_eq!(s.visibility, before);
+}
+
#[test]
fn context_new_child_uses_inactive_parent_placement() {
let mut s = Session::default();
diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rs
index 583cae1b39..c165b05e9c 100644
--- a/crates/ui-egui/src/context_menu.rs
+++ b/crates/ui-egui/src/context_menu.rs
@@ -1507,14 +1507,10 @@ pub fn run_item(app: &mut SolveApp, item: &Item, at: Pos2) {
let _ = app.run("browser.visibility", json!({ "folder": "all", "visible": true }));
}
"ui.isolate" => {
- // The picked bodies stay (shown first if they were hidden), everything else goes.
- let others: Vec<String> =
- crate::open_meshing::session_world(&app.session).bodies.iter().map(|b| b.name.clone()).filter(|b| !bodies.contains(b)).collect();
+ // The picked bodies stay (shown if they were hidden) and everything else goes, other
+ // components as components (#7896), in one step.
if !bodies.is_empty() {
- let _ = app.run("browser.visibility", json!({ "items": bodies, "visible": true }));
- }
- if !others.is_empty() {
- let _ = app.run("browser.visibility", json!({ "items": others, "visible": false }));
+ let _ = app.run("browser.visibility", json!({ "items": bodies, "visible": true, "isolate": true }));
}
}
"ui.lock" => {