From aa4f4780b68fef991039e9a084f99603985c7876 Mon Sep 17 00:00:00 2001 From: BjornFidder Date: Fri, 20 Nov 2026 15:29:09 +0100 Subject: [PATCH 1/3] Retain typed DXF XREF source metadata --- examples/xref_review.rs | 5 +- src/ifcx/external_sources.rs | 37 +++++++++--- src/ifcx/mod.rs | 16 +++++- src/ifcx/xref.rs | 41 ++++++------- src/lib.rs | 2 + tests/ifcx_xref_source_formats.rs | 96 +++++++++++++++++++++++++++++++ tests/ifcx_xrefs.rs | 10 ++-- 7 files changed, 165 insertions(+), 42 deletions(-) create mode 100644 tests/ifcx_xref_source_formats.rs diff --git a/examples/xref_review.rs b/examples/xref_review.rs index 32f5874..9d50452 100644 --- a/examples/xref_review.rs +++ b/examples/xref_review.rs @@ -161,7 +161,10 @@ fn main() -> std::result::Result<(), Error> { assert_eq!(back.drawing.external_sources.len(), 1); let IfccadExternalSourceContent::Dwg { fingerprint_guid, .. - } = &back.drawing.external_sources[0].source; + } = &back.drawing.external_sources[0].source + else { + return Err("Expected a DWG source in the first-slice review project".into()); + }; assert_eq!(fingerprint_guid.is_some(), guid); std::fs::write(drawings.join(name), bytes)?; } diff --git a/src/ifcx/external_sources.rs b/src/ifcx/external_sources.rs index 3687300..c53e85b 100644 --- a/src/ifcx/external_sources.rs +++ b/src/ifcx/external_sources.rs @@ -169,20 +169,39 @@ pub fn validate_cad_external_sources(document: &crate::Document) -> Result<(), E Ok(()) } +fn cad_source_metadata(document: &crate::Document) -> ([f64; 3], Option, Option) { + let code = document.header.insertion_units; + ( + super::array(document.header.model_space_insertion_base), + usize::try_from(code) + .ok() + .and_then(|i| super::UNITS.get(i)) + .map(|v| (*v).to_owned()), + ifccad::normalize_ifccad_fingerprint_guid(&document.header.fingerprint_guid), + ) +} + /// Metadata of an actually opened DWG. This never invents a source UUID. pub fn dwg_external_source_content( document: &crate::Document, ) -> ifccad::IfccadExternalSourceContent { - let code = document.header.insertion_units; + let (base_point, insertion_unit, fingerprint_guid) = cad_source_metadata(document); ifccad::IfccadExternalSourceContent::Dwg { - base_point: super::array(document.header.model_space_insertion_base), - insertion_unit: usize::try_from(code) - .ok() - .and_then(|i| super::UNITS.get(i)) - .map(|v| (*v).to_owned()), - fingerprint_guid: ifccad::normalize_ifccad_fingerprint_guid( - &document.header.fingerprint_guid, - ), + base_point, + insertion_unit, + fingerprint_guid, + } +} + +/// Metadata of an actually opened DXF, including its available header identity. +pub fn dxf_external_source_content( + document: &crate::Document, +) -> ifccad::IfccadExternalSourceContent { + let (base_point, insertion_unit, fingerprint_guid) = cad_source_metadata(document); + ifccad::IfccadExternalSourceContent::Dxf { + base_point, + insertion_unit, + fingerprint_guid, } } diff --git a/src/ifcx/mod.rs b/src/ifcx/mod.rs index 7971e4f..defd024 100644 --- a/src/ifcx/mod.rs +++ b/src/ifcx/mod.rs @@ -48,8 +48,9 @@ mod dimension_kinds; mod external_sources; mod xref; pub use external_sources::{ - dwg_external_source_content, dwg_xref_fingerprint_guid, external_source_location_from_cad_path, - external_source_location_to_cad_path, validate_cad_external_sources, + dwg_external_source_content, dwg_xref_fingerprint_guid, dxf_external_source_content, + external_source_location_from_cad_path, external_source_location_to_cad_path, + validate_cad_external_sources, }; mod font; mod hatch; @@ -235,6 +236,17 @@ impl Ifcx { /// /// Returns an error if IFCCAD validation or CAD document construction fails. pub fn import_ifccad(&self, drawing: &IfccadDocument) -> Result { + if let Some(source) = drawing + .external_sources + .iter() + .find(|source| source.source.format() != ifccad::IfccadExternalSourceFormat::Dwg) + { + return Err(format!( + "XREF {}: convert the external source to DWG before CAD export", + source.name + ) + .into()); + } self.import_ifccad_inner( drawing, #[cfg(feature = "mline-pilot")] diff --git a/src/ifcx/xref.rs b/src/ifcx/xref.rs index dbb7dc2..e30c86e 100644 --- a/src/ifcx/xref.rs +++ b/src/ifcx/xref.rs @@ -45,12 +45,7 @@ pub(super) fn diagnose_dependency_losses(writer: &mut Writer<'_>) { .external_sources .iter() .find(|source| source.id == source_id) - .and_then(|source| { - let IfccadExternalSourceContent::Dwg { - fingerprint_guid, .. - } = &source.source; - fingerprint_guid.as_deref() - }); + .and_then(|source| source.source.fingerprint_guid()); if dependency .fingerprint_guid .as_deref() @@ -132,19 +127,14 @@ pub(super) fn sources_in( } } }; - let IfccadExternalSourceContent::Dwg { - base_point, - insertion_unit, - fingerprint_guid, - } = &source.source; let mut record = BlockRecord::new(source.name.clone()); record.handle = document.allocate_handle(); record.block_entity_handle = document.allocate_handle(); record.block_end_handle = document.allocate_handle(); record.flags.is_xref = true; record.xref_path = path.clone(); - record.base_point = vector(*base_point); - record.units = insertion_unit.as_deref().map(unit_code).unwrap_or(0); + record.base_point = vector(source.source.base_point()); + record.units = source.source.insertion_unit().map(unit_code).unwrap_or(0); let mut block = Block::new(record.name.clone(), record.base_point); block.common.handle = record.block_entity_handle; block.common.owner_handle = record.handle; @@ -157,7 +147,7 @@ pub(super) fn sources_in( document.block_records.add(record)?; document.add_entity(EntityType::Block(block))?; document.add_entity(EntityType::BlockEnd(end))?; - if let Some(guid) = fingerprint_guid { + if let Some(guid) = source.source.fingerprint_guid() { document .file_dependencies .push(acadrust::document::FileDependency { @@ -224,13 +214,6 @@ impl Writer<'_> { source.name = record.name.clone(); source } else { - if record.xref_path.to_ascii_lowercase().ends_with(".dxf") { - self.losses.note(format!( - "XREF {} uses a DXF source outside the first DWG attachment slice", - record.name - )); - return Ok(None); - } let location = match external_sources::external_source_location_from_cad_path(&record.xref_path) { Ok(v) => v, @@ -246,10 +229,18 @@ impl Writer<'_> { id: self.drawing.id_counters.allocate_external_source_id()?, name: record.name.clone(), location, - source: IfccadExternalSourceContent::Dwg { - base_point: array(record.base_point), - insertion_unit: (record.units != 0).then(|| unit_name(record.units)), - fingerprint_guid, + source: if record.xref_path.to_ascii_lowercase().ends_with(".dxf") { + IfccadExternalSourceContent::Dxf { + base_point: array(record.base_point), + insertion_unit: (record.units != 0).then(|| unit_name(record.units)), + fingerprint_guid, + } + } else { + IfccadExternalSourceContent::Dwg { + base_point: array(record.base_point), + insertion_unit: (record.units != 0).then(|| unit_name(record.units)), + fingerprint_guid, + } }, } }; diff --git a/src/lib.rs b/src/lib.rs index 4485563..8b1c1f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -132,6 +132,8 @@ pub use ifcx::dwg_external_source_content; #[cfg(feature = "ifcx")] pub use ifcx::dwg_xref_fingerprint_guid; #[cfg(feature = "ifcx")] +pub use ifcx::dxf_external_source_content; +#[cfg(feature = "ifcx")] pub use ifcx::validate_cad_external_sources; #[cfg(feature = "ifcx")] pub use ifcx::{external_source_location_from_cad_path, external_source_location_to_cad_path}; diff --git a/tests/ifcx_xref_source_formats.rs b/tests/ifcx_xref_source_formats.rs new file mode 100644 index 0000000..1307b7c --- /dev/null +++ b/tests/ifcx_xref_source_formats.rs @@ -0,0 +1,96 @@ +#![cfg(all(feature = "ifcx", feature = "mline-pilot"))] +use opencaddocument::{ifccad::*, *}; + +fn source_drawing() -> IfccadDocument { + let mut drawing = load_ifccad_bytes( + include_bytes!("fixtures/xref/attachment.ifcx"), + Default::default(), + ) + .unwrap() + .into_document(); + drawing.external_sources[0].source = IfccadExternalSourceContent::Dxf { + base_point: [10., 20., 0.], + insertion_unit: Some("m".into()), + fingerprint_guid: Some("e5b4a9af-cf20-4fc9-a30f-ffb20fd987e1".into()), + }; + drawing +} + +#[test] +fn preserving_dxf_source_keeps_typed_identity_even_with_dwg_filename() { + let drawing = source_drawing(); + let (imported, mut context) = Ifcx.import_ifccad_preserving(&drawing).unwrap(); + let saved = Ifcx + .export_ifccad_preserving(&imported.document, &mut context) + .unwrap(); + assert_eq!(saved.drawing.external_sources, drawing.external_sources); + let expected = drawing.model.entities[0].as_native().unwrap(); + let actual = saved.drawing.model.entities[0].as_native().unwrap(); + assert_eq!(actual.id, expected.id); + let (IfccadEntityKind::Xref(expected), IfccadEntityKind::Xref(actual)) = + (&expected.kind, &actual.kind) + else { + panic!("XREF placement expected") + }; + assert_eq!(actual.source_id, expected.source_id); + assert_eq!(actual.scale, expected.scale); + assert_eq!(actual.placement.origin, expected.placement.origin); + for (a, b) in actual + .placement + .x_axis + .iter() + .chain(&actual.placement.y_axis) + .zip( + expected + .placement + .x_axis + .iter() + .chain(&expected.placement.y_axis), + ) + { + assert!((a - b).abs() <= 1e-12); + } + let encoded = encode_ifccad_document(&saved.drawing).unwrap(); + let read = load_ifccad_bytes(encoded.bytes(), Default::default()).unwrap(); + assert_eq!(read.document().external_sources, drawing.external_sources); +} + +#[test] +fn stateless_cad_projection_refuses_non_dwg_source_by_type() { + for uri in [ + "../Refs/grondplan.dxf", + "../Refs/grondplan.dwg", + "../Refs/grondplan", + ] { + let mut drawing = source_drawing(); + drawing.external_sources[0].location = + IfccadExternalSourceLocation::UriReference { uri: uri.into() }; + let error = Ifcx.import_ifccad(&drawing).unwrap_err().to_string(); + assert!(error.contains("DWG"), "{error}"); + } +} + +#[test] +fn dxf_source_metadata_uses_actual_header_identity_without_unit_conversion() { + let mut document = Document::new(); + document.header.model_space_insertion_base = Vector3::new(10., 20., 0.); + document.header.insertion_units = 6; + document.header.fingerprint_guid = "{E5B4A9AF-CF20-4FC9-A30F-FFB20FD987E1}".into(); + let source = dxf_external_source_content(&document); + assert_eq!(source, source_drawing().external_sources[0].source); +} + +#[test] +fn cad_xref_with_dxf_path_maps_to_dxf_content() { + let mut drawing = source_drawing(); + drawing.external_sources[0].location = IfccadExternalSourceLocation::UriReference { + uri: "../Refs/grondplan.dxf".into(), + }; + let (imported, _) = Ifcx.import_ifccad_preserving(&drawing).unwrap(); + let exported = Ifcx.export_ifccad(&imported.document).unwrap(); + assert_eq!(exported.drawing.external_sources.len(), 1); + assert_eq!( + exported.drawing.external_sources[0].source, + drawing.external_sources[0].source + ); +} diff --git a/tests/ifcx_xrefs.rs b/tests/ifcx_xrefs.rs index e4c0768..4458365 100644 --- a/tests/ifcx_xrefs.rs +++ b/tests/ifcx_xrefs.rs @@ -56,7 +56,10 @@ fn offline_xref_roundtrip_keeps_source_and_effective_placement() { base_point, fingerprint_guid, .. - } = &back.drawing.external_sources[0].source; + } = &back.drawing.external_sources[0].source + else { + panic!("DWG source expected") + }; assert_eq!(*base_point, [10., 20., 0.]); assert_eq!(fingerprint_guid.is_some(), keep_guid); let IfccadEntityKind::Xref(x) = &back.drawing.model.entities[0].as_native().unwrap().kind @@ -128,13 +131,10 @@ fn source_without_insert_is_retained_and_deletion_does_not_reuse_id() { #[test] fn unsupported_cad_attachment_variants_are_explicit_losses() { - for variant in ["overlay", "array", "paper", "block", "dxf-source"] { + for variant in ["overlay", "array", "paper", "block"] { let imported = Ifcx.import_ifccad(&drawing()).unwrap(); let mut cad = imported.document; let handle = *imported.entity_handles.get(&1000).unwrap(); - if variant == "dxf-source" { - cad.block_records.get_mut("grondplan").unwrap().xref_path = "source.dxf".into(); - } if variant == "overlay" { cad.block_records .get_mut("grondplan") From 219b49586abb89b3f00c2c6b286475cf6114aa99 Mon Sep 17 00:00:00 2001 From: BjornFidder Date: Fri, 20 Nov 2026 15:29:09 +0100 Subject: [PATCH 2/3] Retain typed IFCCAD XREF sources --- src/ifcx/external_sources.rs | 15 ++++++++ src/ifcx/mod.rs | 2 +- src/lib.rs | 2 + tests/ifcx_xref_ifccad_sources.rs | 63 +++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 tests/ifcx_xref_ifccad_sources.rs diff --git a/src/ifcx/external_sources.rs b/src/ifcx/external_sources.rs index c53e85b..72300c4 100644 --- a/src/ifcx/external_sources.rs +++ b/src/ifcx/external_sources.rs @@ -205,6 +205,21 @@ pub fn dxf_external_source_content( } } +/// Metadata for a native source projected into a host drawing temporarily. +/// Its own external-source graph is outside the nonrecursive source slice. +pub fn ifccad_external_source_content( + drawing: &ifccad::IfccadDocument, +) -> Result { + ifccad::validate_ifccad_document(drawing)?; + if !drawing.external_sources.is_empty() { + return Err("This IFCCAD source contains its own external references; nested XREF sources are not supported yet".into()); + } + Ok(ifccad::IfccadExternalSourceContent::Ifccad { + base_point: [0.; 3], + insertion_unit: Some(drawing.length_unit.clone()), + }) +} + /// Expected XREF identity from the physical feature/filename key, independently /// of the main drawing header and of the source's last-loaded session identity. pub fn dwg_xref_fingerprint_guid( diff --git a/src/ifcx/mod.rs b/src/ifcx/mod.rs index defd024..b1a620a 100644 --- a/src/ifcx/mod.rs +++ b/src/ifcx/mod.rs @@ -50,7 +50,7 @@ mod xref; pub use external_sources::{ dwg_external_source_content, dwg_xref_fingerprint_guid, dxf_external_source_content, external_source_location_from_cad_path, external_source_location_to_cad_path, - validate_cad_external_sources, + ifccad_external_source_content, validate_cad_external_sources, }; mod font; mod hatch; diff --git a/src/lib.rs b/src/lib.rs index 8b1c1f2..ddaca8a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -134,6 +134,8 @@ pub use ifcx::dwg_xref_fingerprint_guid; #[cfg(feature = "ifcx")] pub use ifcx::dxf_external_source_content; #[cfg(feature = "ifcx")] +pub use ifcx::ifccad_external_source_content; +#[cfg(feature = "ifcx")] pub use ifcx::validate_cad_external_sources; #[cfg(feature = "ifcx")] pub use ifcx::{external_source_location_from_cad_path, external_source_location_to_cad_path}; diff --git a/tests/ifcx_xref_ifccad_sources.rs b/tests/ifcx_xref_ifccad_sources.rs new file mode 100644 index 0000000..ab88a73 --- /dev/null +++ b/tests/ifcx_xref_ifccad_sources.rs @@ -0,0 +1,63 @@ +#![cfg(all(feature = "ifcx", feature = "mline-pilot"))] +use opencaddocument::{ifccad::*, Ifcx}; + +fn source() -> IfccadDocument { + let mut source = load_ifccad_bytes( + include_bytes!("fixtures/hello-cad.ifcx"), + Default::default(), + ) + .unwrap() + .into_document(); + source.length_unit = "m".into(); + source +} + +#[test] +fn native_source_metadata_has_zero_origin_and_no_invented_fingerprint() { + let source = source(); + let content = opencaddocument::ifccad_external_source_content(&source).unwrap(); + assert_eq!( + content, + IfccadExternalSourceContent::Ifccad { + base_point: [0.; 3], + insertion_unit: Some("m".into()), + } + ); + assert_eq!(content.fingerprint_guid(), None); +} + +#[test] +fn native_source_with_its_own_external_sources_is_refused() { + let source = load_ifccad_bytes( + include_bytes!("fixtures/xref/attachment.ifcx"), + Default::default(), + ) + .unwrap() + .into_document(); + let error = opencaddocument::ifccad_external_source_content(&source) + .unwrap_err() + .to_string(); + assert!(error.contains("external"), "{error}"); +} + +#[test] +fn native_source_association_survives_edit_projection_and_refuses_cad_export() { + let mut main = load_ifccad_bytes( + include_bytes!("fixtures/xref/attachment.ifcx"), + Default::default(), + ) + .unwrap() + .into_document(); + main.external_sources[0].source = + opencaddocument::ifccad_external_source_content(&source()).unwrap(); + let (imported, mut context) = Ifcx.import_ifccad_preserving(&main).unwrap(); + let saved = Ifcx + .export_ifccad_preserving(&imported.document, &mut context) + .unwrap(); + assert_eq!(saved.drawing.external_sources, main.external_sources); + assert!(Ifcx + .import_ifccad(&main) + .unwrap_err() + .to_string() + .contains("DWG")); +} From eca49894258e95b94d310deaf919fe17295aa20a Mon Sep 17 00:00:00 2001 From: BjornFidder Date: Fri, 20 Nov 2026 15:42:02 +0100 Subject: [PATCH 3/3] Pin staged native XREF source-format contract --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 78427e6..d287a08 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -450,7 +450,7 @@ dependencies = [ [[package]] name = "ifccad" version = "2026.47.0" -source = "git+https://github.com/OpenAEC-Foundation/ifccad.git?rev=96895aea607e3f2a54f9b286115844554f59df77#96895aea607e3f2a54f9b286115844554f59df77" +source = "git+https://github.com/OpenAEC-Foundation/ifccad.git?rev=cd79dfc81873f236c81df205088cb33ae4217960#cd79dfc81873f236c81df205088cb33ae4217960" dependencies = [ "base64", "fpmath", diff --git a/Cargo.toml b/Cargo.toml index 187d075..58bf95b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ acadrust = { git = "https://github.com/hakanaktt/acadrust.git", rev = "a7d378502 rmp-serde = { version = "=1.3.0", optional = true } serde = { version = "1", features = ["derive"], optional = true } # ifccad only reads and writes IFCX; the mapping to `Document` lives here. -ifccad = { git = "https://github.com/OpenAEC-Foundation/ifccad.git", rev = "96895aea607e3f2a54f9b286115844554f59df77", optional = true } +ifccad = { git = "https://github.com/OpenAEC-Foundation/ifccad.git", rev = "cd79dfc81873f236c81df205088cb33ae4217960", optional = true } web-time = { version = "1", optional = true } [dev-dependencies]