fix(measure): select imported mesh outlines and endpoints (#7033)

This commit is contained in:
cronos
2026-10-26 10:40:41 +00:00
committed by GitHub
parent 4a0d519727
commit a0f7529fd6
2 changed files with 91 additions and 2 deletions
@@ -201,3 +201,50 @@ fn the_distance_row_has_a_copy_button_that_copies_the_shown_value() {
}
assert_eq!(copied, [want]);
}
/// STL outlines are real distances even though they are not B-rep operands.
#[test]
fn measure_imported_stl_edges_and_vertices_from_viewport_clicks() {
let mut h = Harness::new();
h.app.home.open = false;
h.app.set_origin_shown(false);
h.app.ui.show_browser = false;
let path = h.dir.join("measure.stl");
h.app.run("solid.box", json!({"length":20,"width":30,"height":10})).unwrap();
h.app.run("file.save_mesh", json!({"path":path})).unwrap();
h.app.run("file.new", json!({})).unwrap();
let imported = h.app.run("file.insert_mesh", json!({"path":path})).unwrap();
h.app.run("component.activate", json!({"component":"root"})).unwrap();
h.app.run("occurrence.copy", json!({"component":imported["component"],"translate":[40,0,0]})).unwrap();
h.call("ui.view", json!({"view":"iso"}));
h.call("ui.view", json!({"view":"fit"}));
h.frames(4);
for offset in [0., 40.] {
h.app.start("inspect.measure");
h.step(&json!({"click":{"world":[offset + 20.,0,5]}})).unwrap();
assert!(
matches!(h.app.dialog.as_ref().unwrap().inputs[0].items.as_slice(), [Sel::Edge { .. }]),
"must select the outline, not the whole STL face"
);
assert_eq!(result(&h)["items"][0]["type"], "edge");
assert!((result(&h)["items"][0]["length_mm"].as_f64().unwrap() - 10.).abs() < 1e-6);
let texts = painted_text(&mut h);
assert!(texts.iter().any(|text| text.contains("10.00 mm")), "{texts:?}");
h.step(&json!({"click":{"world":[offset,0,5]}})).unwrap();
assert!(h.app.dialog.as_ref().unwrap().inputs[0].items.iter().all(|s| matches!(s, Sel::Edge { .. })));
assert!((result(&h)["distance_mm"].as_f64().unwrap() - 20.).abs() < 1e-6);
assert!(result(&h)["angle_deg"].as_f64().unwrap().abs() < 1e-6);
h.call("ui.key", json!({"key":"Escape"}));
h.app.session.selection.clear();
h.app.start("inspect.measure");
for x in [offset, offset + 20.] {
h.step(&json!({"click":{"world":[x,0,10]}})).unwrap();
}
assert!(h.app.dialog.as_ref().unwrap().inputs[0].items.iter().all(|s| matches!(s, Sel::Vertex { .. })));
assert_eq!(h.app.dialog.as_ref().unwrap().inputs[0].items.len(), 2);
assert!((result(&h)["distance_mm"].as_f64().unwrap() - 20.).abs() < 1e-6);
h.call("ui.key", json!({"key":"Escape"}));
h.app.session.selection.clear();
}
}
+44 -2
View File
@@ -1556,6 +1556,16 @@ pub(crate) fn screen_box(proj: &Proj, b: &solidcraft_engine::geom::Aabb3, px: f3
Some(r.expand(px))
}
/// Mesh outlines have no B-rep identity, but their points and lengths can be measured.
/// Keep them out of modeling operands and invalidate hover picks when Measure opens/closes.
fn measuring_mesh_edges(app: &SolveApp) -> bool {
app.dialog.as_ref().is_some_and(|d| matches!(d.kind, crate::dialogs::Kind::Measure { .. }))
}
fn edge_pickable(app: &SolveApp, mesh: &solidcraft_engine::geom::Mesh, index: usize) -> bool {
mesh.edge_pickable(index) || (measuring_mesh_edges(app) && mesh.display_only_edges && mesh.edge_visible(index))
}
/// What is under `pos`, reusing the last answer while the cursor, the camera and the model are
/// unchanged.
pub(crate) fn pick_cached(app: &mut SolveApp, proj: &Proj, pos: Pos2) -> Vec<Hit> {
@@ -1570,6 +1580,7 @@ pub(crate) fn pick_cached(app: &mut SolveApp, proj: &Proj, pos: Pos2) -> Vec<Hit
app.session.active_sketch.hash(&mut h);
app.tool.as_ref().map(|tool| tool.cmd).hash(&mut h);
app.dialog.is_none().hash(&mut h);
measuring_mesh_edges(app).hash(&mut h);
app.preferences.prefer_free_sketch_points.hash(&mut h);
app.viewport.woken_centre.hash(&mut h);
crate::sketch3d::drawing(app).hash(&mut h);
@@ -1894,7 +1905,7 @@ fn pick_hits(app: &SolveApp, proj: &Proj, pos: Pos2, other_sketch_points: bool,
let mut last_edge = None;
for (ei, si) in near {
let Some(e) = m.edges.get(ei) else { continue };
if !m.edge_pickable(ei) {
if !edge_pickable(app, &m, ei) {
continue;
}
if last_edge != Some(ei) {
@@ -4397,7 +4408,7 @@ fn box_select(app: &mut SolveApp, proj: &Proj, bx: BoxSel, add: bool) {
for (ei, e) in m.edges.iter().enumerate() {
let rim = m.circular_rim(ei);
let takes = if bx.crossing { bx.takes(&to2(e)) } else { rim.iter().all(|&i| m.edges.get(i).is_some_and(|p| bx.takes(&to2(p)))) };
if m.edge_pickable(ei) && takes {
if edge_pickable(app, &m, ei) && takes {
out.push(Sel::Edge { body: b.name.clone(), index: ei, point: polyline_mid(e) });
}
}
@@ -6108,6 +6119,37 @@ mod tests {
}
}
#[test]
fn mesh_outline_hover_cache_follows_measure_and_preserves_modeling_picks() {
let h = crate::scenario::Harness::new();
let path = h.dir.join("box.stl");
let mut app = SolveApp::new(solidcraft_engine::Session::default(), crate::Services::default());
app.run("solid.box", json!({"length":20,"width":30,"height":10})).unwrap();
app.run("file.save_mesh", json!({"path":path})).unwrap();
app.run("file.new", json!({})).unwrap();
app.run("file.insert_mesh", json!({"path":path})).unwrap();
app.set_origin_shown(false);
app.fit_view();
let proj = projection(&app, Rect::from_min_size(Pos2::ZERO, vec2(800., 600.)));
let pos = proj.to_screen(Vec3::new(20., 0., 5.)).unwrap();
let measure = crate::dialogs::Dialog::for_command(&app, "inspect.measure").unwrap();
let mut fillet = crate::dialogs::Dialog::for_command(&app, "solid.fillet").unwrap();
// Identical input masks must not make the two dialogs share cached geometry hits.
fillet.inputs[0].accept = measure.inputs[0].accept;
app.dialog = Some(fillet.clone());
assert!(!pick_cached(&mut app, &proj, pos).iter().any(|h| matches!(h, Hit::Edge { .. })));
app.dialog = Some(measure);
assert!(matches!(pick_cached(&mut app, &proj, pos).first(), Some(Hit::Edge { .. })));
let hits = pick_all(&app, &proj, pos);
assert!(hits.iter().any(|h| matches!(h, Hit::Edge { .. })), "Select Other also offers the measured outline");
app.dialog = Some(fillet);
assert!(!pick_cached(&mut app, &proj, pos).iter().any(|h| matches!(h, Hit::Edge { .. })));
app.dialog = None;
let hits = pick_cached(&mut app, &proj, pos);
assert!(!hits.iter().any(|h| matches!(h, Hit::Edge { .. } | Hit::Vertex { .. })));
assert!(app.session.world_state().bodies[0].mesh().display_only_edges);
}
/// Manual GPU benchmark: 25 placements of the mesh `SOLIDCRAFT_BENCH_MESH` (an STL, OBJ or
/// 3MF) drawn offscreen at 3600 × 2190 px (divided by `SOLIDCRAFT_BENCH_DIV`) with 4× MSAA
/// while the camera turns, each frame waited for: the placements' own triangles (`merged`, as