Skip to content

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

Chamfer and Fillet refuse a point where several edges meet (#5173) (#5177)

raykholo189b47c75025
diff --git a/apps/solidcraft-cli/tests/action_matrix.rs b/apps/solidcraft-cli/tests/action_matrix.rs
index 5d0a52b4f0..21c34a8b62 100644
--- a/apps/solidcraft-cli/tests/action_matrix.rs
+++ b/apps/solidcraft-cli/tests/action_matrix.rs
@@ -395,8 +395,10 @@ fn actions(s: &Session) -> Vec<Action> {
let r = round3((shortest * 0.1).max(0.01));
let mut add = |action, act| out.push(Action { entity: entity.clone(), action, act });
if !smooth(e, mid) {
- add("edge.fillet", Act::Run("solid.fillet", json!({"edges": [pt(mid)], "radius": r})));
- add("edge.chamfer", Act::Run("solid.chamfer", json!({"edges": [pt(mid)], "distance": r})));
+ // The midpoint is interior to this edge, but other bodies (including component
+ // copies in their local frames) can have coincident edges. Retain the picked body.
+ add("edge.fillet", Act::Run("solid.fillet", json!({"body": world, "edges": [pt(mid)], "radius": r})));
+ add("edge.chamfer", Act::Run("solid.chamfer", json!({"body": world, "edges": [pt(mid)], "distance": r})));
}
let dir = straight(pts);
if dir.is_some() {
@@ -1278,6 +1280,41 @@ fn rectangle(plane: Value, p0: [f64; 2], p1: [f64; 2]) -> [Value; 3] {
]
}
+#[test]
+fn edge_round_actions_keep_the_picked_body() {
+ for (corner, length) in [([10, 0, 20], 4), ([0, 0, 0], 20)] {
+ let mut s = Session::default();
+ s.execute("solid.box", &json!({"length": 20, "width": 20, "height": 20, "body_name": "Plate"})).unwrap();
+ s.execute(
+ "solid.box",
+ &json!({"corner": corner, "length": length, "width": length, "height": length, "body_name": "Block", "operation": "new"}),
+ )
+ .unwrap();
+ let mut tried = 0;
+ for a in actions(&s) {
+ if !a.entity.starts_with("Plate/") || !matches!(a.action, "edge.chamfer" | "edge.fillet") {
+ continue;
+ }
+ let Act::Run(id, ref p) = a.act else { continue };
+ let q = &p["edges"][0];
+ let x = q[0].as_f64().unwrap();
+ if q[1] != 0.0 || q[2] != 20.0 || !(0.0..20.0).contains(&x) {
+ continue;
+ }
+ // Block either meets this midpoint or coincides with the whole edge.
+ assert_eq!(p["body"], "Plate", "{}: {p}", a.action);
+ assert_eq!(q, &json!([10.0, 0.0, 20.0]));
+ let mut unscoped = p.clone();
+ unscoped.as_object_mut().unwrap().remove("body");
+ let err = s.scratch().execute(id, &unscoped).unwrap_err().to_string();
+ assert!(err.contains("edges meet"), "{err}");
+ perform(&mut s.scratch(), &a.act, &errors(&s)).unwrap();
+ tried += 1;
+ }
+ assert_eq!(tried, 2);
+ }
+}
+
#[test]
fn a_fillet_or_chamfer_on_one_edge_ending_at_a_cylinder() {
for (cmd, size) in [("solid.fillet", "radius"), ("solid.chamfer", "distance")] {
diff --git a/crates/engine/src/cmd/features.rs b/crates/engine/src/cmd/features.rs
index b8f5e6f007..b129091dab 100644
--- a/crates/engine/src/cmd/features.rs
+++ b/crates/engine/src/cmd/features.rs
@@ -1208,7 +1208,8 @@ fn edge_points(s: &Session, p: &Value, cmd: &str) -> Result<Vec<Vec3>> {
// of the body the feature names, or of any body: a pick far from every edge would round
// whichever edge is nearest (#783). Each body's edges are worked out once.
let named = str_(p, "body").filter(|n| st.body(n).is_some());
- let mut candidates: Option<Vec<(f64, Vec<solidcraft_kernel::EdgeInfo>)>> = None;
+ // Per body: its size, its edges' polylines and which of them are seams.
+ let mut candidates: Option<Vec<(f64, Vec<Vec<Vec3>>, Vec<bool>)>> = None;
let mut out = Vec::new();
for e in a {
let pt = match edge_ref_form(e).map_err(|why| bad(cmd, why))? {
@@ -1229,21 +1230,50 @@ fn edge_points(s: &Session, p: &Value, cmd: &str) -> Result<Vec<Vec3>> {
.filter(|b| named.is_none_or(|n| b.name == n) && !b.body.is_mesh())
.filter_map(|b| {
let size = b.body.size();
- b.body.edges((size * 2e-3).max(1e-3)).ok().map(|edges| (size, edges))
+ let tol = (size * 2e-3).max(1e-3);
+ // The display mesh flags seams (a smooth surface meeting itself, as down the side
+ // of a cylinder), which nobody means by a pick where they end.
+ match b.body.display_mesh(tol) {
+ Ok(m) => Some((size, m.edges, m.seams)),
+ Err(_) => b.body.edges(tol).ok().map(|edges| (size, edges.into_iter().map(|e| e.points).collect(), Vec::new())),
+ }
})
.collect()
});
+ // A point where several edges meet (a corner) is ambiguous: the kernel would round one of
+ // them, picked silently (#5173). Edges that carry straight on through the point (the two
+ // halves of a circle) are one edge to whoever picked it, and seams don't count. Directions
+ // come from the polylines' end chords, which lean off a curve's tangent: within about 45°
+ // of opposite counts as carrying on (edges at a real corner are far from opposite).
let mut nearest = f64::INFINITY;
- let on_edge = bodies.iter().any(|(size, edges)| {
- let d = edges
- .iter()
- .flat_map(|e| e.points.windows(2))
- .filter_map(|w| Some(pt.dist_to_segment(*w.first()?, *w.get(1)?)))
- .fold(f64::INFINITY, f64::min);
- nearest = nearest.min(d);
- d <= size * 0.05 + 1e-3
- });
- if !on_edge && !bodies.is_empty() {
+ let mut near = Vec::new();
+ for (size, edges, seams) in bodies.iter() {
+ for (i, points) in edges.iter().enumerate() {
+ let d = points.windows(2).filter_map(|w| Some(pt.dist_to_segment(*w.first()?, *w.get(1)?))).fold(f64::INFINITY, f64::min);
+ nearest = nearest.min(d);
+ if d <= size * 0.05 + 1e-3 {
+ near.push((d, seams.get(i).copied().unwrap_or(false), leaving(points, pt)));
+ }
+ }
+ }
+ let matches = near.len();
+ let mut meeting: Vec<Option<Vec3>> = near.iter().filter(|(d, seam, _)| !seam && *d <= nearest + 1e-3).map(|(_, _, dir)| *dir).collect();
+ let mut chains = 0usize;
+ while let Some(dir) = meeting.pop() {
+ chains += 1;
+ if let Some(dir) = dir
+ && let Some(j) = meeting.iter().position(|o| o.is_some_and(|other| other.dot(dir) < -0.7))
+ {
+ meeting.swap_remove(j);
+ }
+ }
+ if chains > 1 {
+ return Err(bad(
+ cmd,
+ format!("[{}, {}, {}] is where {chains} edges meet: give a point along one edge, such as its midpoint", pt.x, pt.y, pt.z),
+ ));
+ }
+ if matches == 0 && !bodies.is_empty() {
let far = if nearest.is_finite() { format!("the nearest edge is {nearest:.3} mm away") } else { "there is no edge".to_string() };
return Err(bad(cmd, format!("no edge at [{}, {}, {}]: {far}", pt.x, pt.y, pt.z)));
}
@@ -1252,6 +1282,19 @@ fn edge_points(s: &Session, p: &Value, cmd: &str) -> Result<Vec<Vec3>> {
Ok(out)
}
+/// The direction an edge's polyline leaves `pt` in, when `pt` is one of its ends; `None` when
+/// the edge runs on through `pt`.
+fn leaving(points: &[Vec3], pt: Vec3) -> Option<Vec3> {
+ let (first, last) = (points.first()?, points.last()?);
+ if first.dist(pt) <= 1e-3 {
+ (*points.get(1)? - *first).normalized()
+ } else if last.dist(pt) <= 1e-3 {
+ (*points.get(points.len().checked_sub(2)?)? - *last).normalized()
+ } else {
+ None
+ }
+}
+
/// An edge reference as given, before it is looked up: see [`edge_points`].
pub(crate) enum EdgeRefForm<'a> {
Point(Vec3),
diff --git a/crates/engine/src/cmd/script_tests.rs b/crates/engine/src/cmd/script_tests.rs
index 359973b6c6..0b750cebc7 100644
--- a/crates/engine/src/cmd/script_tests.rs
+++ b/crates/engine/src/cmd/script_tests.rs
@@ -105,7 +105,8 @@ fn ids_names_suppression_marker_and_failures_survive() {
run(&mut s, "parameters.favorite", json!({"name": depth}));
// A fillet that a parameter change makes too big fails, and the design keeps it.
run(&mut s, "parameters.add", json!({"name": "r", "expression": "1 mm"}));
- run(&mut s, "solid.fillet", json!({"edges": [[5, 0, 10]], "radius": "r"}));
+ // Pick only the plate's edge, clear of the overlapping box's edge.
+ run(&mut s, "solid.fillet", json!({"edges": [[20, 0, 10]], "radius": "r"}));
let fillet = s.doc.features.last().unwrap().id;
run(&mut s, "parameters.change", json!({"name": "r", "expression": "50 mm"}));
assert!(s.model.result(fillet).and_then(|r| r.error.clone()).is_some(), "the fillet fails");
diff --git a/crates/engine/src/tests.rs b/crates/engine/src/tests.rs
index b094d649a8..ccc86d87f7 100644
--- a/crates/engine/src/tests.rs
+++ b/crates/engine/src/tests.rs
@@ -58,6 +58,26 @@ fn edge_picks_far_from_every_edge_are_refused() {
assert_eq!(s.doc.features.len(), features);
}
+#[test]
+fn edge_picks_at_a_corner_are_refused() {
+ for (id, size) in [("solid.chamfer", "distance"), ("solid.fillet", "radius")] {
+ let mut s = Session::default();
+ run(&mut s, "solid.box", json!({"length": 20, "width": 20, "height": 20}));
+ let features = s.doc.features.len();
+ for edge in [json!([20, 20, 20]), json!({"point": [20, 20, 20]})] {
+ let err = s.execute(id, &json!({"edges": [edge], size: 2})).unwrap_err().to_string();
+ assert!(err.contains("[20, 20, 20]") && err.contains("3 edges") && err.contains("midpoint"), "{id}: {err}");
+ assert_eq!(s.doc.features.len(), features);
+ assert!((volume(&mut s) - 8000.0).abs() < 1e-6);
+ }
+ // 1 mm from the corner along one edge: near the other two, but on just that one.
+ run(&mut s, id, json!({"edges": [[19, 20, 20]], size: 2}));
+ assert_eq!(s.doc.features.len(), features + 1);
+ let removed = if id == "solid.chamfer" { 40.0 } else { (4.0 - PI) * 20.0 };
+ assert!(rel(volume(&mut s), 8000.0 - removed) < 2e-4);
+ }
+}
+
#[test]
fn fillet_as_wide_as_a_face_rounds_it_off() {
// #199: 14 x 6 x 10 box, the four vertical edges at r = 3 (half the 6 mm end faces).