SolidCraft: an open-source parametric 3D CAD application built in pure Rust. Desktop, web, and automation through MCP.
Sketch: Center Point Arc follows the cursor clockwise or counter-clockwise (#6645) (#6664)
sascha2101a95f30f1865c
diff --git a/assets/locales/pending/sketch-center-arc-direction.json b/assets/locales/pending/sketch-center-arc-direction.jsonnew file mode 100644index 0000000000..e2a474e65c--- /dev/null+++ b/assets/locales/pending/sketch-center-arc-direction.json@@ -0,0 +1,30 @@+{+ "en": {+ "ui.center_arc_follows_cursor": "Center point arc follows the cursor's direction",+ "ui.center_arc_follows_cursor_tip": "A center point arc runs clockwise or counter-clockwise the way the cursor goes round its center. Off: always counter-clockwise from the start."+ },+ "ar": {+ "ui.center_arc_follows_cursor": "القوس بنقطة مركز يتبع اتجاه المؤشر",+ "ui.center_arc_follows_cursor_tip": "يُرسم القوس بنقطة مركز باتجاه عقارب الساعة أو عكسها حسب الاتجاه الذي يدور به المؤشر حول مركزه. عند الإيقاف: دائمًا عكس اتجاه عقارب الساعة من البداية."+ },+ "de": {+ "ui.center_arc_follows_cursor": "Mittelpunkt-Kreisbogen folgt der Richtung des Mauszeigers",+ "ui.center_arc_follows_cursor_tip": "Ein Mittelpunkt-Kreisbogen läuft im oder gegen den Uhrzeigersinn, je nachdem, wie der Mauszeiger um seinen Mittelpunkt geführt wird. Aus: immer gegen den Uhrzeigersinn vom Start aus."+ },+ "he": {+ "ui.center_arc_follows_cursor": "קשת מרכז ונקודה עוקבת אחר כיוון הסמן",+ "ui.center_arc_follows_cursor_tip": "קשת מרכז ונקודה נמשכת בכיוון השעון או נגדו, לפי הכיוון שבו הסמן מסתובב סביב המרכז. כבוי: תמיד נגד כיוון השעון מנקודת ההתחלה."+ },+ "ja": {+ "ui.center_arc_follows_cursor": "中心点円弧はカーソルの向きに従う",+ "ui.center_arc_follows_cursor_tip": "中心点円弧は、カーソルが中心の周りを回る向きに合わせて時計回りまたは反時計回りに描かれます。オフ: 常に始点から反時計回りです。"+ },+ "vi": {+ "ui.center_arc_follows_cursor": "Cung tròn xác định bởi tâm và điểm theo hướng con trỏ",+ "ui.center_arc_follows_cursor_tip": "Cung tròn xác định bởi tâm và điểm chạy theo chiều hoặc ngược chiều kim đồng hồ, tùy theo hướng con trỏ đi quanh tâm. Tắt: luôn ngược chiều kim đồng hồ từ điểm đầu."+ },+ "zh": {+ "ui.center_arc_follows_cursor": "中心点圆弧跟随光标方向",+ "ui.center_arc_follows_cursor_tip": "中心点圆弧按光标绕中心移动的方向沿顺时针或逆时针绘制。关闭:始终从起点逆时针绘制。"+ }+}diff --git a/crates/engine/src/cmd/sketch.rs b/crates/engine/src/cmd/sketch.rsindex 4f82b7a953..79ece6966c 100644--- a/crates/engine/src/cmd/sketch.rs+++ b/crates/engine/src/cmd/sketch.rs@@ -99,7 +99,7 @@ pub static COMMANDS: &[CommandSpec] = &[.at("SKETCH", "CREATE").icon("arc_center").enabled(in_sketch)- .params(concat!("center, start, end (counter-clockwise from start) | center, start, sweep: signed degrees; construction?: bool", connect_doc!(), created_doc!())),+ .params(concat!("center, start, end (counter-clockwise from start; clockwise?: bool for clockwise) | center, start, sweep: signed degrees; construction?: bool", connect_doc!(), created_doc!())),CommandSpec::new("sketch.polygon.inscribed", "Inscribed Polygon", polygon_inscribed).at("SKETCH", "CREATE").icon("polygon")@@ -1400,7 +1400,31 @@ fn arc_center(s: &mut Session, p: &Value) -> Result<Value> {return Ok((ids_of(sk, &[ci]), Vec::new()));}let b = req_parg(sk, doc, p, "end", cmd)?;- let ci = sk.add_arc(c.pos(), a.pos(), b.pos(), [c.idx(), a.idx(), b.idx()], id.as_deref())?;+ // Clockwise from start to end (#6645): stored counter-clockwise from the end and reversed,+ // as a negative sweep is. The start keeps the radius and `<id>.start`.+ let ci = if bool_(p, "clockwise").unwrap_or(false) {+ let r = a.pos().dist(c.pos());+ let end = c.pos() + (b.pos() - c.pos()).normalized().ok_or_else(|| bad(cmd, "`end` must not be on `center`"))? * r;+ let cid = match &id {+ Some(i) => i.clone(),+ None => sk.fresh("a")?,+ };+ let ai = match a.idx() {+ Some(i) => i,+ None => sk.add_point(a.pos(), Some(&format!("{cid}.start")))?,+ };+ let bi = match b.idx() {+ Some(i) => i,+ None => sk.add_point(end, Some(&format!("{cid}.end")))?,+ };+ let ci = sk.add_arc(c.pos(), end, a.pos(), [c.idx(), Some(bi), Some(ai)], Some(&cid))?;+ if let Some(cu) = sk.curves.get_mut(ci) {+ cu.reversed = true;+ }+ ci+ } else {+ sk.add_arc(c.pos(), a.pos(), b.pos(), [c.idx(), a.idx(), b.idx()], id.as_deref())?+ };mark_construction(sk, &[ci], p);Ok((ids_of(sk, &[ci]), Vec::new()))})?;diff --git a/crates/engine/src/cmd/sketch_dimensioned_tests.rs b/crates/engine/src/cmd/sketch_dimensioned_tests.rsindex 6436eb0849..81e452e62b 100644--- a/crates/engine/src/cmd/sketch_dimensioned_tests.rs+++ b/crates/engine/src/cmd/sketch_dimensioned_tests.rs@@ -87,6 +87,41 @@ fn signed_arc_sweep_retains_radius_and_logical_endpoint_identity() {}}+/// #6645: a clicked end with `clockwise` sweeps from the start the other way round, keeps the+/// start's radius and its `.start`/`.end` names, with and without a typed radius.+#[test]+fn a_clockwise_centre_arc_runs_from_its_start_to_its_end_the_other_way_round() {+ for dimensions in [None, Some(json!({"radius": 10}))] {+ let mut s = session();+ // The end is off the radius, as a cursor is: it is pulled onto the start's circle.+ let params = json!({"center":[0,0],"start":[10,0],"end":[0,4],"clockwise":true,"id":"a1","connect":false});+ match &dimensions {+ Some(d) => draw(&mut s, "sketch.arc.center_point", params, d.clone()),+ None => run(&mut s, "sketch.arc.center_point", params),+ };+ let sk = sketch(&s);+ let arc = &sk.curves[sk.curve_index("a1").unwrap()];+ assert!(arc.reversed, "{dimensions:?}");+ let at = |r: &str| sk.points[sk.resolve_point(r).unwrap()].pos;+ let (start, end) = (at("a1.start"), at("a1.end"));+ close(start.x, 10.0);+ close(start.y, 0.0);+ close(end.x, 0.0);+ close(end.y, 10.0);+ // Clockwise from 0° to 90° is 270° of arc through the bottom (-90°), not the top.+ let solidcraft_sketch::CurveKind::Arc { c, a, b } = arc.kind else { panic!("not an arc") };+ let (c, a, b) = (sk.points[c].pos, sk.points[a].pos, sk.points[b].pos);+ close(((b - c).angle() - (a - c).angle()).rem_euclid(std::f64::consts::TAU).to_degrees(), 270.0);+ if dimensions.is_some() {+ close(measure(&s, |k| matches!(k, ConstraintKind::Radius { .. })), 10.0);+ }+ }+ // Without the flag the same clicks keep the counter-clockwise quarter.+ let mut s = session();+ run(&mut s, "sketch.arc.center_point", json!({"center":[0,0],"start":[10,0],"end":[0,4],"id":"a1"}));+ assert!(!sketch(&s).curves[sketch(&s).curve_index("a1").unwrap()].reversed);+}+#[test]fn slots_and_polygon_keep_permanent_typed_dimensions() {for command in ["sketch.slot.center_to_center", "sketch.slot.overall", "sketch.slot.center_point"] {diff --git a/crates/ui-egui/src/prefs.rs b/crates/ui-egui/src/prefs.rsindex ac2d4a055f..0dc8c398aa 100644--- a/crates/ui-egui/src/prefs.rs+++ b/crates/ui-egui/src/prefs.rs@@ -1085,6 +1085,9 @@ preference_settings! {"Also place them from the origin (fully constrained)","Each new shape is also dimensioned from the sketch origin until nothing of it is left free. A shape already attached to the origin or to placed geometry gets none.");+ /// A centre-point arc runs the way the cursor goes round its centre from the start (#6645).+ /// Off: it always runs counter-clockwise from the start, as it did before.+ center_arc_follows_cursor: bool = true, PAGE_SKETCH;/// When true: Shift+click adds without removing; Primary toggles./// When false (default): Shift and Primary both toggle.selection_shift_add_only: bool = false, PAGE_SKETCH;diff --git a/crates/ui-egui/src/prefs/sketch.rs b/crates/ui-egui/src/prefs/sketch.rsindex d839755b31..3c95f6ce90 100644--- a/crates/ui-egui/src/prefs/sketch.rs+++ b/crates/ui-egui/src/prefs/sketch.rs@@ -21,6 +21,10 @@ pub(super) fn show(app: &mut SolveApp, ui: &mut egui::Ui) {let locate = setting::sketch_auto_locate(app, ui);crate::scenario::publish_handle("prefs:sketch_auto_locate", locate.rect.center());});+ ui_kit::checkbox(ui, crate::i18n::tr_label("Center point arc follows the cursor's direction"), &mut app.preferences.center_arc_follows_cursor)+ .on_hover_text(crate::i18n::tr_label(+ "A center point arc runs clockwise or counter-clockwise the way the cursor goes round its center. Off: always counter-clockwise from the start.",+ ));ui_kit::settings_block_hint(ui, app.tr("prefs.hold_stop_snapping"), app.tr("prefs.snap_drawing_note"), t.space_24 * 8.0 + t.space_8, |ui| {let combo = &mut app.preferences.suppress_sketch_snap;ui_kit::settings_control_width(ui, t.space_24 * 7.0 + t.space_12, |ui| {diff --git a/crates/ui-egui/src/sketch_corner_ui_tests.rs b/crates/ui-egui/src/sketch_corner_ui_tests.rsindex bf006cec15..bef5975d5b 100644--- a/crates/ui-egui/src/sketch_corner_ui_tests.rs+++ b/crates/ui-egui/src/sketch_corner_ui_tests.rs@@ -27,6 +27,45 @@ fn three_point_arc_follows_its_snapped_through_point_after_dimension_change() {assert!(distance < 1e-6, "the arc misses its snapped through point by {distance} mm");}+/// #6645: the centre-point arc runs the way the cursor went round the centre from the start.+/// Round clockwise to the top it is three quarters through the bottom; counter-clockwise, as+/// before, the quarter through the upper right. The start keeps its place and its name. With+/// the preference off it is counter-clockwise whichever way the cursor went, as it always was.+#[test]+fn center_point_arc_runs_the_way_the_cursor_went_round() {+ let clockwise = &[[7, -7, 0], [0, -10, 0], [-10, 0, 0], [-7, 7, 0]][..];+ let quarter = Vec2::new(7.071_067_811_865_476, 7.071_067_811_865_476);+ for (follow, path, reversed, through) in+ [(true, clockwise, true, Vec2::new(0.0, -10.0)), (true, &[[7, 7, 0]][..], false, quarter), (false, clockwise, false, quarter)]+ {+ let mut h = crate::scenario::Harness::new();+ assert!(h.app.preferences.center_arc_follows_cursor, "on by default");+ h.app.preferences.center_arc_follows_cursor = follow;+ h.app.run("sketch.create", json!({"plane":"XY"})).unwrap();+ h.settle();+ h.step(&json!({"start":"sketch.arc.center_point"})).unwrap();+ h.step(&json!({"click":{"world":[0, 0, 0]}})).unwrap();+ h.step(&json!({"click":{"world":[10, 0, 0]}})).unwrap();+ for point in path {+ h.step(&json!({"move":{"world":point}})).unwrap();+ }+ h.step(&json!({"move":{"world":[0, 10, 0]}})).unwrap();+ let sweep = h.app.tool.as_ref().and_then(|t| t.hover.as_ref().map(|p| crate::tools::center_arc_sweep(t, p.0))).unwrap();+ let expected = if reversed { -270.0 } else { 90.0 };+ assert!((sweep.to_degrees() - expected).abs() < 1e-6, "previewed sweep {}°", sweep.to_degrees());+ h.step(&json!({"click":{"world":[0, 10, 0]}})).unwrap();+ let sid = h.app.session.active_sketch.unwrap();+ let sk = h.app.session.model.state().sketch(sid).unwrap().sketch.clone();+ let arc = sk.curves.iter().position(|c| matches!(c.kind, CurveKind::Arc { .. })).unwrap();+ assert_eq!(sk.curves[arc].reversed, reversed, "{:?}", h.app.status);+ let id = sk.curves[arc].id.clone();+ let start = sk.point(sk.resolve_point(&format!("{id}.start")).unwrap()).unwrap();+ assert!(start.dist(Vec2::new(10.0, 0.0)) < 1e-6, "{start:?}");+ let off = sk.shape(arc).unwrap().dist(through);+ assert!(off < 1e-6, "the arc misses {through:?} by {off} mm");+ }+}+#[test]fn clicked_periodic_fit_closes_at_start_and_right_click_keeps_natural() {let mut h = crate::scenario::Harness::new();diff --git a/crates/ui-egui/src/sketch_dims.rs b/crates/ui-egui/src/sketch_dims.rsindex f601faa84b..5c64b4de33 100644--- a/crates/ui-egui/src/sketch_dims.rs+++ b/crates/ui-egui/src/sketch_dims.rs@@ -282,7 +282,12 @@ fn live(app: &SolveApp, t: &Tool, e: Vec2, px: f64) -> Vec<(f64, Vec2)> {let a0 = (a - first).angle();// Out along the radius, clear of the cursor: a box on it would take the hover and// the click meant for a point there (#2201).- vec![(((e - first).angle() - a0).rem_euclid(std::f64::consts::TAU), e + dir_or_x(e - first) * (2.5 * off))]+ // A centre-point arc runs the way the cursor went round (#6645): its sweep is signed.+ let sweep = match t.cmd {+ "sketch.arc.center_point" => crate::tools::center_arc_sweep(t, e),+ _ => ((e - first).angle() - a0).rem_euclid(std::f64::consts::TAU),+ };+ vec![(sweep, e + dir_or_x(e - first) * (2.5 * off))]}("sketch.conic", 2) => {// `rho` is the curve's fullness, not a distance: the conic passes throughdiff --git a/crates/ui-egui/src/sketch_tools.rs b/crates/ui-egui/src/sketch_tools.rsindex c0f18bc01c..468946eebe 100644--- a/crates/ui-egui/src/sketch_tools.rs+++ b/crates/ui-egui/src/sketch_tools.rs@@ -623,6 +623,7 @@ pub fn tool_for(id: &str) -> Option<Tool> {dim_history: Vec::new(),dims_stage: usize::MAX,dims_focus: false,+ arc_turn: 0.0,})}diff --git a/crates/ui-egui/src/tools.rs b/crates/ui-egui/src/tools.rsindex 783ddab8c3..8e273ddc46 100644--- a/crates/ui-egui/src/tools.rs+++ b/crates/ui-egui/src/tools.rs@@ -91,6 +91,9 @@ pub struct Tool {pub dim_history: Vec<crate::sketch_dims::DimBox>,pub dims_stage: usize,pub dims_focus: bool,+ /// How far (radians, signed) the cursor has gone round a centre-point arc's centre since its+ /// start was placed: its sign is the way the arc runs (#6645).+ pub arc_turn: f64,}impl Tool {@@ -138,6 +141,7 @@ impl Tool {dim_history: Vec::new(),dims_stage: usize::MAX,dims_focus: false,+ arc_turn: 0.0,})}}@@ -152,7 +156,7 @@ 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.arc.center_point" => "Arc: click centre and start, then click end or type a signed sweep".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()}@@ -207,11 +211,47 @@ pub fn on_hover(app: &mut SolveApp, proj: &Proj, pos: Pos2, shift: bool) {} else {sketch_point_at(app, proj, pos)};+ let follow = app.preferences.center_arc_follows_cursor;if let Some(t) = app.tool.as_mut() {t.hover = h;+ if follow {+ follow_arc_turn(t);+ } else {+ t.arc_turn = 0.0;+ }}}+/// Add the cursor's latest step round a centre-point arc's centre to the turn it has made since+/// the start was placed, a step never more than half a turn. A full turn back to the start wraps.+fn follow_arc_turn(t: &mut Tool) {+ let (Some(c), Some(a), Some(h)) = (t.pts.first(), t.pts.get(1), t.hover.as_ref()) else {+ t.arc_turn = 0.0;+ return;+ };+ if t.cmd != "sketch.arc.center_point" || t.pts.len() != 2 {+ t.arc_turn = 0.0;+ return;+ }+ let (c, a, h) = (c.0, a.0, h.0);+ if h.dist(c) <= 1e-9 || a.dist(c) <= 1e-9 {+ return;+ }+ let at = (a - c).angle() + t.arc_turn;+ let step = ((h - c).angle() - at + std::f64::consts::PI).rem_euclid(std::f64::consts::TAU) - std::f64::consts::PI;+ if step.is_finite() {+ t.arc_turn = (t.arc_turn + step) % std::f64::consts::TAU;+ }+}++/// A centre-point arc's sweep from its start to `end` (radians): counter-clockwise, unless the+/// cursor went round the centre clockwise from the start (#6645).+pub(crate) fn center_arc_sweep(t: &Tool, end: Vec2) -> f64 {+ let (Some(c), Some(a)) = (t.pts.first(), t.pts.get(1)) else { return 0.0 };+ let ccw = ((end - c.0).angle() - (a.0 - c.0).angle()).rem_euclid(std::f64::consts::TAU);+ if t.arc_turn < 0.0 && ccw > 0.0 { ccw - std::f64::consts::TAU } else { ccw }+}+pub fn on_click(app: &mut SolveApp, proj: &Proj, pos: Pos2, shift: bool) {// A click away dismisses the inline editor; it must not also pick the first entity of// the next dimension before that cancellation has been processed. The type-first value@@ -566,6 +606,9 @@ fn run_shape(app: &mut SolveApp, t: &Tool) -> Result<Vec<String>, String> {};match crate::sketch_dims::locked_value(app, t, "Sweep") {Some(sweep) => json!({"center": center, "start": arg(&p[1]), "sweep": sweep.to_degrees()}),+ None if center_arc_sweep(t, p[2].0) < 0.0 => {+ json!({"center": center, "start": arg(&p[1]), "end": xy(&p[2]), "clockwise": true})+ }None => json!({"center": center, "start": arg(&p[1]), "end": xy(&p[2])}),}}@@ -909,7 +952,7 @@ pub(crate) fn preview_curve(}("sketch.arc.center_point", [c, a]) => {let start = (*a - *c).angle();- let sweep = sweep.unwrap_or_else(|| ((hover - *c).angle() - start).rem_euclid(std::f64::consts::TAU));+ let sweep = sweep.unwrap_or_else(|| center_arc_sweep(t, hover));if !sweep.is_finite() || sweep.abs() <= 1e-9 || sweep.abs() >= std::f64::consts::TAU {return None;}