From 3d4b596cfa79e0eda5edb4ed28f14ea73df8a034 Mon Sep 17 00:00:00 2001 From: Lukas Neumann Date: Tue, 24 Nov 2026 12:31:19 +0100 Subject: [PATCH 1/2] Test (#645): a linear dimension caches its projected measurement, and the DXF reader without group 42 too --- .../issue_645_linear_dimension_measurement.rs | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 tests/issue_645_linear_dimension_measurement.rs diff --git a/tests/issue_645_linear_dimension_measurement.rs b/tests/issue_645_linear_dimension_measurement.rs new file mode 100644 index 00000000..0ef15f65 --- /dev/null +++ b/tests/issue_645_linear_dimension_measurement.rs @@ -0,0 +1,138 @@ +//! Issue #645: a rotated (linear) dimension measures the distance between its extension-line +//! origins projected onto the dimension-line direction, not the direct distance +//! between them. `DimensionLinear::new`/`rotated`/`vertical` cached the direct +//! distance in `actual_measurement`, and the DXF reader kept that cached value +//! when the optional group 42 is missing, so a diagonal pair of origins on a +//! horizontal or vertical dimension read back with the wrong measurement. + +use acadrust::entities::{Dimension, DimensionLinear}; +use acadrust::io::dxf::DxfReader; +use acadrust::types::Vector3; +use acadrust::EntityType; +use std::io::Cursor; + +/// A minimal AC1015 DXF holding one rotated DIMENSION as AutoCAD writes it: +/// extension-line origins (13, 14) diagonal to each other, rotation `angle` +/// (group 50, omitted when `None`), measurement (group 42) only when given, +/// and the OCS normal (210/220/230). +fn rotated_dimension_dxf(angle: Option, measurement: Option, normal_z: f64) -> String { + let mut s = String::from( + "0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1015\n0\nENDSEC\n\ + 0\nSECTION\n2\nENTITIES\n\ + 0\nDIMENSION\n5\n2A\n100\nAcDbEntity\n8\n0\n100\nAcDbDimension\n\ + 10\n40.0\n20\n80.0\n30\n0.0\n11\n70.0\n21\n80.0\n31\n0.0\n70\n32\n", + ); + if let Some(m) = measurement { + s.push_str(&format!("42\n{m}\n")); + } + s.push_str(&format!( + "3\nStandard\n210\n0.0\n220\n0.0\n230\n{normal_z}\n\ + 100\nAcDbAlignedDimension\n\ + 13\n100.0\n23\n50.0\n33\n0.0\n14\n40.0\n24\n10.0\n34\n0.0\n" + )); + if let Some(a) = angle { + s.push_str(&format!("50\n{a}\n")); + } + s.push_str("100\nAcDbRotatedDimension\n0\nENDSEC\n0\nEOF\n"); + s +} + +fn read_linear(dxf: &str) -> DimensionLinear { + let doc = DxfReader::from_reader(Cursor::new(dxf.as_bytes().to_vec())) + .expect("open DXF") + .read() + .expect("read DXF"); + let linear = doc.entities().find_map(|e| match e { + EntityType::Dimension(d) => match d.as_ref() { + Dimension::Linear(l) => Some(l.clone()), + _ => None, + }, + _ => None, + }); + linear.expect("a linear dimension") +} + +fn assert_close(actual: f64, expected: f64, what: &str) { + assert!( + (actual - expected).abs() < 1e-9, + "{what}: expected {expected}, got {actual}" + ); +} + +// Origins (100, 50) and (40, 10): dx = 60, dy = 40, direct distance ≈ 72.111. + +#[test] +fn dxf_horizontal_dimension_without_group_42_measures_along_x() { + let dim = read_linear(&rotated_dimension_dxf(None, None, 1.0)); + assert_close(dim.base.actual_measurement, 60.0, "horizontal measurement"); + assert_close( + dim.base.actual_measurement, + dim.measurement(), + "cached vs measurement()", + ); +} + +#[test] +fn dxf_vertical_dimension_without_group_42_measures_along_y() { + let dim = read_linear(&rotated_dimension_dxf(Some(90.0), None, 1.0)); + assert_close(dim.base.actual_measurement, 40.0, "vertical measurement"); + assert_close( + dim.base.actual_measurement, + dim.measurement(), + "cached vs measurement()", + ); +} + +#[test] +fn dxf_rotated_dimension_without_group_42_measures_in_its_ocs() { + // Normal -Z: OCS x = -WCS x, OCS y = WCS y, so the OCS difference is + // (60, -40) and the projection onto 30° is 60·cos30° − 40·sin30° ≈ 31.962. + // Measured in WCS instead it would be |−60·cos30° − 40·sin30°| ≈ 71.962. + let dim = read_linear(&rotated_dimension_dxf(Some(30.0), None, -1.0)); + let (sin, cos) = 30f64.to_radians().sin_cos(); + assert_close( + dim.base.actual_measurement, + 60.0 * cos - 40.0 * sin, + "OCS measurement", + ); + assert_close( + dim.base.actual_measurement, + dim.measurement(), + "cached vs measurement()", + ); +} + +#[test] +fn dxf_dimension_keeps_a_stored_group_42() { + // The file's measurement is authoritative when present, even when it + // differs from the geometry (e.g. a dimension stretched without update). + let dim = read_linear(&rotated_dimension_dxf(None, Some(60.5), 1.0)); + assert_close(dim.base.actual_measurement, 60.5, "stored measurement"); +} + +#[test] +fn linear_constructors_cache_the_projected_measurement() { + let a = Vector3::new(0.0, 0.0, 0.0); + let b = Vector3::new(3.0, 4.0, 0.0); + + let horizontal = DimensionLinear::new(a, b); + assert_close(horizontal.base.actual_measurement, 3.0, "new()"); + + let horizontal = DimensionLinear::horizontal(a, b); + assert_close(horizontal.base.actual_measurement, 3.0, "horizontal()"); + + let vertical = DimensionLinear::vertical(a, b); + assert_close(vertical.base.actual_measurement, 4.0, "vertical()"); + + let rotated = DimensionLinear::rotated(a, b, std::f64::consts::FRAC_PI_4); + assert_close( + rotated.base.actual_measurement, + 7.0 * std::f64::consts::FRAC_1_SQRT_2, + "rotated(45°)", + ); + assert_close( + rotated.base.actual_measurement, + rotated.measurement(), + "rotated(45°) cached vs measurement()", + ); +} From 113c7d4b15f7f998520552882460d7b29701dad1 Mon Sep 17 00:00:00 2001 From: Lukas Neumann Date: Tue, 24 Nov 2026 12:50:28 +0100 Subject: [PATCH 2/2] Fix (#645): a linear dimension without group 42 measures along its dimension line --- src/entities/dimension.rs | 21 ++++++++++++--------- src/io/dxf/reader/section_reader.rs | 7 +++++++ 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/entities/dimension.rs b/src/entities/dimension.rs index 478fba08..6f3aa59b 100644 --- a/src/entities/dimension.rs +++ b/src/entities/dimension.rs @@ -268,17 +268,16 @@ pub struct DimensionLinear { impl DimensionLinear { /// Create a new linear dimension pub fn new(first_point: Vector3, second_point: Vector3) -> Self { - let mut base = DimensionBase::new(DimensionType::Linear); - base.actual_measurement = first_point.distance(&second_point); - - Self { - base, + let mut dim = Self { + base: DimensionBase::new(DimensionType::Linear), first_point, second_point, definition_point: Vector3::ZERO, rotation: 0.0, ext_line_rotation: 0.0, - } + }; + dim.refresh_measurement(); + dim } /// Create a horizontal linear dimension @@ -288,15 +287,14 @@ impl DimensionLinear { /// Create a vertical linear dimension pub fn vertical(first_point: Vector3, second_point: Vector3) -> Self { - let mut dim = Self::new(first_point, second_point); - dim.rotation = std::f64::consts::FRAC_PI_2; // 90 degrees - dim + Self::rotated(first_point, second_point, std::f64::consts::FRAC_PI_2) } /// Create a rotated linear dimension pub fn rotated(first_point: Vector3, second_point: Vector3, angle: f64) -> Self { let mut dim = Self::new(first_point, second_point); dim.rotation = angle; + dim.refresh_measurement(); dim } @@ -312,6 +310,11 @@ impl DimensionLinear { } } + /// Recompute the cached measurement. + pub fn refresh_measurement(&mut self) { + self.base.actual_measurement = self.measurement(); + } + /// Set the offset distance pub fn set_offset(&mut self, offset: f64) { let axis_y = Vector3::new(-self.rotation.sin(), self.rotation.cos(), 0.0); diff --git a/src/io/dxf/reader/section_reader.rs b/src/io/dxf/reader/section_reader.rs index db816f48..287d7ca6 100644 --- a/src/io/dxf/reader/section_reader.rs +++ b/src/io/dxf/reader/section_reader.rs @@ -15861,6 +15861,13 @@ impl<'a> SectionReader<'a> { if let Dimension::Ordinate(value) = &mut dimension { value.refresh_measurement(); } + // Without group 42 the measurement follows the geometry, which is + // only complete here: a linear dimension projects in its OCS. + if actual_measurement.is_none() { + if let Dimension::Linear(value) = &mut dimension { + value.refresh_measurement(); + } + } Ok(Some(dimension)) }