diff --git a/crates/doc/src/entity/fill.rs b/crates/doc/src/entity/fill.rs index 7a7799a55..fef6bc513 100644 --- a/crates/doc/src/entity/fill.rs +++ b/crates/doc/src/entity/fill.rs @@ -7,8 +7,9 @@ pub struct HatchLoop { pub vertices: Vec, #[serde(default)] pub outer: bool, - /// Original DXF group 92 bits. Writing keeps only Derived (4), Textbox (8) and - /// Outermost (16), derives External (1) from `outer`, and sets Polyline (2). + /// Original DXF group 92 bits. Writing keeps only Textbox (8) and Outermost (16), derives + /// External (1) from `outer`, and sets Polyline (2). Derived (4) stays here but is not + /// written: the pixel size (group 47) it needs is not kept (#3664). /// None uses the flags of a locally created loop. #[serde(default, skip_serializing_if = "Option::is_none")] pub boundary_flags: Option, diff --git a/crates/io/src/dxf_write/entity.rs b/crates/io/src/dxf_write/entity.rs index c55e91a2b..1824d76b1 100644 --- a/crates/io/src/dxf_write/entity.rs +++ b/crates/io/src/dxf_write/entity.rs @@ -662,7 +662,11 @@ fn write_hatch_entity(w: &mut W, d: &Drawing, e: &Entity, owner: &str, paper: bo w.i(71, i64::from(h.associative)); w.i(91, h.loops.len() as i64); for l in &h.loops { - w.i(92, 2 | i64::from(l.outer) | (l.boundary_flags.unwrap_or(0) & (4 | 8 | 16))); + // Polyline (2), External (1) from `outer`, and Textbox (8) and Outermost (16) as read. + // Not Derived (4): a derived path needs the hatch's pixel size (group 47) before its + // seed points, and a hatch does not keep one. As reported in #3664, AutoCAD refuses a + // file with the bit and no size, and draws pattern fills solid with a stand-in size. + w.i(92, 2 | i64::from(l.outer) | (l.boundary_flags.unwrap_or(0) & (8 | 16))); let has_bulge = l.vertices.iter().any(|v| v.bulge != 0.0); w.i(72, i64::from(has_bulge)); w.i(73, 1); diff --git a/crates/io/tests/it/hatch_loop_flags.rs b/crates/io/tests/it/hatch_loop_flags.rs index 71e8e8cf3..0c3ecbb62 100644 --- a/crates/io/tests/it/hatch_loop_flags.rs +++ b/crates/io/tests/it/hatch_loop_flags.rs @@ -58,11 +58,17 @@ fn flags_written(drawing: &Drawing) -> Vec { .collect() } +/// The group 92 of a path read with `flags`, as saved: Polyline set, and Derived cleared because +/// the pixel size it needs is not kept (#3664). +fn saved(flags: i64) -> i64 { + (flags | 2) & !4 +} + #[test] fn boundary_flags_survive_dxf_open_and_save() { for flags in [[7, 22, 7, 6], [7, 22, 6, 20], [0, 9, 16, 31]] { let drawing = read_dxf(logo(flags).as_bytes()).unwrap(); - let expected = flags.map(|flag| flag | 2); + let expected = flags.map(saved); assert_eq!(flags_written(&drawing), expected); let back = read_dxf(write_dxf(&drawing).as_bytes()).unwrap(); assert_eq!(flags_written(&back), expected); @@ -92,7 +98,7 @@ fn reclassified_loops_write_current_external_bit() { }); let written = flags_written(&drawing)[1]; assert_eq!(written & 1, 0); - assert_eq!(written & (4 | 16), imported & (4 | 16)); + assert_eq!(written & (4 | 16), imported & 16); assert_ne!(written & 2, 0); let back = read_dxf(write_dxf(&drawing).as_bytes()).unwrap(); assert_eq!(flags_written(&back), flags_written(&drawing)); @@ -103,7 +109,7 @@ fn reclassified_loops_write_current_external_bit() { fn undocumented_boundary_bits_are_not_written_back() { let flags = [i64::MAX, -1, 32 | 7, i64::MIN | 22]; let drawing = read_dxf(logo(flags).as_bytes()).unwrap(); - let expected = flags.map(|flag| (flag & 31) | 2); + let expected = flags.map(|flag| saved(flag & 31)); assert_eq!(flags_written(&drawing), expected); let back = read_dxf(write_dxf(&drawing).as_bytes()).unwrap(); assert_eq!(flags_written(&back), expected); @@ -117,7 +123,7 @@ fn boundary_flags_survive_dwg_open_and_both_save_formats() { let drawing = read_dxf(logo(flags).as_bytes()).unwrap(); let bytes = cadcraft_io::write(&drawing, "logo.dwg").unwrap(); let back = cadcraft_io::read(&bytes, "logo.dwg").unwrap(); - let expected = flags.map(|flag| flag | 2); + let expected = flags.map(saved); assert_eq!(flags_written(&back), expected); let again = cadcraft_io::write(&back, "logo.dwg").unwrap(); assert_eq!(flags_written(&cadcraft_io::read(&again, "logo.dwg").unwrap()), expected); @@ -143,3 +149,97 @@ fn old_serialized_loops_keep_locally_created_flags() { } assert_eq!(flags_written(&drawing), expected); } + +/// The first of the four LINEs in [`derived_hatches`]. +const LINES: u64 = 0x1A0; + +/// Invented geometry: a 30 by 20 rectangle drawn as four LINEs and two hatches on it. The first +/// is a NET pattern linked to the lines (71 = 1, 97 = 4), with a derived outer path (92 = 7), a +/// derived edge-path island (92 = 20) and its pixel size (47) after the pattern lines. The second +/// is solid and not linked, with one derived edge path (92 = 5) and a pixel size. +fn derived_hatches() -> String { + let corners = [(0.0, 0.0), (30.0, 0.0), (30.0, 20.0), (0.0, 20.0)]; + let island = [(10.0, 5.0), (20.0, 5.0), (20.0, 15.0), (10.0, 15.0)]; + let mut text = String::from("0\nSECTION\n2\nHEADER\n9\n$ACADVER\n1\nAC1015\n0\nENDSEC\n0\nSECTION\n2\nENTITIES\n"); + for (index, (start, end)) in (0..).zip(corners.iter().zip(corners.iter().cycle().skip(1))) { + text.push_str(&format!( + "0\nLINE\n5\n{:X}\n100\nAcDbEntity\n8\n0\n100\nAcDbLine\n10\n{}\n20\n{}\n30\n0\n11\n{}\n21\n{}\n31\n0\n", + LINES + index, + start.0, + start.1, + end.0, + end.1 + )); + } + let head = |handle: &str, pattern: &str, solid: i64, linked: i64, paths: usize| { + format!( + "0\nHATCH\n5\n{handle}\n100\nAcDbEntity\n8\n0\n100\nAcDbHatch\n10\n0\n20\n0\n30\n0\n210\n0\n220\n0\n230\n1\n2\n{pattern}\n70\n{solid}\n71\n{linked}\n91\n{paths}\n" + ) + }; + text.push_str(&head("1A4", "NET", 0, 1, 2)); + text.push_str(boundary(&corners, 7).strip_suffix("97\n0\n").unwrap()); + text.push_str("97\n4\n"); + for index in 0..4 { + text.push_str(&format!("330\n{:X}\n", LINES + index)); + } + text.push_str(&boundary(&island, 20)); + text.push_str("75\n0\n76\n1\n52\n0\n41\n2\n77\n0\n78\n2\n"); + text.push_str("53\n0\n43\n0\n44\n0\n45\n0\n46\n2.5\n79\n0\n53\n90\n43\n0\n44\n0\n45\n-2.5\n46\n0\n79\n0\n47\n1.5\n98\n0\n"); + text.push_str(&head("1A5", "SOLID", 1, 0, 1)); + text.push_str(&boundary(&corners, 5)); + text.push_str("75\n0\n76\n1\n47\n1.5\n98\n0\n0\nENDSEC\n0\nEOF\n"); + text +} + +/// The (code, value) pairs of each HATCH record in a DXF text. +fn hatch_records(text: &str) -> Vec> { + let lines: Vec<_> = text.lines().collect(); + let mut records: Vec> = Vec::new(); + let mut hatch = false; + for pair in lines.as_chunks::<2>().0 { + let code: i32 = pair[0].trim().parse().unwrap(); + let value = pair[1].trim().to_string(); + if code == 0 { + hatch = value == "HATCH"; + if hatch { + records.push(Vec::new()); + } + } else if hatch && let Some(record) = records.last_mut() { + record.push((code, value)); + } + } + records +} + +/// The values of `code` in a HATCH record, from its boundary paths (group 91) on. +fn path_values(record: &[(i32, String)], code: i32) -> Vec { + record.iter().skip_while(|(group, _)| *group != 91).filter(|(group, _)| *group == code).map(|(_, value)| value.clone()).collect() +} + +/// A derived path (92 bit 4) needs the hatch's pixel size (47), which is not kept (#3664). The +/// path is saved without the bit and no stand-in size is written; the hatch keeps its other path +/// bits, its pattern and its link to the lines, through a second save too. +#[test] +fn derived_paths_are_saved_without_the_bit_or_a_stand_in_pixel_size() { + let drawing = read_dxf(derived_hatches().as_bytes()).unwrap(); + let first = write_dxf(&drawing); + let again = write_dxf(&read_dxf(first.as_bytes()).unwrap()); + for text in [first, again] { + let hatches = hatch_records(&text); + assert_eq!(hatches.len(), 2); + for hatch in &hatches { + let derived = path_values(hatch, 92).iter().any(|flags| flags.parse::().unwrap() & 4 != 0); + assert!(!derived, "{hatch:?}"); + assert!(!hatch.iter().any(|(code, _)| *code == 47), "{hatch:?}"); + } + assert_eq!(path_values(&hatches[0], 92), [saved(7), saved(20)].map(|flags| flags.to_string())); + assert_eq!(path_values(&hatches[1], 92), [saved(5).to_string()]); + // Still a NET pattern fill with its two lines, and still linked to the lines it was made from. + assert!(hatches[0].contains(&(2, "NET".to_string())) && hatches[0].contains(&(70, "0".to_string())), "{:?}", hatches[0]); + assert_eq!(path_values(&hatches[0], 78), ["2"]); + assert!(hatches[0].contains(&(71, "1".to_string())), "{:?}", hatches[0]); + assert_eq!(path_values(&hatches[0], 97), ["4", "0"]); + let lines: Vec = (0..4).map(|index| format!("{:X}", LINES + index)).collect(); + assert_eq!(path_values(&hatches[0], 330), lines); + } +}