From a4b15ec5f1003ba3402ae34affe5cd14e9268fc8 Mon Sep 17 00:00:00 2001 From: Walter Haslbeck Date: Tue, 24 Nov 2026 14:59:21 +0100 Subject: [PATCH] Kept dimensions: place the block reference at its base point, ByBlock transparency and plot style; snaps follow it --- crates/engine/src/snap.rs | 17 ++++++++++++++ .../engine/tests/it/kept_dimension_snaps.rs | 19 +++++++++++++++ crates/engine/tests/it/main.rs | 1 + crates/io/src/dxf_read.rs | 7 +++++- crates/io/tests/it/large_radial_dimension.rs | 23 +++++++++++++++++++ 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 crates/engine/tests/it/kept_dimension_snaps.rs diff --git a/crates/engine/src/snap.rs b/crates/engine/src/snap.rs index e9d6f1914..eea37e398 100644 --- a/crates/engine/src/snap.rs +++ b/crates/engine/src/snap.rs @@ -531,6 +531,15 @@ fn gather_in( if let EntityKind::Dimension(dm) = &e.kind { sink.dimension(d, dm, &mut budget, frozen); } + // A dimension kept as read draws its block through a reference in its graphics (#3438), + // and snaps to the objects inside it the same way. + if let EntityKind::Unknown(u) = &e.kind { + for g in &u.graphics { + if let EntityKind::Insert(ins) = &g.kind { + block_leaves(d, ins, &Mat3::IDENTITY, 0, &mut budget, frozen, &mut |k, m| sink.object(k, Some(m))); + } + } + } } Some((cands, near_prims)) } @@ -862,6 +871,14 @@ fn block_leaves(d: &Drawing, ins: &Insert, m: &Mat3, depth: usize, budget: &mut match &be.kind { EntityKind::AttDef(_) => {} EntityKind::Insert(inner) => block_leaves(d, inner, &mm, depth + 1, budget, frozen, f), + k @ EntityKind::Unknown(u) => { + f(k, &mm); + for g in &u.graphics { + if let EntityKind::Insert(inner) = &g.kind { + block_leaves(d, inner, &mm, depth + 1, budget, frozen, f); + } + } + } k => f(k, &mm), } } diff --git a/crates/engine/tests/it/kept_dimension_snaps.rs b/crates/engine/tests/it/kept_dimension_snaps.rs new file mode 100644 index 000000000..80d5c4730 --- /dev/null +++ b/crates/engine/tests/it/kept_dimension_snaps.rs @@ -0,0 +1,19 @@ +//! A dimension kept as read (LARGE_RADIAL_DIMENSION, ARC_DIMENSION) draws its `*D` block through +//! a reference in its graphics (#3438); object snaps reach the objects inside that block. + +use cadcraft_engine::doc::Space; +use cadcraft_engine::geom::Vec2; +use cadcraft_engine::snap::{self, mode}; + +#[test] +fn a_kept_dimension_snaps_to_its_blocks_objects() { + // The block's base point isn't the origin; its line is where the dimension draws it. + let dxf = "0\nSECTION\n2\nBLOCKS\n0\nBLOCK\n8\n0\n2\n*D1\n70\n1\n10\n100\n20\n50\n3\n*D1\n0\nLINE\n8\n0\n10\n110\n20\n60\n11\n120\n21\n70\n0\nENDBLK\n0\nENDSEC\n\ +0\nSECTION\n2\nENTITIES\n0\nLARGE_RADIAL_DIMENSION\n5\n41\n100\nAcDbEntity\n8\n0\n100\nAcDbDimension\n2\n*D1\n0\nENDSEC\n0\nEOF\n"; + let d = cadcraft_io::read_dxf(dxf.as_bytes()).expect("read"); + let at = |p: Vec2, m: u32| snap::osnap(&d, &Space::Model, p + Vec2::new(0.02, -0.01), 0.1, m, None, false).map(|h| (h.point, h.mode)); + let end = Vec2::new(120.0, 70.0); + assert!(at(end, mode::END).is_some_and(|(p, m)| m == mode::END && p.dist(end) < 1e-9), "{:?}", at(end, mode::END)); + let mid = Vec2::new(115.0, 65.0); + assert!(at(mid, mode::MID).is_some_and(|(p, m)| m == mode::MID && p.dist(mid) < 1e-9), "{:?}", at(mid, mode::MID)); +} diff --git a/crates/engine/tests/it/main.rs b/crates/engine/tests/it/main.rs index d4a64d47a..6f3f64215 100644 --- a/crates/engine/tests/it/main.rs +++ b/crates/engine/tests/it/main.rs @@ -82,6 +82,7 @@ mod insert_prompts; mod isometric; mod join_close; mod json_param_validation; +mod kept_dimension_snaps; mod layer_cmdline; mod layer_merge_delete; mod layer_state_cmdline; diff --git a/crates/io/src/dxf_read.rs b/crates/io/src/dxf_read.rs index 2cbed711b..0021f13ff 100644 --- a/crates/io/src/dxf_read.rs +++ b/crates/io/src/dxf_read.rs @@ -111,12 +111,17 @@ fn draw_kept_dimensions_from_their_blocks(d: &mut Drawing) { .filter_map(|e| match &e.kind { EntityKind::Unknown(u) => { let block = d.block(u.dimension_block()?)?; - let reference = Insert { block: block.name.clone(), ..Insert::default() }; + // The block holds the dimension as drawn, in the dimension's coordinates: placed + // at its base point it draws where it is, as a DIMENSION's block does. + let reference = Insert { block: block.name.clone(), insert: block.base, ..Insert::default() }; + // Everything ByBlock, so what the block gives ByBlock is the dimension's own. let common = Common { layer: "0".into(), color: Color::ByBlock, linetype: "ByBlock".into(), lineweight: Lineweight::ByBlock, + transparency: Transparency::ByBlock, + plot_style: "ByBlock".into(), ..Common::default() }; (!block.entities.is_empty()).then(|| (e.handle, Entity { handle: e.handle, common, kind: EntityKind::Insert(reference) })) diff --git a/crates/io/tests/it/large_radial_dimension.rs b/crates/io/tests/it/large_radial_dimension.rs index e33c8d301..44e7fd3c8 100644 --- a/crates/io/tests/it/large_radial_dimension.rs +++ b/crates/io/tests/it/large_radial_dimension.rs @@ -141,6 +141,15 @@ fn a_kept_dimension_draws_its_block_and_saving_keeps_it() { let d = read_dxf(file(true).as_bytes()).expect("read"); // A reference to the block, which holds the arrowhead the proxy graphics lack. assert_eq!(graphics_kinds(&d), ["INSERT"]); + // ByBlock throughout, so the block's ByBlock objects take the dimension's own. + let reference = d.model.iter().find_map(|e| match &e.kind { + EntityKind::Unknown(u) => u.graphics.first().map(|g| g.common.clone()), + _ => None, + }); + assert!( + reference.is_some_and(|c| c.transparency == cadcraft_doc::Transparency::ByBlock && c.plot_style == "ByBlock" && c.layer == "0"), + "the reference passes the dimension's traits on" + ); let b = d.extents(&Space::Model); assert!(b.max.x >= 353.0 && b.min.x <= 250.0, "{b:?}"); let again = read_dxf(write_dxf(&d).as_bytes()).expect("reopen"); @@ -330,3 +339,17 @@ fn a_jogged_radius_dimension_draws_the_picture_it_was_saved_with() { let b = d.extents(&Space::Model); assert!(b.min.x <= 250.0 && b.min.y <= 150.0, "{b:?}"); } + +/// A `*D` block whose base point isn't the origin: its contents are in the dimension's +/// coordinates, so it draws where they are, as a DIMENSION's block does. +#[test] +fn a_dimension_block_with_a_base_point_draws_in_place() { + let dxf = "0\nSECTION\n2\nBLOCKS\n0\nBLOCK\n8\n0\n2\n*D1\n70\n1\n10\n100\n20\n50\n3\n*D1\n0\nLINE\n8\n0\n10\n110\n20\n60\n11\n120\n21\n70\n0\nENDBLK\n0\nENDSEC\n\ +0\nSECTION\n2\nENTITIES\n0\nLARGE_RADIAL_DIMENSION\n5\n41\n100\nAcDbEntity\n8\n0\n100\nAcDbDimension\n2\n*D1\n0\nENDSEC\n0\nEOF\n"; + let d = read_dxf(dxf.as_bytes()).expect("read"); + let b = d.extents(&Space::Model); + assert!( + (b.min.x - 110.0).abs() < 1e-9 && (b.min.y - 60.0).abs() < 1e-9 && (b.max.x - 120.0).abs() < 1e-9 && (b.max.y - 70.0).abs() < 1e-9, + "{b:?}" + ); +}