UCS: UCSICON is saved with the drawing; deleting a named UCS clears it everywhere (review)

UCSICON became a drawing setting but the DXF header did not carry it, so
it came back on after a save; write and read it (group 70). The menu toggle
is disabled with no drawing open and no longer fails on doc_mut. UCS NAmed
Delete cleared the name only in the current space; clear it in model space
and every layout. Tests: ucsicon_survives_a_dxf_round_trip,
deleting_a_named_ucs_clears_it_in_every_space.
This commit is contained in:
SBaksa
2026-11-05 12:24:14 +11:00
parent 935e13cc52
commit d75bc7fa60
5 changed files with 57 additions and 1 deletions
+12
View File
@@ -332,6 +332,18 @@ fn delete(s: &mut Session, patterns: &str) -> Result<usize> {
if gone.iter().any(|g| g.eq_ignore_ascii_case(&current)) {
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())
}
+21
View File
@@ -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)));
}
+2
View File
@@ -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),
+15
View File
@@ -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);
}
+7 -1
View File
@@ -242,6 +242,10 @@ fn run_ui_toggle_command(app: &mut CadApp, id: &str, params: &Value) -> Option<R
}
// UCSICON's bits, through its command: 1 shown, 2 at the UCS origin.
"ui.toggle.ucsicon" | "ui.toggle.ucsicon.origin" => {
// 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<Entry>)> {
}
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") {