From 821b8ad18c083eef004603c6ad307e71a525ab56 Mon Sep 17 00:00:00 2001 From: SBaksa Date: Mon, 19 Oct 2026 14:44:41 +1100 Subject: [PATCH] UCS: UCSICON is undoable, one message per failed menu pick, no stray UCS name in DXF - UCSICON gets its own undo step, as SETVAR does: it lives in the drawing, so undoing the edit before it put the old icon setting back. - A failed pick in the coordinate-system menu is reported once (CadApp::run already echoes it). - Model space's UCS name is written to DXF ($UCSNAME and CADCraft's copy) only for a UCS the drawing has, beside the axes it came with. --- crates/engine/src/cmd/ucs.rs | 3 ++- crates/engine/tests/ucs.rs | 20 ++++++++++++++++++++ crates/io/src/dxf_write.rs | 22 ++++++++++++++++------ crates/io/tests/ucs_roundtrip.rs | 26 ++++++++++++++++++++++++++ crates/ui-egui/src/viewcube.rs | 28 ++++++++++++++++++++++++---- 5 files changed, 88 insertions(+), 11 deletions(-) diff --git a/crates/engine/src/cmd/ucs.rs b/crates/engine/src/cmd/ucs.rs index 75a477817..cdc950d3d 100644 --- a/crates/engine/src/cmd/ucs.rs +++ b/crates/engine/src/cmd/ucs.rs @@ -59,7 +59,8 @@ pub fn specs() -> Vec { .interactive(|_| Ok(Box::new(UcsM::starting(Stage::ZAngle)))), CommandSpec::new("ucsicon", "UCS Icon", run_ucsicon) .params("{on?: bool, origin?: bool} (UCSICON: the icon shown, and at the UCS origin when that is on screen) → {on, origin}") - .noundo() + // Undoable like SETVAR: UCSICON lives in the drawing, so without a step of its own an + // undo of the edit before it would put the old value back. .interactive(|_| Ok(Box::new(UcsIconM))), ] } diff --git a/crates/engine/tests/ucs.rs b/crates/engine/tests/ucs.rs index 5adebbde2..82bd34e2d 100644 --- a/crates/engine/tests/ucs.rs +++ b/crates/engine/tests/ucs.rs @@ -475,6 +475,26 @@ fn ucsicon_options() { assert!(cadcraft_engine::sysvars::set(&mut s, "UCSICON", &json!(9)).is_err()); } +/// UCSICON is its own undo step: undoing it brings the icon back, and an undo never undoes the +/// edit before it while leaving the icon setting behind (or the other way round). +#[test] +fn ucsicon_is_undone_on_its_own() { + let mut s = session(); + let lines = |s: &Session| s.doc().unwrap().model.iter().filter(|e| matches!(e.kind, EntityKind::Line(_))).count(); + s.execute("line", &json!({"points": [[0, 0], [10, 0]]})).unwrap(); + typed(&mut s, &["ucsicon off"]); + assert_eq!(s.ucsicon_bits() & 1, 0); + s.undo().unwrap(); + assert_eq!(s.ucsicon_bits() & 1, 1, "undo takes back UCSICON OFF"); + assert_eq!(lines(&s), 1, "and leaves the line drawn before it"); + s.undo().unwrap(); + assert_eq!(lines(&s), 0); + assert_eq!(s.ucsicon_bits() & 1, 1); + s.redo().unwrap(); + s.redo().unwrap(); + assert_eq!((lines(&s), s.ucsicon_bits() & 1), (1, 0), "redo brings both back"); +} + #[test] fn a_tilted_ucs_from_a_file_is_kept_and_input_stays_in_the_world_ucs() { let mut s = session(); diff --git a/crates/io/src/dxf_write.rs b/crates/io/src/dxf_write.rs index c979f313d..5384e36a1 100644 --- a/crates/io/src/dxf_write.rs +++ b/crates/io/src/dxf_write.rs @@ -304,11 +304,15 @@ fn model_ucs(d: &Drawing) -> Ucs { let h = &d.header; let p = |name: &str| h.point(name).filter(|v| v.is_finite()); let axis = |name: &str| p(name).filter(|v| v.len() > 1e-12); - let (x, y) = match (axis("UCSXDIR"), axis("UCSYDIR")) { - (Some(x), Some(y)) if x.cross(y).len() > 1e-9 * x.len() * y.len() => (x, y), - _ => (Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, 1.0, 0.0)), + let (x, y, usable) = match (axis("UCSXDIR"), axis("UCSYDIR")) { + (Some(x), Some(y)) if x.cross(y).len() > 1e-9 * x.len() * y.len() => (x, y, true), + _ => (Vec3::new(1.0, 0.0, 0.0), Vec3::new(0.0, 1.0, 0.0), false), }; - Ucs { name: h.str("UCSNAME", ""), origin: p("UCSORG").unwrap_or(Vec3::ZERO), x_axis: x, y_axis: y } + // The name only of a UCS table record, and only with the axes it came with: a name read from a + // file without its record, or beside axes replaced by the World UCS's, is dropped. + let raw = h.str("UCSNAME", ""); + let name = d.ucss.iter().find(|u| usable && u.name.eq_ignore_ascii_case(raw.trim())).map(|u| u.name.clone()).unwrap_or_default(); + Ucs { name, origin: p("UCSORG").unwrap_or(Vec3::ZERO), x_axis: x, y_axis: y } } fn header_vars(w: &mut W, d: &Drawing) { @@ -344,6 +348,11 @@ fn header_vars(w: &mut W, d: &Drawing) { w.i(c, i); } } + // Only the name of a UCS the drawing has, with the axes written above. + (c, _) if *name == "UCSNAME" => { + w.s(9, "$UCSNAME"); + w.s(c, model_ucs(d).name); + } (c, v) => { if let Some(t) = v.as_str() { w.s(9, format!("${name}")); @@ -1783,8 +1792,9 @@ fn tags_for(d: &Drawing, version: Version, target: Version) -> Vec { if let Some(name) = d.header.get("CMLEADERSTYLE").and_then(HVal::as_str) { xrecords.push((dxf_ext::MLEADER_STYLE_KEY, w.h(), vec![name.to_string()])); } - if let Some(name) = d.header.get("UCSNAME").and_then(HVal::as_str).map(str::trim).filter(|n| !n.is_empty()) { - xrecords.push((dxf_ext::UCS_NAME_KEY, w.h(), vec![name.to_string()])); + let ucs_name = model_ucs(d).name; + if !ucs_name.is_empty() { + xrecords.push((dxf_ext::UCS_NAME_KEY, w.h(), vec![ucs_name])); } if let Some(chunks) = dxf_ext::constraint_chunks(&d.constraints, &d.parametric) { xrecords.push((dxf_ext::CONSTRAINTS_KEY, w.h(), chunks)); diff --git a/crates/io/tests/ucs_roundtrip.rs b/crates/io/tests/ucs_roundtrip.rs index 1f78834f8..e396994b6 100644 --- a/crates/io/tests/ucs_roundtrip.rs +++ b/crates/io/tests/ucs_roundtrip.rs @@ -183,6 +183,32 @@ fn broken_ucs_values_are_not_written_back() { assert!(back.layouts[0].ucs.is_none()); } +/// Model space's UCS name is written only for a UCS the drawing has, with the axes it came with: +/// a name without its record, or beside axes replaced by the World UCS's, is left out of both +/// $UCSNAME and CADCraft's copy of it. +#[test] +fn a_ucs_name_without_its_ucs_is_not_written() { + let name_written = |d: &Drawing| { + let text = cadcraft_io::write_dxf(d); + let back = cadcraft_io::read_dxf(text.as_bytes()).unwrap(); + ( + group(&header_var(&text, "UCSNAME"), "2").unwrap_or_default().trim().to_string(), + text.contains("CADCRAFT_UCSNAME"), + back.header.str("UCSNAME", ""), + ) + }; + // Kept: SITE is in the UCS table and its axes are current. + assert_eq!(name_written(&drawing()), ("SITE".to_string(), true, "SITE".to_string())); + // No such record (as a file can say). + let mut ghost = drawing(); + ghost.header.set_str("UCSNAME", "GHOST"); + assert_eq!(name_written(&ghost), (String::new(), false, String::new())); + // The record is there, but the axes beside the name are unusable: written as the World UCS. + let mut broken = drawing(); + broken.header.set("UCSXDIR", HVal::Point(Vec3::ZERO)); + assert_eq!(name_written(&broken), (String::new(), false, String::new())); +} + #[test] fn hostile_layout_ucs_groups_are_ignored() { let d = Drawing::default(); diff --git a/crates/ui-egui/src/viewcube.rs b/crates/ui-egui/src/viewcube.rs index 0dfb9660f..26659b8f8 100644 --- a/crates/ui-egui/src/viewcube.rs +++ b/crates/ui-egui/src/viewcube.rs @@ -298,13 +298,17 @@ fn ucs_menu(app: &mut CadApp, popup: egui::Popup<'_>) { ui.close(); } }); - if let Some((id, params)) = action - && let Err(e) = app.run(id, params) - { - app.session.echo(e); + if let Some((id, params)) = action { + menu_action(app, id, params); } } +/// Run a coordinate-system menu pick. A failure is already in the command history: +/// [`CadApp::run`] echoes it (and the icon switch runs UCSICON through it). +fn menu_action(app: &mut CadApp, id: &str, params: serde_json::Value) { + let _ = app.run(id, params); +} + #[cfg(test)] mod tests { use super::*; @@ -348,6 +352,22 @@ mod tests { app.canvas.rect.unwrap_or(Rect::NOTHING) } + /// A menu pick that fails (a tilted named UCS) tells the user once, not twice. + #[test] + fn a_failed_menu_pick_is_reported_once() { + let (mut app, _ctx) = app_and_ctx(); + let tilted = cadcraft_engine::doc::Ucs { + name: "T".into(), + origin: cadcraft_engine::geom::Vec3::ZERO, + x_axis: cadcraft_engine::geom::Vec3::new(1.0, 0.0, 0.0), + y_axis: cadcraft_engine::geom::Vec3::new(0.0, 0.0, 1.0), + }; + app.session.doc_mut().unwrap().ucss.push(tilted); + menu_action(&mut app, "ucs", json!({"option": "named", "action": "restore", "name": "T"})); + let told = app.session.log.iter().filter(|l| l.contains("tilted out of the XY plane")).count(); + assert_eq!(told, 1, "{:?}", app.session.log); + } + fn view(app: &CadApp) -> (f64, f64, f64) { let v = app.session.state().map(|s| s.view()).unwrap_or_default(); (v.center.x, v.center.y, v.height)