* claim: #4440 * Flat Pattern view: draw perforation holes (#4440) The flat view drew the outline, cut-outs, pockets and bend lines but not perforation fields, though the flat-pattern data and the DXF export list their holes. The view now reads the hole centres from the sheet through SheetBody::perforation_holes, caches them per model state and body, and draws them as circles in the cut-out style. Holes outside the viewport or smaller than one pixel are skipped, and the cache is capped at one million holes.
This commit is contained in:
1 parent
3acb81964a
commit
2d54cb31bc
2 files changed
+121
-4
No files matched your search
@@ -11,7 +11,7 @@ use solidcraft_engine::Sel;
|
||||
use solidcraft_engine::doc::expr::Kind as ValueKind;
|
||||
use solidcraft_engine::doc::sheet::SheetRule;
|
||||
use solidcraft_engine::doc::{FeatureKind, ProfileSel};
|
||||
use solidcraft_engine::geom::Vec3;
|
||||
use solidcraft_engine::geom::{Vec2, Vec3};
|
||||
|
||||
use crate::SolveApp;
|
||||
use crate::dialogs::{Dialog, Kind, edge_sel, face_sel, form_row, profile_indices};
|
||||
@@ -756,8 +756,8 @@ fn bend_colors() -> (Color32, Color32) {
|
||||
(t.bend_up, t.bend_down)
|
||||
}
|
||||
|
||||
/// The flat pattern view: the outline, cut-outs and bend lines (up and down in their colours)
|
||||
/// drawn flat over the viewport, fitted to it. True when the dialog is a flat pattern.
|
||||
/// The flat pattern view: outline, cut-outs, perforations and bend lines (up and down in
|
||||
/// their colours), fitted over the viewport. True when the dialog is a flat pattern.
|
||||
pub fn overlay(app: &mut SolveApp, painter: &egui::Painter, rect: Rect, d: &Dialog) -> bool {
|
||||
let Kind::Sheet(Sm::FlatPattern { data, .. }) = &d.kind else { return false };
|
||||
let t = Tokens::get();
|
||||
@@ -819,6 +819,22 @@ pub fn overlay(app: &mut SolveApp, painter: &egui::Painter, rect: Rect, d: &Dial
|
||||
painter.add(egui::Shape::closed_line(pts, Stroke::new(1.4, t.text)));
|
||||
}
|
||||
}
|
||||
if let Some(body) = v["body"].as_str() {
|
||||
app.flat_view.cache_perforations(&app.session, body);
|
||||
let clip = rect.intersect(painter.clip_rect());
|
||||
let pixels_per_point = painter.ctx().pixels_per_point();
|
||||
for &(centre, radius) in &app.flat_view.perforations {
|
||||
let radius = (radius * scale) as f32;
|
||||
// Egui uses logical points; reject holes smaller than one physical pixel.
|
||||
if !radius.is_finite() || radius * 2.0 * pixels_per_point < 1.0 {
|
||||
continue;
|
||||
}
|
||||
let centre = to((centre.x, centre.y));
|
||||
if centre.x.is_finite() && centre.y.is_finite() && clip.intersects(Rect::from_center_size(centre, vec2(radius * 2.0, radius * 2.0))) {
|
||||
painter.circle_stroke(centre, radius, Stroke::new(1.4, t.text));
|
||||
}
|
||||
}
|
||||
}
|
||||
// A pocket retains material below (or above) the cut. Dashed contours and a depth
|
||||
// annotation distinguish it from the solid through-cut boundaries.
|
||||
let pockets: Vec<&Value> = v["pockets"].as_array().into_iter().flatten().collect();
|
||||
@@ -894,6 +910,9 @@ pub struct FlatView {
|
||||
pub cam: solidcraft_engine::render::Camera,
|
||||
/// The camera frames the pattern; cleared by Fit and Home, and whenever the dialog is closed.
|
||||
pub fitted: bool,
|
||||
/// Flat centres stay out of the command JSON and are reused while navigating.
|
||||
perforation_key: Option<(std::sync::Weak<solidcraft_engine::doc::ModelState>, String)>,
|
||||
perforations: Vec<(Vec2, f64)>,
|
||||
}
|
||||
|
||||
impl Default for FlatView {
|
||||
@@ -902,11 +921,35 @@ impl Default for FlatView {
|
||||
cam.set_view(solidcraft_engine::render::StandardView::Top);
|
||||
cam.fov = 0.0;
|
||||
cam.up = solidcraft_engine::render::UpAxis::Z;
|
||||
FlatView { cam, fitted: false }
|
||||
FlatView { cam, fitted: false, perforation_key: None, perforations: Vec::new() }
|
||||
}
|
||||
}
|
||||
|
||||
impl FlatView {
|
||||
fn cache_perforations(&mut self, session: &solidcraft_engine::Session, body: &str) {
|
||||
let state = session.world_state();
|
||||
let state_key = std::sync::Arc::downgrade(&state);
|
||||
if self.perforation_key.as_ref().is_some_and(|(cached, name)| cached.ptr_eq(&state_key) && name == body) {
|
||||
return;
|
||||
}
|
||||
self.perforation_key = Some((state_key, body.to_owned()));
|
||||
self.perforations.clear();
|
||||
let Some(sheet) = state.sheets.iter().find(|sheet| sheet.body == body) else { return };
|
||||
// Bound overlay storage and drawing work, including designs with many fields.
|
||||
const MAX_HOLES: usize = 1_000_000;
|
||||
for field in &sheet.perforations {
|
||||
if self.perforations.len() >= MAX_HOLES {
|
||||
break;
|
||||
}
|
||||
if !field.diameter.is_finite() || field.diameter <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
let Ok(centres) = sheet.perforation_holes(field) else { continue };
|
||||
let remaining = MAX_HOLES.saturating_sub(self.perforations.len());
|
||||
self.perforations.extend(centres.into_iter().take(remaining).map(|centre| (centre, field.diameter / 2.0)));
|
||||
}
|
||||
}
|
||||
|
||||
/// Frame the pattern's bounds `lo..hi` in `area`, the part of `rect` the command panel leaves.
|
||||
fn fit(&mut self, rect: Rect, area: Rect, lo: (f64, f64), hi: (f64, f64)) {
|
||||
let scale = (f64::from(area.width()) / (hi.0 - lo.0)).min(f64::from(area.height()) / (hi.1 - lo.1));
|
||||
|
||||
@@ -879,3 +879,77 @@ fn fit_and_home_frame_the_flat_pattern() {
|
||||
assert!(!app.flat_view.fitted);
|
||||
}
|
||||
}
|
||||
|
||||
fn perforated_flat() -> (SolveApp, Dialog) {
|
||||
let mut app = plate(crate::shell::LayoutMode::Floating);
|
||||
app.run("sheet.flange", json!({"sketch": "Base"})).unwrap();
|
||||
app.run("sheet.perforate", json!({"diameter": 3, "pitch": 6})).unwrap();
|
||||
let flat = app.session.execute("sheet.flat_pattern", &json!({})).unwrap();
|
||||
assert_eq!(flat["perforations"][0]["count"], 155);
|
||||
app.start("sheet.flat_pattern");
|
||||
let mut dialog = app.dialog.clone().unwrap();
|
||||
let Kind::Sheet(Sm::FlatPattern { data, .. }) = &mut dialog.kind else { panic!("flat dialog") };
|
||||
*data = Some(Ok(flat));
|
||||
(app, dialog)
|
||||
}
|
||||
|
||||
fn flat_circles(app: &mut SolveApp, dialog: &Dialog, rect: Rect) -> Vec<egui::epaint::CircleShape> {
|
||||
let ctx = crate::fonts::test_context();
|
||||
let input = egui::RawInput { screen_rect: Some(rect), ..Default::default() };
|
||||
let output = crate::test_support::frame(&ctx, input, |ui| {
|
||||
let painter = ui.ctx().layer_painter(egui::LayerId::new(egui::Order::Middle, egui::Id::new("perforation-test")));
|
||||
assert!(overlay(app, &painter, rect, dialog));
|
||||
});
|
||||
output.shapes.into_iter().filter_map(|s| if let egui::Shape::Circle(c) = s.shape { Some(c) } else { None }).collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flat_perforations_match_export_centres_and_cutout_style() {
|
||||
let (mut app, dialog) = perforated_flat();
|
||||
let state = app.session.world_state();
|
||||
let sheet = state.sheets.last().unwrap();
|
||||
let holes = sheet.perforation_holes(&sheet.perforations[0]).unwrap();
|
||||
let rect = Rect::from_min_size(Pos2::ZERO, vec2(1400.0, 900.0));
|
||||
let circles = flat_circles(&mut app, &dialog, rect);
|
||||
assert_eq!(circles.len(), holes.len());
|
||||
let cam = app.flat_view.cam;
|
||||
let scale = f64::from(rect.height()) / (2.0 * cam.half_height());
|
||||
for (circle, hole) in circles.iter().zip(holes) {
|
||||
let centre = pos2(
|
||||
(f64::from(rect.center().x) + (hole.x - cam.target.x) * scale) as f32,
|
||||
(f64::from(rect.center().y) - (hole.y - cam.target.y) * scale) as f32,
|
||||
);
|
||||
assert!(circle.center.distance(centre) < 0.001);
|
||||
assert!((circle.radius - (1.5 * scale) as f32).abs() < 0.001);
|
||||
assert_eq!(circle.stroke, Stroke::new(1.4, Tokens::get().text));
|
||||
assert_eq!(circle.fill, Color32::TRANSPARENT);
|
||||
}
|
||||
let cached = app.flat_view.perforations.as_ptr();
|
||||
assert_eq!(flat_circles(&mut app, &dialog, rect).len(), 155);
|
||||
assert_eq!(app.flat_view.perforations.as_ptr(), cached, "navigation reuses the centres");
|
||||
app.run("sheet.perforate", json!({"diameter": 2, "pitch": 10})).unwrap();
|
||||
let state = app.session.world_state();
|
||||
let sheet = state.sheets.last().unwrap();
|
||||
let expected: usize = sheet.perforations.iter().map(|p| sheet.perforation_holes(p).unwrap().len()).sum();
|
||||
assert!(expected > 155);
|
||||
assert_eq!(flat_circles(&mut app, &dialog, rect).len(), expected, "model changes refresh the centres");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn flat_perforations_cull_offscreen_and_subpixel_holes() {
|
||||
let (mut app, dialog) = perforated_flat();
|
||||
let rect = Rect::from_min_size(Pos2::ZERO, vec2(1400.0, 900.0));
|
||||
assert_eq!(flat_circles(&mut app, &dialog, rect).len(), 155);
|
||||
let fitted_distance = app.flat_view.cam.distance;
|
||||
// Keep a hole crossing the left viewport edge, even when its centre is outside.
|
||||
let scale = f64::from(rect.height()) / (2.0 * app.flat_view.cam.half_height());
|
||||
app.flat_view.cam.target.x = 40.5 + (f64::from(rect.width()) / 2.0 + 0.75 * scale) / scale;
|
||||
let partial = flat_circles(&mut app, &dialog, rect);
|
||||
assert!(!partial.is_empty() && partial.len() < 155);
|
||||
assert!(partial.iter().any(|c| c.center.x < rect.left() && c.center.x + c.radius > rect.left()));
|
||||
app.flat_view.cam.target.x = 1e6;
|
||||
assert!(flat_circles(&mut app, &dialog, rect).is_empty());
|
||||
app.flat_view.cam.target.x = 50.0;
|
||||
app.flat_view.cam.distance = fitted_distance * 1000.0;
|
||||
assert!(flat_circles(&mut app, &dialog, rect).is_empty());
|
||||
}
|
||||
Reference in new issue
Block a user