diff --git a/crates/engine/src/cmd/ucs.rs b/crates/engine/src/cmd/ucs.rs index cdc950d3d..a87e32f4c 100644 --- a/crates/engine/src/cmd/ucs.rs +++ b/crates/engine/src/cmd/ucs.rs @@ -332,6 +332,18 @@ fn delete(s: &mut Session, patterns: &str) -> Result { if gone.iter().any(|g| g.eq_ignore_ascii_case(¤t)) { s.set_ucs(frame, "")?; } + // The other spaces whose current UCS carries a deleted name keep their axes and lose the name + // too (model space's UCSNAME, every layout's UCS). + let d = s.doc_mut()?; + let is_gone = |n: &str| gone.iter().any(|g| g.eq_ignore_ascii_case(n.trim())); + if is_gone(&d.header.str("UCSNAME", "")) { + d.header.set_str("UCSNAME", ""); + } + for l in &mut d.layouts { + if let Some(u) = l.ucs.as_mut().filter(|u| is_gone(&u.name)) { + u.name.clear(); + } + } Ok(gone.len()) } diff --git a/crates/engine/tests/ucs.rs b/crates/engine/tests/ucs.rs index f3a7bca8b..a342c0425 100644 --- a/crates/engine/tests/ucs.rs +++ b/crates/engine/tests/ucs.rs @@ -587,3 +587,24 @@ fn deferred_line_polar_follows_the_ucs() { let (a, b) = last_line(&s); assert!((b - a).len() > 9.999 && (b.x - a.x).abs() < 1e-9 && (b.y - a.y - 10.0).abs() < 1e-9, "{a:?} {b:?}"); } + +/// Deleting a named UCS clears its name wherever it is the current UCS: model space and every +/// layout, not only the space deleted from. +#[test] +fn deleting_a_named_ucs_clears_it_in_every_space() { + let mut s = session(); + turned(&mut s); + s.execute("ucs", &json!({"option": "named", "action": "save", "name": "SITE"})).unwrap(); + let layout = s.doc().unwrap().layouts[0].name.clone(); + s.execute("layout.set", &json!({"name": layout})).unwrap(); + s.execute("ucs", &json!({"option": "named", "action": "restore", "name": "SITE"})).unwrap(); + assert_eq!(s.ucs_named().1, "SITE"); + // Delete from paper space: model space is still named SITE. + s.execute("ucs", &json!({"option": "named", "action": "delete", "name": "SITE"})).unwrap(); + assert_eq!(s.ucs_named().1, "", "the current space"); + assert_eq!(s.doc().unwrap().header.str("UCSNAME", ""), "", "model space"); + assert!(s.doc().unwrap().layouts.iter().all(|l| l.ucs.as_ref().is_none_or(|u| u.name.is_empty())), "every layout"); + // The axes stay. + s.execute("layout.set", &json!({"name": "Model"})).unwrap(); + assert!(near3(s.ucs().x, Vec3::new(0.0, 1.0, 0.0))); +} diff --git a/crates/io/src/dxf_write/header.rs b/crates/io/src/dxf_write/header.rs index bb8fb296e..86eba574c 100644 --- a/crates/io/src/dxf_write/header.rs +++ b/crates/io/src/dxf_write/header.rs @@ -42,6 +42,8 @@ const HEADER_VARS: &[(&str, i32)] = &[ ("UCSORG", -3), ("UCSXDIR", -3), ("UCSYDIR", -3), + // Whether the UCS icon is shown (bit 1) and at the UCS origin (bit 2). + ("UCSICON", 70), ("PDMODE", 70), ("PDSIZE", 40), ("PLINEWID", 40), diff --git a/crates/io/tests/ucs_roundtrip.rs b/crates/io/tests/ucs_roundtrip.rs index a79e5db94..5707e7e3f 100644 --- a/crates/io/tests/ucs_roundtrip.rs +++ b/crates/io/tests/ucs_roundtrip.rs @@ -413,3 +413,18 @@ fn a_stale_ucs_name_copy_is_not_brought_back() { } assert_eq!(cadcraft_io::read_dxf(same.as_bytes()).unwrap().header.str("UCSNAME", ""), "SITE"); } + +/// UCSICON is a setting of the drawing: icon off (bit 1 clear) comes back off after a DXF save. +#[test] +fn ucsicon_survives_a_dxf_round_trip() { + let mut d = Drawing::default(); + d.header.set_i64("UCSICON", 0); + let text = cadcraft_io::write_dxf(&d); + assert_eq!(group(&header_var(&text, "UCSICON"), "70"), Some("0"), "written to the header"); + let back = cadcraft_io::read_dxf(text.as_bytes()).unwrap(); + assert_eq!(back.header.i64("UCSICON", 3), 0); + // And a value with the origin bit only. + d.header.set_i64("UCSICON", 2); + let back = cadcraft_io::read_dxf(cadcraft_io::write_dxf(&d).as_bytes()).unwrap(); + assert_eq!(back.header.i64("UCSICON", 3), 2); +} diff --git a/crates/ui-egui/src/menus.rs b/crates/ui-egui/src/menus.rs index 63e431880..bdbc863b4 100644 --- a/crates/ui-egui/src/menus.rs +++ b/crates/ui-egui/src/menus.rs @@ -242,6 +242,10 @@ fn run_ui_toggle_command(app: &mut CadApp, id: &str, params: &Value) -> Option { + // The icon is a setting of the drawing: with none open there is nothing to toggle. + if app.session.doc().is_err() { + return Some(Err("No drawing open.".into())); + } let (key, bit) = if id == "ui.toggle.ucsicon" { ("on", 1) } else { ("origin", 2) }; let mut on = app.session.ucsicon_bits() & bit != 0; toggle(&mut on, params); @@ -620,7 +624,9 @@ pub fn tree(app: &CadApp) -> Vec<(String, Vec)> { } for (id, label, menu, sc) in UI_COMMANDS { if !menu.is_empty() { - add(menu, label, id, *sc, true); + // The UCS icon is a setting of the drawing: off the menu's reach when none is open. + let enabled = !id.starts_with("ui.toggle.ucsicon") || app.session.doc().is_ok(); + add(menu, label, id, *sc, enabled); } } if let Some((_, file)) = out.iter_mut().find(|(m, _)| m == "File") {