diff --git a/assets/locales/pending/spline-insert-point.json b/assets/locales/pending/spline-insert-point.json new file mode 100644 index 000000000..26f4180b8 --- /dev/null +++ b/assets/locales/pending/spline-insert-point.json @@ -0,0 +1,23 @@ +{ + "en": { + "cmd.sketch_spline_insert_point": "Add Spline Point" + }, + "ar": { + "cmd.sketch_spline_insert_point": "إضافة نقطة إلى المنحنى الشريحي" + }, + "de": { + "cmd.sketch_spline_insert_point": "Splinepunkt hinzufügen" + }, + "he": { + "cmd.sketch_spline_insert_point": "הוספת נקודה לספליין" + }, + "ja": { + "cmd.sketch_spline_insert_point": "スプライン点を追加" + }, + "vi": { + "cmd.sketch_spline_insert_point": "Thêm điểm vào đường cong Spline" + }, + "zh": { + "cmd.sketch_spline_insert_point": "添加样条曲线点" + } +} diff --git a/crates/engine/src/cmd/sketch_create.rs b/crates/engine/src/cmd/sketch_create.rs index dfa0d9328..8c1cebce2 100644 --- a/crates/engine/src/cmd/sketch_create.rs +++ b/crates/engine/src/cmd/sketch_create.rs @@ -79,6 +79,10 @@ pub static COMMANDS: &[CommandSpec] = &[ .icon("spline_cv") .enabled(in_sketch) .params(concat!("points: control points (2…500), degree 5", created_doc!())), + CommandSpec::new("sketch.spline.insert_point", "Add Spline Point", spline_insert_point) + .icon("spline") + .enabled(in_sketch) + .params("curve: fit point spline id, at: [x,y]: a fit point is inserted where the curve passes nearest to it, between its neighbours (the curve still runs through it and all its old points; constraints on them stay); end?: start|end: `at` becomes the spline's new first or last point instead, and the curve runs on to it (refused when that end is joined to other geometry). Control point splines take no new points yet. Returns point: the new point's id"), CommandSpec::new("sketch.conic", "Conic Curve", conic) .at("SKETCH", "CREATE") .icon("conic") @@ -678,6 +682,28 @@ fn spline_cv5(s: &mut Session, p: &Value) -> Result { spline(s, p, "sketch.spline.control_point_5", true, 5) } +fn spline_insert_point(s: &mut Session, p: &Value) -> Result { + let cmd = "sketch.spline.insert_point"; + let at = req_vec2(cmd, p, "at")?; + let end = match p.get("end") { + None | Some(Value::Null) => None, + Some(Value::String(e)) if e == "start" => Some(true), + Some(Value::String(e)) if e == "end" => Some(false), + Some(_) => return Err(bad(cmd, "`end` must be start or end")), + }; + let (point, info) = edit(s, p, cmd, false, |sk, _| { + let id = str_(p, "curve").ok_or_else(|| bad(cmd, "`curve` must be a spline id"))?; + let c = sk.curve_index(id).ok_or_else(|| bad(cmd, format!("unknown curve `{id}`")))?; + let new = match end { + None => sk.insert_spline_point(c, at), + Some(at_start) => sk.extend_spline(c, at, at_start), + } + .map_err(|e| bad(cmd, e.to_string()))?; + sk.points.get(new).map(|q| q.id.clone()).ok_or_else(|| bad(cmd, "the new point is missing")) + })?; + Ok(json!({"point": point, "curve": p.get("curve"), "sketch": info})) +} + fn conic(s: &mut Session, p: &Value) -> Result { let cmd = "sketch.conic"; let rho = num(p, "rho").unwrap_or(0.5); diff --git a/crates/engine/src/cmd/sketch_modify_tests.rs b/crates/engine/src/cmd/sketch_modify_tests.rs index a3e27e654..534ccfba0 100644 --- a/crates/engine/src/cmd/sketch_modify_tests.rs +++ b/crates/engine/src/cmd/sketch_modify_tests.rs @@ -1085,6 +1085,63 @@ fn ellipse_splines_conic() { assert_eq!(ids(&r["curves"]).len(), 2); } +/// #6533: Add Spline Point puts a fit point on the curve at the spot, between its neighbours, as +/// one undo step. A dimension between two old fit points and a line joined to the spline's end +/// stay, and the curve still runs through every old point. +#[test] +fn a_point_added_to_a_spline_keeps_its_constraints_and_undoes() { + let mut s = new_sketch(); + let sid = + ids(&run(&mut s, "sketch.spline.fit_point", json!({"points": [[0, 0], [10, 8], [20, 0], [30, 6]], "connect": false}))["curves"])[0].clone(); + run(&mut s, "sketch.line", json!({"points": [format!("{sid}.end"), [40, 6]], "connect": false})); + let sk = sketch(&s); + let si = sk.curve_index(&sid).unwrap(); + let old = sk.curves[si].kind.point_ids(); + let names: Vec = old.iter().map(|i| sk.points[*i].id.clone()).collect(); + run(&mut s, "sketch.dimension", json!({"entities": [names[0], names[1]], "value": 12.8})); + let before = sketch(&s); + let fit: Vec = old.iter().map(|i| before.point(*i).unwrap()).collect(); + let curve = before.exact_segs(si).unwrap(); + let off = + |segs: &[solidcraft_geom::Seg2], q: Vec2| segs.iter().map(|seg| seg.point_at(seg.closest_param(q)).dist(q)).fold(f64::INFINITY, f64::min); + + let out = run(&mut s, "sketch.spline.insert_point", json!({"curve": sid, "at": [15, 6]})); + let after = sketch(&s); + let new = after.point_index(out["point"].as_str().unwrap()).unwrap(); + let now = after.curves[si].kind.point_ids(); + assert_eq!(now.len(), old.len() + 1); + assert_eq!(now[2], new, "between the second and third fit points: {now:?}"); + let q = after.point(new).unwrap(); + assert!(off(&curve, q) < 1e-9, "the new point is on the curve as it was: {q:?}"); + let segs = after.exact_segs(si).unwrap(); + for (i, p) in old.iter().zip(&fit) { + assert!(after.point(*i).unwrap().dist(*p) < 1e-9, "fit point {i} stays"); + assert!(off(&segs, *p) < 1e-9, "the curve still runs through {p:?}"); + } + assert_eq!(after.constraints.len(), before.constraints.len(), "the dimension and the end join stay"); + let d = after.constraints.iter().find(|c| c.kind.value().is_some()).unwrap(); + let (mut on, _) = d.kind.indices(); + on.sort_unstable(); + assert_eq!(on, vec![old[0], old[1]], "the dimension still measures the first two fit points: {:?}", d.kind); + assert!(after.curves.iter().any(|c| matches!(c.kind, CurveKind::Line { a, .. } if a == old[3])), "the line still starts at the spline's end"); + + run(&mut s, "edit.undo", json!({})); + assert_eq!(sketch(&s), before, "undo restores the spline"); + // A control point spline, a line, and ends joined to a line or a dimension are refused. + let cv = + ids(&run(&mut s, "sketch.spline.control_point", json!({"points": [[0, 20], [10, 30], [20, 20]], "connect": false}))["curves"])[0].clone(); + assert!(s.execute("sketch.spline.insert_point", &json!({"curve": cv, "at": [10, 24]})).is_err()); + let line = before.curves.iter().find(|c| matches!(c.kind, CurveKind::Line { .. })).unwrap().id.clone(); + let unchanged = sketch(&s); + assert!(s.execute("sketch.spline.insert_point", &json!({"curve": line, "at": [35, 6]})).is_err()); + assert!(s.execute("sketch.spline.insert_point", &json!({"curve": sid, "at": [35, 10], "end": "end"})).is_err()); + assert!( + s.execute("sketch.spline.insert_point", &json!({"curve": sid, "at": [-10, 4], "end": "start"})).is_err(), + "the start carries the dimension" + ); + assert_eq!(sketch(&s), unchanged, "refusals change nothing"); +} + #[test] fn text_makes_profiles_that_extrude_and_edit() { let mut s = new_sketch(); diff --git a/crates/sketch/src/lib.rs b/crates/sketch/src/lib.rs index 5b977eeeb..18cea9a80 100644 --- a/crates/sketch/src/lib.rs +++ b/crates/sketch/src/lib.rs @@ -36,6 +36,7 @@ mod profiles; pub mod profiling; mod shape; mod solver; +mod spline_points; mod text; mod thin; mod union_find; diff --git a/crates/sketch/src/spline_points.rs b/crates/sketch/src/spline_points.rs new file mode 100644 index 000000000..6e3206d91 --- /dev/null +++ b/crates/sketch/src/spline_points.rs @@ -0,0 +1,129 @@ +//! New points on an existing fit spline (#6533): a fit point inserted where the curve already +//! runs, or a new first or last point the spline runs on to. +//! +//! The spline keeps its curve index and every point it had (the new point is appended to +//! `Sketch::points`), so constraints on its points and on the curve stay as they were. +//! +//! Control point splines take no new points yet: their knots are uniform, so one more control +//! point re-spreads every span and the whole curve moves, wherever the point goes. + +use solidcraft_geom::Vec2; + +use crate::model::{ConstraintKind, CurveKind, Result, Sketch, SketchError}; + +/// Closer than this (relative to the spline's size) to one of its points, a new point would +/// double it. +const SAME_POINT: f64 = 1e-6; + +impl Sketch { + /// Insert a fit point into spline `ci` where the curve passes nearest to `at`, and return its + /// index. The point lies on the curve, which still runs through it and through all its old + /// points; between them the curve shifts slightly, as interpolation through one more point + /// does. + pub fn insert_spline_point(&mut self, ci: usize, at: Vec2) -> Result { + let pts = self.editable_spline(ci)?; + if !at.is_finite() { + return Err(SketchError::Invalid("the position must be finite".into())); + } + let pos: Vec = pts.iter().map(|i| self.point(*i).ok_or_else(|| SketchError::Unknown(format!("point {i}")))).collect::>()?; + // A fit spline has one exact piece between each pair of neighbouring fit points. + let pieces = self.exact_segs(ci).filter(|p| !p.is_empty() && p.len() + 1 == pos.len()); + let pieces = pieces.ok_or_else(|| SketchError::Invalid("the spline has no exact pieces to place a point on".into()))?; + let (piece, new) = pieces + .iter() + .enumerate() + .map(|(i, seg)| { + let q = seg.point_at(seg.closest_param(at)); + (i, q, q.dist(at)) + }) + .min_by(|a, b| a.2.total_cmp(&b.2)) + .map(|(i, q, _)| (i, q)) + .ok_or_else(|| SketchError::Invalid("the spline has no exact pieces to place a point on".into()))?; + let size = spread(&pos); + if [pos.get(piece), pos.get(piece + 1)].into_iter().flatten().any(|p| p.dist(new) <= SAME_POINT * size) { + return Err(SketchError::Invalid("there is already a spline point there".into())); + } + let p = self.add_point(new, None)?; + if let Some(CurveKind::Spline { pts, .. }) = self.curves.get_mut(ci).map(|c| &mut c.kind) { + pts.insert(piece + 1, p); + } + Ok(p) + } + + /// Add `to` to fit spline `ci` as its new first (`at_start`) or last point, and return its + /// index: the curve runs on from its old end through it. The old end becomes an inner point, + /// so it must not be joined to anything (another curve, or a constraint on that point or a + /// tangency of the spline). + pub fn extend_spline(&mut self, ci: usize, to: Vec2, at_start: bool) -> Result { + let pts = self.editable_spline(ci)?; + if matches!(self.curves.get(ci).map(|c| &c.kind), Some(CurveKind::Spline { periodic: true, .. })) { + return Err(SketchError::Invalid("a closed spline has no end to extend".into())); + } + if !to.is_finite() { + return Err(SketchError::Invalid("the position must be finite".into())); + } + let end = if at_start { pts.first() } else { pts.last() }.copied().ok_or_else(|| SketchError::Invalid("empty spline".into()))?; + let joined = self.curves.iter().enumerate().any(|(i, c)| i != ci && c.kind.uses(end)) + || self.constraints.iter().any(|k| { + let (points, curves) = k.kind.indices(); + points.contains(&end) || (curves.contains(&ci) && matches!(k.kind, ConstraintKind::Tangent { .. } | ConstraintKind::Smooth { .. })) + }); + if joined { + return Err(SketchError::Invalid("that end of the spline is joined to other geometry; add the point inside the spline instead".into())); + } + let pos: Vec = pts.iter().filter_map(|i| self.point(*i)).collect(); + if pos.iter().any(|p| p.dist(to) <= SAME_POINT * spread(&pos)) { + return Err(SketchError::Invalid("there is already a spline point there".into())); + } + let p = self.add_point(to, None)?; + if let Some(CurveKind::Spline { pts, .. }) = self.curves.get_mut(ci).map(|c| &mut c.kind) { + if at_start { + pts.insert(0, p); + } else { + pts.push(p); + } + } + Ok(p) + } + + /// The points of spline `ci`, when it may take another one. + fn editable_spline(&self, ci: usize) -> Result> { + let curve = self.curves.get(ci).ok_or_else(|| SketchError::Unknown(format!("curve {ci}")))?; + let CurveKind::Spline { pts, control, .. } = &curve.kind else { + return Err(SketchError::WrongKind(curve.id.clone(), "only a spline takes another point".into())); + }; + if *control { + return Err(SketchError::Invalid( + "not supported yet: adding a point to a control point spline (it would reshape the whole curve)".into(), + )); + } + if curve.link.is_some() { + return Err(SketchError::Invalid("a projected spline follows its source; it takes no points of its own".into())); + } + if self.freeform_offsets.iter().any(|o| o.output == curve.id) { + return Err(SketchError::Invalid("an offset spline follows its source; break the offset association first".into())); + } + if self.freeform_offsets.iter().any(|o| o.source == curve.id) { + return Err(SketchError::Invalid( + "not supported yet: adding a point to a spline that has an offset; break the offset association first".into(), + )); + } + if self.patterns.iter().any(|p| p.contains(&curve.id)) { + return Err(SketchError::Invalid("not supported yet: adding a point to a patterned spline".into())); + } + if pts.len() >= crate::curves::MAX_SPLINE_POINTS { + return Err(SketchError::Invalid(format!("a spline has at most {} points", crate::curves::MAX_SPLINE_POINTS))); + } + Ok(pts.clone()) + } +} + +/// The size of a set of points (its bounding box diagonal, at least 1 mm). +fn spread(pos: &[Vec2]) -> f64 { + let (mut lo, mut hi) = (Vec2::new(f64::INFINITY, f64::INFINITY), Vec2::new(f64::NEG_INFINITY, f64::NEG_INFINITY)); + for p in pos { + lo = Vec2::new(lo.x.min(p.x), lo.y.min(p.y)); + hi = Vec2::new(hi.x.max(p.x), hi.y.max(p.y)); + } + if lo.is_finite() && hi.is_finite() { lo.dist(hi).max(1.0) } else { 1.0 } +} diff --git a/crates/sketch/src/tests.rs b/crates/sketch/src/tests.rs index 29a58496e..55f1d5555 100644 --- a/crates/sketch/src/tests.rs +++ b/crates/sketch/src/tests.rs @@ -1071,6 +1071,101 @@ fn a_degree_nine_control_spline_stays_exact_and_closes_a_profile_exactly() { assert!(sk.add_curve(CurveKind::Spline { pts: high, control: true, degree: 10, periodic: false }, None).is_err()); } +/// How far apart two curves run: the largest distance from a sample of either to the other. +fn curves_apart(a: &[Vec2], b: &[Vec2]) -> f64 { + let one_way = |from: &[Vec2], to: &[Vec2]| from.iter().map(|q| crate::curves::polyline_signed_dist(to, *q).abs()).fold(0.0, f64::max); + one_way(a, b).max(one_way(b, a)) +} + +/// A fit spline through five points, its points and curve index. +fn wave_spline(sk: &mut Sketch, periodic: bool) -> (Vec, usize) { + let mut shape = vec![v(0.0, 0.0), v(10.0, 12.0), v(25.0, 4.0), v(38.0, 15.0), v(50.0, 0.0)]; + if periodic { + shape.push(v(0.0, 0.0)); + } + let mut pts: Vec = shape.iter().map(|q| sk.add_point(*q, None).unwrap()).collect(); + if periodic { + pts.pop(); + pts.push(pts[0]); + } + let s = sk.add_curve(CurveKind::Spline { pts: pts.clone(), control: false, degree: 3, periodic }, None).unwrap(); + (pts, s) +} + +/// The distance from `q` to the exact curve `ci`. +fn off_curve(sk: &Sketch, ci: usize, q: Vec2) -> f64 { + sk.exact_pieces(ci).iter().map(|seg| seg.point_at(seg.closest_param(q)).dist(q)).fold(f64::INFINITY, f64::min) +} + +/// #6533: a fit point added to a spline lands on the curve where it was asked for, in order +/// between its neighbours. The curve still runs through every old point and the new one, and +/// elsewhere barely moves: most where interpolation through one more point bends the end span. +#[test] +fn a_fit_point_inserted_into_a_spline_keeps_the_curve_through_its_points() { + let mut sk = Sketch::new(); + let (pts, s) = wave_spline(&mut sk, false); + let before = sk.polyline(s); + let old: Vec = pts.iter().map(|i| sk.point(*i).unwrap()).collect(); + for (at, slot, apart) in [(v(18.0, 9.0), 2, 0.05), (v(31.0, 9.0), 3, 0.05), (v(44.0, 9.0), 4, 1.0)] { + let mut k = sk.clone(); + let p = k.insert_spline_point(s, at).unwrap(); + let new = k.point(p).unwrap(); + assert!(off_curve(&sk, s, new) < 1e-9, "the new point is on the old curve: {new:?}"); + assert!(new.dist(at) < 2.0, "near the spot asked for: {new:?}"); + let CurveKind::Spline { pts: now, control, periodic, .. } = &k.curves[s].kind else { panic!() }; + assert!(!control && !periodic); + assert_eq!(now.len(), pts.len() + 1); + assert_eq!(now[slot], p, "between its neighbours: {now:?}"); + assert_eq!(now.iter().filter(|i| **i != p).copied().collect::>(), pts, "the old points keep their order"); + for q in old.iter().chain([&new]) { + assert!(off_curve(&k, s, *q) < 1e-9, "the new curve runs through {q:?}"); + } + let moved = curves_apart(&before, &k.polyline(s)); + assert!(moved < apart, "at {at:?} the curve moved {moved}"); + } + // A spot on an existing point, a control point spline and something not a spline are refused. + let mut k = sk.clone(); + assert!(k.insert_spline_point(s, old[2]).is_err()); + assert_eq!(k, sk, "a refused insertion changes nothing"); + let cv: Vec = old.iter().map(|q| k.add_point(*q + v(0.0, 30.0), None).unwrap()).collect(); + let c = k.add_curve(CurveKind::Spline { pts: cv, control: true, degree: 3, periodic: false }, None).unwrap(); + assert!(k.insert_spline_point(c, v(20.0, 38.0)).is_err()); + let l = k.add_line(v(0.0, -10.0), v(50.0, -10.0), None, None, None).unwrap(); + assert!(k.insert_spline_point(l, v(20.0, -10.0)).is_err()); + assert!(k.insert_spline_point(s, v(f64::NAN, 0.0)).is_err()); +} + +/// #6533: a closed spline takes a point in its closing span too, before the repeated first point, +/// and stays closed. +#[test] +fn a_fit_point_inserted_into_a_closed_spline_keeps_it_closed() { + let mut sk = Sketch::new(); + let (pts, s) = wave_spline(&mut sk, true); + let p = sk.insert_spline_point(s, v(25.0, -8.0)).unwrap(); + let CurveKind::Spline { pts: now, periodic, .. } = &sk.curves[s].kind else { panic!() }; + assert!(periodic); + assert_eq!(now.len(), pts.len() + 1); + assert_eq!((now[now.len() - 2], now.first(), now.last()), (p, Some(&pts[0]), Some(&pts[0]))); + assert!(off_curve(&sk, s, sk.point(p).unwrap()) < 1e-9); + assert!(sk.extend_spline(s, v(60.0, 0.0), false).is_err(), "a closed spline has no end"); +} + +/// #6533: a spline runs on to a new first or last point, unless that end is joined to something. +#[test] +fn a_spline_extends_to_a_new_end_unless_the_end_is_joined() { + let mut sk = Sketch::new(); + let (pts, s) = wave_spline(&mut sk, false); + let end = sk.extend_spline(s, v(60.0, 10.0), false).unwrap(); + let start = sk.extend_spline(s, v(-10.0, 5.0), true).unwrap(); + let CurveKind::Spline { pts: now, .. } = &sk.curves[s].kind else { panic!() }; + assert_eq!((now.first(), now.last(), now.len()), (Some(&start), Some(&end), pts.len() + 2)); + assert_eq!(sk.curves[s].kind.ends(), Some((start, end))); + // A line from the end: the end is shared, so the spline does not run past it. + sk.add_line(v(60.0, 10.0), v(70.0, 10.0), Some(end), None, None).unwrap(); + assert!(sk.extend_spline(s, v(65.0, 20.0), false).is_err()); + assert!(sk.extend_spline(s, v(-10.0, 5.0), true).is_err(), "already a point there"); +} + /// #755: a line drawn through a junction of a loop, but 2e-5 off it, still splits the loop. The /// junction lies on the loop's two curves only, so it cuts neither of them where the line /// crosses, and that crossing, next to the junction, was taken for one already cut there: the diff --git a/crates/ui-egui/src/context_menu.rs b/crates/ui-egui/src/context_menu.rs index 96118ef69..692b1a788 100644 --- a/crates/ui-egui/src/context_menu.rs +++ b/crates/ui-egui/src/context_menu.rs @@ -1046,6 +1046,17 @@ fn sketch_entity_items(app: &SolveApp) -> Vec { if curves.len() == 1 && ss.sketch.offset_distance(&curves[0]).is_some() { v.push(cmd(app, "sketch.offset.break", "Break Offset Association").with(json!({"curve":curves[0]}))); } + // #6533: a fit point on a fit point spline, where the menu was opened over it. + if let [curve] = curves.as_slice() + && ss + .sketch + .curve_index(curve) + .and_then(|i| ss.sketch.curves.get(i)) + .is_some_and(|c| matches!(c.kind, CurveKind::Spline { control: false, .. })) + && let Some(at) = menu_sketch_point(app, &ss.plane) + { + v.push(cmd(app, "sketch.spline.insert_point", "Add Spline Point").with(json!({"curve": curve, "at": [at.x, at.y]}))); + } v.push(act("ui.fix", "Fix/Unfix", "c_fix").with(json!({ "entities": ents }))); // The clipboard beside Delete, as in every menu. v.push(act("ui.cut", "Cut", "cut").key("Ctrl+X")); @@ -1107,6 +1118,14 @@ fn sketch_entity_items(app: &SolveApp) -> Vec { v } +/// Where on the active sketch's plane the viewport menu was opened (sketch coordinates). +fn menu_sketch_point(app: &SolveApp, plane: &solidcraft_engine::geom::Plane) -> Option { + let (at, _) = app.menu.opened?; + let proj = crate::viewport::projection(app, app.viewport.rect?); + let (o, d) = proj.ray(at); + Some(plane.to_local(plane.intersect_ray(o, d)?)) +} + // --------------------------------------------------------------------------------------------- // Running an item // --------------------------------------------------------------------------------------------- @@ -2353,6 +2372,61 @@ mod tests { assert!(remaining.dist(cut) > 1.0); } + /// #6533: a right-click on a fit point spline offers Add Spline Point at the clicked spot, and + /// the item puts a fit point there. A control point spline is not offered it. + #[test] + fn spline_context_menu_adds_a_fit_point_where_it_was_clicked() { + let mut h = crate::scenario::Harness::new(); + h.app.home.open = false; + h.app.run("sketch.create", json!({"plane":"XY"})).unwrap(); + let fit = h.app.run("sketch.spline.fit_point", json!({"points":[[0,0],[10,8],[20,0],[30,6]],"connect":false})).unwrap()["curves"][0] + .as_str() + .unwrap() + .to_owned(); + let control = h.app.run("sketch.spline.control_point", json!({"points":[[0,-12],[10,-4],[20,-12]],"connect":false})).unwrap()["curves"][0] + .as_str() + .unwrap() + .to_owned(); + let sid = h.app.session.active_sketch.unwrap(); + let spot = |h: &crate::scenario::Harness, id: &str, piece: usize| { + let state = h.app.session.world_state(); + let sk = &state.sketch(sid).unwrap().sketch; + sk.exact_segs(sk.curve_index(id).unwrap()).unwrap()[piece].point_at(0.5) + }; + let count = |h: &crate::scenario::Harness, id: &str| { + let state = h.app.session.world_state(); + let sk = &state.sketch(sid).unwrap().sketch; + sk.curves[sk.curve_index(id).unwrap()].kind.point_ids().len() + }; + h.call("ui.view", json!({"view":"top"})); + h.app.cam.distance = 80.0; + h.settle(); + + let c = spot(&h, &control, 0); + h.step(&json!({"click":{"world":[c.x,c.y,0]},"button":"right"})).unwrap(); + assert_eq!(h.app.session.selection, vec![Sel::SketchCurve { id: control.clone(), sketch: None }]); + assert!(items(&h.app, &Target::Viewport).iter().all(|item| item.id != "sketch.spline.insert_point")); + close(&mut h.app); + + let at = spot(&h, &fit, 1); + h.step(&json!({"click":{"world":[at.x,at.y,0]},"button":"right"})).unwrap(); + assert_eq!(h.app.session.selection, vec![Sel::SketchCurve { id: fit.clone(), sketch: None }]); + let menu = items(&h.app, &Target::Viewport); + let item = menu.iter().find(|item| item.id == "sketch.spline.insert_point").expect("a fit spline offers Add Spline Point"); + assert_eq!(item.label, "Add Spline Point"); + assert!(item.enabled); + let p = item.params.clone().unwrap(); + assert_eq!(p["curve"], json!(fit)); + let clicked = solidcraft_engine::geom::Vec2::new(p["at"][0].as_f64().unwrap(), p["at"][1].as_f64().unwrap()); + assert!(clicked.dist(at) < 0.5, "the item carries the clicked spot: {clicked:?} for {at:?}"); + h.step(&json!({"click":{"handle":"menu:sketch.spline.insert_point"}})).unwrap(); + assert_eq!(count(&h, &fit), 5); + let state = h.app.session.world_state(); + let sk = &state.sketch(sid).unwrap().sketch; + let added = sk.point(sk.curves[sk.curve_index(&fit).unwrap()].kind.point_ids()[2]).unwrap(); + assert!(added.dist(at) < 0.5, "the new fit point is where the menu was opened: {added:?}"); + } + #[test] fn solidworks_face_menu_runs_normal_to_selection_and_rejects_curved_faces() { let mut h = crate::scenario::Harness::new();