diff --git a/crates/io/src/dxf_ext.rs b/crates/io/src/dxf_ext.rs index 781019fae..48760323f 100644 --- a/crates/io/src/dxf_ext.rs +++ b/crates/io/src/dxf_ext.rs @@ -38,6 +38,12 @@ pub(crate) const MLEADER_STYLE_KEY: &str = "CADCRAFT_MLEADERSTYLE"; /// Named-object-dictionary key of the XRECORD holding model space's current UCS name ($UCSNAME, /// which the DWG bridge drops). pub(crate) const UCS_NAME_KEY: &str = "CADCRAFT_UCSNAME"; + +/// Whether the UCS at `origin` with axes `x`, `y` is exactly the named UCS `u` (to rounding): +/// only then is the current UCS still that named one, read or written. +pub(crate) fn is_named_frame(u: &cadcraft_doc::Ucs, origin: cadcraft_geom::Vec3, x: cadcraft_geom::Vec3, y: cadcraft_geom::Vec3) -> bool { + (origin - u.origin).len() <= 1e-9 * (1.0 + origin.len()) && (x - u.x_axis).len() <= 1e-9 && (y - u.y_axis).len() <= 1e-9 +} /// Most plot style tables and plot style names read from a file. pub(crate) const MAX_PLOT_STYLE_TABLES: usize = 1000; pub(crate) const MAX_PLOT_STYLE_NAMES: usize = 100_000; diff --git a/crates/io/src/dxf_read.rs b/crates/io/src/dxf_read.rs index 404c4d797..5779fcb56 100644 --- a/crates/io/src/dxf_read.rs +++ b/crates/io/src/dxf_read.rs @@ -2021,9 +2021,7 @@ impl Objects { && let Some(name) = self.xrecord_text(crate::dxf_ext::UCS_NAME_KEY).map(|n| n.trim().to_string()).filter(|n| !n.is_empty()) && let Some(u) = d.ucss.iter().find(|u| u.name.eq_ignore_ascii_case(&name)) && let (Some(o), Some(x), Some(y)) = (d.header.point("UCSORG"), d.header.point("UCSXDIR"), d.header.point("UCSYDIR")) - && (o - u.origin).len() <= 1e-9 * (1.0 + o.len()) - && (x - u.x_axis).len() <= 1e-9 - && (y - u.y_axis).len() <= 1e-9 + && crate::dxf_ext::is_named_frame(u, o, x, y) { let n = u.name.clone(); d.header.set_str("UCSNAME", &n); diff --git a/crates/io/src/dxf_write.rs b/crates/io/src/dxf_write.rs index 5384e36a1..c9da0250f 100644 --- a/crates/io/src/dxf_write.rs +++ b/crates/io/src/dxf_write.rs @@ -308,11 +308,18 @@ fn model_ucs(d: &Drawing) -> Ucs { (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), }; - // 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. + // The name only of a UCS table record whose frame the current UCS still is: a name read from + // a file without its record, beside axes replaced by the World UCS's, or beside an origin or + // axes of its own (restoring the name would move the UCS) is dropped. + let origin = p("UCSORG").unwrap_or(Vec3::ZERO); 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 } + let name = d + .ucss + .iter() + .find(|u| usable && u.name.eq_ignore_ascii_case(raw.trim()) && crate::dxf_ext::is_named_frame(u, origin, x, y)) + .map(|u| u.name.clone()) + .unwrap_or_default(); + Ucs { name, origin, x_axis: x, y_axis: y } } fn header_vars(w: &mut W, d: &Drawing) { diff --git a/crates/io/tests/ucs_roundtrip.rs b/crates/io/tests/ucs_roundtrip.rs index e396994b6..c75c81834 100644 --- a/crates/io/tests/ucs_roundtrip.rs +++ b/crates/io/tests/ucs_roundtrip.rs @@ -207,6 +207,15 @@ fn a_ucs_name_without_its_ucs_is_not_written() { let mut broken = drawing(); broken.header.set("UCSXDIR", HVal::Point(Vec3::ZERO)); assert_eq!(name_written(&broken), (String::new(), false, String::new())); + // Usable but not SITE's frame (moved, or turned): restoring SITE would move the UCS, so the + // current one is unnamed. + let mut moved = drawing(); + moved.header.set("UCSORG", HVal::Point(Vec3::new(101.0, 50.0, 0.0))); + assert_eq!(name_written(&moved), (String::new(), false, String::new())); + let mut turned = drawing(); + turned.header.set("UCSXDIR", HVal::Point(Vec3::new(1.0, 0.0, 0.0))); + turned.header.set("UCSYDIR", HVal::Point(Vec3::new(0.0, 1.0, 0.0))); + assert_eq!(name_written(&turned), (String::new(), false, String::new())); } #[test]