diff --git a/crates/ui-egui/src/browser.rs b/crates/ui-egui/src/browser.rs index c6e662b61..cad32c7be 100644 --- a/crates/ui-egui/src/browser.rs +++ b/crates/ui-egui/src/browser.rs @@ -765,10 +765,11 @@ impl Lit { // which does not rewrite the browser selection. let dialog_picks = app.dialog.as_ref().map(|d| d.items()).unwrap_or_default(); for s in app.session.selection.iter().chain(&dialog_picks) { - selected.insert(match s { - Sel::Body { name } | Sel::Face { body: name, .. } | Sel::Edge { body: name, .. } | Sel::Vertex { body: name, .. } => { - Thing::Body(name.clone()) - } + let thing = match s { + Sel::Body { name } => Thing::Body(name.clone()), + // A picked face, edge or vertex lights the feature it belongs to (see + // `browser::features`), not the whole body as if the body were selected. + Sel::Face { .. } | Sel::Edge { .. } | Sel::Vertex { .. } => continue, Sel::Feature { id } => Thing::Sketch(*id), Sel::Profile { sketch, .. } | Sel::ExternalSketchCurve { sketch, .. } | Sel::ExternalSketchPoint { sketch, .. } => { Thing::Sketch(*sketch) @@ -783,7 +784,8 @@ impl Lit { Sel::Axis { name } => Thing::Axis(name.clone()), Sel::ConstructionPoint { name } => Thing::Point(name.clone()), Sel::Occurrence { .. } => continue, - }); + }; + selected.insert(thing); } let hovered = match &app.viewport.hover { Some(Hit::Face { body, .. } | Hit::Edge { body, .. } | Hit::Vertex { body, .. }) => Some(Thing::Body(body.clone())), diff --git a/crates/ui-egui/src/browser/features.rs b/crates/ui-egui/src/browser/features.rs index 424fef29d..a4d269c76 100644 --- a/crates/ui-egui/src/browser/features.rs +++ b/crates/ui-egui/src/browser/features.rs @@ -250,7 +250,11 @@ fn feature_row( let sketch = crate::timeline::TimelineKind::of(&feature.kind).profile_sketch(); // Behind the history marker or suppressed: not built, drawn faint as in the timeline. let dim = feature.suppressed || app.session.doc.marker.is_some_and(|marker| index >= marker); - let selected = app.session.selection.contains(&Sel::Feature { id }); + // A feature picked by its geometry (a face or edge of it chosen in the view) shows as + // selected in the tree, as NX marks the feature of a picked face; the session's selection + // stays the face or edge. + let by_geometry = !app.session.selection.contains(&Sel::Feature { id }) && crate::timeline::picked_feature(app) == Some(id); + let selected = app.session.selection.contains(&Sel::Feature { id }) || by_geometry; app.tree.remember(&format!("f:{id}")); let action = sketch.map(|_| ("sketch", "Edit Profile Sketch")); // A sketch row has the eye its Sketches folder row has. A feature's eye shows and hides the diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 4516bdb69..1d95d04e1 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -2806,12 +2806,63 @@ fn feature_parts(app: &SolveApp, id: u64, shown_only: bool) -> Vec { let names = solidcraft_engine::doc::naming::face_names(b); let m = b.mesh(); for (index, _) in names.iter().enumerate().filter(|(_, n)| solidcraft_engine::doc::persistent_name::made_by(n) == Some(id)) { - if let Some(point) = m.triangles.iter().zip(&m.tri_face).find_map(|(t, f)| { - if *f as usize != index { - return None; + if let Some(point) = first_triangle_point(&m, index) { + out.push(Sel::Face { body: b.name.clone(), index, point }); + } + } + } + if out.is_empty() { + // A later edit renamed the faces this feature made (an Offset Face, Replace Face or + // Delete Face leaves them named after whatever last touched them): light the faces it + // changed, found by comparing the states either side of it. + out = changed_faces(app, id, &st, shown_only); + } + out +} + +/// A point on face `index` of `m`: the middle of its first triangle. +fn first_triangle_point(m: &solidcraft_engine::geom::Mesh, index: usize) -> Option { + m.triangles.iter().zip(&m.tri_face).find_map(|(t, f)| (*f as usize == index).then(|| m.tri(t)).flatten().map(|[a, b, c]| (a + b + c) / 3.0)) +} + +/// The faces a feature changed, for the states either side of it, on the bodies as they are now: +/// a face whose name is new, or whose area, centroid or plane moved. A face a later feature +/// renamed is found again by its plane and nearest centre. +fn changed_faces(app: &SolveApp, id: u64, now: &solidcraft_engine::doc::ModelState, shown_only: bool) -> Vec { + let Some(index) = app.session.doc.features.iter().position(|f| f.id == id) else { return Vec::new() }; + let after = match app.session.doc.features.get(index + 1) { + Some(next) => app.session.model.state_before(next.id), + None => app.session.model.state(), + }; + let before = app.session.model.state_before(id); + let mut out = Vec::new(); + for b in &after.bodies { + if shown_only && (!app.session.body_visible(&b.name) || app.preview.effective_replaced().contains(&b.name)) { + continue; + } + let (Some(now_body), Some(old)) = (now.body(&b.name), before.body(&b.name)) else { continue }; + let tol = (b.body.size() * 1e-3).max(1e-4); + let (Ok(new_faces), Ok(old_faces)) = (b.body.faces(tol), old.body.faces(tol)) else { continue }; + let names = solidcraft_engine::doc::naming::face_names(b); + let old_names = solidcraft_engine::doc::naming::face_names(old); + for (i, name) in names.iter().enumerate() { + let Some(new) = new_faces.get(i) else { continue }; + // A face the feature moved takes a new name (Offset Face); a face it only trimmed + // or stretched keeps its own. Either is a face the feature changed. + let moved = match old_names.iter().position(|old| old == name) { + None => true, + Some(j) => { + let Some(was) = old_faces.get(j) else { continue }; + (new.area - was.area).abs() > new.area.max(1e-9) * 1e-4 + || new.centroid.dist(was.centroid) > tol + || matches!((new.plane_normal, was.plane_normal), (Some(a), Some(b)) if a.dot(b) < 1.0 - 1e-7) } - m.tri(t).map(|[a, b, c]| (a + b + c) / 3.0) - }) { + }; + if !moved { + continue; + } + let Some((index, point)) = current_face(now_body, name, new, tol) else { continue }; + if !out.iter().any(|s| matches!(s, Sel::Face { body, index: i, .. } if body == &b.name && *i == index)) { out.push(Sel::Face { body: b.name.clone(), index, point }); } } @@ -2819,6 +2870,34 @@ fn feature_parts(app: &SolveApp, id: u64, shown_only: bool) -> Vec { out } +/// Face `name` of `now` (or the nearest face on its plane when a later feature renamed it), +/// with a point on it. +fn current_face( + now: &solidcraft_engine::doc::ModelBody, + name: &str, + sample: &solidcraft_engine::kernel::FaceInfo, + tol: f64, +) -> Option<(usize, solidcraft_engine::geom::Vec3)> { + let m = now.mesh(); + let names = solidcraft_engine::doc::naming::face_names(now); + if let Some(index) = names.iter().position(|n| n == name) { + return first_triangle_point(&m, index).map(|point| (index, point)); + } + let normal = sample.plane_normal?; + let faces = now.body.faces(tol).ok()?; + // The same face, moved on by later edits: the nearest centre on a parallel plane, a face + // still on the plane it was on winning over one that only faces the same way. + let mut best: Option<(f64, f64, usize)> = None; + for f in faces.iter().filter(|f| f.plane_normal.is_some_and(|n| n.dot(normal) > 1.0 - 1e-7)) { + let (off, d) = ((f.centroid - sample.centroid).dot(normal).abs(), f.centroid.dist(sample.centroid)); + if best.is_none_or(|b| off.total_cmp(&b.0).then(d.total_cmp(&b.1)).is_lt()) { + best = Some((off, d, f.index)); + } + } + let index = best?.2; + first_triangle_point(&m, index).map(|point| (index, point)) +} + /// The sketches picked among `items` (a sketch row in the Browser or the timeline) that the /// highlight draws, shown or hidden (#7156). The sketch being edited shows its own selection. fn selected_sketches(app: &SolveApp, items: &[Sel]) -> Vec { @@ -12245,3 +12324,22 @@ mod standalone_point_tests { assert!(standalone_points(&app).is_empty(), "sketches hidden"); } } + +#[cfg(test)] +mod feature_highlight_tests { + use super::*; + + /// A feature's faces take a new name when a later Offset Face moves them again; selecting + /// the feature still lights the faces it changed. + #[test] + fn a_feature_whose_face_was_renamed_later_still_lights_it() { + let mut s = solidcraft_engine::Session::default(); + s.execute("solid.box", &json!({"length": 40, "width": 30, "height": 20, "body_name": "Base"})).unwrap(); + let first = + s.execute("solid.offset_face", &json!({"faces": [[20, 15, 20]], "distance": 5, "name": "Raise"})).unwrap()["feature"].as_u64().unwrap(); + s.execute("solid.offset_face", &json!({"faces": [[20, 15, 25]], "distance": 2, "name": "RaiseAgain"})).unwrap(); + let app = SolveApp::new(s, crate::Services::default()); + let parts = feature_parts(&app, first, true); + assert!(parts.iter().any(|p| matches!(p, Sel::Face { .. })), "{parts:?}"); + } +}