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
6 changes: 6 additions & 0 deletions src/io/dwg/dwg_document_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3856,6 +3856,12 @@ impl DwgDocumentBuilder {
}
document.header.handle_seed = document.header.handle_seed.max(next_handle);

// A seeded default the file has no record of - the Standard dimension
// style of a drawing that only uses ISO-25 - still names the seeded
// text style's handle, which the file may have given to a record of
// another table; point it at the file's text style of the same name.
crate::io::dxf::rewire_stale_text_style_references(document);

document.header.current_layer_name = document
.layers
.iter()
Expand Down
2 changes: 1 addition & 1 deletion src/io/dxf/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ mod writer;

pub use dxf_code::DxfCode;
pub use group_code_value::GroupCodeValueType;
pub(crate) use reader::{rewire_stale_text_style_references, DxfStreamReader, DxfTextReader};
pub use reader::{DxfReader, DxfReaderConfiguration};
pub(crate) use reader::{DxfStreamReader, DxfTextReader};
pub use writer::{value_type_for_code, write_binary_dxf, write_dxf};
pub use writer::{
DxfBinaryWriter, DxfOmission, DxfStreamWriter, DxfStreamWriterExt, DxfTextWriter, DxfWriter,
Expand Down
4 changes: 2 additions & 2 deletions src/io/dxf/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,8 +1007,8 @@ fn rewire_dangling_layout_block_records(document: &mut CadDocument) {
/// has since given to an unrelated record - e.g. the ByBlock linetype - and the
/// writer emits a `340`/`342` that consumers resolve to the wrong table
/// (issues #64 and #68). Re-point such stale handles at the file's text style
/// of the same name.
fn rewire_stale_text_style_references(document: &mut CadDocument) {
/// of the same name. The DWG reader seeds the same defaults and runs this too.
pub(crate) fn rewire_stale_text_style_references(document: &mut CadDocument) {
let text_style_handles: std::collections::HashSet<u64> = document
.text_styles
.iter()
Expand Down
97 changes: 97 additions & 0 deletions tests/dwg_seeded_dimstyle_text_style.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//! The DWG reader seeds a Standard dimension style before it reads the file.
//! When the file has none of its own, that seeded style survives, and its text
//! style must be the file's Standard text style - not whatever record the file
//! keeps at the handle the seed was wired to (the DXF reader's fix for #64).

use std::io::Cursor;

use acadrust::tables::DimStyle;
use acadrust::types::{DxfVersion, Handle};
use acadrust::{CadDocument, DwgReader, DwgWriter};

/// A drawing as other CAD programs save it: the Standard text style at #11,
/// the ByBlock linetype at #14 and ISO-25 as its only dimension style.
fn iso_only_dwg() -> Vec<u8> {
let mut doc = CadDocument::with_version(DxfVersion::AC1032);
doc.text_styles.get_mut("Standard").unwrap().handle = Handle::new(0x11);
doc.line_types.get_mut("ByBlock").unwrap().handle = Handle::new(0x14);
doc.dim_styles.remove("Standard").unwrap();
let mut iso = DimStyle::new("ISO-25");
iso.handle = doc.allocate_handle();
iso.dimtxsty_handle = Handle::new(0x11);
iso.dimtxsty = "Standard".to_string();
doc.header.current_dimstyle_handle = iso.handle;
doc.header.current_dimstyle_name = "ISO-25".to_string();
doc.header.dim_text_style_handle = Handle::new(0x11);
doc.dim_styles.add(iso).unwrap();
DwgWriter::write_to_vec(&doc).expect("write dwg")
}

fn read(bytes: Vec<u8>) -> CadDocument {
DwgReader::from_stream(Cursor::new(bytes))
.read()
.expect("read dwg")
}

/// Every dimension style's DIMTXSTY names a text style of the document.
fn assert_dim_styles_name_text_styles(doc: &CadDocument) {
let text_styles: Vec<Handle> = doc.text_styles.iter().map(|s| s.handle).collect();
for style in doc.dim_styles.iter() {
assert!(
text_styles.contains(&style.dimtxsty_handle),
"dimension style {} names {:?} as its text style, which is no text style \
(ByBlock linetype = {:?})",
style.name,
style.dimtxsty_handle,
doc.line_types.get("ByBlock").map(|l| l.handle),
);
}
}

#[test]
fn a_seeded_standard_dim_style_points_at_the_files_text_style() {
let loaded = read(iso_only_dwg());
assert!(
loaded.dim_styles.get("Standard").is_some(),
"the reader keeps its seeded Standard dimension style"
);
assert_dim_styles_name_text_styles(&loaded);
assert_eq!(
loaded.dim_styles.get("Standard").unwrap().dimtxsty_handle,
loaded.text_styles.get("Standard").unwrap().handle,
"the seeded style uses the file's Standard text style"
);
}

/// What a consumer sees: the drawing read and written again carries a
/// Standard dimension style whose DIMTXSTY is a text style.
#[test]
fn a_dwg_written_again_names_a_text_style_in_every_dim_style() {
let loaded = read(iso_only_dwg());
let again = read(DwgWriter::write_to_vec(&loaded).expect("write dwg again"));
assert_dim_styles_name_text_styles(&again);
assert_eq!(
again.dim_styles.get("ISO-25").unwrap().dimtxsty_handle,
again.text_styles.get("Standard").unwrap().handle,
"the file's own style keeps its text style"
);
}

/// The repair only re-points what names no text style: a file's own Standard
/// dimension style keeps the text style it names, even one not called Standard.
#[test]
fn a_files_own_standard_dim_style_keeps_its_text_style() {
let mut doc = CadDocument::with_version(DxfVersion::AC1032);
let mut romans = acadrust::tables::TextStyle::new("ROMANS");
romans.handle = doc.allocate_handle();
let romans_handle = romans.handle;
doc.text_styles.add(romans).unwrap();
let standard = doc.dim_styles.get_mut("Standard").unwrap();
standard.dimtxsty_handle = romans_handle;
standard.dimtxsty = "ROMANS".to_string();
let loaded = read(DwgWriter::write_to_vec(&doc).expect("write dwg"));
assert_eq!(
loaded.dim_styles.get("Standard").unwrap().dimtxsty_handle,
romans_handle
);
}
Loading