diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs index 407811860..44c249d56 100644 --- a/crates/engine/src/cmd/features.rs +++ b/crates/engine/src/cmd/features.rs @@ -365,7 +365,7 @@ pub(super) fn stable_profiles(s: &Session, kind: &mut FeatureKind) -> Result<()> } ProfileSel::All => continue, }; - let captured = solidcraft_doc::capture_profile_selection(ss, &indices)?; + let captured = solidcraft_doc::capture_profile_selection(ss, &drawn_profile_regions(ss, &indices))?; *sel = match captured { ProfileSel::OffsetBoundaries { associations } if associations.iter().any(|id| { @@ -396,6 +396,82 @@ pub(super) fn stable_profiles(s: &Session, kind: &mut FeatureKind) -> Result<()> Ok(()) } +/// The regions a picked profile stands for. +/// +/// A drawn profile cut by reference geometry that crosses it (the projected outline of the face +/// a sketch is on) is several regions of the sketch's graph, and picking one of them takes the +/// whole drawn profile (#5302): the regions that share a drawn curve with the picked one are +/// the parts of that outline, split by the reference geometry crossing it. Reference geometry +/// that stays inside the drawn profile (the rim of a counterbored hole) bounds an inner region +/// of it, a real boundary of the material, so the pick keeps its own region. The drawn profiles +/// are the disjoint faces of the drawn graph, so a drawn hole keeps filling only itself +/// (#5176). +fn drawn_profile_regions(ss: &solidcraft_doc::SolvedSketch, indices: &[usize]) -> Vec { + const MAX_PROFILES: usize = 10_000; + if indices.is_empty() || indices.len() > MAX_PROFILES || ss.profiles.len() > MAX_PROFILES { + return indices.to_vec(); + } + let drawn = solidcraft_sketch::find_drawn_profiles(&ss.sketch); + if drawn.is_empty() || drawn.len() >= ss.profiles.len() { + return indices.to_vec(); + } + // The curves the user drew: the drawn profiles are made of them, the reference geometry is + // not part of them. + let drawn_curves: std::collections::BTreeSet<&str> = ss + .sketch + .curves + .iter() + .enumerate() + .filter(|(index, curve)| { + !curve.construction && !curve.centerline && !curve.infinite && (!ss.sketch.is_linked_curve(*index) || ss.sketch.is_text_curve(*index)) + }) + .map(|(_, curve)| curve.id.as_str()) + .collect(); + let mut out: Vec = Vec::with_capacity(indices.len()); + for index in indices { + let Some(picked) = ss.profiles.get(*index) else { continue }; + // The drawn profile the picked region belongs to: the region around it that the drawn + // geometry alone bounds. + let probe = picked.region.interior_point(); + let Some(owner) = drawn.iter().find(|profile| profile.region.contains(probe)) else { + out.push(*index); + continue; + }; + // The drawn curves the picked region is bounded by. + let picked_curves: std::collections::BTreeSet<&str> = picked + .outer_curves + .iter() + .chain(picked.hole_curves.iter().flatten()) + .map(String::as_str) + .filter(|id| drawn_curves.contains(id)) + .collect(); + if picked_curves.is_empty() { + out.push(*index); + continue; + } + // The regions of the drawn profile that share a drawn curve with the picked one: the + // parts of the same outline, cut apart by reference geometry crossing it. + let pieces: Vec = ss + .profiles + .iter() + .enumerate() + .filter(|(_, profile)| owner.region.contains(profile.region.interior_point())) + .filter(|(_, profile)| { + profile.outer_curves.iter().chain(profile.hole_curves.iter().flatten()).any(|id| picked_curves.contains(id.as_str())) + }) + .map(|(index, _)| index) + .collect(); + if pieces.is_empty() { + out.push(*index); + } else { + out.extend(pieces); + } + } + out.sort_unstable(); + out.dedup(); + out +} + pub(crate) fn profiles(p: &Value, cmd: &str) -> Result { let Some(v) = p.get("profiles") else { return Ok(ProfileSel::All) }; match v { diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index cfd801a3a..dd44f7279 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3494,6 +3494,77 @@ fn default_extrude_profiles_keep_even_depth_islands_and_separate_outlines() { assert!(rel(actual, expected) < 1e-6, "{actual} vs {expected}"); } +/// The face of `body` whose normal points along +x, as a plane pick. +fn side_face_pick(s: &Session, body: &str) -> Value { + let world = s.world_state(); + let b = world.body(body).unwrap(); + let face = b + .body + .faces(0.01) + .unwrap() + .into_iter() + .filter(|f| f.plane_normal.is_some_and(|n| n.dot(solidcraft_geom::Vec3::X) > 0.999)) + .max_by(|a, b| a.centroid.x.total_cmp(&b.centroid.x)) + .unwrap(); + json!({"body": b.name, "index": face.index, "face": [face.centroid.x, face.centroid.y, face.centroid.z]}) +} + +#[test] +fn extruding_a_sketch_on_a_side_face_takes_the_whole_drawn_profile() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": 20, "body_name": "Base"})); + let pick = side_face_pick(&s, "Base"); + let id = run(&mut s, "sketch.create", json!({"plane": pick, "name": "Side"}))["sketch"].as_u64().unwrap(); + // The rectangle straddles the top edge of the face, so the projected outline cuts it in two. + run(&mut s, "sketch.rectangle.two_point", json!({"p0": [10, 15], "p1": [20, 25]})); + run(&mut s, "sketch.finish", json!({})); + for selection in [json!([1]), json!([2]), json!([{"point": [15, 17]}]), json!([{"point": [15, 22]}])] { + let mut s2 = Session::new((*s.doc).clone()); + run(&mut s2, "solid.extrude", json!({"sketch": id, "distance": 5, "operation": "new", "body_name": "Rib", "profiles": selection.clone()})); + let state = s2.model.state(); + let rib = state.bodies.iter().find(|b| b.name == "Rib").unwrap(); + let v = rib.body.tessellate(1e-2).unwrap().measure().volume; + assert!(rel(v, 500.0) < 1e-6, "{selection}: {v}"); + } +} + +/// A pick on a region of a drawn profile that reference geometry bounds from inside (the rim of +/// a hole in the face) takes only that region: the rim is a real boundary of the material, and +/// the drawn outline around it is not cut by it (#5302). +#[test] +fn a_pick_bounded_by_a_projected_rim_takes_only_that_region() { + let mut s = Session::default(); + run(&mut s, "solid.box", json!({"length": 40, "width": 30, "height": 20, "body_name": "Base"})); + run(&mut s, "solid.hole", json!({"position": [20, 15, 20], "diameter": 10})); + let id = run(&mut s, "sketch.create", json!({"plane": {"face": [20, 5, 20]}}))["sketch"].as_u64().unwrap(); + // The square around the hole: the hole's rim is projected into the sketch and bounds an + // inner region of it, but does not cut the square. + run(&mut s, "sketch.rectangle.two_point", json!({"p0": [10, 5], "p1": [30, 25]})); + run(&mut s, "sketch.finish", json!({})); + let mut s2 = Session::new((*s.doc).clone()); + run(&mut s2, "solid.extrude", json!({"sketch": id, "distance": 5, "operation": "new", "body_name": "Pad", "profiles": [{"point": [12, 7]}]})); + let state = s2.model.state(); + let pad = state.bodies.iter().find(|b| b.name == "Pad").unwrap(); + let v = pad.body.tessellate(1e-2).unwrap().measure().volume; + // The square with the hole's rim cut out of it, not the whole square. The mesh measures the + // round rim as a polygon, so the volume is a little under the analytic one. + let want = (400.0 - PI * 25.0) * 5.0; + assert!(rel(v, want) < 5e-3, "{v} vs {want}"); +} + +/// An explicit pick of a drawn hole still fills only that hole (#5176). +#[test] +fn an_explicit_pick_of_a_drawn_hole_fills_only_that_hole() { + let mut s = Session::default(); + run(&mut s, "sketch.create", json!({"plane": "XY"})); + run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [60, 40]})); + run(&mut s, "sketch.circle.center", json!({"center": [30, 20], "radius": 5, "id": "ring"})); + run(&mut s, "sketch.finish", json!({})); + let ring = s.model.state().sketches[0].profiles.iter().position(|p| p.outer_curves == ["ring"]).unwrap(); + run(&mut s, "solid.extrude", json!({"distance": 5, "profiles": [ring]})); + assert!(rel(volume(&mut s), 25.0 * PI * 5.0) < 1e-4, "the hole alone: {}", volume(&mut s)); +} + /// Every body a multi-profile `new` extrude creates carries its `body_name`, not a generic BodyN (#266). #[test] fn multi_profile_extrude_names_every_body_after_body_name() {