Light a feature's faces when it is selected, even after a later edit renamed them (#9848)

* Light a feature's faces after a later edit renamed them

Selecting a feature in the Browser or the timeline lights its geometry on the
model, by the face names the feature gave (`F<feature>:…`). A later edit that
moves or renames those faces (Offset Face, Replace Face, Delete Face) leaves
them named after whatever last touched them, so selecting the earlier feature
lit nothing.

When no face carries the feature's name, compare the model states either side
of it instead: a face whose name is new, or whose area, centroid or plane
moved, is a face the feature changed. Each is found on the body as it is now
by its name, or by a parallel plane and nearest centre, so a face a later
feature moved again is still lit.

The Browser row of the feature a picked face belongs to already lights; that
is unchanged (a body row lights too, as #3009 asks).

Test: a box raised twice with Offset Face; selecting the first raise lights
the face the second one renamed.

* Browser: light the feature of a picked face, not its whole body

Picking a face in the view lit the body's row as if the body were selected,
which in NX is not what a face pick shows: the Part Navigator marks the
feature the face belongs to. Faces, edges and vertices no longer light their
body row; the feature row of the picked face shows as selected instead
(`picked_feature`), while the session's selection stays the face or edge, so
Replace Face, sketches and the rest keep the pick they take.

The live face of the view still lights the face itself; the timeline already
marked the feature of a picked face and is unchanged.

Tests: the browser's 178 tests pass (selection arrays unchanged; only the
rows lit differ).

---------

Co-authored-by: clarkliu <277482660@qq.com>
This commit is contained in:
clark19990
2026-10-31 16:26:05 +00:00
committed by GitHub
parent d95b8208b3
commit b410c577e0
3 changed files with 115 additions and 11 deletions
+7 -5
View File
@@ -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())),
+5 -1
View File
@@ -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
+103 -5
View File
@@ -2806,12 +2806,63 @@ fn feature_parts(app: &SolveApp, id: u64, shown_only: bool) -> Vec<Sel> {
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<solidcraft_engine::geom::Vec3> {
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<Sel> {
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<Sel> {
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<u64> {
@@ -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:?}");
}
}