Skip to content

SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.

Extrude: a profile still found after a sketch line cuts off a sliver or splits an offset curve (#9368) (#9563)

rlartote02e1d40b591
diff --git a/crates/doc/src/profile_refs.rs b/crates/doc/src/profile_refs.rs
index 549f5df51d..e6796a3a12 100644
--- a/crates/doc/src/profile_refs.rs
+++ b/crates/doc/src/profile_refs.rs
@@ -120,6 +120,22 @@ pub fn profile_fallback_notes(ss: &SolvedSketch, selection: &ProfileSel) -> Vec<
profile_selection_indices_lenient(ss, selection).map(|(_, notes)| notes).unwrap_or_default()
}
+/// A curve that is a line, an arc or a circle, and not the output of a free-form offset.
+fn is_analytic(sketch: &solidcraft_sketch::Sketch, id: &str) -> bool {
+ sketch.curves.iter().any(|curve| {
+ curve.id == id
+ && matches!(
+ curve.kind,
+ solidcraft_sketch::CurveKind::Line { .. } | solidcraft_sketch::CurveKind::Arc { .. } | solidcraft_sketch::CurveKind::Circle { .. }
+ )
+ }) && !sketch.freeform_offsets.iter().any(|offset| offset.output == id)
+}
+
+/// A loop made only of analytic curves: how many pieces each is cut into does not matter.
+fn is_analytic_loop(sketch: &solidcraft_sketch::Sketch, curves: &[String]) -> bool {
+ !curves.is_empty() && curves.iter().all(|id| is_analytic(sketch, id))
+}
+
fn quoted(ids: &[String]) -> String {
ids.iter().map(|id| format!("`{id}`")).collect::<Vec<_>>().join(", ")
}
@@ -135,17 +151,32 @@ fn quoted(ids: &[String]) -> String {
/// them; a tie goes to the one with the fewest curves the loop did not have, and a tie left
/// after that is not guessed.
///
-/// Loops of offset curves and legacy graph-span multisets (a curve listed once per piece) keep
-/// their exact meaning and never fall back.
+/// Analytic loops (lines, arcs and circles only) may list a curve more than once, as the pieces a
+/// circle was cut into when the loop was saved; the repeats are one curve here, since a sketch
+/// edit changes how many pieces a curve has but not the region it bounds. The outputs of an
+/// analytic offset (`sketch.offsets`) take step 1 only: their loop closes on its own or the pick
+/// is lost, a partial match by identifiers never stands for it. Loops with free-form offset
+/// outputs, free curves (splines, ellipses, conics) and legacy graph-span multisets keep their
+/// exact meaning and never fall back.
fn fallback_loop(ss: &SolvedSketch, curves: &[String]) -> Option<(Vec<usize>, String)> {
+ let sketch = &ss.sketch;
+ let analytic_loop = is_analytic_loop(sketch, curves);
+ let deduped: Vec<String> = if analytic_loop {
+ let mut seen = BTreeSet::new();
+ curves.iter().filter(|id| seen.insert(id.as_str())).cloned().collect()
+ } else {
+ curves.to_vec()
+ };
+ let curves = deduped.as_slice();
let stored: BTreeSet<&str> = curves.iter().map(String::as_str).collect();
if stored.is_empty() || stored.len() != curves.len() || curves.len() > 512 || ss.profiles.len() > 10_000 {
return None;
}
- let sketch = &ss.sketch;
- let offset =
- |id: &str| sketch.offsets.iter().any(|offset| offset.owns_output(id)) || sketch.freeform_offsets.iter().any(|offset| offset.output == id);
- if stored.iter().any(|id| offset(id)) {
+ if stored.iter().any(|id| sketch.freeform_offsets.iter().any(|offset| offset.output == *id)) {
+ return None;
+ }
+ let from_offset = stored.iter().any(|id| sketch.offsets.iter().any(|offset| offset.owns_output(id)));
+ if from_offset && !analytic_loop {
return None;
}
let drawn = |id: &str| sketch.curves.iter().any(|curve| curve.id == id && !curve.construction && !curve.centerline);
@@ -180,6 +211,9 @@ fn fallback_loop(ss: &SolvedSketch, curves: &[String]) -> Option<(Vec<usize>, St
}
}
}
+ if from_offset {
+ return None;
+ }
// 2. The closest loop by the curves it shares.
let count = |profile: &solidcraft_sketch::Profile, inside: bool| {
profile.outer_curves.iter().map(String::as_str).filter(|id| stored.contains(id) == inside).collect::<BTreeSet<_>>().len()
@@ -231,9 +265,12 @@ fn selection_indices(ss: &SolvedSketch, selection: &ProfileSel, mut notes: Optio
}
indices.push(unique_profile(ss, &ss.sketch.offset_boundary(id)?)?);
} else {
- // Repeated saved IDs identify exact graph spans. Their multiplicity
- // must not silently turn into a whole-source certificate on resize.
+ // Repeated saved IDs of free curves identify exact graph spans. Their
+ // multiplicity must not silently turn into a whole-source certificate on
+ // resize. Lines, arcs and circles are exempt: how many pieces such a curve is
+ // cut into changes with every line drawn across it (#9368).
if curves.iter().collect::<BTreeSet<_>>().len() != curves.len()
+ && !is_analytic_loop(&ss.sketch, curves)
&& !ss.profiles.iter().any(|profile| same_ids(curves, &profile.outer_curves))
{
return Err(crate::curve_pick_error(solidcraft_sketch::CurvePick::Missing, curves, &ss.name));
diff --git a/crates/engine/src/profile_fallback_tests.rs b/crates/engine/src/profile_fallback_tests.rs
index 38642a1dfa..a89ff1ce59 100644
--- a/crates/engine/src/profile_fallback_tests.rs
+++ b/crates/engine/src/profile_fallback_tests.rs
@@ -112,6 +112,52 @@ fn exact_matches_stay_quiet_and_nothing_plausible_is_an_error() {
assert!(s.model.state().bodies.is_empty());
}
+/// The public example of #9368: a 40 x 20 rectangle with a circle tangent to its top and bottom
+/// (radius 10 at (20, 10)), or a ring offset 1 mm around a circle of radius 9 with `offset`.
+/// `Gauche` is the region left of the circle, `Disque` (joined) the disc or the ring.
+fn rectangle_and_circle(offset: bool) -> Session {
+ let mut s = Session::default();
+ run(&mut s, "sketch.create", json!({"plane": "XY", "name": "S1"}));
+ run(&mut s, "sketch.rectangle.two_point", json!({"p0": [0, 0], "p1": [40, 20]}));
+ run(&mut s, "sketch.circle.center", json!({"center": [20, 10], "radius": if offset { 9 } else { 10 }}));
+ if offset {
+ run(&mut s, "sketch.offset", json!({"curves": ["c1"], "distance": 1, "side": [20, 25]}));
+ }
+ run(&mut s, "sketch.finish", json!({}));
+ run(&mut s, "solid.extrude", json!({"distance": "10 mm", "profiles": [{"point": [3, 10]}], "name": "Gauche"}));
+ let disque = if offset { [20.0, 19.5] } else { [20.0, 15.0] };
+ run(&mut s, "solid.extrude", json!({"distance": "10 mm", "profiles": [{"point": disque}], "operation": "join", "name": "Disque"}));
+ s
+}
+
+/// A line from the top side to the circle cuts a sliver off the left region and gives the circle
+/// more arcs: the disc is still the same region, so it is found without a word, and the left region
+/// is still found, as its two pieces.
+#[test]
+fn an_extrude_survives_a_line_ending_on_a_circle_split_by_tangents() {
+ let mut s = rectangle_and_circle(false);
+ edit(&mut s, &[("sketch.line", json!({"points": [[14, 20], [14, 18]]}))]);
+ assert_eq!(result(&s, "Disque"), (None, None));
+ let (error, warning) = result(&s, "Gauche");
+ assert_eq!(error, None);
+ assert!(warning.unwrap_or_default().contains("2 pieces"));
+ assert!((volume(&s) - 5570.6).abs() < 0.5, "volume {}", volume(&s));
+}
+
+/// Same sketch with a ring offset from a circle: the loops name the offset's output. The line
+/// cuts the left region (its closed loop is taken as two pieces, with a warning), and the ring,
+/// only given more arcs, is found as it was.
+#[test]
+fn an_extrude_with_an_offset_curve_survives_a_line_splitting_its_profile() {
+ let mut s = rectangle_and_circle(true);
+ edit(&mut s, &[("sketch.line", json!({"points": [[14, 20], [14, 18]]}))]);
+ let (error, warning) = result(&s, "Gauche");
+ assert_eq!(error, None);
+ assert!(warning.unwrap_or_default().contains("2 pieces"));
+ assert_eq!(result(&s, "Disque"), (None, None));
+ assert!((volume(&s) - 3026.2).abs() < 0.5, "volume {}", volume(&s));
+}
+
/// A block `E1` with a sketch `S2` on its top cap and a hole cut from it, `E2`.
fn block_with_hole() -> Session {
let mut s = Session::default();
diff --git a/docs/book/src/dev/naming.md b/docs/book/src/dev/naming.md
index a99c5cae0b..4650865c9e 100644
--- a/docs/book/src/dev/naming.md
+++ b/docs/book/src/dev/naming.md
@@ -128,7 +128,10 @@ best match and warns, instead of failing and taking its body and everything afte
fewest curves the loop did not have, and a tie left after that is an error, as is a loop
that matches nothing.
-Loops of offset curves and legacy graph-span multisets keep their exact meaning. Commands still
+A line, arc or circle listed twice (the pieces a circle was cut into when the pick was saved) is
+one curve to a pick, so a line that ends on a circle and gives it more arcs changes nothing; the
+outputs of an analytic offset take step 1 only, never step 2 (#9368). Loops of free-form offset
+curves, free curves and legacy graph-span multisets keep their exact meaning. Commands still
check a pick with the exact rule, so a curve list that names no profile is refused when given.
A sketch on such an extrude's cap falls back the same way: its certificate names the loop, and
the cap of the same solid extrude at the same end, from the same sketch and on the same body,