From a1eb63c1694c6c0441c6a3abc54e9345db8bfd46 Mon Sep 17 00:00:00 2001 From: HiroKenEz <59478073+HiroKenEz@users.noreply.github.com> Date: Mon, 26 Oct 2026 21:02:43 +0000 Subject: [PATCH] Extrude: the picked region is the one extruded again (#6724) (#6766) * claim: #6724 * Extrude: the picked region is the one extruded again (#6724) Since #5530 (#5302) every index or point pick grew to the whole drawn profile that reference geometry cuts, so a lens or crescent clicked in Extrude, where the face's projected outline cuts a circle, extruded the whole circle while the dialog highlighted and counted one region. Dialogs now send exact_regions: true with their profile picks, and the command keeps each pick to the region it names. Picks without the flag (scripts, agents, the CLI) still take the whole drawn profile (#5302). Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- crates/engine/src/cmd/edit.rs | 2 +- crates/engine/src/cmd/features.rs | 20 ++++++-- crates/engine/src/schema/features.rs | 5 ++ crates/engine/src/schema/features_tests.rs | 1 + crates/engine/src/tests.rs | 58 ++++++++++++++++++++++ crates/ui-egui/src/dialogs.rs | 20 +++++++- crates/ui-egui/src/dialogs_all_tests.rs | 40 +++++++++++++++ 7 files changed, 140 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/cmd/edit.rs b/crates/engine/src/cmd/edit.rs index 9eb99fc29..d4c757fac 100644 --- a/crates/engine/src/cmd/edit.rs +++ b/crates/engine/src/cmd/edit.rs @@ -1070,7 +1070,7 @@ fn edit_feature(s: &mut Session, p: &Value) -> Result { } // Profiles picked by index or point are kept by their curves, as when the feature was made. if nf.kind.clone().profile_sels_mut() != f.kind.clone().profile_sels_mut() { - super::features::stable_profiles(s, &mut nf.kind)?; + super::features::stable_profiles(s, &mut nf.kind, false)?; } // New face points: their faces' names now. if nf.kind.face_points() != f.kind.face_points() { diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs index e3c12ef03..fe094a494 100644 --- a/crates/engine/src/cmd/features.rs +++ b/crates/engine/src/cmd/features.rs @@ -25,7 +25,7 @@ pub static COMMANDS: &[CommandSpec] = &[ .key("E") .contract(&EXTRUDE_CONTRACT) .output(output_schema_for::) - .params("distance: expr (or through_all: true); taper?: angle expr (positive: outward, so the far end is larger along the extrusion direction; negative: inward, so the far end is smaller); sketch?: id|name (default: active or last sketch); profiles?: all | [index] | [{curves: [ids]}] (the profile whose outer loop is made of exactly these curves, or pieces of them; found again on every recompute, so it follows parameter changes; islands inside are its holes; an error when no profile or several match) | [{point:[x,y]}] (index and point picks are stored by their curves when that finds the same profile); default: every drawn profile, except that a loop inside another is its hole and a loop inside that hole is material again; face?: [x,y,z] (extrude a planar body face instead of a sketch profile); faces?: [[x,y,z],…] (coplanar faces of one body); face_body?: name (whose face, where faces of several bodies meet at the point); direction?: positive|negative|symmetric (distance is total thickness, split evenly around the start plane); convention?: total_distance|legacy_per_side (default total_distance; legacy preserves older symmetric extents); distance2?: expr (Two Sides: a second distance the other way from the first side, which may be through_all or to; with direction: negative the first side goes against the normal and distance2 along it); start_offset?: expr (along the sketch normal, also with direction: negative); to?: [x,y,z] (To Object: a face, vertex or point; to_offset?: expr past it); from?: [x,y,z] (From Object: start at that face or point); operation?: new|join|cut|intersect|auto (cut into a body, otherwise join existing solids; new in an empty component); targets?: [body] (Join from a face, or from a sketch on a body's face, defaults to that face's body; explicit targets or participants override); name?; body_name?; thin?: true (Thin: a wall along sketch curves instead of the profiles) with curves?: [curve ids] (open chains and closed loops, joined where they meet end to end; default: every drawn curve of the sketch), thickness: expr, side?: center|inside|outside|two_sides (a closed loop's inside is the area it encloses, an open chain's the side it bends toward, a straight line's the left of its first curve as drawn; default center), thickness2?: expr (two_sides: the thickness outside); corners are mitred, crossing walls are one"), + .params("distance: expr (or through_all: true); taper?: angle expr (positive: outward, so the far end is larger along the extrusion direction; negative: inward, so the far end is smaller); sketch?: id|name (default: active or last sketch); profiles?: all | [index] | [{curves: [ids]}] (the profile whose outer loop is made of exactly these curves, or pieces of them; found again on every recompute, so it follows parameter changes; islands inside are its holes; an error when no profile or several match) | [{point:[x,y]}] (index and point picks are stored by their curves when that finds the same profile; a pick on a drawn profile that reference geometry, such as the projected outline of the face the sketch is on, cuts into regions takes the whole drawn profile); exact_regions?: bool (each index or point pick is exactly the region it names, as the dialogs send); default: every drawn profile, except that a loop inside another is its hole and a loop inside that hole is material again; face?: [x,y,z] (extrude a planar body face instead of a sketch profile); faces?: [[x,y,z],…] (coplanar faces of one body); face_body?: name (whose face, where faces of several bodies meet at the point); direction?: positive|negative|symmetric (distance is total thickness, split evenly around the start plane); convention?: total_distance|legacy_per_side (default total_distance; legacy preserves older symmetric extents); distance2?: expr (Two Sides: a second distance the other way from the first side, which may be through_all or to; with direction: negative the first side goes against the normal and distance2 along it); start_offset?: expr (along the sketch normal, also with direction: negative); to?: [x,y,z] (To Object: a face, vertex or point; to_offset?: expr past it); from?: [x,y,z] (From Object: start at that face or point); operation?: new|join|cut|intersect|auto (cut into a body, otherwise join existing solids; new in an empty component); targets?: [body] (Join from a face, or from a sketch on a body's face, defaults to that face's body; explicit targets or participants override); name?; body_name?; thin?: true (Thin: a wall along sketch curves instead of the profiles) with curves?: [curve ids] (open chains and closed loops, joined where they meet end to end; default: every drawn curve of the sketch), thickness: expr, side?: center|inside|outside|two_sides (a closed loop's inside is the area it encloses, an open chain's the side it bends toward, a straight line's the left of its first curve as drawn; default center), thickness2?: expr (two_sides: the thickness outside); corners are mitred, crossing walls are one"), CommandSpec::new("solid.revolve", "Revolve", revolve) .at("SOLID", "CREATE") .icon("revolve") @@ -326,7 +326,11 @@ pub(super) fn feature_sketch(s: &Session, p: &Value, cmd: &str) -> Result { /// whose curves would not find that same profile again (two profiles bounded by the same /// curves) stays as given. Curve picks are checked: one that names no profile, or several, is /// refused now rather than failing later. -pub(super) fn stable_profiles(s: &Session, kind: &mut FeatureKind) -> Result<()> { +/// +/// `exact` keeps each index or point pick to the region it names. Otherwise a pick on a drawn +/// profile that reference geometry cuts into regions takes the whole drawn profile +/// ([`drawn_profile_regions`], #5302). +pub(super) fn stable_profiles(s: &Session, kind: &mut FeatureKind, exact: bool) -> Result<()> { use solidcraft_doc::sketch::profile_at_point; let st = s.model.state(); for (sketch, sel) in kind.profile_sels_mut() { @@ -390,7 +394,8 @@ pub(super) fn stable_profiles(s: &Session, kind: &mut FeatureKind) -> Result<()> } ProfileSel::All => continue, }; - let captured = solidcraft_doc::capture_profile_selection(ss, &drawn_profile_regions(ss, &indices))?; + let picked = if exact { indices } else { drawn_profile_regions(ss, &indices) }; + let captured = solidcraft_doc::capture_profile_selection(ss, &picked)?; *sel = match captured { ProfileSel::OffsetBoundaries { associations } if associations.iter().any(|id| { @@ -497,6 +502,13 @@ fn drawn_profile_regions(ss: &solidcraft_doc::SolvedSketch, indices: &[usize]) - out } +/// `exact_regions: true`: each index or point pick is the region it names, never grown to the +/// whole drawn profile. The dialogs send it: they highlight exactly the regions picked, and OK +/// must build those (#6724). +pub(super) fn exact_regions(p: &Value) -> bool { + bool_(p, "exact_regions").unwrap_or(false) +} + pub(crate) fn profiles(p: &Value, cmd: &str) -> Result { let Some(v) = p.get("profiles") else { return Ok(ProfileSel::All) }; match v { @@ -605,7 +617,7 @@ fn add_feature_in(s: &mut Session, p: &Value, kind: FeatureKind, world: bool) -> } // Profiles picked by index are kept by the curves around them: a recompute can renumber a // sketch's profiles (a face sketch's projected outline rebuilt), never the curves. - stable_profiles(s, &mut kind)?; + stable_profiles(s, &mut kind, exact_regions(p))?; // Blends remember where their edges sat, to find them again after upstream edits. let (refs, names) = match &kind { FeatureKind::Fillet { edges, body, .. } | FeatureKind::Chamfer { edges, body, .. } => { diff --git a/crates/engine/src/schema/features.rs b/crates/engine/src/schema/features.rs index 5ae99b310..81286aa33 100644 --- a/crates/engine/src/schema/features.rs +++ b/crates/engine/src/schema/features.rs @@ -71,6 +71,11 @@ pub struct ExtrudeParams { #[serde(default, deserialize_with = "present")] #[schemars(with = "ProfilesParam")] pub profiles: Option, + /// Each index or point pick is exactly the region it names. Without it, a pick on a drawn + /// profile that reference geometry (a face's projected outline) cuts into regions takes the + /// whole drawn profile. The dialogs send `true`: they highlight the regions they pick. + #[serde(default)] + pub exact_regions: Option, /// A point on a planar body face to extrude instead of a sketch profile. #[serde(default)] pub face: Option, diff --git a/crates/engine/src/schema/features_tests.rs b/crates/engine/src/schema/features_tests.rs index 80b497368..4ed208cde 100644 --- a/crates/engine/src/schema/features_tests.rs +++ b/crates/engine/src/schema/features_tests.rs @@ -22,6 +22,7 @@ fn schemas_and_the_key_check_agree() { "direction", "distance", "distance2", + "exact_regions", "extent", "face", "face_body", diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs index 6d84ed265..6882959c2 100644 --- a/crates/engine/src/tests.rs +++ b/crates/engine/src/tests.rs @@ -3597,6 +3597,64 @@ fn a_pick_bounded_by_a_projected_rim_takes_only_that_region() { assert!(rel(v, want) < 5e-3, "{v} vs {want}"); } +/// A circle drawn partly past the edge of the top face its sketch is on: the face's projected +/// outline cuts it into a lens on the face and a crescent past it. A pick that asks for exactly +/// its region, as the dialogs do, builds that region alone (#6724); a plain pick still takes the +/// whole drawn circle (#5302). +#[test] +fn an_exact_pick_of_a_region_cut_by_the_face_outline_takes_only_that_region() { + let mut s = Session::default(); + run(&mut s, "sketch.create", json!({"plane": "XY"})); + run(&mut s, "sketch.circle.center", json!({"center": [0, 0], "radius": 10})); + run(&mut s, "sketch.finish", json!({})); + run(&mut s, "solid.extrude", json!({"distance": 10, "body_name": "Base"})); + let id = run(&mut s, "sketch.create", json!({"plane": {"face": [0, 0, 10]}}))["sketch"].as_u64().unwrap(); + run(&mut s, "sketch.circle.center", json!({"center": [10, 0], "radius": 6})); + run(&mut s, "sketch.finish", json!({})); + // The two circles' lens, and the rest of the drawn circle past the face. + let (r1, r2, d) = (10.0_f64, 6.0_f64, 10.0_f64); + let lens = r1 * r1 * ((d * d + r1 * r1 - r2 * r2) / (2.0 * d * r1)).acos() + r2 * r2 * ((d * d + r2 * r2 - r1 * r1) / (2.0 * d * r2)).acos() + - 0.5 * ((-d + r1 + r2) * (d + r1 - r2) * (d - r1 + r2) * (d + r1 + r2)).sqrt(); + let disc = PI * r2 * r2; + let crescent = disc - lens; + let state = s.model.state(); + let ss = state.sketch(id).unwrap(); + assert_eq!(ss.profiles.len(), 3, "the lens, the crescent past the face and the rest of the face"); + let at = |x: f64| ss.profiles.iter().position(|p| p.region.contains(solidcraft_geom::Vec2::new(x, 0.0))).unwrap(); + let (on_face, past_face) = (at(7.0), at(14.0)); + // The volume of the new body, and how many profiles its extrude is built from. + let extrude = |extra: Value| -> (f64, usize) { + let mut s2 = Session::new((*s.doc).clone()); + let mut p = json!({"sketch": id, "distance": 5, "operation": "new", "body_name": "Pad"}); + if let (Some(p), Value::Object(extra)) = (p.as_object_mut(), extra) { + p.extend(extra); + } + run(&mut s2, "solid.extrude", p); + let state = s2.model.state(); + let Some(solidcraft_doc::FeatureKind::Extrude { profiles, .. }) = s2.doc.features.last().map(|f| &f.kind) else { panic!("an extrude") }; + let count = solidcraft_doc::profile_selection_indices(state.sketch(id).unwrap(), profiles).unwrap().len(); + let v = state.bodies.iter().filter(|b| b.name.starts_with("Pad")).map(|b| b.body.tessellate(1e-2).unwrap().measure().volume).sum(); + (v, count) + }; + for (profiles, want, n) in [ + (json!([on_face]), lens, 1), + (json!([past_face]), crescent, 1), + (json!([{"point": [7, 0]}]), lens, 1), + (json!([{"point": [14, 0]}]), crescent, 1), + (json!([on_face, past_face]), disc, 2), + ] { + let (v, count) = extrude(json!({"profiles": profiles.clone(), "exact_regions": true})); + assert!(rel(v, want * 5.0) < 5e-3, "{profiles}: {v} vs {}", want * 5.0); + assert_eq!(count, n, "{profiles}"); + } + // Without `exact_regions`, a pick on either piece takes the whole drawn circle (#5302). + for profiles in [json!([on_face]), json!([{"point": [14, 0]}])] { + let (v, count) = extrude(json!({"profiles": profiles.clone()})); + assert!(rel(v, disc * 5.0) < 5e-3, "{profiles}: {v} vs {}", disc * 5.0); + assert_eq!(count, 2, "{profiles}"); + } +} + /// 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() { diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 4bce5c97d..48cc5db40 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -3546,7 +3546,8 @@ pub fn apply_commands(app: &SolveApp, d: &Dialog) -> Result *value = crate::length_input::expression(&app.session.doc, value, ValueKind::Length); } crate::length_input::extra(&mut normalized.extra, |e| crate::length_input::expression(&app.session.doc, e, ValueKind::Length)); - let cmds = dialog_commands(app, &normalized)?; + let mut cmds = dialog_commands(app, &normalized)?; + exact_regions(d, &mut cmds); if let Some((id, _)) = d.editing { let (cmd, mut params) = cmds.into_iter().next().ok_or("nothing to apply")?; if let Some(feature) = app.session.doc.feature(id) { @@ -3579,6 +3580,23 @@ pub fn apply_commands(app: &SolveApp, d: &Dialog) -> Result Ok(cmds) } +/// A dialog highlights exactly the profile regions it picks, so its commands build exactly those: +/// a region cut off by the projected outline of the face a sketch is on stays the one region +/// clicked (#6724). Without `exact_regions` a command grows such a pick to the whole drawn profile +/// (#5302), which the highlight and the count would not show. +fn exact_regions(d: &Dialog, cmds: &mut [(String, Value)]) { + if !d.inputs.iter().any(|input| input.items.iter().any(|item| matches!(item, Sel::Profile { .. }))) { + return; + } + for (_, params) in cmds { + if let Some(map) = params.as_object_mut() + && (map.contains_key("profiles") || map.contains_key("sections")) + { + map.insert("exact_regions".into(), json!(true)); + } + } +} + /// The commands the live preview runs: OK's, with a joint's motion while it animates. pub fn preview_commands(app: &SolveApp, d: &Dialog) -> Result, String> { let mut cmds = apply_commands(app, d)?; diff --git a/crates/ui-egui/src/dialogs_all_tests.rs b/crates/ui-egui/src/dialogs_all_tests.rs index bd4b6b650..84cdbf85c 100644 --- a/crates/ui-egui/src/dialogs_all_tests.rs +++ b/crates/ui-egui/src/dialogs_all_tests.rs @@ -776,3 +776,43 @@ fn viewport_click_preserves_active_secondary_dialog_value() { h.step(&json!({"text":"30"})).unwrap(); assert!(matches!(&h.app.dialog.as_ref().unwrap().kind, Kind::Extrude { distance, distance2, .. } if distance == "10 mm" && distance2 == "30")); } + +/// A circle drawn partly past the top face its sketch is on is cut by the face's projected outline +/// into a lens on the face and a crescent past it. Clicking one of them in Extrude builds that +/// region alone, the one highlighted and counted, not the whole circle (#6724). +#[test] +fn extrude_builds_the_one_region_clicked_where_the_face_outline_cuts_a_profile() { + for x in [7.0, 14.0] { + let mut h = Harness::new(); + h.app.run("sketch.create", json!({"plane": "XY"})).unwrap(); + h.app.run("sketch.circle.center", json!({"center": [0, 0], "radius": 10})).unwrap(); + h.app.run("sketch.finish", json!({})).unwrap(); + h.app.run("solid.extrude", json!({"distance": 10, "body_name": "Base"})).unwrap(); + let sketch = h.app.run("sketch.create", json!({"plane": {"face": [0, 0, 10]}})).unwrap()["sketch"].as_u64().unwrap(); + h.app.run("sketch.circle.center", json!({"center": [10, 0], "radius": 6})).unwrap(); + h.app.run("sketch.finish", json!({})).unwrap(); + let state = h.app.session.world_state(); + let ss = state.sketch(sketch).unwrap(); + assert_eq!(ss.profiles.len(), 3, "the lens, the crescent past the face and the rest of the face"); + let index = ss.profiles.iter().position(|p| p.region.contains(solidcraft_engine::geom::Vec2::new(x, 0.0))).unwrap(); + let area = ss.profiles[index].area; + let mut d = Dialog::for_command(&h.app, "solid.extrude").unwrap(); + if let Kind::Extrude { distance, .. } = &mut d.kind { + *distance = "5".into(); + } + d.pick(&h.app.session, Sel::Profile { sketch, index }); + assert_eq!(d.items(), vec![Sel::Profile { sketch, index }], "one region picked at x = {x}"); + let commands = apply_commands(&h.app, &d).unwrap(); + assert_eq!(commands.len(), 1, "{commands:?}"); + let (id, p) = &commands[0]; + assert_eq!(p["exact_regions"], json!(true), "{p}"); + let before = h.app.run("inspect.measure", json!({})).unwrap()["total"]["volume_mm3"].as_f64().unwrap(); + let made = h.app.run(id, p.clone()).unwrap()["feature"].as_u64().unwrap(); + let Some(FeatureKind::Extrude { profiles, .. }) = h.app.session.doc.feature(made).map(|f| f.kind.clone()) else { panic!("an extrude") }; + let after = h.app.session.world_state(); + let used = solidcraft_engine::doc::profile_selection_indices(after.sketch(sketch).unwrap(), &profiles).unwrap(); + assert_eq!(used, vec![index], "the region clicked at x = {x}, and only it"); + let volume = h.app.run("inspect.measure", json!({})).unwrap()["total"]["volume_mm3"].as_f64().unwrap(); + assert!(((volume - before) - area * 5.0).abs() < 5e-3 * area * 5.0, "x = {x}: {volume} - {before} vs {}", area * 5.0); + } +}