Skip to content

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

Browser: a feature's eye shows or hides the bodies it made (#5099) (#6109)

DuBearte3918f23c3d4a
diff --git a/crates/engine/src/cmd/browser.rs b/crates/engine/src/cmd/browser.rs
index 4ad70c863a..5bd8a8836c 100644
--- a/crates/engine/src/cmd/browser.rs
+++ b/crates/engine/src/cmd/browser.rs
@@ -45,6 +45,7 @@ pub static COMMANDS: &[CommandSpec] = &[
CommandSpec::new("browser.visibility", "Show/Hide", visibility).icon("eye").params(
"visible: bool; items?: [sketch id or name | body name | construction name | joint name | O|X|Y|Z|XY|XZ|YZ]; \
or joints?: [joint id or name] (their glyphs; they still constrain and move parts); \
+ 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] (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)",
@@ -314,6 +315,12 @@ fn visibility(s: &mut Session, p: &Value) -> Result<Value> {
return Ok(json!({"visible":visible,"changed":paths.len(),"visibility":s.visibility}));
}
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()) {
+ return Err(bad(cmd, "give `features`, `items`, `folder` or `joints`, not several"));
+ }
+ feature_items(s, p, cmd)?
+ }
_ if p.get("joints").is_some() => {
if p.get("items").is_some() || p.get("folder").is_some() {
return Err(bad(cmd, "give `joints`, `items` or `folder`, not several"));
@@ -570,6 +577,44 @@ fn collapse_all(s: &mut Session, p: &Value) -> Result<Value> {
fold_tree(s, p, false, "browser.collapse_all")
}
+/// `features` (and `path`) of `browser.visibility`: the bodies those features made (#5099).
+fn feature_items(s: &Session, p: &Value, cmd: &str) -> Result<Vec<Shown>> {
+ let keys = keys(p, "features", cmd)?;
+ if keys.is_empty() {
+ return Err(bad(cmd, "`features` must list feature ids or names"));
+ }
+ let mut ids = Vec::with_capacity(keys.len());
+ let mut component = None;
+ for k in &keys {
+ let f = s.doc.find_feature(k).ok_or_else(|| bad(cmd, format!("no feature `{k}`")))?;
+ if matches!(f.kind, FeatureKind::Sketch { .. }) {
+ return Err(bad(cmd, format!("`{k}` is a sketch: give it in `items`")));
+ }
+ ids.push(f.id);
+ component = Some(f.component);
+ }
+ let path = match p.get("path") {
+ None | Some(Value::Null) => None,
+ Some(v) => {
+ let path = v
+ .as_array()
+ .filter(|a| a.len() <= 64)
+ .and_then(|a| a.iter().map(Value::as_u64).collect::<Option<Vec<_>>>())
+ .ok_or_else(|| bad(cmd, "`path` must list at most 64 occurrence ids"))?;
+ let placement = s.doc.placements().into_iter().find(|(_, at, _)| *at == path).ok_or_else(|| bad(cmd, "no placement at `path`"))?;
+ if ids.iter().any(|id| s.doc.feature(*id).is_some_and(|f| f.component != placement.0)) {
+ return Err(bad(cmd, "`path` must be a placement of the features' component"));
+ }
+ Some((component.unwrap_or(placement.0), path))
+ }
+ };
+ let bodies = s.feature_bodies(&ids, path.as_ref().map(|(c, at)| (*c, at.as_slice())));
+ if bodies.is_empty() {
+ return Err(bad(cmd, "those features made no bodies that are in the design now (a feature that changes bodies shows and hides with them)"));
+ }
+ Ok(bodies.into_iter().map(Shown::Body).collect())
+}
+
fn document_units(s: &mut Session, p: &Value) -> Result<Value> {
let cmd = "document.units";
let u = str_(p, "units").ok_or_else(|| bad(cmd, "`units` is required"))?;
@@ -1922,4 +1967,38 @@ mod tests {
}
assert!(s.execute("browser.expand_all", &json!({})).is_ok());
}
+
+ /// #5099: a feature's bodies show and hide by the feature, in every placement or one.
+ #[test]
+ fn a_feature_shows_and_hides_the_bodies_it_made() {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 10, "width": 10, "height": 10}));
+ run(&mut s, "solid.sphere", json!({"diameter": 4, "center": [30, 0, 0]}));
+ // A join makes no body of its own.
+ run(&mut s, "solid.box", json!({"length": 2, "width": 2, "height": 20, "operation": "join"}));
+ let hide = |s: &mut Session, p: Value| run(s, "browser.visibility", p)["visibility"]["hidden_bodies"].clone();
+ assert_eq!(hide(&mut s, json!({"visible": false, "features": ["Sphere1"]})), json!(["Body2"]));
+ assert_eq!(hide(&mut s, json!({"visible": true, "features": ["Sphere1"]})), json!([]));
+ for (p, why) in [
+ (json!({"visible": false, "features": ["Box2"]}), "made no bodies"),
+ (json!({"visible": false, "features": ["Nope"]}), "no feature"),
+ (json!({"visible": false, "features": ["Box1"], "items": ["Body2"]}), "not several"),
+ ] {
+ let e = s.execute("browser.visibility", &p).expect_err("refused").to_string();
+ assert!(e.contains(why), "{p}: {e}");
+ }
+
+ // A component placed twice: every instance's body, or one placement's.
+ let first = run(&mut s, "component.create", json!({"name": "Part", "grounded": false}))["occurrence"].as_u64().unwrap();
+ run(&mut s, "solid.box", json!({"length": 5, "width": 5, "height": 5, "corner": [0, 40, 0]}));
+ let second = run(&mut s, "occurrence.copy", json!({"occurrence": first}))["occurrence"].as_u64().unwrap();
+ let all = hide(&mut s, json!({"visible": false, "features": ["Box3"]}));
+ assert_eq!(all.as_array().unwrap().len(), 2, "{all}");
+ hide(&mut s, json!({"visible": true, "features": ["Box3"]}));
+ let one = hide(&mut s, json!({"visible": false, "features": ["Box3"], "path": [second]}));
+ assert_eq!(one.as_array().unwrap().len(), 1, "{one}");
+ assert!(all.as_array().unwrap().contains(&one[0]) && one[0] != all[0], "the copy's body: {one} of {all}");
+ let e = s.execute("browser.visibility", &json!({"visible": false, "features": ["Sphere1"], "path": [first]})).expect_err("other component");
+ assert!(e.to_string().contains("component"), "{e}");
+ }
}
diff --git a/crates/engine/src/occurrences.rs b/crates/engine/src/occurrences.rs
index 1411091668..29c0ba8306 100644
--- a/crates/engine/src/occurrences.rs
+++ b/crates/engine/src/occurrences.rs
@@ -253,6 +253,30 @@ impl Session {
Ok(out)
}
+ /// The world names of the bodies `features` made (#5099), in every placement of their
+ /// component, or only in `placement` (a component and the path of one of its placements).
+ pub fn feature_bodies(&self, features: &[u64], placement: Option<(u64, &[u64])>) -> Vec<String> {
+ let state = self.model.state();
+ let made: Vec<_> = state.bodies.iter().filter(|b| features.contains(&b.feature)).collect();
+ if made.is_empty() {
+ return Vec::new();
+ }
+ let placements = match placement {
+ Some((component, path)) => vec![(component, path.to_vec())],
+ None => self.doc.placements().into_iter().map(|(component, path, _)| (component, path)).collect(),
+ };
+ let mut out = Vec::new();
+ for (component, path) in &placements {
+ for b in made.iter().filter(|b| self.doc.model_body_component(b) == *component) {
+ let name = self.doc.world_body_name(&state, &b.name, *component, path);
+ if !out.contains(&name) {
+ out.push(name);
+ }
+ }
+ }
+ out
+ }
+
/// Selected authoring placement, or the compatible first-placement frame.
pub fn active_component_frame(&self) -> Mat {
if let Some(path) = &self.active_occurrence_path
diff --git a/crates/ui-egui/src/browser/features.rs b/crates/ui-egui/src/browser/features.rs
index eda9a0c1fc..5ebae12a0a 100644
--- a/crates/ui-egui/src/browser/features.rs
+++ b/crates/ui-egui/src/browser/features.rs
@@ -80,11 +80,24 @@ pub(super) fn rows(app: &mut SolveApp, ui: &mut egui::Ui, component: u64, path:
let selected = app.session.selection.contains(&Sel::Feature { id });
app.tree.remember(&format!("f:{id}"));
let action = sketch.map(|_| ("sketch", "Edit Profile Sketch"));
+ // The bodies it made show and hide with its eye (#5099); a component listed on its own
+ // stands for all of its placements.
+ let placement = (component == 0 || !path.is_empty()).then_some((component, path));
+ let bodies = app.session.feature_bodies(&[id], placement);
+ let visible = bodies.iter().any(|b| !app.session.visibility.hidden_bodies.contains(b));
+ let eye = (!bodies.is_empty()).then_some(visible);
let r = draw_row(
ui,
ui.id().with(("browser_feature", id)),
- &Row { depth: depth + 1, icon, label: &label, selected, dim, action, ..Default::default() },
+ &Row { depth: depth + 1, eye, icon, label: &label, selected, dim: dim || eye == Some(false), action, ..Default::default() },
);
+ if r.eye {
+ let mut p = json!({"visible": !visible, "features": [id]});
+ if !path.is_empty() {
+ p["path"] = json!(path);
+ }
+ let _ = app.run("browser.visibility", p);
+ }
if r.action
&& let Some(sketch) = sketch
{
@@ -131,6 +144,19 @@ mod tests {
drawn_rows().into_iter().map(|(label, ..)| label).collect()
}
+ /// #5099: a feature row's eye shows and hides the bodies the feature made.
+ #[test]
+ fn a_feature_rows_eye_hides_the_bodies_it_made() {
+ let mut h = Harness::new();
+ h.app.run("solid.box", json!({"length":10,"width":10,"height":10,"body_name":"Plate"})).unwrap();
+ h.app.tree.expanded.insert("c0/features".into());
+ h.settle();
+ h.step(&json!({"click":{"handle":"eye:Box1"}})).unwrap();
+ assert_eq!(h.app.session.visibility.hidden_bodies, ["Plate"]);
+ h.step(&json!({"click":{"handle":"eye:Box1"}})).unwrap();
+ assert!(h.app.session.visibility.hidden_bodies.is_empty());
+ }
+
#[test]
fn browser_feature_sketch_only_has_one_sketch_row_and_no_features_folder() {
let mut h = Harness::new();