diff --git a/crates/ui-egui/src/dialogs.rs b/crates/ui-egui/src/dialogs.rs index 22a101559..6e778857c 100644 --- a/crates/ui-egui/src/dialogs.rs +++ b/crates/ui-egui/src/dialogs.rs @@ -116,6 +116,14 @@ pub enum Kind { }, /// Halfway between two planes or planar faces. Midplane, + /// A construction axis, point or plane made from picks alone (#2474): input `i` is the + /// command's parameter `keys[i]`, and `t` (along a path) when it takes one. + Construct { + command: &'static str, + title: &'static str, + keys: Vec<&'static str>, + t: Option, + }, /// A construction plane through an axis at an angle to a plane. AnglePlane { angle: String, @@ -529,6 +537,13 @@ impl Dialog { Kind::Midplane, vec![SelInput::new("First plane", PLANES | PLANAR_FACES, false), SelInput::new("Second plane", PLANES | PLANAR_FACES, false)], ), + id if construct_picks(id).is_some() => { + let (command, title, picks) = construct_picks(id)?; + let inputs = picks.iter().map(|(label, accept, _)| SelInput::new(label, *accept, false)).collect(); + let keys = picks.iter().map(|(_, _, key)| *key).collect(); + let t = (command == "construct.point.along_path").then(|| "0.5".to_string()); + Dialog::new(Kind::Construct { command, title, keys, t }, inputs) + } "construct.plane.along_path" => { Dialog::new(Kind::PathPlane { t: "0.5".into(), sketch: None }, vec![SelInput::new("Path", CURVES | EDGES, false)]) } @@ -805,6 +820,7 @@ impl Dialog { | Kind::Sweep { .. } | Kind::PathPlane { .. } | Kind::Midplane + | Kind::Construct { .. } | Kind::Move { .. } | Kind::Hole { .. } | Kind::Primitive { .. } @@ -1411,6 +1427,7 @@ fn title(k: &Kind) -> &'static str { Kind::Material { .. } => "PHYSICAL MATERIAL", Kind::OffsetPlane { .. } => "OFFSET PLANE", Kind::Midplane => "MIDPLANE", + Kind::Construct { title, .. } => title, Kind::PathPlane { .. } => "PLANE ALONG PATH", Kind::Rib { web: false, .. } => "RIB", Kind::Rib { web: true, .. } => "WEB", @@ -2728,7 +2745,12 @@ pub fn show(app: &mut SolveApp, ctx: &egui::Context) { enter |= field(ui, t); ui.end_row(); } - Kind::Split | Kind::ReplaceFace | Kind::Remove | Kind::Midplane => {} + Kind::Split | Kind::ReplaceFace | Kind::Remove | Kind::Midplane | Kind::Construct { t: None, .. } => {} + Kind::Construct { t: Some(t), .. } => { + row_label(ui, "Position (0 to 1)"); + enter |= field(ui, t); + ui.end_row(); + } Kind::Rib { thickness, depth, flip, normal, .. } => { row_label(ui, "Direction"); let mut i = usize::from(*normal); @@ -4013,6 +4035,18 @@ fn dialog_commands(app: &SolveApp, d: &Dialog) -> Result, S let b = plane_arg(app, sels(d, 1).first())?; ("construct.plane.midplane", json!({"a": a, "b": b})) } + Kind::Construct { command, keys, t, .. } => { + let mut p = serde_json::Map::new(); + for (i, key) in keys.iter().enumerate() { + let input = d.inputs.get(i).map_or("", |inp| inp.label); + let sel = sels(d, i).first().ok_or_else(|| format!("pick the {}", input.to_lowercase()))?; + p.insert((*key).to_string(), construct_ref(app, sel)?); + } + if let Some(t) = t { + p.insert("t".into(), json!(t)); + } + (*command, Value::Object(p)) + } Kind::PathPlane { t, sketch } => { need(0, "a path")?; let path = match sels(d, 0).first() { @@ -4610,6 +4644,87 @@ fn emboss_face_sel(s: &Session, component: u64, face: &solidcraft_engine::doc::E Some(Sel::Face { body: world_name, index, point }) } +/// The construct tools that take only picks (#2474, #2494): their command, dialog title and +/// inputs as (label, what it accepts, parameter). Offset Plane, Midplane, Plane Along Path and +/// Plane At Angle have dialogs of their own. +fn construct_picks(id: &str) -> Option<(&'static str, &'static str, &'static [(&'static str, Accept, &'static str)])> { + const PLANE: Accept = PLANES | PLANAR_FACES; + const POINT: Accept = selection::VERTICES | selection::POINTS; + const LINE: Accept = EDGES | AXES; + const CURVE: Accept = EDGES | CURVES; + Some(match id { + "construct.axis.through_cylinder" => ("construct.axis.through_cylinder", "AXIS THROUGH CYLINDER", &[("Face", FACES | EDGES, "face")]), + "construct.axis.normal_to_face" => { + ("construct.axis.normal_to_face", "AXIS PERPENDICULAR TO FACE", &[("Face", FACES, "face"), ("Point", POINT, "point")]) + } + "construct.axis.two_planes" => { + ("construct.axis.two_planes", "AXIS THROUGH TWO PLANES", &[("First plane", PLANE, "a"), ("Second plane", PLANE, "b")]) + } + "construct.axis.two_points" => { + ("construct.axis.two_points", "AXIS THROUGH TWO POINTS", &[("First point", POINT, "a"), ("Second point", POINT, "b")]) + } + "construct.axis.edge" => ("construct.axis.edge", "AXIS THROUGH EDGE", &[("Edge", LINE, "edge")]), + "construct.point.vertex" => ("construct.point.vertex", "POINT AT VERTEX", &[("Point", POINT, "point")]), + "construct.point.two_edges" => { + ("construct.point.two_edges", "POINT THROUGH TWO EDGES", &[("First edge", CURVE, "a"), ("Second edge", CURVE, "b")]) + } + "construct.point.three_planes" => ( + "construct.point.three_planes", + "POINT THROUGH THREE PLANES", + &[("First plane", PLANE, "a"), ("Second plane", PLANE, "b"), ("Third plane", PLANE, "c")], + ), + "construct.point.center" => ("construct.point.center", "POINT AT CENTER", &[("Circle or sphere", FACES | CURVE, "of")]), + "construct.point.edge_plane" => { + ("construct.point.edge_plane", "POINT AT EDGE AND PLANE", &[("Edge", CURVE, "edge"), ("Plane", PLANE, "plane")]) + } + "construct.point.along_path" => ("construct.point.along_path", "POINT ALONG PATH", &[("Path", CURVE, "path")]), + "construct.plane.two_edges" => { + ("construct.plane.two_edges", "PLANE THROUGH TWO EDGES", &[("First edge", LINE, "a"), ("Second edge", LINE, "b")]) + } + "construct.plane.three_points" => ( + "construct.plane.three_points", + "PLANE THROUGH THREE POINTS", + &[("First point", POINT, "a"), ("Second point", POINT, "b"), ("Third point", POINT, "c")], + ), + "construct.plane.tangent" => ("construct.plane.tangent", "TANGENT PLANE", &[("Face", FACES, "face")]), + "construct.plane.perpendicular" => { + ("construct.plane.perpendicular", "PERPENDICULAR PLANE", &[("Plane", PLANE, "plane"), ("Line", LINE, "line")]) + } + _ => return None, + }) +} + +/// A dialog pick as a construction reference (`construct.*`'s `{face|edge|vertex: [x,y,z]}`, +/// `{plane}`, `{axis}` or `{sketch, point|curve}`). +fn construct_ref(app: &SolveApp, sel: &Sel) -> Result { + Ok(match sel { + Sel::Face { point, .. } => json!({"face": pt(*point)}), + Sel::Edge { point, .. } => json!({"edge": pt(*point)}), + Sel::Vertex { point, .. } => json!({"vertex": pt(*point)}), + Sel::Plane { name } => json!({"plane": name}), + Sel::Axis { name } => json!({"axis": name}), + // Picked across sketches as ":". + Sel::SketchPoint { id } => match id.split_once(':').and_then(|(sk, p)| Some((sk.parse::().ok()?, p))) { + Some((sketch, point)) => json!({"sketch": sketch, "point": point}), + None => return Err("pick the point in the viewport".into()), + }, + Sel::SketchCurve { id } => { + let st = app.session.world_state(); + let sketches: Vec = st + .sketches + .iter() + .filter(|ss| ss.sketch.curve_index(id).is_some() || ss.sketch.wire_index(id).is_some()) + .map(|ss| ss.feature) + .collect(); + match sketches.as_slice() { + [sketch] => json!({"sketch": sketch, "curve": id}), + _ => return Err("pick a model edge, or a sketch curve in only one sketch".into()), + } + } + _ => return Err("this pick can't be used here".into()), + }) +} + /// A picked plane or planar face as `construct.plane.*` takes it. fn plane_arg(app: &SolveApp, sel: Option<&Sel>) -> Result { match sel { diff --git a/crates/ui-egui/src/preselect_tests.rs b/crates/ui-egui/src/preselect_tests.rs index ddb870660..c7270f2e5 100644 --- a/crates/ui-egui/src/preselect_tests.rs +++ b/crates/ui-egui/src/preselect_tests.rs @@ -1717,3 +1717,74 @@ fn midplane_asks_for_two_planes_then_builds_the_one_between_them() { assert!(pl.normal().cross(Vec3::Z).len() < 1e-6, "{pl:?}"); assert!((pl.origin.z - 10.0).abs() < 1e-6, "{pl:?}"); } + +/// #2474, #2494: every construct tool asks for what it needs. Started from the toolbar with +/// nothing selected, none of them runs its command and reports that `a` (or `face`, `edge`…) +/// is required; a construct command added later without a dialog fails here too. +#[test] +fn every_construct_tool_opens_a_dialog_instead_of_failing() { + let mut h = crate::scenario::Harness::new(); + // A body, so the tools that need one are enabled. + h.app.run("solid.box", json!({"length":20,"width":20,"height":20})).unwrap(); + let commands = h.app.session.execute("engine.commands", &json!({})).unwrap(); + let ids: Vec = + commands.as_array().unwrap().iter().filter_map(|c| c["id"].as_str()).filter(|id| id.starts_with("construct.")).map(String::from).collect(); + assert!(ids.len() >= 17, "{ids:?}"); + for id in &ids { + h.app.session.selection.clear(); + h.app.status = None; + h.app.start(id); + h.frames(1); + let status = h.app.status.as_ref().map(|s| s.0.clone()).unwrap_or_default(); + assert!(!status.contains("is required"), "{id}: {status}"); + assert!(h.app.dialog.as_ref().is_some_and(|d| !d.inputs.is_empty()), "{id} opens a dialog that asks for picks"); + crate::dialogs::cancel(&mut h.app); + } +} + +/// #2494: two planes selected first, Axis Through Two Planes takes both and makes the axis +/// where they meet. +#[test] +fn axis_through_two_preselected_planes_builds_their_intersection() { + let mut h = crate::scenario::Harness::new(); + h.app.session.selection = vec![Sel::Plane { name: "XY".into() }, Sel::Plane { name: "XZ".into() }]; + h.app.start("construct.axis.two_planes"); + h.frames(1); + let d = h.app.dialog.as_ref().unwrap(); + assert!(matches!(d.kind, Kind::Construct { .. })); + assert_eq!((d.inputs[0].items.len(), d.inputs[1].items.len()), (1, 1), "each plane fills one input"); + h.step(&json!({"key":"Enter"})).unwrap(); + assert!(h.app.dialog.is_none(), "{:?}", h.app.dialog.as_ref().and_then(|d| d.error.clone())); + let axes = solidcraft_engine::view::construction_axes(&h.app.session); + let (_, _, _, dir) = axes.first().expect("the axis"); + assert!(dir.cross(Vec3::X).len() < 1e-6, "XY and XZ meet along X: {dir:?}"); +} + +/// #2474: Plane Through Two Edges takes two edges picked in the dialog. +#[test] +fn plane_through_two_picked_edges() { + let mut h = crate::scenario::Harness::new(); + h.app.run("solid.box", json!({"length":20,"width":20,"height":20})).unwrap(); + h.app.start("construct.plane.two_edges"); + h.frames(1); + let edge = |p: Vec3| Sel::Edge { body: "Body1".into(), index: 0, point: p }; + let d = h.app.dialog.as_mut().unwrap(); + d.inputs[0].items = vec![edge(Vec3::new(10.0, 0.0, 20.0))]; + d.inputs[1].items = vec![edge(Vec3::new(0.0, 10.0, 20.0))]; + h.step(&json!({"key":"Enter"})).unwrap(); + assert!(h.app.dialog.is_none(), "{:?}", h.app.dialog.as_ref().and_then(|d| d.error.clone())); + let (_, _, pl) = solidcraft_engine::view::construction_planes(&h.app.session).into_iter().next().expect("the plane"); + assert!(pl.normal().cross(Vec3::Z).len() < 1e-6 && (pl.origin.z - 20.0).abs() < 1e-6, "the top face's plane: {pl:?}"); +} + +/// Point Along Path asks for its position too, and a missing pick names the input. +#[test] +fn point_along_path_has_its_position_and_missing_picks_are_named() { + let mut h = crate::scenario::Harness::new(); + h.app.start("construct.point.along_path"); + h.frames(1); + assert!(matches!(&h.app.dialog.as_ref().unwrap().kind, Kind::Construct { t: Some(t), .. } if t == "0.5")); + h.step(&json!({"key":"Enter"})).unwrap(); + let e = h.app.dialog.as_ref().and_then(|d| d.error.clone()).unwrap_or_default(); + assert!(e.contains("path"), "{e}"); +}