Skip to content

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

Chamfer: a manipulator for Distance 2 with Two Distances (#2711) (#2856)

Logan Nightb0c4b6e9397b
diff --git a/crates/ui-egui/src/blend_arrow_tests.rs b/crates/ui-egui/src/blend_arrow_tests.rs
index a2dbfeec9b..9ad9f0d22f 100644
--- a/crates/ui-egui/src/blend_arrow_tests.rs
+++ b/crates/ui-egui/src/blend_arrow_tests.rs
@@ -101,3 +101,31 @@ fn dragging_out_along_the_arrow_grows_fillet_and_chamfer_on_both_kinds_of_edge()
}
}
}
+
+/// Chamfer Type "Two Distances" (#2711): Distance 1 runs along the face that faces up most (the
+/// other one with Flip) and Distance 2 along the remaining face, so the window shows an arrow
+/// for each, from the edge into its face.
+#[test]
+fn two_distances_chamfer_has_an_arrow_along_each_face() {
+ let mut h = Harness::new();
+ h.app.run("solid.box", json!({"length": 40, "width": 30, "height": 20})).unwrap();
+ // The top front edge: between the top face (up) and the front face (facing -y).
+ let top_front = Vec3::new(20.0, 0.0, 20.0);
+ let (along_top, along_front) = (Vec3::new(0.0, 1.0, 0.0), Vec3::new(0.0, 0.0, -1.0));
+ for (flip, expected) in [(false, (along_top, along_front)), (true, (along_front, along_top))] {
+ h.app.session.selection = vec![edge(&h, top_front)];
+ h.app.start("solid.chamfer");
+ // Equal distances keep the arrow along the bisector, and have no second one.
+ let d = h.app.dialog.as_ref().unwrap();
+ assert!(crate::canvas::chamfer_face_dirs(&h.app, d, top_front).is_none());
+ let Some(Kind::Fillet { ctype, flip: f, .. }) = h.app.dialog.as_mut().map(|d| &mut d.kind) else { panic!("a chamfer window") };
+ (*ctype, *f) = (1, flip);
+ let d = h.app.dialog.as_ref().unwrap();
+ let (t1, t2) = crate::canvas::chamfer_face_dirs(&h.app, d, top_front).expect("both faces");
+ assert!(close(t1, expected.0) && close(t2, expected.1), "flip {flip}: {t1:?} {t2:?}");
+ // Distance 1's arrow is the first one.
+ let dir = anchor(&h.app, d).unwrap().1.unwrap();
+ assert!(close(dir, expected.0), "flip {flip}: arrow {dir:?}");
+ h.app.dialog = None;
+ }
+}
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rs
index d3cd1bae93..a7178638f9 100644
--- a/crates/ui-egui/src/canvas.rs
+++ b/crates/ui-egui/src/canvas.rs
@@ -119,9 +119,11 @@ pub fn anchor(app: &SolveApp, d: &Dialog) -> Option<(Vec3, Option<Vec3>)> {
// Fillet radii and chamfer distances grow the way `blend_grow_dir` gives (into the body
// on an outside edge, into the open corner on an inside one); shell thickness grows into
// the body.
+ // A chamfer of two distances measures Distance 1 along its own face, not along the bisector.
Kind::Fillet { .. } if matches!(first, Sel::Edge { .. }) => {
let Sel::Edge { body, index, .. } = first else { return None };
- (at, st.body(body).and_then(|b| blend_grow_dir(&b.mesh(), *index, at)))
+ let along_face = chamfer_face_dirs(app, d, at).map(|(first_face, _)| first_face);
+ (at, along_face.or_else(|| st.body(body).and_then(|b| blend_grow_dir(&b.mesh(), *index, at))))
}
Kind::Fillet { .. } | Kind::Shell { .. } => (at, normal.map(|n| -n)),
Kind::Sheet(_) => (at, crate::dialogs_sheet::arrow(app, d).or(normal)),
@@ -241,6 +243,27 @@ pub(crate) fn blend_grow_dir(m: &Mesh, index: usize, p: Vec3) -> Option<Vec3> {
Some(if edge_concave(m, index, p).unwrap_or(false) { n } else { -n })
}
+/// The directions Distance 1 and Distance 2 of a chamfer of two distances run from its edge at
+/// `at`: along the face that faces up most, or the other one with Flip, and along the remaining
+/// face (the kernel's `chamfer_tool_sides` picks them the same way). None for any other dialog.
+pub(crate) fn chamfer_face_dirs(app: &SolveApp, d: &Dialog, at: Vec3) -> Option<(Vec3, Vec3)> {
+ let Kind::Fillet { chamfer: true, ctype: 1, flip, .. } = &d.kind else { return None };
+ let Some(Sel::Edge { body, index, .. }) = d.inputs.first()?.items.first() else { return None };
+ let st = app.session.world_state();
+ let mesh = st.body(body)?.mesh();
+ let sides = edge_sides(&mesh, *index, at)?;
+ let [(n1, c1), (n2, c2)] = sides.as_slice() else { return None };
+ let along = n1.cross(*n2).normalized()?;
+ // From the edge into each face: the way to its centroid, square to the edge.
+ let into = |c: Vec3| {
+ let v = c - at;
+ (v - along * v.dot(along)).normalized()
+ };
+ let (t1, t2) = (into(*c1)?, into(*c2)?);
+ let up = Vec3::new(1e-6, 1e-3, 1.0);
+ Some(if (n2.dot(up) > n1.dot(up)) != *flip { (t2, t1) } else { (t1, t2) })
+}
+
/// Whether a dynamically registered canvas handle owns this point before navigation.
pub(crate) fn owns_pointer(app: &SolveApp, proj: &Proj, p: Pos2) -> bool {
// The triad only registers an egui hit widget after a handle is under the pointer.
@@ -417,6 +440,13 @@ fn draw(app: &SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj: &Proj,
{
drag_arrow(ui, painter, proj, base, bs, -(n * sign), l2, true, egui::Id::new("sc_manipulator2"), half_height, distance2);
}
+ // A chamfer of two distances: Distance 2 gets its own arrow along the other face.
+ let faces = chamfer_face_dirs(app, d, base);
+ if let (Kind::Fillet { distance2, .. }, Some((_, t2))) = (&mut d.kind, faces)
+ && let Some(l2) = app.session.doc.eval(distance2, ValueKind::Length).ok().filter(|v| v.is_finite())
+ {
+ drag_arrow(ui, painter, proj, base, bs, t2, l2, false, egui::Id::new("sc_manipulator2"), half_height, distance2);
+ }
// A rectangular pattern's second direction gets its own arrow for Spacing 2 (#2462), as the
// first direction's arrow sets Spacing.
if let Some(n2) = pattern_dir2(app, d) {