diff --git a/crates/doc/src/joints_face.rs b/crates/doc/src/joints_face.rs index 9f1433288..9e142b915 100644 --- a/crates/doc/src/joints_face.rs +++ b/crates/doc/src/joints_face.rs @@ -10,6 +10,7 @@ use solidcraft_geom::Vec3; use solidcraft_kernel::CylinderFace; use super::{Snap, XRef, component_bodies}; +use crate::history_names::Match; use crate::{DocError, Document, IDENTITY, Mat, ModelBody, ModelState, Result, apply_point, apply_vector, mat_inverse}; /// Rim points sampled to find the face across a cylinder's rim. @@ -170,21 +171,32 @@ impl Round { pub(super) fn named_round(doc: &Document, st: &ModelState, pick: Vec3, face: &str, end: Option<&str>) -> Result> { let nf = named_face(doc, st, pick, face)?; let Some(own) = nf.own_cylinder() else { return Ok(None) }; - let chosen = end - .and_then(|name| { - let hits: Vec<(f64, f64)> = ends(&own) - .into_iter() - .filter(|(at, _)| rim_neighbour(nf.body, nf.face, &own, *at).is_some_and(|f| crate::name_lookup::face_has_name(nf.body, f, name))) - .collect(); - match hits[..] { - [one] => Some(one), - _ => None, - } - }) - .unwrap_or_else(|| end_by_pick(&nf, &own)); + let chosen = end.and_then(|name| end_named(nf.body, nf.face, &own, name)).unwrap_or_else(|| end_by_pick(&nf, &own)); Ok(Some(Round::at(placed(&nf.offset, own), chosen))) } +/// The end of cylinder `own` (face `face` of `b`) whose rim meets the face called `name` better +/// than the other end's rim does: the very face, or one merged into it, else a piece of it whose +/// neighbours have changed (the face round a hole in a sheet is told apart from its other pieces +/// by the faces around it, and those change when the hole moves). `None` when neither rim, or +/// both, meet it as well. +fn end_named(b: &ModelBody, face: usize, own: &CylinderFace, name: &str) -> Option<(f64, f64)> { + let names = crate::naming::names(b); + let how = |at: f64| { + let f = rim_neighbour(b, face, own, at)?; + if crate::name_lookup::legacy_name(name).is_some() { + return crate::name_lookup::face_has_name(b, f, name).then_some(Match::Exact); + } + crate::history_names::matches(&names, f, name) + }; + let [start, end] = ends(own); + match (how(start.0), how(end.0)) { + (s, e) if s > e => Some(start), + (s, e) if e > s => Some(end), + _ => None, + } +} + /// The x axis an x reference gives a frame at `o` with axis `z` (`None`: no reference). pub(super) fn x_axis(doc: &Document, st: &ModelState, x: Option<&XRef>, o: Vec3) -> Result> { Ok(match x { diff --git a/crates/engine/src/cmd/joints_face_tests.rs b/crates/engine/src/cmd/joints_face_tests.rs index a97e2694b..9ef057f72 100644 --- a/crates/engine/src/cmd/joints_face_tests.rs +++ b/crates/engine/src/cmd/joints_face_tests.rs @@ -265,3 +265,31 @@ fn origins_on_a_body_placed_by_an_offset_agree_named_or_not() { assert!(near(named.0, unnamed.0) && near(named.1, unnamed.1), "{named:?} {unnamed:?}"); assert!(near(named.0, at(Vec3::new(20.0, 20.0, 5.0))) && near(named.1, up), "{named:?}"); } + +#[test] +fn a_round_origin_keeps_its_end_when_the_face_it_opens_onto_gets_other_neighbours() { + // A plate `T` thick with a groove across its top (so the top face is in pieces, each named + // by the faces round it), a small hole and a 6 mm hole in the left piece. The origin is + // picked on the 6 mm hole's wall near the top, and names the top piece as its end. + let mut s = Session::default(); + run(&mut s, "parameters.change", json!({"name": "T", "expression": "2 mm"})); + run(&mut s, "component.create", json!({"name": "Plate"})); + run(&mut s, "solid.box", json!({"length": 60, "width": 100, "height": "T", "corner": [0, 0, 0]})); + run(&mut s, "solid.box", json!({"length": 4, "width": 100, "height": 1, "corner": [28, 0, "T - 1 mm"], "operation": "cut"})); + run(&mut s, "solid.hole", json!({"position": [10, 50, "T"], "diameter": 4})); + let small = s.doc.features.last().map_or(0, |f| f.id); + run(&mut s, "solid.hole", json!({"position": [20, 50, "T"], "diameter": 6})); + run(&mut s, "component.activate", json!({"component": "root"})); + run(&mut s, "joint.origin", json!({"occurrence": "Plate", "circle": [23, 50, 1.6], "name": "O"})); + let top = |s: &Session| { + let o = s.doc.assembly.origins.iter().find(|o| o.name == "O").map(|o| o.origin.clone()); + o.and_then(|o| solidcraft_doc::joints::resolve_origin(&s.doc, &s.model.state(), &o).ok()).map(|m| (m[3][2], m[2][2])) + }; + assert_eq!(top(&s), Some((2.0, 1.0))); + // Thicker, the pick is nearer the bottom; without the small hole the top piece is named by + // fewer faces. The origin stays on the top. + run(&mut s, "parameters.change", json!({"name": "T", "expression": "4 mm"})); + run(&mut s, "timeline.suppress", json!({"feature": small, "suppressed": true})); + let (z, dz) = top(&s).unwrap_or_default(); + assert!((z - 4.0).abs() < 1e-9 && (dz - 1.0).abs() < 1e-9, "{z} {dz}"); +}