SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Extrude by pressing on a face or profile and pulling (#4767)
Nathalie Schönwettercf1b0d67ccb9
diff --git a/crates/ui-egui/src/canvas.rs b/crates/ui-egui/src/canvas.rsindex 34404ac3e7..6bc558f239 100644--- a/crates/ui-egui/src/canvas.rs+++ b/crates/ui-egui/src/canvas.rs@@ -368,7 +368,7 @@ pub fn snap_step(half_height: f64) -> f64 {}/// Parameter along the line `base + dir * t` closest to the view ray through `pos`.-fn along(proj: &Proj, pos: Pos2, base: Vec3, dir: Vec3) -> Option<f64> {+pub(crate) fn along(proj: &Proj, pos: Pos2, base: Vec3, dir: Vec3) -> Option<f64> {let (o, r) = proj.ray(pos);let w0 = base - o;let (b, d, e) = (dir.dot(r), dir.dot(w0), r.dot(w0));@@ -575,7 +575,8 @@ fn draw(app: &SolveApp, ui: &mut egui::Ui, painter: &egui::Painter, proj: &Proj,box_at = ts + vec2(14.0, -12.0);}}- value_box(ui, label, value, box_at, focus || *value != before);+ let note = crate::drag_extrude::step_note(app);+ value_box_noted(ui, label, value, box_at, focus || *value != before, note.as_deref());if *value != before&& let Kind::Extrude { all, .. } = &mut d.kind{@@ -600,17 +601,36 @@ fn value_box_with(ui: &mut egui::Ui, label: &str, value: &mut String, at: Pos2,value_box_in(ui, ids, label, value, (at, egui::Align2::LEFT_TOP), select_all, keep_tab);}+/// [`value_box`] with a note after the field (a pull's step size).+fn value_box_noted(ui: &mut egui::Ui, label: &str, value: &mut String, at: Pos2, select_all: bool, note: Option<&str>) {+ let ids = (egui::Id::new("sc_canvas_area"), egui::Id::new("sc_canvas_value"));+ value_box_full(ui, ids, label, value, (at, egui::Align2::LEFT_TOP), (select_all, false), note);+}+/// [`value_box_with`] under its own ids (its area's and its field's), for a value beside the main/// one (a hole's reference distances), with the point of the box that sits at `at`. Returns where/// the field is.pub(crate) fn value_box_in(ui: &mut egui::Ui,- (area, id): (egui::Id, egui::Id),+ ids: (egui::Id, egui::Id),label: &str,value: &mut String,- (at, pivot): (Pos2, egui::Align2),+ at: (Pos2, egui::Align2),select_all: bool,keep_tab: bool,+) -> Option<egui::Rect> {+ value_box_full(ui, ids, label, value, at, (select_all, keep_tab), None)+}++/// [`value_box_in`] with a short dimmed note after the field (a pull's step size).+fn value_box_full(+ ui: &mut egui::Ui,+ (area, id): (egui::Id, egui::Id),+ label: &str,+ value: &mut String,+ (at, pivot): (Pos2, egui::Align2),+ (select_all, keep_tab): (bool, bool),+ note: Option<&str>,) -> Option<egui::Rect> {let t = Tokens::get();let mut field = None;@@ -627,6 +647,9 @@ pub(crate) fn value_box_in(st.cursor.set_char_range(Some(egui::text_selection::CCursorRange::two(egui::text::CCursor::new(0), egui::text::CCursor::new(n))));st.store(ui.ctx(), id);}+ if let Some(note) = note {+ ui.label(egui::RichText::new(note).color(t.text_dim).size(crate::theme::Tokens::get().font_caption));+ }field = Some(r.rect);});});diff --git a/crates/ui-egui/src/drag_extrude.rs b/crates/ui-egui/src/drag_extrude.rsnew file mode 100644index 0000000000..2616665407--- /dev/null+++ b/crates/ui-egui/src/drag_extrude.rs@@ -0,0 +1,219 @@+//! Extrude by dragging: press on a planar face (or a sketch profile) and pull. Once the pointer+//! has moved far enough upward, or along the surface's normal on screen, Extrude starts on the+//! pick; the distance follows the pointer along the normal in steps. Moving the pointer sideways+//! while pulling sets the step: to the right it grows (1, 2, 3 ... 10 mm), to the left it+//! shrinks (0.8, 0.5 ... 0.1 mm). The step and the distance show beside the pointer. Letting go+//! leaves the Extrude window open to adjust and confirm with OK. A press that goes any other+//! way stays a box selection.++use egui::Pos2;+use solidcraft_engine::Sel;+use solidcraft_engine::geom::Vec3;++use crate::SolveApp;+use crate::dialogs::{Kind, planar_face};+use crate::viewport::{Hit, Proj};++/// Screen distance the pointer must travel before a press counts as a pull.+const START_PX: f32 = 6.0;+/// How closely (cosine) a drag must follow the normal's screen direction to start a pull.+const ALIGNED: f32 = 0.8;+/// Screen distance the pointer travels sideways for one step of the ladder.+const PX_PER_STEP: f32 = 44.0;+/// The step sizes (mm) a pull can snap to, finest first.+const STEPS: [f64; 25] =+ [0.01, 0.02, 0.05, 0.1, 0.2, 0.3, 0.4, 0.5, 0.65, 0.7, 0.8, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 8.0, 10.0, 20.0, 50.0, 100.0, 200.0, 500.0, 1000.0];++/// The ladder position nearest to `step`.+fn nearest_step(step: f64) -> usize {+ (0..STEPS.len()).min_by(|a, b| (STEPS[*a] / step).ln().abs().total_cmp(&(STEPS[*b] / step).ln().abs())).unwrap_or(0)+}++/// The ladder position `dx` screen points sideways from `base`, clamped to the ladder.+fn step_index(base: usize, dx: f32) -> usize {+ let moved = (dx / PX_PER_STEP).round() as i64;+ (base as i64 + moved).clamp(0, STEPS.len() as i64 - 1) as usize+}++/// A press on a surface that may turn into a pull.+#[derive(Clone, Debug)]+pub struct Gesture {+ press: Pos2,+ sel: Sel,+ /// A point on the surface where it was pressed, and its unit normal.+ base: Vec3,+ normal: Vec3,+ /// Where along the normal the press was: the distance is measured from here.+ grab: f64,+ /// Extrude has started (the window is open and follows the pointer).+ started: bool,+ /// The step at the start (a ladder position) and the pointer's x there: sideways moves+ /// from it change the step.+ step0: usize,+ x0: f32,+ /// The step now, and where the pointer is, for the label.+ step: f64,+ at: Pos2,+}++#[derive(Debug, Default)]+pub struct State {+ gesture: Option<Gesture>,+ /// The step of the last pull, shown in the window until it closes.+ last_step: Option<f64>,+}++/// A pull has started: the box selection stays out of it.+pub fn started(app: &SolveApp) -> bool {+ app.viewport.drag_extrude.gesture.as_ref().is_some_and(|g| g.started)+}++/// Note a press at `at` when it is on a planar face or a profile.+pub fn press(app: &mut SolveApp, proj: &Proj, at: Pos2) {+ app.viewport.drag_extrude.gesture = None;+ if !app.preferences.direct_interaction {+ return;+ }+ let hits = crate::viewport::pick(app, proj, at);+ let Some((hit, Some(sel))) = crate::viewport::candidate(app, &hits).map(|(h, s)| (h, Some(s))) else { return };+ let (o, d) = proj.ray(at);+ let (base, normal) = match (&hit, &sel) {+ (Hit::Face { point, .. }, Sel::Face { body, index, .. }) => match planar_face(&app.session, body, *index) {+ Some((_, n)) => (*point, n),+ None => return,+ },+ (Hit::Profile { sketch, .. }, Sel::Profile { .. }) => {+ let Some(plane) = crate::canvas::profile_plane(app, *sketch) else { return };+ match plane.intersect_ray(o, d) {+ Some(p) => (p, plane.normal()),+ None => return,+ }+ }+ _ => return,+ };+ let Some(grab) = crate::canvas::along(proj, at, base, normal) else { return };+ app.viewport.drag_extrude.gesture = Some(Gesture {+ press: at,+ sel,+ base,+ normal,+ grab,+ started: false,+ step0: nearest_step(crate::canvas::snap_step(app.cam.half_height())),+ x0: at.x,+ step: 0.0,+ at,+ });+}++/// Whether the move from the press to `p` is a pull: upward, or along the normal on screen.+fn pulls(proj: &Proj, g: &Gesture, p: Pos2) -> bool {+ let delta = p - g.press;+ if delta.length() < START_PX {+ return false;+ }+ let unit = delta / delta.length();+ let upward = delta.y < 0.0 && -unit.y > ALIGNED;+ let along_normal = proj+ .to_screen(g.base)+ .zip(proj.to_screen(g.base + g.normal))+ .map(|(a, b)| b - a)+ .filter(|n| n.length() > 1e-3)+ .is_some_and(|n| (n / n.length()).dot(unit).abs() > ALIGNED);+ upward || along_normal+}++/// The pointer is at `p` with the button down.+pub fn to(app: &mut SolveApp, proj: &Proj, p: Pos2) {+ let Some(mut g) = app.viewport.drag_extrude.gesture.clone() else { return };+ if !g.started {+ if app.dialog.is_some() || !pulls(proj, &g, p) {+ return;+ }+ crate::viewport::select(app, Some(g.sel.clone()), false, false);+ app.start("solid.press_pull");+ if !matches!(app.dialog.as_ref().map(|d| &d.kind), Some(Kind::Extrude { .. })) {+ app.viewport.drag_extrude.gesture = None;+ return;+ }+ g = Gesture { started: true, x0: p.x, ..g };+ }+ g.at = p;+ g.step = STEPS[step_index(g.step0, p.x - g.x0)];+ let Some(v) = crate::canvas::along(proj, p, g.base, g.normal) else {+ app.viewport.drag_extrude.gesture = Some(g);+ return;+ };+ let free = crate::tool_modifiers::manipulator_free();+ let travelled = v - g.grab;+ let travelled = if free { travelled } else { (travelled / g.step).round() * g.step };+ app.viewport.drag_extrude.last_step = Some(g.step);+ app.viewport.drag_extrude.gesture = Some(g);+ let Some(Kind::Extrude { distance, direction, .. }) = app.dialog.as_mut().map(|d| &mut d.kind) else { return };+ // A reversed extrude measures its distance against the normal.+ let sign = if matches!(*direction, 1 | crate::dialogs::TWO_SIDES_REVERSED) { -1.0 } else { 1.0 };+ *distance = crate::canvas::format_len(travelled * sign);+}++/// The button is up: the window stays open for the user to adjust and confirm.+pub fn end(app: &mut SolveApp) {+ app.viewport.drag_extrude.gesture = None;+}++/// Forget the last pull's step once the window is gone.+pub fn tick(app: &mut SolveApp) {+ if app.dialog.is_none() {+ app.viewport.drag_extrude.last_step = None;+ }+}++/// The step of the last pull, for the value box next to the distance.+pub fn step_note(app: &SolveApp) -> Option<String> {+ let step = app.viewport.drag_extrude.last_step?;+ Some(format!("Step {}", crate::canvas::format_len(step)))+}++/// The ladder of steps beside the pointer while pulling: the one in use between its neighbours.+/// It sits below the pointer, clear of the distance box at the arrow tip.+pub fn show(app: &SolveApp, painter: &egui::Painter) {+ let Some(g) = app.viewport.drag_extrude.gesture.as_ref().filter(|g| g.started) else { return };+ let t = crate::theme::Tokens::get();+ let now = nearest_step(g.step);+ let galley = |i: usize, current: bool| {+ let size = if current { t.font_body * 1.25 } else { t.font_body };+ let color = if current { t.accent } else { t.text_dim };+ let text = STEPS.get(i).map(|s| crate::canvas::format_len(*s)).unwrap_or_default();+ painter.layout_no_wrap(text, egui::FontId::proportional(size), color)+ };+ let cells: Vec<_> = (now.saturating_sub(2)..=(now + 2).min(STEPS.len() - 1)).map(|i| (galley(i, i == now), i == now)).collect();+ let gap = 12.0;+ let width = cells.iter().map(|(g, _)| g.size().x).sum::<f32>() + gap * cells.len().saturating_sub(1) as f32;+ let height = cells.iter().map(|(g, _)| g.size().y).fold(0.0, f32::max);+ let pos = g.at + egui::vec2(18.0, 22.0);+ let rect = egui::Rect::from_min_size(pos, egui::vec2(width, height)).expand(5.0);+ painter.rect_filled(rect, 4.0, t.overlay);+ painter.rect_stroke(rect, 4.0, egui::Stroke::new(1.0, t.accent), egui::StrokeKind::Inside);+ let mut x = pos.x;+ for (g, _) in cells {+ let w = g.size().x;+ let y = pos.y + (height - g.size().y) / 2.0;+ painter.galley(egui::pos2(x, y), g, t.text);+ x += w + gap;+ }+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn sideways_moves_walk_the_step_ladder() {+ let one = nearest_step(1.0);+ assert_eq!(STEPS[one], 1.0);+ assert_eq!(STEPS[step_index(one, PX_PER_STEP)], 2.0);+ assert_eq!(STEPS[step_index(one, -PX_PER_STEP)], 0.8);+ assert_eq!(STEPS[step_index(one, -8.0 * PX_PER_STEP)], 0.1);+ assert_eq!(step_index(one, -1e6), 0);+ assert_eq!(step_index(one, 1e6), STEPS.len() - 1);+ }+}diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rsindex 2978e76ec8..d8841628b0 100644--- a/crates/ui-egui/src/lib.rs+++ b/crates/ui-egui/src/lib.rs@@ -52,6 +52,7 @@ pub mod dim_view;#[cfg(test)]mod dimension_create_ui_tests;pub mod documents;+pub mod drag_extrude;pub mod drag_snap;pub mod edit_clipboard;#[cfg(test)]diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rsindex ec5e76d709..2048ce72d3 100644--- a/crates/ui-egui/src/prefs.rs+++ b/crates/ui-egui/src/prefs.rs@@ -457,6 +457,8 @@ preference_settings! {navigator_style: NavigatorStyle = NavigatorStyle::default(), PAGE_NAVIGATION;/// Show the radial ring and enable hold/flick gestures for viewport context menus.radial_context_menu: bool = false, PAGE_NAVIGATION => check("Radial context menu");+ /// Work on the model directly in the viewport (pull a face to extrude or cut); off by default.+ direct_interaction: bool = false, PAGE_NAVIGATION => check("Direct interaction", "Work on the model directly in the viewport. Press a flat face or a profile and pull: up extrudes, down into a body cuts, and sideways movement sets the step size");/// Mouse gestures: a ring of tools on the right button.mouse_gestures: crate::mouse_gesture::Config = Default::default(), PAGE_NAVIGATION;/// The wheel zooms out when turned forward.diff --git a/crates/ui-egui/src/prefs/navigation.rs b/crates/ui-egui/src/prefs/navigation.rsindex c69a79b9ce..40895ccf6f 100644--- a/crates/ui-egui/src/prefs/navigation.rs+++ b/crates/ui-egui/src/prefs/navigation.rs@@ -123,6 +123,7 @@ pub(super) fn show(app: &mut SolveApp, ui: &mut egui::Ui, t: &Tokens) {.on_hover_text(if touchpad { crate::i18n::tr("nav.touchpad.scroll") } else { crate::i18n::tr("ui.trackpad_pan_tip") });});setting::radial_context_menu(app, ui);+ setting::direct_interaction(app, ui);});});ui_kit::divider(ui, "navigation_view_controls");diff --git a/crates/ui-egui/src/smart_extrude_ui_tests.rs b/crates/ui-egui/src/smart_extrude_ui_tests.rsindex f4beae9f2f..f1d451fac4 100644--- a/crates/ui-egui/src/smart_extrude_ui_tests.rs+++ b/crates/ui-egui/src/smart_extrude_ui_tests.rs@@ -151,3 +151,69 @@ fn press_pull_drags_a_hole_wall() {let v = volume(&h);assert!((v - (24000.0 - std::f64::consts::PI * 9.0 * 20.0)).abs() / v < 5e-4, "{v}");}++/// Pressing on a top face and pulling it up extrudes it by the distance dragged, without any+/// tool being started; letting go leaves the window open until OK.+#[test]+fn pulling_a_face_up_extrudes_it() {+ let mut h = Harness::new();+ h.app.preferences.direct_interaction = true;+ h.app.run("solid.box", json!({"length":20,"width":20,"height":10,"corner":[0,0,0]})).unwrap();+ h.step(&json!({"call":"ui.view","params":{"view":"home"}})).unwrap();+ h.step(&json!({"call":"ui.view","params":{"view":"fit"}})).unwrap();+ h.settle();+ h.step(&json!({"drag":[{"world":[10,10,10]},{"world":[10,10,20]}]})).unwrap();+ h.frames(4);+ assert!(h.app.dialog.is_some(), "the window stays open after the release");+ assert_eq!(state(&h), (0, JOIN, true), "pulled out: Join");+ h.step(&json!({"key":"Enter"})).unwrap();+ assert!(h.app.dialog.is_none(), "OK applied the extrude");+ let (lo, hi) = bounds_z(&h);+ assert!(lo.abs() < 1e-6 && hi > 10.5, "the box grew upward: z {lo}..{hi}");+}++/// Direct interaction is a setting, off until someone turns it on: a pull then does nothing.+#[test]+fn direct_interaction_is_off_until_the_setting_is_on() {+ let mut h = Harness::new();+ assert!(!h.app.preferences.direct_interaction);+ h.app.run("solid.box", json!({"length":20,"width":20,"height":10,"corner":[0,0,0]})).unwrap();+ h.step(&json!({"call":"ui.view","params":{"view":"home"}})).unwrap();+ h.step(&json!({"call":"ui.view","params":{"view":"fit"}})).unwrap();+ h.settle();+ let before = volume(&h);+ h.step(&json!({"drag":[{"world":[10,10,10]},{"world":[10,10,20]}]})).unwrap();+ h.frames(4);+ assert!(h.app.dialog.is_none(), "no extrude window");+ assert!((volume(&h) - before).abs() < 1e-6);+}++/// A press on a face that moves sideways is still a box selection: nothing is extruded.+#[test]+fn dragging_sideways_over_a_face_does_not_extrude() {+ let mut h = Harness::new();+ h.app.preferences.direct_interaction = true;+ h.app.run("solid.box", json!({"length":20,"width":20,"height":10,"corner":[0,0,0]})).unwrap();+ h.step(&json!({"call":"ui.view","params":{"view":"home"}})).unwrap();+ h.step(&json!({"call":"ui.view","params":{"view":"fit"}})).unwrap();+ h.settle();+ let before = volume(&h);+ h.step(&json!({"drag":[{"world":[10,10,10]},{"world":[18,10,10]}]})).unwrap();+ h.frames(4);+ assert!(h.app.dialog.is_none());+ assert!((volume(&h) - before).abs() < 1e-6);+}++/// Pulling a face down into the body cuts, and the window waits for OK as well.+#[test]+fn pulling_a_face_down_into_the_body_cuts() {+ let mut h = Harness::new();+ h.app.preferences.direct_interaction = true;+ h.app.run("solid.box", json!({"length":20,"width":20,"height":10,"corner":[0,0,0]})).unwrap();+ h.step(&json!({"call":"ui.view","params":{"view":"home"}})).unwrap();+ h.step(&json!({"call":"ui.view","params":{"view":"fit"}})).unwrap();+ h.settle();+ h.step(&json!({"drag":[{"world":[10,10,10]},{"world":[10,10,5]}]})).unwrap();+ h.frames(4);+ assert_eq!(state(&h).1, CUT, "pushed into the box: Cut");+}diff --git a/crates/ui-egui/src/viewport.rs b/crates/ui-egui/src/viewport.rsindex 4dd6d4c823..b2183e89f2 100644--- a/crates/ui-egui/src/viewport.rs+++ b/crates/ui-egui/src/viewport.rs@@ -159,6 +159,8 @@ pub struct ViewportState {pub(crate) joint_drag: Option<crate::joint_drag::Drag>,/// A free part being dragged to a new place (#2804).pub(crate) occurrence_drag: Option<crate::occurrence_drag::Drag>,+ /// Extrude by pulling a face or profile.+ pub(crate) drag_extrude: crate::drag_extrude::State,/// The plane the grid was last drawn on while Preferences ▸ Grid follows the view is on/// (#1470), kept so the choice is hysteretic near the thresholds (#1993).pub(crate) grid_plane: std::sync::Mutex<GridPlane>,@@ -3533,8 +3535,9 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {&& !crate::shell::covered(app, a)&& !axes.contains(a)&& !crate::occurrence_drag::start(app, &proj, a)+ && !crate::joint_drag::start(app, &proj, a){- crate::joint_drag::start(app, &proj, a);+ crate::drag_extrude::press(app, &proj, a);}if resp.dragged_by(egui::PointerButton::Primary)&& let Some(p) = hover@@ -3554,6 +3557,15 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {if crate::occurrence_drag::active(app) && (resp.drag_stopped() || !ui.input(|i| i.pointer.primary_down())) {crate::occurrence_drag::end(app);}+ // Pulling a face or profile extrudes it; the release applies.+ if resp.dragged_by(egui::PointerButton::Primary)+ && let Some(p) = hover+ {+ crate::drag_extrude::to(app, &proj, p);+ }+ if resp.drag_stopped() || !primary_down {+ crate::drag_extrude::end(app);+ }// Box selection: a primary drag on the model when no navigation mode is on.if app.tool.is_none()&& !held@@ -3562,6 +3574,7 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {&& app.viewport.point_drag.is_none()&& !crate::joint_drag::active(app)&& !crate::occurrence_drag::active(app)+ && !crate::drag_extrude::started(app)&& !crate::dim_view::dragging()&& !crate::sketch3d::dragging()&& !crate::sketch_text_handles::dragging(app)@@ -3599,6 +3612,8 @@ pub fn show(app: &mut SolveApp, ui: &mut egui::Ui) {crate::inference::show(app, &painter, &proj);crate::drag_snap::show(app, &painter, &proj);hover_highlight(app, &painter, &proj);+ crate::drag_extrude::tick(app);+ crate::drag_extrude::show(app, &painter);points_2d(app, &painter, &proj);if let Some((plane, sketch)) = crate::sketch_attachment_ui::lost_guide(app)&& let Some((low, high)) = sketch.bounds()