From a304a1018874dba5d2a57f74940e73d1f570af8e Mon Sep 17 00:00:00 2001 From: schoeller Date: Tue, 24 Nov 2026 22:14:13 +0100 Subject: [PATCH] Civil: read the point databases the application writes The point database reader decoded no application drawing: it required the per-record marker short to be exactly 0x0420 (this crate's records) where the application writes 0x0421, or 0x0621 for points a survey created, and it expected an empty list where the application's records carry a leading tagged list before the named points. Both forms are documented and read; this crate's own records keep an empty leading list, so its writer is unchanged and its files read as before. Over the whole tutorial corpus this is 53,521 of 53,522... (continued) Full message in PR body. --- src/classes/civil/cogo.rs | 44 ++++++++++---- tests/civil_read_cogo_points_app.rs | 90 +++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 tests/civil_read_cogo_points_app.rs diff --git a/src/classes/civil/cogo.rs b/src/classes/civil/cogo.rs index 4ec84f41..c882ac76 100644 --- a/src/classes/civil/cogo.rs +++ b/src/classes/civil/cogo.rs @@ -12,20 +12,28 @@ //! //! ```text //! BS 0x04BE, BL 34 (its key in the root tree node), BL 0, BL 0, 3 bits (010) -//! BL count, count × { BL 1, 3BD position, BL number, BS 0x0420 } +//! BL count, count × { BL 1, 3BD position, BL number, BS 0x0420 marker } +//! (the marker is `0x0420` in this crate's records; the application +//! writes `0x0421`, and `0x0621` for points a survey created) //! BL n, n × BL number points with their own point style //! BL m, m × BL number points with their own label style -//! BL 0 +//! BL jA, jA × { BL number, BL tag } the leading tagged list (see +//! `tagged_leading` below; empty in this crate's records) //! BL j, j × { BL number, BL name tag } named points //! BL j, j × { BL name tag, BL number } the same, keyed by name //! BL k, k × { BL number, BL description tag } //! 13 × slot: BL e, e × { BL key | 0x40000000, BL c, c × { BL number, value } } //! handles: n × point style, m × label style, the AeccDbCogoPoint class node -//! strings: "", the names of the j points, the same names again, the raw -//! description of each of the k points in order, then for each -//! slot entry its property's key text and its text values +//! strings: "", the texts of the leading tagged list in order, the names of +//! the j points, the same names again, the raw description of each +//! of the k points in order, then for each slot entry its +//! property's key text and its text values //! ``` //! +//! The leading tagged list follows the styles in every record the +//! application writes (the crate reads it and keeps nothing from it yet); +//! this crate's own records place an empty one there. +//! //! The slots hold the values of user-defined properties by kind: slot 4 //! doubles (BD), 5 booleans (B; RC in the definitions), 6 texts (none inline: each text follows //! the key text), 7 integers (BL); the others are empty in every sample. @@ -231,11 +239,17 @@ pub(super) fn entity_number(data: &[u8], start: usize, end: usize) -> Option 0) } +/// Flag bits a point record's marker short carries beyond its fixed +/// `0x0420` base: `0x0001` the application sets on its points, `0x0200` +/// points a survey created (both observed over the whole tutorial +/// corpus; the record's layout does not change with them). +const RECORD_MARKER_FLAGS: i16 = 0x0201; + fn record(bits: &mut Bits) -> Option<(u32, Vector3)> { (bits.bit_long()? == 1).then_some(())?; let position = Vector3::new(bits.bit_double()?, bits.bit_double()?, bits.bit_double()?); let number = u32::try_from(bits.bit_long()?).ok().filter(|n| *n > 0)?; - (bits.bit_short()? == 0x0420).then_some(())?; + (bits.bit_short()? & !RECORD_MARKER_FLAGS == 0x0420).then_some(())?; Some((number, position)) } @@ -249,6 +263,8 @@ struct PointDatabase { records: Vec<(u32, Vector3)>, styled: Vec, labelled: Vec, + /// The leading tagged list (numbers; its texts precede the names). + tagged_leading: Vec, named: Vec, /// Length of the list after the names (the same names keyed by text). again: usize, @@ -277,7 +293,7 @@ fn database(bits: &mut Bits) -> Option { let records = (0..count).map(|_| record(bits)).collect::>>()?; let styled = numbers(bits)?; let labelled = numbers(bits)?; - (bits.bit_long()? == 0).then_some(())?; + let tagged_leading = tagged(bits)?; let named = tagged(bits)?; // The same names keyed by text: { BL tag, BL number }. let again = usize::try_from(bits.bit_long()?).ok().filter(|n| *n <= bits.remaining() / 4)?; @@ -306,7 +322,7 @@ fn database(bits: &mut Bits) -> Option { values.push((key, list)); } } - Some(PointDatabase { records, styled, labelled, named, again, described, values }) + Some(PointDatabase { records, styled, labelled, tagged_leading, named, again, described, values }) } /// Every point of a point database, in record order. `handles` and @@ -316,7 +332,8 @@ pub(super) fn points(data: &[u8], start: usize, end: usize, handles: &[u64], str return Vec::new(); }; // Each value entry: its key text, then its text values. - let mut texts = strings.iter().skip(1 + db.named.len() + db.again + db.described.len()).cloned(); + let text_base = 1 + db.tagged_leading.len() + db.named.len() + db.again + db.described.len(); + let mut texts = strings.iter().skip(text_base).cloned(); let mut values: Vec<(String, Vec<(u32, PropertyValue)>)> = Vec::new(); for (_, list) in &db.values { let Some(key) = texts.next() else { return Vec::new() }; @@ -343,9 +360,14 @@ pub(super) fn points(data: &[u8], start: usize, end: usize, handles: &[u64], str .described .iter() .position(|n| *n == number) - .and_then(|i| strings.get(1 + db.named.len() + db.again + i)) + .and_then(|i| strings.get(text_base - db.described.len() + i)) + .cloned(), + name: db + .named + .iter() + .position(|n| *n == number) + .and_then(|i| strings.get(1 + db.tagged_leading.len() + i)) .cloned(), - name: db.named.iter().position(|n| *n == number).and_then(|i| strings.get(1 + i)).cloned(), properties: values .iter() .filter_map(|(key, list)| list.iter().find(|(n, _)| *n == number).map(|(_, v)| (key.clone(), v.clone()))) diff --git a/tests/civil_read_cogo_points_app.rs b/tests/civil_read_cogo_points_app.rs new file mode 100644 index 00000000..1cf6650a --- /dev/null +++ b/tests/civil_read_cogo_points_app.rs @@ -0,0 +1,90 @@ +//! The point database of drawings the application wrote. +//! +//! Two record forms the application writes and this crate's writer does +//! not (the D016 packet-1 read gap W1-5): the per-record marker short is +//! `0x0421` (and `0x0621` for points a survey created), not the `0x0420` +//! this crate writes, and the database carries a leading tagged list +//! before the named points where the crate's records keep an empty one. +//! Without both, `cogo_points` decoded no application point database at +//! all (0 of 538 in Survey-3.dwg, 0 of 2405 in Points-4a.dwg). + +use acadrust::classes::civil::{cogo_points, CogoPoint}; +use acadrust::DwgReader; + +/// The application's tutorial drawing, from `ACADRUST_CIVIL_TUTORIALS_DIR`. +fn app_drawing(name: &str) -> Option { + let dir = std::env::var_os("ACADRUST_CIVIL_TUTORIALS_DIR")?; + let path = std::path::Path::new(&dir).join(name); + if !path.exists() { + return None; + } + Some(DwgReader::from_file(&path).unwrap().read().unwrap()) +} + +fn by_number<'a>(points: &'a [CogoPoint], number: u32) -> &'a CogoPoint { + points.iter().find(|p| p.number == number).unwrap_or_else(|| panic!("point {number} missing")) +} + +/// Survey-3.dwg: 538 points a survey created (marker `0x0621`, a leading +/// tagged list of 0, raw descriptions of all points, names of none). +/// Positions and raw descriptions of the application's oracle reads +/// (execute_civil_code `CogoPoint.Easting/Northing/Elevation/RawDescription`, +/// 2026-11-24). +#[test] +fn survey_created_points_decode() { + let Some(doc) = app_drawing("Survey-3.dwg") else { return }; + let points = cogo_points(&doc); + assert_eq!(points.len(), 538); + let p = by_number(&points, 456); + assert_eq!(p.position.x, 5675.002124308634); + assert_eq!(p.position.y, 5040.000833228553); + assert_eq!(p.position.z, 255.95998686447933); + assert_eq!(p.name, None); + let p = by_number(&points, 816); + assert_eq!(p.position.x, 5518.053784082379); + assert_eq!(p.position.y, 5445.828551114028); + assert_eq!(p.position.z, 247.7690529209087); + // every point has a raw description, none has a name + assert_eq!(points.iter().filter(|p| p.description.is_some()).count(), 538); + assert_eq!(points.iter().filter(|p| p.name.is_some()).count(), 0); +} + +/// Points-4a.dwg: 2405 points of the create-points commands (marker +/// `0x0421`, a leading tagged list of 51, raw descriptions of all points, +/// names of none). The first three points exactly as the application +/// reads them (execute_civil_code, D017 packet 2, 2026-11-24: +/// E=19857.2577 N=20260.4556 Z=100.3058 name= etc.). +#[test] +fn create_points_commands_points_decode() { + let Some(doc) = app_drawing("Points-4a.dwg") else { return }; + let points = cogo_points(&doc); + assert_eq!(points.len(), 2405); + for (number, x, y, z) in [ + (1, 19857.2577, 20260.4556, 100.3058), + (2, 19875.4801, 20207.7198, 101.0929), + (3, 19887.3953, 20692.713, 96.4194), + ] { + let p = by_number(&points, number); + assert_eq!(p.position.x, x, "point {number} easting"); + assert_eq!(p.position.y, y, "point {number} northing"); + assert_eq!(p.position.z, z, "point {number} elevation"); + assert_eq!(p.name, None, "point {number} name"); + } +} + +/// The same database in the drawing the application saves the points +/// tutorial's work into keeps all 2405 points, and a drawing where the +/// points carry their own label styles (Points-4a: 51) maps them. +#[test] +fn point_label_styles_and_counts_hold_across_the_tutorial_set() { + for (name, count) in [("Points-2.dwg", 2405), ("Points-3.dwg", 2405), ("Points-4e.dwg", 2405), ("Survey-2B.dwg", 538)] { + let Some(doc) = app_drawing(name) else { continue }; + let points = cogo_points(&doc); + assert_eq!(points.len(), count, "{name}"); + assert!(points.iter().all(|p| p.position.x.is_finite() && p.number > 0), "{name}"); + } + let Some(doc) = app_drawing("Points-4a.dwg") else { return }; + let points = cogo_points(&doc); + let styled_labels = points.iter().filter(|p| p.label_style != 0).count(); + assert_eq!(styled_labels, 51, "Points-4a label styles"); +}