From 81f0aa53ffe5b67fb9aa5cc921f3f3577e2a5d77 Mon Sep 17 00:00:00 2001 From: MarekSpodBiedry <73122968+MarekSpodBiedry@users.noreply.github.com> Date: Mon, 19 Oct 2026 22:27:04 +0200 Subject: [PATCH] Keep a layout's plot window corners when saving DXF (#1667) --- crates/doc/src/tables.rs | 3 +++ crates/io/src/dxf_read.rs | 1 + crates/io/src/dxf_write.rs | 8 ++++---- crates/io/tests/layout_plot_window.rs | 25 +++++++++++++++++++++++++ 4 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 crates/io/tests/layout_plot_window.rs diff --git a/crates/doc/src/tables.rs b/crates/doc/src/tables.rs index 498296237..45e7a469b 100644 --- a/crates/doc/src/tables.rs +++ b/crates/doc/src/tables.rs @@ -752,6 +752,8 @@ pub struct PageSetup { pub landscape: bool, /// "layout" | "extents" | "display" | "window" | "limits". pub plot_area: String, + /// Plot window corners `[x1, y1, x2, y2]` (DXF plot layout groups 48, 49, 140, 141); used when `plot_area` is "window". + pub plot_window: [f64; 4], pub scale_to_fit: bool, /// Paper units per drawing unit. pub scale: f64, @@ -779,6 +781,7 @@ impl Default for PageSetup { margins_mm: [6.35, 6.35, 6.35, 6.35], landscape: true, plot_area: "layout".into(), + plot_window: [0.0; 4], scale_to_fit: false, scale: 1.0, plot_style_table: String::new(), diff --git a/crates/io/src/dxf_read.rs b/crates/io/src/dxf_read.rs index 92d803e17..2bd7e8d8d 100644 --- a/crates/io/src/dxf_read.rs +++ b/crates/io/src/dxf_read.rs @@ -2248,6 +2248,7 @@ fn page_setup(t: &T) -> PageSetup { _ => "layout", } .into(); + p.plot_window = [t.fd(48, 0.0), t.fd(49, 0.0), t.fd(140, 0.0), t.fd(141, 0.0)].map(|v| if v.is_finite() { v } else { 0.0 }); p.plot_style_table = t.s(7).unwrap_or_default(); p } diff --git a/crates/io/src/dxf_write.rs b/crates/io/src/dxf_write.rs index 521654e3d..7991d013b 100644 --- a/crates/io/src/dxf_write.rs +++ b/crates/io/src/dxf_write.rs @@ -2565,10 +2565,10 @@ fn tags_for(d: &Drawing, version: Version, target: Version) -> Vec { (45, page.height_mm), (46, 0.0), (47, 0.0), - (48, 0.0), - (49, 0.0), - (140, 0.0), - (141, 0.0), + (48, page.plot_window[0]), + (49, page.plot_window[1]), + (140, page.plot_window[2]), + (141, page.plot_window[3]), (142, scale), (143, 1.0), ] { diff --git a/crates/io/tests/layout_plot_window.rs b/crates/io/tests/layout_plot_window.rs new file mode 100644 index 000000000..448fa83e5 --- /dev/null +++ b/crates/io/tests/layout_plot_window.rs @@ -0,0 +1,25 @@ +//! #1667: a layout whose page setup plots a window (DXF plot type 4) lost the window corners +//! (groups 48, 49, 140, 141) on save: they were written as zero. + +use cadcraft_doc::{Drawing, Layout, PageSetup}; +use cadcraft_io::{read, write_dxf}; + +#[test] +fn plot_window_corners_round_trip_through_dxf() { + let mut d = Drawing::new_metric(); + let mut l = Layout::new("WIN", 3); + l.page = PageSetup { + width_mm: 297.0, + height_mm: 420.0, + margins_mm: [0.0; 4], + plot_area: "window".into(), + plot_window: [0.0, -297.0, 420.0, 0.0], + ..PageSetup::default() + }; + d.layouts.push(l); + + let back = read(write_dxf(&d).as_bytes(), "win.dxf").unwrap(); + let page = &back.layouts.iter().find(|l| l.name == "WIN").unwrap().page; + assert_eq!(page.plot_area, "window"); + assert_eq!(page.plot_window, [0.0, -297.0, 420.0, 0.0]); +}