diff --git a/crates/doc/src/eval_more.rs b/crates/doc/src/eval_more.rs index 55ea98d29..7723642a2 100644 --- a/crates/doc/src/eval_more.rs +++ b/crates/doc/src/eval_more.rs @@ -744,7 +744,9 @@ pub(super) fn sheet_eval( None => p.0, }); } - let (mut sh, notes) = sheet::convert_body(&mb, &r.name, &rv, face.0, &picks)?; + let placement = doc.component_transform(doc.model_body_component(&mb)); + let body_to_world = doc.model_body_offset(&mb).map(|offset| crate::mat_mul(&placement, offset)).unwrap_or(placement); + let (mut sh, notes) = sheet::convert_body(&mb, &r.name, &rv, face.0, &picks, &body_to_world)?; for note in notes { append_warning(warning, note); } diff --git a/crates/doc/src/sheet_convert.rs b/crates/doc/src/sheet_convert.rs index 9628047ff..ef9fd60ec 100644 --- a/crates/doc/src/sheet_convert.rs +++ b/crates/doc/src/sheet_convert.rs @@ -330,10 +330,32 @@ struct Chamfer { fn chamfer_at(mb: &ModelBody, mesh: &Mesh, p: Vec3, tol: f64, near: f64) -> Option { let b = &mb.body; let (d, f) = crate::appearance::nearest_face(mb, p)?; - if d <= near - && let Some(c) = bend_cylinder(b, mesh, f, tol) - { - return Some(c); + if d <= near { + if let Some(c) = bend_cylinder(b, mesh, f, tol) { + return Some(c); + } + // A tangent edge belongs to both the cylinder and a planar panel. The nearest + // face can be either: resolve the cylinder from the edge's incident faces too. + let edge = mesh + .edges + .iter() + .zip(&mesh.edge_faces) + .map(|(line, faces)| { + let distance = line + .windows(2) + .filter_map(|w| match w { + [a, b] => Some(p.dist_to_segment(*a, *b)), + _ => None, + }) + .fold(f64::INFINITY, f64::min); + (distance, faces) + }) + .min_by(|a, b| a.0.total_cmp(&b.0)); + if let Some((_, faces)) = edge.filter(|(distance, _)| *distance <= near) + && let Some(c) = faces.iter().find_map(|g| bend_cylinder(b, mesh, *g as usize, tol)) + { + return Some(c); + } } if d > tol { return None; @@ -672,8 +694,17 @@ pub fn bend_edge_names(st: &crate::ModelState, body: &str, pts: &[Vec3]) -> Vec< /// Convert body `mb` to a sheet under the rule `rule` with values `rv`: `face` is a point on the /// fixed face and `bends` are points on the sharp edges that become bends, in the middle of /// chamfers or on bend cylinders that do (the body's coordinates, mm). See the module docs. +/// `body_to_world` places diagnostic points in world coordinates. Geometry and picks stay local. /// Returns the sheet and a note for every corner or end of a bend not finished as the rule says. -pub fn convert_body(mb: &ModelBody, rule: &str, rv: &RuleValues, face: Vec3, bends: &[Vec3]) -> Result<(SheetBody, Vec)> { +pub fn convert_body( + mb: &ModelBody, + rule: &str, + rv: &RuleValues, + face: Vec3, + bends: &[Vec3], + body_to_world: &crate::Mat, +) -> Result<(SheetBody, Vec)> { + let at = |p| at(apply_point(body_to_world, p)); if bends.is_empty() || bends.len() > MAX_BENDS { return err(format!("pick 1…{MAX_BENDS} bend edges")); } diff --git a/crates/engine/src/cmd/sheet_convert_tests.rs b/crates/engine/src/cmd/sheet_convert_tests.rs index fd108ad93..ddfa6682c 100644 --- a/crates/engine/src/cmd/sheet_convert_tests.rs +++ b/crates/engine/src/cmd/sheet_convert_tests.rs @@ -119,6 +119,41 @@ fn trapezoid_section(s: &mut Session, t: f64) -> Vec<[f64; 3]> { section(s, &[(-x + 15.0, y), (-x, y), (-100.0, 0.0), (100.0, 0.0), (x, y), (x - 15.0, y)], t, 300.0) } +#[test] +fn rotated_component_bend_picks_follow_connected_panels() { + let mut sizes = Vec::new(); + for rotated in [false, true] { + let mut s = Session::default(); + let component = run(&mut s, "component.create", json!({"name": "Bent part"})); + rule(&mut s, "Test", 2.0, 0.5, 1.0, 0.5); + let corners = bent_section(&mut s, &[(0.0, 0.0), (60.0, 0.0), (60.0, 40.0), (90.0, 40.0)], 2.0, 1.0, 100.0); + // The remote bend is picked on its tangent edge, where it meets the web. + let bends = [corners[0].0, [60.0, 39.0, 50.0]]; + if rotated { + run(&mut s, "occurrence.move", json!({"occurrence": component["occurrence"], "axis": [1, 0, 0], "angle": "90 deg"})); + } + run(&mut s, "component.activate", json!({"component": 0})); + let world = |p: [f64; 3]| if rotated { [p[0], -p[2], p[1]] } else { p }; + let bends: Vec<_> = bends.into_iter().map(world).collect(); + run(&mut s, "sheet.convert", json!({"body": "Section", "face": world([30.0, 0.0, 50.0]), "bends": bends})); + assert_eq!(bend_rows(&mut s).len(), 2); + sizes.push(flat_size(&mut s)); + } + assert!(same_size(sizes[0], sizes[1], 1e-6), "{sizes:?}"); +} + +#[test] +fn rotated_component_disconnected_bend_reports_world_point() { + let mut s = Session::default(); + let component = run(&mut s, "component.create", json!({"name": "Bent part"})); + rule(&mut s, "Test", 2.0, 0.5, 1.0, 0.5); + section(&mut s, &[(0.0, 0.0), (60.0, 0.0), (60.0, 40.0), (90.0, 40.0)], 2.0, 100.0); + run(&mut s, "occurrence.move", json!({"occurrence": component["occurrence"], "axis": [1, 0, 0], "angle": "90 deg"})); + run(&mut s, "component.activate", json!({"component": 0})); + let e = s.execute("sheet.convert", &json!({"body": "Section", "face": [30, -50, 0], "bends": [[60, -50, 40]]})).unwrap_err().to_string(); + assert!(e.contains("the bend at (60.000, -50.000, 40.000) does not connect"), "{e}"); +} + #[test] fn a_shelled_tray_keeps_its_walls_and_unfolds_by_the_k_factor() { let (t, k, r, gap) = (2.0, 0.44, 2.0, 0.5);