Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ acadrust = { git = "https://github.com/hakanaktt/acadrust.git", rev = "14f7da106
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]
Expand Down
5 changes: 4 additions & 1 deletion examples/xref_review.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
}
Expand Down
52 changes: 43 additions & 9 deletions src/ifcx/external_sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,55 @@ pub fn validate_cad_external_sources(document: &crate::Document) -> Result<(), E
Ok(())
}

fn cad_source_metadata(document: &crate::Document) -> ([f64; 3], Option<String>, Option<String>) {
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,
}
}

/// 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::IfccadExternalSourceContent, Error> {
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
Expand Down
16 changes: 14 additions & 2 deletions src/ifcx/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
ifccad_external_source_content, validate_cad_external_sources,
};
mod font;
mod hatch;
Expand Down Expand Up @@ -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<IfccadImportOutcome, Error> {
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")]
Expand Down
16 changes: 12 additions & 4 deletions src/ifcx/multi_leader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,11 +562,19 @@ fn text_out(
cad::TextAttachmentPointType::TopCenter => acadrust::entities::AttachmentPoint::TopCenter,
cad::TextAttachmentPointType::TopRight => acadrust::entities::AttachmentPoint::TopRight,
cad::TextAttachmentPointType::MiddleLeft => acadrust::entities::AttachmentPoint::MiddleLeft,
cad::TextAttachmentPointType::MiddleCenter => acadrust::entities::AttachmentPoint::MiddleCenter,
cad::TextAttachmentPointType::MiddleRight => acadrust::entities::AttachmentPoint::MiddleRight,
cad::TextAttachmentPointType::MiddleCenter => {
acadrust::entities::AttachmentPoint::MiddleCenter
}
cad::TextAttachmentPointType::MiddleRight => {
acadrust::entities::AttachmentPoint::MiddleRight
}
cad::TextAttachmentPointType::BottomLeft => acadrust::entities::AttachmentPoint::BottomLeft,
cad::TextAttachmentPointType::BottomCenter => acadrust::entities::AttachmentPoint::BottomCenter,
cad::TextAttachmentPointType::BottomRight => acadrust::entities::AttachmentPoint::BottomRight,
cad::TextAttachmentPointType::BottomCenter => {
acadrust::entities::AttachmentPoint::BottomCenter
}
cad::TextAttachmentPointType::BottomRight => {
acadrust::entities::AttachmentPoint::BottomRight
}
};
m.drawing_direction = match value.text_flow_direction {
cad::FlowDirectionType::Horizontal => acadrust::entities::DrawingDirection::LeftToRight,
Expand Down
41 changes: 16 additions & 25 deletions src/ifcx/xref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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,
Expand All @@ -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,
}
},
}
};
Expand Down
4 changes: 4 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ 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::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};
63 changes: 63 additions & 0 deletions tests/ifcx_xref_ifccad_sources.rs
Original file line number Diff line number Diff line change
@@ -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"));
}
Loading
Loading