From 35601da3f0b6c3a0dab00df1b9e03445535e066b Mon Sep 17 00:00:00 2001 From: Dahak Builds Date: Thu, 29 Oct 2026 15:10:41 +0000 Subject: [PATCH] 3D sketch: 3D Line, Spline and Point buttons start tools (#8040) (#8041) * claim: #8040 * wip: 3D line/spline/point tools (#8040) * Take sketch.move3d off the toolbar help backlog now that it is agent-only (#8040) --------- Co-authored-by: Dahak Builds --- crates/engine/src/cmd/sketch_3d.rs | 12 +-- .../engine/src/cmd/sketch_3d_frame_tests.rs | 22 +++++ crates/ui-egui/src/command_help.rs | 1 - crates/ui-egui/src/sketch3d.rs | 28 ++++++- crates/ui-egui/src/sketch3d_frame_ui_tests.rs | 80 +++++++++++++++++++ crates/ui-egui/src/tools.rs | 6 ++ crates/ui-egui/src/workspace.rs | 17 +++- 7 files changed, 153 insertions(+), 13 deletions(-) diff --git a/crates/engine/src/cmd/sketch_3d.rs b/crates/engine/src/cmd/sketch_3d.rs index 938ecfedc..de10b0603 100644 --- a/crates/engine/src/cmd/sketch_3d.rs +++ b/crates/engine/src/cmd/sketch_3d.rs @@ -14,13 +14,13 @@ use crate::params::{bad, req_vec3, vec3}; use crate::{Result, Session}; pub static COMMANDS: &[CommandSpec] = &[ - CommandSpec::new("sketch.line3d", "3D Line", line3d).icon("line").enabled(in_sketch).params(concat!( + CommandSpec::new("sketch.line3d", "3D Line", line3d).icon("line").enabled(in_sketch_3d).params(concat!( "points: [[x,y,z]…] world points (2 or more): one 3D line per segment, joined end to end (needs 3D Sketch on)", created_doc!() )), CommandSpec::new("sketch.spline3d", "3D Spline", spline3d) .icon("spline") - .enabled(in_sketch) + .enabled(in_sketch_3d) .params(concat!("points: [[x,y,z]…] world fit points (2 or more): a smooth 3D curve through them (needs 3D Sketch on)", created_doc!())), CommandSpec::new("sketch.arc3d", "3D Arc", arc3d) .icon("arc3") @@ -31,7 +31,7 @@ pub static COMMANDS: &[CommandSpec] = &[ )), CommandSpec::new("sketch.point3d", "3D Point", point3d) .icon("point") - .enabled(in_sketch) + .enabled(in_sketch_3d) .params(concat!("point: [x,y,z] world point (needs 3D Sketch on)", created_doc!())), CommandSpec::new("sketch.move3d", "Move 3D Point", move3d) .icon("sketch_move") @@ -51,13 +51,13 @@ fn needs_3d(sk: &solidcraft_sketch::Sketch, cmd: &str) -> Result<()> { if sk.view.three_d { Ok(()) } else { Err(bad(cmd, "turn on 3D Sketch (sketch.options sketch_3d: true) to draw off the sketch plane")) } } -/// The 3D Arc button starts an interactive tool, so it is only offered while a 3D sketch is -/// being edited; otherwise the tool could only fail on its third click. +/// The 3D Line, Spline, Arc and Point buttons start interactive tools, so they are only offered +/// while a 3D sketch is being edited; otherwise the tools could only fail on their first click. fn in_sketch_3d(s: &Session) -> std::result::Result<(), String> { let id = sketch_store::active(s).ok_or("no sketch is being edited (Create Sketch or Edit Sketch first)")?; match sketch_store::read(&s.doc, id) { Ok(sk) if sk.view.three_d => Ok(()), - Ok(_) => Err("turn on 3D Sketch (sketch.options sketch_3d: true) to draw 3D arcs".into()), + Ok(_) => Err("turn on 3D Sketch (sketch.options sketch_3d: true) to draw 3D curves".into()), Err(_) => Err("no sketch is being edited (Create Sketch or Edit Sketch first)".into()), } } diff --git a/crates/engine/src/cmd/sketch_3d_frame_tests.rs b/crates/engine/src/cmd/sketch_3d_frame_tests.rs index 34ddee7b1..a46cbad77 100644 --- a/crates/engine/src/cmd/sketch_3d_frame_tests.rs +++ b/crates/engine/src/cmd/sketch_3d_frame_tests.rs @@ -417,3 +417,25 @@ fn arc3d_pipes_along_the_arc() { let quarter = 20.0 * std::f64::consts::FRAC_PI_2; assert!((measure.volume - 4.0 * std::f64::consts::PI * quarter).abs() < 0.5, "{}", measure.volume); } + +#[test] +fn line3d_spline3d_point3d_need_a_3d_sketch() { + // Same rule as sketch.arc3d: these buttons start interactive tools, so they are only + // offered while a 3D sketch is being edited. + let ids = ["sketch.line3d", "sketch.spline3d", "sketch.point3d"]; + let mut s = Session::default(); + for id in ids { + let info = crate::cmd::find_command(id).unwrap().info(&s); + assert!(!info.enabled, "{id} offered with no sketch being edited"); + } + run(&mut s, "sketch.create", json!({"plane": "XY"})); + for id in ids { + let info = crate::cmd::find_command(id).unwrap().info(&s); + assert!(!info.enabled, "{id} offered in a plain 2D sketch: {:?}", info.disabled_reason); + } + run(&mut s, "sketch.options", json!({"sketch_3d": true})); + for id in ids { + let info = crate::cmd::find_command(id).unwrap().info(&s); + assert!(info.enabled, "{id} not offered inside a 3D sketch: {:?}", info.disabled_reason); + } +} diff --git a/crates/ui-egui/src/command_help.rs b/crates/ui-egui/src/command_help.rs index 8ab813bc8..ec1d5fd0c 100644 --- a/crates/ui-egui/src/command_help.rs +++ b/crates/ui-egui/src/command_help.rs @@ -265,7 +265,6 @@ mod tests { "sketch.line3d", "sketch.mirror", "sketch.move", - "sketch.move3d", "sketch.offset", "sketch.paste", "sketch.pattern.circular", diff --git a/crates/ui-egui/src/sketch3d.rs b/crates/ui-egui/src/sketch3d.rs index 76ddb2971..c966b3bd2 100644 --- a/crates/ui-egui/src/sketch3d.rs +++ b/crates/ui-egui/src/sketch3d.rs @@ -1,7 +1,8 @@ //! 3D sketching in the viewport (the Sketch Palette's 3D Sketch option): the Line, Arc, Spline //! and Point tools take points off the sketch plane by snapping to model vertices, edges and //! faces and to points already drawn in 3D; segments that stay on the plane are ordinary sketch -//! lines and arcs. The 3D Arc command has its own tool: every point it takes is a world point. +//! lines and arcs. The 3D Line, Arc, Spline and Point commands have their own tools: every point +//! they take is a world point, with no on-plane downgrade. //! A drawn 3D point (or curve end) clicked without a tool gets a move triad: //! drag an arrow to move along X, Y or Z, or the square to move parallel to the sketch plane. //! Everything runs `sketch.line3d`, `sketch.arc3d`, `sketch.spline3d`, `sketch.point3d`, @@ -39,7 +40,17 @@ pub fn active(app: &SolveApp) -> bool { } fn takes(cmd: &str) -> bool { - matches!(cmd, "sketch.line" | "sketch.arc.three_point" | "sketch.arc3d" | "sketch.spline.fit_point" | "sketch.point") + matches!( + cmd, + "sketch.line" + | "sketch.arc.three_point" + | "sketch.arc3d" + | "sketch.spline.fit_point" + | "sketch.point" + | "sketch.line3d" + | "sketch.spline3d" + | "sketch.point3d" + ) } /// Only 3D point-placing tools broaden sketch-point picking; ordinary selection stays local. @@ -140,6 +151,11 @@ pub fn on_click(app: &mut SolveApp, tool: &mut Tool, proj: &Proj, pos: Pos2) -> }; report(app, r); } + "sketch.point3d" => { + // The dedicated 3D Point tool: every click is a world point, and the tool stays. + let r = app.run("sketch.point3d", json!({"point": [p.x, p.y, p.z]})); + report(app, r); + } "sketch.line" => { if let Some(a) = chain().last().copied() { let r = match (on_plane(app, a), on_plane(app, p)) { @@ -206,6 +222,12 @@ pub fn finish(app: &mut SolveApp) -> bool { }; report(app, r); } + // The dedicated 3D tools keep every point as a world point: no on-plane downgrade. + if matches!(cmd, "sketch.line3d" | "sketch.spline3d") && pts.len() >= 2 { + let q: Vec = pts.iter().map(|p| json!([p.x, p.y, p.z])).collect(); + let r = app.run(cmd, json!({ "points": q })); + report(app, r); + } set_chain(Some(cmd), Vec::new()); true } @@ -256,7 +278,7 @@ pub fn preview(app: &SolveApp, t: &Tool, painter: &egui::Painter, proj: &Proj) - }; let screen: Vec = poly.into_iter().filter_map(|p| proj.to_screen(p)).collect(); painter.add(Shape::line(screen, stroke)); - } else if t.cmd == "sketch.spline.fit_point" && all.len() >= 2 { + } else if matches!(t.cmd, "sketch.spline.fit_point" | "sketch.spline3d") && all.len() >= 2 { let mut w: Vec = pts.clone(); if let Some((h, _)) = hover { w.push(h); diff --git a/crates/ui-egui/src/sketch3d_frame_ui_tests.rs b/crates/ui-egui/src/sketch3d_frame_ui_tests.rs index 6a3291e77..e539fb95a 100644 --- a/crates/ui-egui/src/sketch3d_frame_ui_tests.rs +++ b/crates/ui-egui/src/sketch3d_frame_ui_tests.rs @@ -260,3 +260,83 @@ fn arc3d_button_starts_its_own_tool() { h.app.start("sketch.arc3d"); assert!(h.app.tool.is_none(), "3D Arc is disabled while 3D Sketch is off"); } + +/// Shared setup: a box whose top-face vertices sit off the active 3D sketch's XY plane. +fn box_and_3d_sketch(name: &str) -> (crate::scenario::Harness, u64) { + let mut h = crate::scenario::Harness::new(); + h.app.run("solid.box", json!({"corner":[10,0,5],"length":10,"width":10,"height":5,"body_name":name})).unwrap(); + let sketch = h.app.run("sketch.create", json!({"plane":"XY","name":name})).unwrap()["sketch"].as_u64().unwrap(); + h.app.run("sketch.options", json!({"sketch_3d":true})).unwrap(); + h.app.session.visibility.origin_hidden = Some(true); + h.call("ui.view", json!({"view":"home"})); + h.call("ui.view", json!({"view":"fit"})); + h.frames(3); + (h, sketch) +} + +/// Three top-face box vertices: off the XY sketch plane, so the 3D tools keep them as world points. +const OFF_PLANE: [[f64; 3]; 3] = [[10.0, 0.0, 10.0], [20.0, 10.0, 10.0], [10.0, 10.0, 10.0]]; + +#[test] +fn line3d_tool_draws_polyline_off_plane() { + let (mut h, sketch) = box_and_3d_sketch("Line3dPicks"); + h.app.start("sketch.line3d"); + assert!(h.app.tool.as_ref().is_some_and(|t| t.cmd == "sketch.line3d"), "the 3D Line button starts a tool: {:?}", h.app.status); + h.frames(2); + h.app.status = None; + for p in OFF_PLANE { + let [x, y] = h.at(&json!({"world": p})).unwrap(); + let proj = crate::viewport::projection(&h.app, h.app.viewport.rect.unwrap()); + let picked = crate::sketch3d::point_at(&h.app, &proj, egui::pos2(x as f32, y as f32)); + assert!(picked.is_some_and(|(q, _)| (q - Vec3::new(p[0], p[1], p[2])).dist(Vec3::ZERO) < 1e-5), "p={p:?}, picked={picked:?}"); + h.step(&json!({"click": {"world": p}})).unwrap(); + } + h.step(&json!({"key": "Escape"})).unwrap(); + let state = h.app.session.world_state(); + let wires = &state.sketch(sketch).unwrap().sketch.wires; + assert_eq!(wires.len(), 2, "three off-plane clicks finish as two joined 3D segments"); + let expected = [[Vec3::new(10.0, 0.0, 10.0), Vec3::new(20.0, 10.0, 10.0)], [Vec3::new(20.0, 10.0, 10.0), Vec3::new(10.0, 10.0, 10.0)]]; + for (w, pair) in wires.iter().zip(expected) { + assert!(w.fit[0].dist(pair[0]) < 1e-6 && w.fit[1].dist(pair[1]) < 1e-6, "{:?}", w.fit); + } +} + +#[test] +fn spline3d_tool_fit_points_off_plane() { + let (mut h, sketch) = box_and_3d_sketch("Spline3dPicks"); + h.app.start("sketch.spline3d"); + assert!(h.app.tool.as_ref().is_some_and(|t| t.cmd == "sketch.spline3d"), "the 3D Spline button starts a tool: {:?}", h.app.status); + h.frames(2); + h.app.status = None; + for p in OFF_PLANE { + h.step(&json!({"click": {"world": p}})).unwrap(); + } + h.step(&json!({"key": "Escape"})).unwrap(); + let state = h.app.session.world_state(); + let wires = &state.sketch(sketch).unwrap().sketch.wires; + assert_eq!(wires.len(), 1, "three off-plane clicks finish as one 3D spline"); + let w = &wires[0]; + assert_eq!(w.fit.len(), 3, "{:?}", w.fit); + for (fit, p) in w.fit.iter().zip(OFF_PLANE) { + assert!(fit.dist(Vec3::new(p[0], p[1], p[2])) < 1e-6, "{:?}", w.fit); + } +} + +#[test] +fn point3d_tool_places_points_and_stays() { + let (mut h, sketch) = box_and_3d_sketch("Point3dPicks"); + h.app.start("sketch.point3d"); + assert!(h.app.tool.as_ref().is_some_and(|t| t.cmd == "sketch.point3d"), "the 3D Point button starts a tool: {:?}", h.app.status); + h.frames(2); + h.app.status = None; + for p in [OFF_PLANE[0], OFF_PLANE[1]] { + h.step(&json!({"click": {"world": p}})).unwrap(); + } + let state = h.app.session.world_state(); + let wires = &state.sketch(sketch).unwrap().sketch.wires; + assert_eq!(wires.len(), 2, "two clicks place two drawn 3D points"); + assert!(wires.iter().all(|w| w.fit.len() == 1), "{:?}", wires.iter().map(|w| &w.fit).collect::>()); + assert!(wires[0].fit[0].dist(Vec3::new(10.0, 0.0, 10.0)) < 1e-6, "{:?}", wires[0].fit); + assert!(wires[1].fit[0].dist(Vec3::new(20.0, 10.0, 10.0)) < 1e-6, "{:?}", wires[1].fit); + assert!(h.app.tool.as_ref().is_some_and(|t| t.cmd == "sketch.point3d"), "the tool stays for the next point"); +} diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rs index 930703d3a..2780e35a8 100644 --- a/crates/ui-egui/src/tools.rs +++ b/crates/ui-egui/src/tools.rs @@ -109,6 +109,9 @@ impl Tool { "sketch.circle.three_point" => ("sketch.circle.three_point", Kind::Draw(3)), "sketch.arc.three_point" => ("sketch.arc.three_point", Kind::Draw(3)), "sketch.arc3d" => ("sketch.arc3d", Kind::Draw(3)), + "sketch.line3d" => ("sketch.line3d", Kind::Draw(0)), + "sketch.spline3d" => ("sketch.spline3d", Kind::Draw(0)), + "sketch.point3d" => ("sketch.point3d", Kind::Draw(1)), "sketch.arc.center_point" => ("sketch.arc.center_point", Kind::Draw(3)), "sketch.polygon.inscribed" => ("sketch.polygon.inscribed", Kind::Draw(2)), "sketch.polygon.circumscribed" => ("sketch.polygon.circumscribed", Kind::Draw(2)), @@ -156,6 +159,9 @@ pub fn hint(id: &str) -> String { "sketch.circle.center" => "Circle: click the centre, then a point on the circle".into(), "sketch.arc.three_point" => "Arc: click start, end, then a point on the arc".into(), "sketch.arc3d" => "3D Arc: click start, end, then a point on the arc (points may leave the sketch plane)".into(), + "sketch.line3d" => "3D Line: click world points; right-click or Esc to finish (points may leave the sketch plane)".into(), + "sketch.spline3d" => "3D Spline: click fit points in 3D; right-click or Esc to finish".into(), + "sketch.point3d" => "3D Point: click where a 3D point goes".into(), "sketch.arc.center_point" => "Arc: click centre and start, go round either way and click the end, or type a signed sweep".into(), "sketch.dimension" => { "Dimension: pick one or two entities and type the value (or click to place it first); click a dimension to edit it; Enter applies".into() diff --git a/crates/ui-egui/src/workspace.rs b/crates/ui-egui/src/workspace.rs index ab34263cb..654800bef 100644 --- a/crates/ui-egui/src/workspace.rs +++ b/crates/ui-egui/src/workspace.rs @@ -94,15 +94,16 @@ pub struct Section { const CONFIGURE: &[&str] = &["parameters.change", "config.table", "config.configure"]; /// Commands that stay for agents, MCP and scripts but are not offered to people: the sketch solves -/// itself after every edit, so a Solve button (and the search hit for it) only confuses. -pub const AGENT_ONLY: &[&str] = &["sketch.solve"]; +/// itself after every edit, so a Solve button (and the search hit for it) only confuses; drawn 3D +/// points are moved with the triad that appears when one is clicked, not a Move button. +pub const AGENT_ONLY: &[&str] = &["sketch.solve", "sketch.move3d"]; pub const SECTIONS: &[Section] = &[ Section { tab: "MODEL", panel: "SKETCH", promoted: 1, - ids: &["sketch.create", "sketch.edit", "sketch.line3d", "sketch.arc3d", "sketch.spline3d", "sketch.point3d", "sketch.move3d"], + ids: &["sketch.create", "sketch.edit", "sketch.line3d", "sketch.arc3d", "sketch.spline3d", "sketch.point3d"], }, Section { tab: "MODEL", panel: "CREATE", promoted: 3, ids: &["solid.extrude", "solid.revolve", "solid.hole"] }, Section { @@ -1441,4 +1442,14 @@ mod tests { assert!(h.app.session.active_sketch.is_some()); assert_eq!(h.app.ui.tab, "SKETCH"); } + + /// Move 3D Point stays for agents, MCP and scripts; people move a drawn 3D point with the + /// triad that appears when it is clicked, not a ribbon button. + #[test] + fn move3d_is_agent_only() { + assert!(AGENT_ONLY.contains(&"sketch.move3d"), "sketch.move3d must stay available to agents"); + for section in SECTIONS.iter().filter(|s| s.panel == "SKETCH") { + assert!(!section.ids.contains(&"sketch.move3d"), "{}/{} must not offer sketch.move3d on the ribbon", section.tab, section.panel); + } + } }