diff --git a/crates/doc/src/name_lookup.rs b/crates/doc/src/name_lookup.rs index 44f941c22..c21152d69 100644 --- a/crates/doc/src/name_lookup.rs +++ b/crates/doc/src/name_lookup.rs @@ -82,6 +82,15 @@ pub fn locate_edge(b: &ModelBody, name: &str, near: Vec3) -> Result= Match::Piece) { return exact(*i); } + // Only the root of the name fits, on faces named from geometry, and the reference's point lies + // on another edge: geometry once named that edge differently (a design saved when a round wall + // took the name of a join's new face), so the point says which edge this is. + if level == Match::Stem + && cands.iter().all(|i| !verified(*i) && !edge_near(b, &m, *i, near)) + && let Some(i) = (0..m.edges.len()).find(|i| edge_near(b, &m, *i, near)) + { + return Ok(EdgeHit { edge: i, point: near, split: false, verified: false, on_point: true, legacy: false }); + } cands .iter() .filter_map(|i| mid(*i).map(|p| (p, *i))) diff --git a/crates/doc/src/naming.rs b/crates/doc/src/naming.rs index d653516b5..8a81d580b 100644 --- a/crates/doc/src/naming.rs +++ b/crates/doc/src/naming.rs @@ -538,6 +538,19 @@ fn geometric_from(b: &ModelBody, o: &NamingOrigin, nf: usize, names_of: fn(&Mode } } } + // A feature that built a tool and reported no history. Two meshes of one curved face are only + // as close as their chord tolerances, which follow the body's size: a tool that makes the body + // bigger divides its round walls differently, and those walls miss their own faces at `tol`. + // Named by role they would become faces of this feature, nowhere near what it built. + if matches!(o.feature.kind, FeatureKind::Extrude { .. } | FeatureKind::Revolve { .. } | FeatureKind::Sweep { .. } | FeatureKind::Loft { .. }) { + let chord = b.display_tol() + inputs.iter().map(|(x, _, _)| x.display_tol()).fold(0.0, f64::max); + for (fi, name) in names.iter_mut().enumerate().filter(|(_, name)| name.is_none()) { + let curved = ns.get(fi).is_some_and(|n| n.windows(2).any(|w| w[0].cross(w[1]).len() > 1e-6)); + if curved && let Some(ps) = pts.get(fi) { + *name = vote(ps, &inputs, chord, None); + } + } + } let empty = Vec::new(); let curves = std::cell::OnceCell::new(); let names: Vec = names diff --git a/crates/engine/src/face_name_tests.rs b/crates/engine/src/face_name_tests.rs index 946907640..ee134f639 100644 --- a/crates/engine/src/face_name_tests.rs +++ b/crates/engine/src/face_name_tests.rs @@ -265,3 +265,56 @@ fn a_face_name_on_two_bodies_is_no_split_face() { run(&mut s, "solid.hole", json!({"position": [30, 10, 10], "diameter": 5, "name": "Hole1", "targets": ["Body1"]})); assert_eq!(said(&s, "Hole1"), ""); } + +/// A plate with a round wall on each long side, and a tall tab with a curved notch joined to its +/// far end, 12 mm from those walls: this join reports no history, and the bigger body meshes the +/// walls differently. Returns the plate's and the tab's feature ids. +fn plate_and_notched_tab(s: &mut Session) -> (Option, Option) { + let reach = 57.0_f64.sqrt(); // Where a circle of radius 11 meets y = ±8. + run(s, "sketch.create", json!({"plane": "XY"})); + run(s, "sketch.rectangle.two_point", json!({"p0": [-20, -8], "p1": [20, 8]})); + run(s, "sketch.arc.three_point", json!({"start": [-reach, 8], "end": [reach, 8], "through": [0, 11]})); + run(s, "sketch.arc.three_point", json!({"start": [-reach, -8], "end": [reach, -8], "through": [0, -11]})); + run(s, "sketch.finish", json!({})); + let plate = run(s, "solid.extrude", json!({"distance": 10, "name": "Plate"}))["feature"].as_u64(); + run(s, "sketch.create", json!({"plane": {"face": [20, 0, 5]}, "project_edges": false})); + run(s, "sketch.line", json!({"points": [[-8, -5], [8, -5], [8, 23], [-8, 23], [-8, -5]]})); + run(s, "sketch.circle.center", json!({"center": [0, 23], "radius": 8.0667})); + run(s, "sketch.finish", json!({})); + let tab = + run(s, "solid.extrude", json!({"distance": 10, "operation": "join", "name": "Tab", "profiles": [{"point": [0, -3]}]}))["feature"].as_u64(); + (plate, tab) +} + +/// The round walls stay the plate's whole, and an edge picked on one stores no trace of the tab. +#[test] +fn a_join_that_makes_the_body_bigger_leaves_round_walls_it_never_touched_alone() { + let mut s = Session::default(); + let (plate, tab) = plate_and_notched_tab(&mut s); + let tab_name = format!("F{}", tab.unwrap_or_default()); + let made_by = |face: &Value| face["name"].as_str().and_then(solidcraft_doc::persistent_name::made_by); + let all = faces(&mut s, "Body1"); + let round: Vec<&Value> = all.iter().filter(|f| f.get("plane_normal").is_none_or(Value::is_null)).collect(); + let walls: Vec<&&Value> = round.iter().filter(|f| vec(&f["point"]).x < 15.0).collect(); + assert_eq!(walls.len(), 2, "{all:?}"); + for wall in walls { + assert_eq!(made_by(wall), plate, "{wall}"); + assert!(wall["name"].as_str().is_some_and(|name| !name.contains(&tab_name)), "{wall}"); + } + assert!(round.iter().filter(|f| vec(&f["point"]).x > 20.0).all(|notch| made_by(notch) == tab), "{all:?}"); + + let volume_before = volume(&mut s, "Body1"); + run(&mut s, "solid.fillet", json!({"edges": [[0, -11, 10]], "radius": 1, "name": "Round"})); + assert!(volume(&mut s, "Body1") < volume_before); + let filleted = volume(&mut s, "Body1"); + let round_id = s.doc.features.iter().position(|f| f.name == "Round").unwrap(); + assert!(s.doc.features[round_id].edge_names.iter().all(|name| !name.contains(&tab_name)), "{:?}", s.doc.features[round_id].edge_names); + + // A design saved while the wall was named after the tab still finds its edge. + let mut saved = (*s.doc).clone(); + saved.features[round_id].edge_names = vec![format!("F2:top|{tab_name}:side:l4#[F2:side:l1]")]; + let mut reopened = Session::opened_at(saved, None, None); + let result = reopened.model.result(reopened.doc.features[round_id].id).unwrap(); + assert!(!result.skipped && result.error.is_none(), "{:?}", result.error); + assert!((volume(&mut reopened, "Body1") - filleted).abs() < 1e-6); +} diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rs index 301242ae8..829fad676 100644 --- a/crates/ui-egui/src/viewport.rs +++ b/crates/ui-egui/src/viewport.rs @@ -12895,4 +12895,31 @@ mod feature_highlight_tests { let parts = feature_parts(&app, first, true); assert!(parts.iter().any(|p| matches!(p, Sel::Face { .. })), "{parts:?}"); } + + /// A joined tab that made the body bigger lights its own faces, not round walls of the plate + /// 12 mm away: the join reports no history, and the bigger body meshes those walls differently. + #[test] + fn a_joined_tab_does_not_light_round_walls_it_never_touched() { + let mut s = solidcraft_engine::Session::default(); + let reach = 57.0_f64.sqrt(); // Where a circle of radius 11 meets y = ±8. + s.execute("sketch.create", &json!({"plane": "XY"})).unwrap(); + s.execute("sketch.rectangle.two_point", &json!({"p0": [-20, -8], "p1": [20, 8]})).unwrap(); + s.execute("sketch.arc.three_point", &json!({"start": [-reach, 8], "end": [reach, 8], "through": [0, 11]})).unwrap(); + s.execute("sketch.arc.three_point", &json!({"start": [-reach, -8], "end": [reach, -8], "through": [0, -11]})).unwrap(); + s.execute("sketch.finish", &json!({})).unwrap(); + s.execute("solid.extrude", &json!({"distance": 10})).unwrap(); + s.execute("sketch.create", &json!({"plane": {"face": [20, 0, 5]}, "project_edges": false})).unwrap(); + s.execute("sketch.line", &json!({"points": [[-8, -5], [8, -5], [8, 23], [-8, 23], [-8, -5]]})).unwrap(); + s.execute("sketch.circle.center", &json!({"center": [0, 23], "radius": 8.0667})).unwrap(); + s.execute("sketch.finish", &json!({})).unwrap(); + let tab = s.execute("solid.extrude", &json!({"distance": 10, "operation": "join", "profiles": [{"point": [0, -3]}]})).unwrap()["feature"] + .as_u64() + .unwrap(); + let app = SolveApp::new(s, crate::Services::default()); + let parts = feature_parts(&app, tab, true); + let faces: Vec<_> = parts.iter().filter_map(|p| if let Sel::Face { point, .. } = p { Some(*point) } else { None }).collect(); + assert!(faces.iter().any(|p| p.x > 20.0), "the tab lights its own faces: {parts:?}"); + // The plate's walls bulge past y = ±8; nothing of the tab does. + assert!(faces.iter().all(|p| p.y.abs() <= 8.0 + 1e-6), "{parts:?}"); + } }