UCS: write model space's UCS name only while the UCS is still that named frame

A $UCSNAME beside an origin or axes that differ from its UCS table record
was written back as the current named UCS, so restoring that name later
would move the UCS. The writer now keeps the name only when the current
frame is the record's, by the same rule the reader uses to restore it
(dxf_ext::is_named_frame, now shared).
This commit is contained in:
SBaksa
2026-10-19 15:27:51 +11:00
parent 821b8ad18c
commit d7de5ccdf3
4 changed files with 27 additions and 7 deletions
+6
View File
@@ -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;
+1 -3
View File
@@ -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);
+11 -4
View File
@@ -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) {
+9
View File
@@ -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]