SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Highlight the selected sketch in the viewport (#7156) (#7347)
semmelrindefb7ca86dc748
diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rsindex 41488a2e74..08c3b40334 100644--- a/crates/ui-egui/src/viewport.rs+++ b/crates/ui-egui/src/viewport.rs@@ -1538,17 +1538,7 @@ fn build_sketch_scene(app: &SolveApp, proj: &Proj) -> GpuScene {for (pts, col, cons) in sketch_lines_with_sampler(&ss.sketch, &ss.plane, active, &ss.report.curve_determined, |_, index, plane| {crate::sketch_display::curve_sample(app, sid, index, plane, proj).parts.clone()}) {- let segs = sketch_segments(- &pts,- if !cons {- 0- } else if col == colors::SKETCH_CENTERLINE {- 2- } else {- 1- },- unit,- );+ let segs = sketch_segments(&pts, sketch_line_mode(col, cons), unit);let haloed = app.preferences.sketch_halo && !active && col != colors::SKETCH_PROJECTED && col != colors::SKETCH_LOST;strokes.push((segs, sc.color(sketch_color(&tk, col)), if cons { 1.2 } else { 2.0 }, haloed));}@@ -1629,6 +1619,18 @@ fn ground_shadow(sc: &mut GpuScene, app: &SolveApp, st: &std::sync::Arc<solidcra}}+/// The [`sketch_segments`] line type of a [`sketch_lines_with_sampler`] polyline: solid,+/// construction (dashed) or centerline (dash-dot).+fn sketch_line_mode(col: Rgb, cons: bool) -> u8 {+ if !cons {+ 0+ } else if col == colors::SKETCH_CENTERLINE {+ 2+ } else {+ 1+ }+}+/// Sketch line types share the same world-space dash phase in the scene and overlays.pub(crate) fn sketch_segments(pts: &[Vec3], mode: u8, unit: f64) -> Vec<(Vec3, Vec3)> {match mode {@@ -2468,6 +2470,23 @@ fn feature_parts(app: &SolveApp, id: u64, shown_only: bool) -> Vec<Sel> {out}+/// 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> {+ let st = crate::open_meshing::shown_world(app);+ let mut out = Vec::new();+ for item in items {+ if let Sel::Feature { id } = item+ && app.session.active_sketch != Some(*id)+ && st.sketch(*id).is_some()+ && !out.contains(id)+ {+ out.push(*id);+ }+ }+ out+}+fn displayed_selection(app: &SolveApp, item: &Sel) -> Vec<Sel> {match item {Sel::Feature { id } => feature_geometry(app, *id),@@ -3025,7 +3044,8 @@ fn highlight_key(app: &SolveApp) -> u64 {// Body triangles and outlines live in world space; the GPU applies the new camera.// Only sketch feedback needs projected tessellation and the profile's lift toward the eye.let projected = selected.iter().any(|s| matches!(s, Sel::Profile { .. } | Sel::SketchCurve { .. } | Sel::ExternalSketchCurve { .. }))- || matches!(app.viewport.hover, Some(Hit::Profile { .. }));+ || matches!(app.viewport.hover, Some(Hit::Profile { .. }))+ || !selected_sketches(app, &selected).is_empty();if projected {crate::sketch_display::projection_key(&display_proj(app), app.viewport.pixels_per_point()).hash(&mut h);}@@ -3224,9 +3244,11 @@ pub(crate) fn build_highlight(app: &SolveApp) -> GpuScene {let active_bodies = s.active_occurrence_path.as_ref().and_then(|p| s.occurrence_bodies(p, true).ok());// Faces lit over the model use the triangles the model shows them with.let shown = |b: &solidcraft_engine::doc::ModelBody| shown_mesh(app, b, &active_bodies);+ let items = app.highlighted();+ let sketches = selected_sketches(app, &items);let mut sel = Vec::new();- for item in app.highlighted() {- for piece in displayed_selection(app, &item) {+ for item in &items {+ for piece in displayed_selection(app, item) {if !sel.iter().any(|x| crate::selection::same_item(x, &piece)) {sel.push(piece);}@@ -3318,7 +3340,8 @@ pub(crate) fn build_highlight(app: &SolveApp) -> GpuScene {}// Reference widgets follow zoom; the often much larger world-space feedback does not.let projected = sel.iter().any(|s| matches!(s, Sel::Profile { .. } | Sel::SketchCurve { .. } | Sel::ExternalSketchCurve { .. }))- || matches!(hover, Some(Hit::Profile { .. }));+ || matches!(hover, Some(Hit::Profile { .. }))+ || !sketches.is_empty();if !projected&& let Some(cached) = app.viewport.hover_block.lock().ok().and_then(|cache| {cache.as_ref().filter(|c| c.key == key && std::sync::Arc::ptr_eq(&c.state, &st) && std::sync::Arc::ptr_eq(&c.doc, &s.doc)).cloned()@@ -3419,6 +3442,19 @@ pub(crate) fn build_highlight(app: &SolveApp) -> GpuScene {_ => {}}}+ // A sketch picked in the Browser or the timeline: all its curves in their line types, over+ // the model, also while the sketch is hidden or inside a body, so it is easy to find.+ let (unit, proj) = (grid_step(app.cam.half_height()).0, display_proj(app));+ let sel_edge = feedback.color(c4(t.sel_edge));+ for ss in sketches.iter().filter_map(|id| st.sketch(*id)) {+ for (pts, col, cons) in sketch_lines_with_sampler(&ss.sketch, &ss.plane, false, &[], |_, index, plane| {+ crate::sketch_display::curve_sample(app, ss.feature, index, plane, &proj).parts.clone()+ }) {+ for (a, b) in sketch_segments(&pts, sketch_line_mode(col, cons), unit) {+ feedback.line(a.to_f32(), b.to_f32(), sel_edge, 3.5, true);+ }+ }+ }// Timeline modifiers light only their resulting faces; browser rows light whole bodies.let mut hovered = app.viewport.hover_feature.map(|id| feature_geometry(app, id)).unwrap_or_default();hovered.extend(app.viewport.hover_bodies.iter().map(|name| Sel::Body { name: name.clone() }));@@ -6673,6 +6709,51 @@ mod tests {}}+ /// A sketch picked in the timeline or the Browser lights all its curves in the selection+ /// colour, also while it is hidden (#7156); nothing is lit once it is no longer picked.+ #[test]+ fn a_picked_sketch_lights_its_curves_shown_or_hidden() {+ let mut h = crate::scenario::Harness::new();+ h.app.set_origin_shown(false);+ h.app.run("solid.box", json!({"length":20,"width":20,"height":20})).unwrap();+ h.app.run("sketch.create", json!({"plane":"XY","name":"Outline"})).unwrap();+ h.app.run("sketch.rectangle.two_point", json!({"p0":[30,0],"p1":[40,10]})).unwrap();+ h.app.run("sketch.circle.center", json!({"center":[35,5],"radius":3,"construction":true})).unwrap();+ h.app.run("sketch.finish", json!({})).unwrap();+ let id = h.app.session.doc.find_feature("Outline").unwrap().id;+ h.frames(2);+ let sel_edge = c4(Tokens::get().sel_edge);+ // The selection-coloured strokes of the highlight, drawn over the model (a line holds its+ // colour's palette index).+ let lit = |app: &SolveApp| {+ let scene = build_highlight(app).merged();+ scene+ .overlay+ .as_chunks::<{ crate::gpu::LINE_SIZE }>()+ .0+ .iter()+ .filter(|line| {+ let index = line.get(24..28).and_then(|b| b.try_into().ok()).map_or(0, u32::from_le_bytes);+ scene.palette.get(crate::gpu::PaletteIndex(index)) == Some(sel_edge)+ })+ .count()+ };+ assert_eq!(lit(&h.app), 0, "nothing is picked");+ let p = crate::scenario::handle_at("timeline:Outline").unwrap();+ h.step(&json!({"click":[p.x,p.y]})).unwrap();+ assert_eq!(h.app.session.selection, vec![Sel::Feature { id }]);+ let shown = lit(&h.app);+ // Four sides, the circle's many segments and its dashes.+ assert!(shown > 20, "the picked sketch's curves are lit: {shown}");+ h.app.session.visibility.hidden_sketches.push(id);+ assert!(!visible_sketches(&h.app).contains(&id));+ assert_eq!(lit(&h.app), shown, "a hidden sketch is lit too");+ h.app.session.selection = vec![Sel::Feature { id: h.app.session.doc.features[0].id }];+ assert_eq!(lit(&h.app), 0, "another feature picked");+ h.app.session.selection.clear();+ assert_eq!(lit(&h.app), 0, "nothing is picked");+ }+#[test]fn history_marker_shows_only_its_sketch_without_changing_visibility() -> Result<(), Box<dyn std::error::Error>> {for explicitly_hidden in [false, true] {