diff --git a/docs/public-api.md b/docs/public-api.md index ceb7c03..c4f7381 100644 --- a/docs/public-api.md +++ b/docs/public-api.md @@ -80,8 +80,8 @@ offset or line number. A warning never contains text from the file. Warnings are | Type | Use | | --- | --- | -| `TableSection` | A section of fixed-size rows, from `Db1Document.ReadTables()` or `DbvDocument.Sections`. `Ordinal`, `Offset`, `End`, `Key` (schema table number in `.db1`, else null), `RowSize`, `Descriptors` (primary and secondary index specifications), `RowsOffset` and `Rows`. `Rows` is a view over the data: a row is built when it is read. | -| `TableRow` | `Offset` (of the marker byte), `Marker` (0x04 active, 0x0C deleted/superseded in `.db1`), `Body` (`RowSize` raw bytes), `A` / `CreationTimeId` (creation transaction TimeId) and `B` / `DeletionTimeId` (deletion or superseding transaction TimeId). `IsActive` and `IsDeleted` inspect the marker. | +| `TableSection` | A section of fixed-size rows, from `Db1Document.ReadTables()` or `DbvDocument.Sections`. `Ordinal`, `Offset`, `End`, `Key` (schema table number in `.db1`, else null), `RowSize`, `Descriptors` (primary and secondary index specifications), `RowsOffset` and `Rows`. `Rows` is a view over the data: a row is built when it is read. Typed bulk decoders (`AsObjects()`, `AsParts()`, `AsPoints()`, etc.) decode all rows into typed record structs. | +| `TableRow` | `Offset` (of the marker byte), `Marker` (0x04 active, 0x0C deleted/superseded in `.db1`), `Body` (`RowSize` raw bytes), `A` / `CreationTimeId` (creation transaction TimeId) and `B` / `DeletionTimeId` (deletion or superseding transaction TimeId). `IsActive` and `IsDeleted` inspect the marker. Typed decoders (`AsObject()`, `AsPart()`, `AsPoint()`, etc.) decode the row body into typed record structs. | | `DbvDocument` | A DBV@ file: `Name`, `DomainId` (`Prologue[1]`), `Generation` (`ListHeader[3]`), `BaseOffset` (`B`), `EventLevel` (`C`), `CurrentOffset` (`T[1]`), `LastEventOffset` (`T[4]`), `Prologue`, `ListHeader`, `GuidRecords`, `B`, `C`, `L1`, `L2`, `T`, `C9Records`, `Sections` and `Payload`. The raw numbers are preserved; named aliases identify the domain, generation and event offsets. | | `DbvGuidRecord` | `Value` (raw), `Index` (1-based, checked against the position) and `Id`. | | `DbvC9Record` | A record that starts with `C9 C0 CE DB`: four raw numbers `R0` to `R3`. | @@ -90,6 +90,34 @@ The walk of a model database starts at its first table section and must end exac without a key after one with a key, a gap or bytes after the last section is an error. The bytes before the first section, other than the header and the id map, are not interpreted; read them from `Payload`. +## Decoded table rows + +Typed record structs decoded from the row bodies of the most common model database table sections: + +| Type | Table Key | Size | Contents | +| --- | --- | --- | --- | +| `ObjectRow` | 1 | 72 | `Id`, `ParentId` (`kuuluu`), `AssemblyId`, `Guid` (128-bit RFC 4122), `Type`, `Subtype`, `ExternalType`, `Mask1`, `Mask2`, `ObjFlag`, `OwnerId`, `PrivilegeId`. | +| `AssemblyRow` | 2 | 92 | `Id`, `Type`, `Class`, `MainPartId` (`dum`), `Lot`, `Name`, `Flags`, `MainPartByUser`. | +| `CoordsysRow` | 3 | 40 | `Id`, `AttrId`, origin coordinates `X`, `Y`, `Z`, `Length`. | +| `PartRow` | 10 | 64 | `Id`, `PartAttrId`, `PositioningAttrId`, `StartPointId` (`p1`), `EndPointId` (`p2`), `GeometryTreeRootId`, `CoordsysAttrId`, `Reserved`, local coordinate system `CoordsysX`, `CoordsysY`, `CoordsysZ`, `Length`. | +| `PointRow` | 13 | 32 | `Id`, `Visibility`, 3D coordinates `X`, `Y`, `Z` in millimeters. | +| `RelationRow` | 16 | 60 | `Id`, `Type`, `Id1`, `Id2`, `Guid` (string), `Flags`. | +| `WeldingRow` | 20 | 24 | `Id`, `CommonAttrId`, `Seam1Id`, `Seam2Id`, `CompoundSeam1Id`, `CompoundSeam2Id`. | +| `NumAttrRow` | 38 | 24 | `Id`, `AttrId`, `FatherId`, `OwnerId`, `Flags`, `NameId`. | +| `StrAttrRow` | 39 | 24 | `Id`, `AttrId`, `FatherId`, `OwnerId`, `Flags`, `NameId`. | +| `PartAttrRow` | 44 | 372 | `Id`, `ObjType`, `FormType`, `ObjClass`, `AssStartNo`, `Flags`, `NPoints`, `PartFlags`, `Prefix`, `Name`, `Profile`, `ParameterStringId`, `AssemblyPosition`, `AssemblyName`, `Material`, `Finish`, `AssemblyType`, `PourPhase`. | +| `NumAttrAttrRow` | 48 | 44 | `Id`, `Type`, `Value` (double), `Name`, `PrivilegeId`. | +| `StrAttrAttrRow` | 49 | 120 | `Id`, `Type`, `Name`, `Value` (string), `ValueId`, `PrivilegeId`. | +| `StringPoolRow` | 50 | 45 | `Id`, `NextId`, `HashId`, `RefCount`, `Text`. | +| `PartNumberingRow` | 94 | 68 | `Id`, `SeriesId`, `StartNumber`, `FPos`, `MultiFPos`, `Prefix`. | +| `AssemblyNumberingRow` | 95 | 76 | `Id`, `SeriesId`, `StartNumber`, `FPos`, `FamilyFPos`, `QualifierFPos`, `MultiFPos`, `Prefix`. | +| `ObjectNumberingRow` | 105 | 12 | `Id`, `NumberId`, `NumberingGroup`. | +| `BoltRow` | 107 | 24 | `Id`, `BoltAttrId`, `PositioningAttrId`, `FirstPointId` (`p1`), `SecondPointId` (`p2`), `PolygonId`. | +| `ObjectModificationRow` | 132 | 20 | `ModificationId`, `Id`, `ModType`, `DependencyId`, `ModTime`. | +| `BoltHoleRow` | 140 | 48 | `BoltId`, `No`, `PartId`, `PointZ`, `Length`, `LongHoleDir`, `LongHoleType`, `ShorteningLength`. | +| `ObjectPhaseRow` | 141 | 8 | `ObjectId`, `PhaseNumber`. | +| `BoltHolePointsRow` | 142 | 60 | `BoltId`, intermediate coordinates `IntPoint1X`, `IntPoint1Y`, `IntPoint1Z`, `IntPoint2X`, `IntPoint2Y`, `IntPoint2Z`, `NumHoles`. | + ## Files next to the model | Type | Use | diff --git a/src/TdbIO/Internal/Bytes.cs b/src/TdbIO/Internal/Bytes.cs index 1bc77c2..0de7ada 100644 --- a/src/TdbIO/Internal/Bytes.cs +++ b/src/TdbIO/Internal/Bytes.cs @@ -1,4 +1,5 @@ using System.Buffers.Binary; +using System.Text; namespace TdbIO.Internal; @@ -37,4 +38,40 @@ public static uint U32BE(ReadOnlySpan d, long at) Need(at, 4, d.Length, "u32"); return BinaryPrimitives.ReadUInt32BigEndian(d[(int)at..]); } + + public static double F64(ReadOnlySpan d, long at) + { + Need(at, 8, d.Length, "f64"); + return BinaryPrimitives.ReadDoubleLittleEndian(d[(int)at..]); + } + + public static float F32(ReadOnlySpan d, long at) + { + Need(at, 4, d.Length, "f32"); + return BinaryPrimitives.ReadSingleLittleEndian(d[(int)at..]); + } + + public static Guid Guid(ReadOnlySpan d, long at) + { + Need(at, 16, d.Length, "guid"); + return new Guid(d.Slice((int)at, 16), bigEndian: true); + } + + private static readonly UTF8Encoding StrictUtf8 = new(encoderShouldEmitUTF8Identifier: false, throwOnInvalidBytes: true); + + public static string AsciiString(ReadOnlySpan d, long at, int length) + { + Need(at, length, d.Length, "string"); + var span = d.Slice((int)at, length); + int nul = span.IndexOf((byte)0); + if (nul >= 0) span = span[..nul]; + try + { + return StrictUtf8.GetString(span); + } + catch (DecoderFallbackException e) + { + throw FormatError.At("invalid UTF-8 string", at + Math.Max(0, e.Index), e); + } + } } diff --git a/src/TdbIO/ModelRows.cs b/src/TdbIO/ModelRows.cs new file mode 100644 index 0000000..9c0526e --- /dev/null +++ b/src/TdbIO/ModelRows.cs @@ -0,0 +1,812 @@ +using TdbIO.Internal; + +namespace TdbIO; + +/// Decoded row of table 1 (object, size 72): object hierarchy, GUID, type, and status flags. +/// The unique object identifier (primary key). +/// The parent object identifier (kuuluu). +/// The assembly identifier the object belongs to. +/// The 128-bit RFC 4122 GUID of the object. +/// The object type code (e.g. 1 for part, 2 for assembly). +/// The object subtype code. +/// The external type GUID. +/// The first mask word. +/// The second mask word. +/// The object status flags. +/// The owner identifier. +/// The privilege or access control identifier. +public readonly record struct ObjectRow( + int Id, + int ParentId, + int AssemblyId, + Guid Guid, + int Type, + int Subtype, + Guid ExternalType, + int Mask1, + int Mask2, + int ObjFlag, + int OwnerId, + int PrivilegeId) +{ + /// The expected row body size in bytes: 72. + public const int ExpectedRowSize = 72; + + /// The table key in model database sections: 1. + public const int TableKey = 1; + + /// Decodes a row body into an . + public static ObjectRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.Guid(body, 12), + Bytes.I32(body, 28), + Bytes.I32(body, 32), + Bytes.Guid(body, 36), + Bytes.I32(body, 52), + Bytes.I32(body, 56), + Bytes.I32(body, 60), + Bytes.I32(body, 64), + Bytes.I32(body, 68)); + } + + /// Decodes the body of into an . + public static ObjectRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 2 (assembly, size 92): assembly definition and main part reference. +/// The assembly identifier (primary key). +/// The assembly type code. +/// The assembly class code. +/// The identifier of the main part (dum). +/// The lot/tracking number. +/// The assembly name (e.g. "BEAM", "COLUMN", "SLAB"). +/// Assembly flags. +/// Whether the main part was set explicitly by the user. +public readonly record struct AssemblyRow( + int Id, + int Type, + int Class, + int MainPartId, + int Lot, + string Name, + int Flags, + int MainPartByUser) +{ + /// The expected row body size in bytes: 92. + public const int ExpectedRowSize = 92; + + /// The table key in model database sections: 2. + public const int TableKey = 2; + + /// Decodes a row body into an . + public static AssemblyRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.I32(body, 16), + Bytes.AsciiString(body, 20, 64), + Bytes.I32(body, 84), + Bytes.I32(body, 88)); + } + + /// Decodes the body of into an . + public static AssemblyRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 3 (coordsys, size 40): coordinate system origin and length. +/// The coordinate system identifier (primary key). +/// The coordinate system attribute identifier. +/// Origin X coordinate. +/// Origin Y coordinate. +/// Origin Z coordinate. +/// Coordinate system length or scale factor. +public readonly record struct CoordsysRow(int Id, int AttrId, double X, double Y, double Z, double Length) +{ + /// The expected row body size in bytes: 40. + public const int ExpectedRowSize = 40; + + /// The table key in model database sections: 3. + public const int TableKey = 3; + + /// Decodes a row body into a . + public static CoordsysRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.F64(body, 8), + Bytes.F64(body, 16), + Bytes.F64(body, 24), + Bytes.F64(body, 32)); + } + + /// Decodes the body of into a . + public static CoordsysRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 10 (part, size 64): part reference points and local coordinate system. +/// The part identifier (primary key). +/// The attribute identifier in table 44 (part_attr). +/// The positioning attribute identifier. +/// The start point identifier in table 13 (point). +/// The end point identifier in table 13 (point). +/// The geometry tree root node identifier. +/// The coordinate system attribute identifier. +/// Reserved integer field. +/// Local coordinate system origin X. +/// Local coordinate system origin Y. +/// Local coordinate system origin Z. +/// Length of the part. +public readonly record struct PartRow( + int Id, + int PartAttrId, + int PositioningAttrId, + int StartPointId, + int EndPointId, + int GeometryTreeRootId, + int CoordsysAttrId, + int Reserved, + double CoordsysX, + double CoordsysY, + double CoordsysZ, + double Length) +{ + /// The expected row body size in bytes: 64. + public const int ExpectedRowSize = 64; + + /// The table key in model database sections: 10. + public const int TableKey = 10; + + /// Decodes a row body into a . + public static PartRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.I32(body, 16), + Bytes.I32(body, 20), + Bytes.I32(body, 24), + Bytes.I32(body, 28), + Bytes.F64(body, 32), + Bytes.F64(body, 40), + Bytes.F64(body, 48), + Bytes.F64(body, 56)); + } + + /// Decodes the body of into a . + public static PartRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 13 (point, size 32): 3D coordinates and visibility. +/// The point identifier (primary key). +/// Point visibility flags. +/// 3D X coordinate in millimeters. +/// 3D Y coordinate in millimeters. +/// 3D Z coordinate in millimeters. +public readonly record struct PointRow(int Id, int Visibility, double X, double Y, double Z) +{ + /// The expected row body size in bytes: 32. + public const int ExpectedRowSize = 32; + + /// The table key in model database sections: 13. + public const int TableKey = 13; + + /// Decodes a row body into a . + public static PointRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.F64(body, 8), + Bytes.F64(body, 16), + Bytes.F64(body, 24)); + } + + /// Decodes the body of into a . + public static PointRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 16 (relation, size 60): relationship between two objects. +/// The relation identifier (primary key). +/// The relation type code. +/// The first related object identifier. +/// The second related object identifier. +/// The relation GUID string. +/// Relation flags. +public readonly record struct RelationRow(int Id, int Type, int Id1, int Id2, string Guid, int Flags) +{ + /// The expected row body size in bytes: 60. + public const int ExpectedRowSize = 60; + + /// The table key in model database sections: 16. + public const int TableKey = 16; + + /// Decodes a row body into a . + public static RelationRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.AsciiString(body, 16, 40), + Bytes.I32(body, 56)); + } + + /// Decodes the body of into a . + public static RelationRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 20 (welding, size 24): weld connection and seam references. +/// The weld identifier (primary key). +/// Identifier of common weld attributes in table 98 (welding_common_attr). +/// Identifier of seam 1 in table 46 (welding_attr). +/// Identifier of seam 2 in table 46 (welding_attr). +/// Compound seam 1 identifier. +/// Compound seam 2 identifier. +public readonly record struct WeldingRow(int Id, int CommonAttrId, int Seam1Id, int Seam2Id, int CompoundSeam1Id, int CompoundSeam2Id) +{ + /// The expected row body size in bytes: 24. + public const int ExpectedRowSize = 24; + + /// The table key in model database sections: 20. + public const int TableKey = 20; + + /// Decodes a row body into a . + public static WeldingRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.I32(body, 16), + Bytes.I32(body, 20)); + } + + /// Decodes the body of into a . + public static WeldingRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 38 (numattr, size 24): numeric attribute association. +/// The numeric attribute row identifier (primary key). +/// Identifier of the attribute value in table 48 (numattr_attr). +/// Identifier of the object owning the attribute. +/// Attribute owner identifier. +/// Attribute flags. +/// Identifier of the attribute name. +public readonly record struct NumAttrRow(int Id, int AttrId, int FatherId, int OwnerId, int Flags, int NameId) +{ + /// The expected row body size in bytes: 24. + public const int ExpectedRowSize = 24; + + /// The table key in model database sections: 38. + public const int TableKey = 38; + + /// Decodes a row body into a . + public static NumAttrRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.I32(body, 16), + Bytes.I32(body, 20)); + } + + /// Decodes the body of into a . + public static NumAttrRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 39 (strattr, size 24): string attribute association. +/// The string attribute row identifier (primary key). +/// Identifier of the attribute value in table 49 (strattr_attr). +/// Identifier of the object owning the attribute. +/// Attribute owner identifier. +/// Attribute flags. +/// Identifier of the attribute name. +public readonly record struct StrAttrRow(int Id, int AttrId, int FatherId, int OwnerId, int Flags, int NameId) +{ + /// The expected row body size in bytes: 24. + public const int ExpectedRowSize = 24; + + /// The table key in model database sections: 39. + public const int TableKey = 39; + + /// Decodes a row body into a . + public static StrAttrRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.I32(body, 16), + Bytes.I32(body, 20)); + } + + /// Decodes the body of into a . + public static StrAttrRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 44 (part_attr, size 372): part profile, material, finish, and classification. +/// The part attribute identifier (primary key). +/// Object type code. +/// Form type code. +/// Part class number (color/display class). +/// Assembly start number. +/// Attribute flags. +/// Number of points. +/// Additional part flags. +/// Part numbering prefix / group. +/// Part name (e.g. "BEAM", "COLUMN", "PLATE"). +/// Part profile name (e.g. "IPE300", "HEA200", "PL10*100"). +/// Identifier in the string table for parameter strings. +/// Assembly position prefix. +/// Assembly name. +/// Material grade (e.g. "S235JR", "S355J2"). +/// Part surface finish. +/// Assembly type code. +/// Pour phase number. +public readonly record struct PartAttrRow( + int Id, + int ObjType, + int FormType, + int ObjClass, + int AssStartNo, + int Flags, + int NPoints, + int PartFlags, + string Prefix, + string Name, + string Profile, + int ParameterStringId, + string AssemblyPosition, + string AssemblyName, + string Material, + string Finish, + int AssemblyType, + int PourPhase) +{ + /// The expected row body size in bytes: 372. + public const int ExpectedRowSize = 372; + + /// The table key in model database sections: 44. + public const int TableKey = 44; + + /// Decodes a row body into a . + public static PartAttrRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.I32(body, 16), + Bytes.I32(body, 20), + Bytes.I32(body, 24), + Bytes.I32(body, 28), + Bytes.AsciiString(body, 32, 22), + Bytes.AsciiString(body, 54, 62), + Bytes.AsciiString(body, 116, 62), + Bytes.I32(body, 180), + Bytes.AsciiString(body, 184, 22), + Bytes.AsciiString(body, 206, 62), + Bytes.AsciiString(body, 268, 32), + Bytes.AsciiString(body, 300, 62), + Bytes.I32(body, 364), + Bytes.I32(body, 368)); + } + + /// Decodes the body of into a . + public static PartAttrRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 48 (numattr_attr, size 44): numeric attribute definition and value. +/// The attribute value identifier (primary key). +/// Value type code. +/// The numeric attribute value. +/// The attribute name. +/// Access privilege identifier. +public readonly record struct NumAttrAttrRow(int Id, int Type, double Value, string Name, int PrivilegeId) +{ + /// The expected row body size in bytes: 44. + public const int ExpectedRowSize = 44; + + /// The table key in model database sections: 48. + public const int TableKey = 48; + + /// Decodes a row body into a . + public static NumAttrAttrRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.F64(body, 8), + Bytes.AsciiString(body, 16, 21), + Bytes.I32(body, 40)); + } + + /// Decodes the body of into a . + public static NumAttrAttrRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 49 (strattr_attr, size 120): string attribute definition and value. +/// The attribute value identifier (primary key). +/// Value type code. +/// The attribute name. +/// The string attribute value. +/// Extended value identifier. +/// Access privilege identifier. +public readonly record struct StrAttrAttrRow(int Id, int Type, string Name, string Value, int ValueId, int PrivilegeId) +{ + /// The expected row body size in bytes: 120. + public const int ExpectedRowSize = 120; + + /// The table key in model database sections: 49. + public const int TableKey = 49; + + /// Decodes a row body into a . + public static StrAttrAttrRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.AsciiString(body, 8, 21), + Bytes.AsciiString(body, 29, 81), + Bytes.I32(body, 112), + Bytes.I32(body, 116)); + } + + /// Decodes the body of into a . + public static StrAttrAttrRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 50 (string, size 45): string pool chunk. +/// The string chunk identifier (primary key). +/// Identifier of the next chunk in a chained string, or negative if terminating. +/// Hash code of the string. +/// Reference count. +/// The text content of this chunk. +public readonly record struct StringPoolRow(int Id, int NextId, int HashId, int RefCount, string Text) +{ + /// The expected row body size in bytes: 45. + public const int ExpectedRowSize = 45; + + /// The table key in model database sections: 50. + public const int TableKey = 50; + + /// Decodes a row body into a . + public static StringPoolRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.AsciiString(body, 16, 29)); + } + + /// Decodes the body of into a . + public static StringPoolRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 94 (part_numbering, size 68): part numbering series and mark prefix. +/// The numbering identifier (primary key). +/// Numbering series identifier. +/// Start number of the numbering series. +/// Fabrication position number. +/// Multi-position number. +/// Part mark prefix string. +public readonly record struct PartNumberingRow(int Id, int SeriesId, int StartNumber, int FPos, int MultiFPos, string Prefix) +{ + /// The expected row body size in bytes: 68. + public const int ExpectedRowSize = 68; + + /// The table key in model database sections: 94. + public const int TableKey = 94; + + /// Decodes a row body into a . + public static PartNumberingRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.I32(body, 16), + Bytes.AsciiString(body, 20, 48)); + } + + /// Decodes the body of into a . + public static PartNumberingRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 95 (assembly_numbering, size 76): assembly numbering series and mark prefix. +/// The numbering identifier (primary key). +/// Numbering series identifier. +/// Start number of the numbering series. +/// Assembly position number. +/// Family position number. +/// Qualifier position number. +/// Multi-position number. +/// Assembly mark prefix string. +public readonly record struct AssemblyNumberingRow( + int Id, + int SeriesId, + int StartNumber, + int FPos, + int FamilyFPos, + int QualifierFPos, + int MultiFPos, + string Prefix) +{ + /// The expected row body size in bytes: 76. + public const int ExpectedRowSize = 76; + + /// The table key in model database sections: 95. + public const int TableKey = 95; + + /// Decodes a row body into an . + public static AssemblyNumberingRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.I32(body, 16), + Bytes.I32(body, 20), + Bytes.I32(body, 24), + Bytes.AsciiString(body, 28, 48)); + } + + /// Decodes the body of into an . + public static AssemblyNumberingRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 105 (object_numbering, size 12): object numbering association. +/// The object identifier (primary key). +/// Numbering entry identifier. +/// Numbering group code. +public readonly record struct ObjectNumberingRow(int Id, int NumberId, int NumberingGroup) +{ + /// The expected row body size in bytes: 12. + public const int ExpectedRowSize = 12; + + /// The table key in model database sections: 105. + public const int TableKey = 105; + + /// Decodes a row body into an . + public static ObjectNumberingRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8)); + } + + /// Decodes the body of into an . + public static ObjectNumberingRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 107 (bolt, size 24): bolt group definition and reference points. +/// The bolt object identifier (primary key). +/// Identifier in table 108 (bolt_attr). +/// Positioning attribute identifier. +/// First reference point identifier in table 13 (point). +/// Second reference point identifier in table 13 (point). +/// Polygon shape identifier. +public readonly record struct BoltRow(int Id, int BoltAttrId, int PositioningAttrId, int FirstPointId, int SecondPointId, int PolygonId) +{ + /// The expected row body size in bytes: 24. + public const int ExpectedRowSize = 24; + + /// The table key in model database sections: 107. + public const int TableKey = 107; + + /// Decodes a row body into a . + public static BoltRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.I32(body, 16), + Bytes.I32(body, 20)); + } + + /// Decodes the body of into a . + public static BoltRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 132 (object_modification, size 20): object modification timestamp and dependency. +/// The modification event identifier (primary key). +/// The object identifier that was modified. +/// The modification type code. +/// The dependency object identifier. +/// The modification timestamp identifier. +public readonly record struct ObjectModificationRow(int ModificationId, int Id, int ModType, int DependencyId, int ModTime) +{ + /// The expected row body size in bytes: 20. + public const int ExpectedRowSize = 20; + + /// The table key in model database sections: 132. + public const int TableKey = 132; + + /// Decodes a row body into an . + public static ObjectModificationRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.I32(body, 12), + Bytes.I32(body, 16)); + } + + /// Decodes the body of into an . + public static ObjectModificationRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 140 (bolt_hole, size 48): bolt hole through a connected part. +/// The bolt group identifier. +/// The hole index in the bolt group. +/// The part identifier through which this hole passes. +/// The Z offset of the hole along the bolt axis. +/// The grip length of the hole in this part. +/// Slotted hole direction. +/// Slotted hole type. +/// Hole shortening length. +public readonly record struct BoltHoleRow( + int BoltId, + int No, + int PartId, + double PointZ, + double Length, + int LongHoleDir, + int LongHoleType, + double ShorteningLength) +{ + /// The expected row body size in bytes: 48. + public const int ExpectedRowSize = 48; + + /// The table key in model database sections: 140. + public const int TableKey = 140; + + /// Decodes a row body into a . + public static BoltHoleRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4), + Bytes.I32(body, 8), + Bytes.F64(body, 16), + Bytes.F64(body, 24), + Bytes.I32(body, 32), + Bytes.I32(body, 36), + Bytes.F64(body, 40)); + } + + /// Decodes the body of into a . + public static BoltHoleRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 141 (object_phase, size 8): project phase assignment. +/// The object identifier. +/// The phase number. +public readonly record struct ObjectPhaseRow(int ObjectId, int PhaseNumber) +{ + /// The expected row body size in bytes: 8. + public const int ExpectedRowSize = 8; + + /// The table key in model database sections: 141. + public const int TableKey = 141; + + /// Decodes a row body into an . + public static ObjectPhaseRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.I32(body, 4)); + } + + /// Decodes the body of into an . + public static ObjectPhaseRow Decode(TableRow row) => Decode(row.Body.Span); +} + +/// Decoded row of table 142 (bolt_hole_points, size 60): intermediate bolt pattern coordinates. +/// The bolt group identifier. +/// First intermediate coordinate X. +/// First intermediate coordinate Y. +/// First intermediate coordinate Z. +/// Second intermediate coordinate X. +/// Second intermediate coordinate Y. +/// Second intermediate coordinate Z. +/// Number of holes in the pattern. +public readonly record struct BoltHolePointsRow( + int BoltId, + double IntPoint1X, + double IntPoint1Y, + double IntPoint1Z, + double IntPoint2X, + double IntPoint2Y, + double IntPoint2Z, + int NumHoles) +{ + /// The expected row body size in bytes: 60. + public const int ExpectedRowSize = 60; + + /// The table key in model database sections: 142. + public const int TableKey = 142; + + /// Decodes a row body into a . + public static BoltHolePointsRow Decode(ReadOnlySpan body) + { + if (body.Length < ExpectedRowSize) + throw new ArgumentException($"Row body length {body.Length} is smaller than expected size {ExpectedRowSize}.", nameof(body)); + return new( + Bytes.I32(body, 0), + Bytes.F64(body, 8), + Bytes.F64(body, 16), + Bytes.F64(body, 24), + Bytes.F64(body, 32), + Bytes.F64(body, 40), + Bytes.F64(body, 48), + Bytes.I32(body, 56)); + } + + /// Decodes the body of into a . + public static BoltHolePointsRow Decode(TableRow row) => Decode(row.Body.Span); +} diff --git a/src/TdbIO/TableRow.cs b/src/TdbIO/TableRow.cs index c94fd92..0e60a25 100644 --- a/src/TdbIO/TableRow.cs +++ b/src/TdbIO/TableRow.cs @@ -24,4 +24,67 @@ public readonly record struct TableRow(long Offset, byte Marker, ReadOnlyMemory< /// True if this row has the deleted or superseded marker (0x0C) in .db1 files. public bool IsDeleted => Marker == 0x0C; + + /// Decodes the row body as an (table 1, size 72). + public ObjectRow AsObject() => ObjectRow.Decode(this); + + /// Decodes the row body as an (table 2, size 92). + public AssemblyRow AsAssembly() => AssemblyRow.Decode(this); + + /// Decodes the row body as a (table 3, size 40). + public CoordsysRow AsCoordsys() => CoordsysRow.Decode(this); + + /// Decodes the row body as a (table 10, size 64). + public PartRow AsPart() => PartRow.Decode(this); + + /// Decodes the row body as a (table 13, size 32). + public PointRow AsPoint() => PointRow.Decode(this); + + /// Decodes the row body as a (table 16, size 60). + public RelationRow AsRelation() => RelationRow.Decode(this); + + /// Decodes the row body as a (table 20, size 24). + public WeldingRow AsWelding() => WeldingRow.Decode(this); + + /// Decodes the row body as a (table 38, size 24). + public NumAttrRow AsNumAttr() => NumAttrRow.Decode(this); + + /// Decodes the row body as a (table 39, size 24). + public StrAttrRow AsStrAttr() => StrAttrRow.Decode(this); + + /// Decodes the row body as a (table 44, size 372). + public PartAttrRow AsPartAttr() => PartAttrRow.Decode(this); + + /// Decodes the row body as a (table 48, size 44). + public NumAttrAttrRow AsNumAttrAttr() => NumAttrAttrRow.Decode(this); + + /// Decodes the row body as a (table 49, size 120). + public StrAttrAttrRow AsStrAttrAttr() => StrAttrAttrRow.Decode(this); + + /// Decodes the row body as a (table 50, size 45). + public StringPoolRow AsStringPool() => StringPoolRow.Decode(this); + + /// Decodes the row body as a (table 94, size 68). + public PartNumberingRow AsPartNumbering() => PartNumberingRow.Decode(this); + + /// Decodes the row body as an (table 95, size 76). + public AssemblyNumberingRow AsAssemblyNumbering() => AssemblyNumberingRow.Decode(this); + + /// Decodes the row body as an (table 105, size 12). + public ObjectNumberingRow AsObjectNumbering() => ObjectNumberingRow.Decode(this); + + /// Decodes the row body as a (table 107, size 24). + public BoltRow AsBolt() => BoltRow.Decode(this); + + /// Decodes the row body as an (table 132, size 20). + public ObjectModificationRow AsObjectModification() => ObjectModificationRow.Decode(this); + + /// Decodes the row body as a (table 140, size 48). + public BoltHoleRow AsBoltHole() => BoltHoleRow.Decode(this); + + /// Decodes the row body as an (table 141, size 8). + public ObjectPhaseRow AsObjectPhase() => ObjectPhaseRow.Decode(this); + + /// Decodes the row body as a (table 142, size 60). + public BoltHolePointsRow AsBoltHolePoints() => BoltHolePointsRow.Decode(this); } diff --git a/src/TdbIO/TableSection.cs b/src/TdbIO/TableSection.cs index d30dc80..7a99e26 100644 --- a/src/TdbIO/TableSection.cs +++ b/src/TdbIO/TableSection.cs @@ -46,4 +46,77 @@ internal TableSection(int ordinal, long offset, long end, int? key, int rowSize, /// The rows in file order. The list is a view over the data: each row is built when it is read. public IReadOnlyList Rows { get; } + + private IReadOnlyList DecodeRows(int expectedSize, Func decode) + { + if (RowSize != expectedSize) + throw new InvalidOperationException($"Section row size {RowSize} does not match expected size {expectedSize} for {typeof(T).Name}."); + var list = new T[Rows.Count]; + for (int i = 0; i < Rows.Count; i++) + list[i] = decode(Rows[i]); + return list; + } + + /// Decodes all rows of this section as (size 72). + public IReadOnlyList AsObjects() => DecodeRows(ObjectRow.ExpectedRowSize, r => r.AsObject()); + + /// Decodes all rows of this section as (size 92). + public IReadOnlyList AsAssemblies() => DecodeRows(AssemblyRow.ExpectedRowSize, r => r.AsAssembly()); + + /// Decodes all rows of this section as (size 40). + public IReadOnlyList AsCoordsys() => DecodeRows(CoordsysRow.ExpectedRowSize, r => r.AsCoordsys()); + + /// Decodes all rows of this section as (size 64). + public IReadOnlyList AsParts() => DecodeRows(PartRow.ExpectedRowSize, r => r.AsPart()); + + /// Decodes all rows of this section as (size 32). + public IReadOnlyList AsPoints() => DecodeRows(PointRow.ExpectedRowSize, r => r.AsPoint()); + + /// Decodes all rows of this section as (size 60). + public IReadOnlyList AsRelations() => DecodeRows(RelationRow.ExpectedRowSize, r => r.AsRelation()); + + /// Decodes all rows of this section as (size 24). + public IReadOnlyList AsWeldings() => DecodeRows(WeldingRow.ExpectedRowSize, r => r.AsWelding()); + + /// Decodes all rows of this section as (size 24). + public IReadOnlyList AsNumAttrs() => DecodeRows(NumAttrRow.ExpectedRowSize, r => r.AsNumAttr()); + + /// Decodes all rows of this section as (size 24). + public IReadOnlyList AsStrAttrs() => DecodeRows(StrAttrRow.ExpectedRowSize, r => r.AsStrAttr()); + + /// Decodes all rows of this section as (size 372). + public IReadOnlyList AsPartAttrs() => DecodeRows(PartAttrRow.ExpectedRowSize, r => r.AsPartAttr()); + + /// Decodes all rows of this section as (size 44). + public IReadOnlyList AsNumAttrAttrs() => DecodeRows(NumAttrAttrRow.ExpectedRowSize, r => r.AsNumAttrAttr()); + + /// Decodes all rows of this section as (size 120). + public IReadOnlyList AsStrAttrAttrs() => DecodeRows(StrAttrAttrRow.ExpectedRowSize, r => r.AsStrAttrAttr()); + + /// Decodes all rows of this section as (size 45). + public IReadOnlyList AsStringPool() => DecodeRows(StringPoolRow.ExpectedRowSize, r => r.AsStringPool()); + + /// Decodes all rows of this section as (size 68). + public IReadOnlyList AsPartNumberings() => DecodeRows(PartNumberingRow.ExpectedRowSize, r => r.AsPartNumbering()); + + /// Decodes all rows of this section as (size 76). + public IReadOnlyList AsAssemblyNumberings() => DecodeRows(AssemblyNumberingRow.ExpectedRowSize, r => r.AsAssemblyNumbering()); + + /// Decodes all rows of this section as (size 12). + public IReadOnlyList AsObjectNumberings() => DecodeRows(ObjectNumberingRow.ExpectedRowSize, r => r.AsObjectNumbering()); + + /// Decodes all rows of this section as (size 24). + public IReadOnlyList AsBolts() => DecodeRows(BoltRow.ExpectedRowSize, r => r.AsBolt()); + + /// Decodes all rows of this section as (size 20). + public IReadOnlyList AsObjectModifications() => DecodeRows(ObjectModificationRow.ExpectedRowSize, r => r.AsObjectModification()); + + /// Decodes all rows of this section as (size 48). + public IReadOnlyList AsBoltHoles() => DecodeRows(BoltHoleRow.ExpectedRowSize, r => r.AsBoltHole()); + + /// Decodes all rows of this section as (size 8). + public IReadOnlyList AsObjectPhases() => DecodeRows(ObjectPhaseRow.ExpectedRowSize, r => r.AsObjectPhase()); + + /// Decodes all rows of this section as (size 60). + public IReadOnlyList AsBoltHolePoints() => DecodeRows(BoltHolePointsRow.ExpectedRowSize, r => r.AsBoltHolePoints()); } diff --git a/tests/TdbIO.Tests/TableRowDecoderTests.cs b/tests/TdbIO.Tests/TableRowDecoderTests.cs new file mode 100644 index 0000000..0014315 --- /dev/null +++ b/tests/TdbIO.Tests/TableRowDecoderTests.cs @@ -0,0 +1,684 @@ +using System.Buffers.Binary; +using System.Text; +using TdbIO.Internal; +using Xunit; + +namespace TdbIO.Tests; + +public class TableRowDecoderTests +{ + private delegate void FillSpan(Span span); + + private static TableRow MakeRow(int size, FillSpan fill) + { + var bytes = new byte[size]; + fill(bytes); + return new TableRow(100, 4, bytes, 1, 2); + } + + private static TableSection MakeSection(int rowSize, int? key, params TableRow[] rows) => + new(0, 0, 100, key, rowSize, [0, 0, 0], 20, rows); + + [Fact] + public void ObjectRowDecodesCorrectly() + { + var guid = Guid.NewGuid(); + var extType = Guid.NewGuid(); + var row = MakeRow(ObjectRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 12345); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 67890); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 11111); + guid.TryWriteBytes(span[12..], bigEndian: true, out _); + BinaryPrimitives.WriteInt32LittleEndian(span[28..], 1); + BinaryPrimitives.WriteInt32LittleEndian(span[32..], 2); + extType.TryWriteBytes(span[36..], bigEndian: true, out _); + BinaryPrimitives.WriteInt32LittleEndian(span[52..], 100); + BinaryPrimitives.WriteInt32LittleEndian(span[56..], 200); + BinaryPrimitives.WriteInt32LittleEndian(span[60..], 300); + BinaryPrimitives.WriteInt32LittleEndian(span[64..], 400); + BinaryPrimitives.WriteInt32LittleEndian(span[68..], 500); + }); + + var decoded = row.AsObject(); + Assert.Equal(12345, decoded.Id); + Assert.Equal(67890, decoded.ParentId); + Assert.Equal(11111, decoded.AssemblyId); + Assert.Equal(guid, decoded.Guid); + Assert.Equal(1, decoded.Type); + Assert.Equal(2, decoded.Subtype); + Assert.Equal(extType, decoded.ExternalType); + Assert.Equal(100, decoded.Mask1); + Assert.Equal(200, decoded.Mask2); + Assert.Equal(300, decoded.ObjFlag); + Assert.Equal(400, decoded.OwnerId); + Assert.Equal(500, decoded.PrivilegeId); + } + + [Fact] + public void AssemblyRowDecodesCorrectly() + { + var row = MakeRow(AssemblyRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 10); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 20); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 30); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 40); + BinaryPrimitives.WriteInt32LittleEndian(span[16..], 50); + Encoding.UTF8.GetBytes("COLUMN\0").CopyTo(span[20..]); + BinaryPrimitives.WriteInt32LittleEndian(span[84..], 60); + BinaryPrimitives.WriteInt32LittleEndian(span[88..], 1); + }); + + var decoded = row.AsAssembly(); + Assert.Equal(10, decoded.Id); + Assert.Equal(20, decoded.Type); + Assert.Equal(30, decoded.Class); + Assert.Equal(40, decoded.MainPartId); + Assert.Equal(50, decoded.Lot); + Assert.Equal("COLUMN", decoded.Name); + Assert.Equal(60, decoded.Flags); + Assert.Equal(1, decoded.MainPartByUser); + } + + [Fact] + public void AssemblyRowWithInvalidUtf8SequenceThrowsInvalidDataException() + { + var row = MakeRow(AssemblyRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 10); + span[20] = 0xFF; + }); + + var e = Assert.Throws(() => row.AsAssembly()); + Assert.Equal(20, e.Offset); + } + + [Fact] + public void CoordsysRowDecodesCorrectly() + { + var row = MakeRow(CoordsysRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 1); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 2); + BinaryPrimitives.WriteDoubleLittleEndian(span[8..], 100.5); + BinaryPrimitives.WriteDoubleLittleEndian(span[16..], 200.75); + BinaryPrimitives.WriteDoubleLittleEndian(span[24..], 300.125); + BinaryPrimitives.WriteDoubleLittleEndian(span[32..], 1000.0); + }); + + var decoded = row.AsCoordsys(); + Assert.Equal(1, decoded.Id); + Assert.Equal(2, decoded.AttrId); + Assert.Equal(100.5, decoded.X); + Assert.Equal(200.75, decoded.Y); + Assert.Equal(300.125, decoded.Z); + Assert.Equal(1000.0, decoded.Length); + } + + [Fact] + public void PartRowDecodesCorrectly() + { + var row = MakeRow(PartRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 101); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 102); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 103); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 104); + BinaryPrimitives.WriteInt32LittleEndian(span[16..], 105); + BinaryPrimitives.WriteInt32LittleEndian(span[20..], 106); + BinaryPrimitives.WriteInt32LittleEndian(span[24..], 107); + BinaryPrimitives.WriteInt32LittleEndian(span[28..], 0); + BinaryPrimitives.WriteDoubleLittleEndian(span[32..], 10.0); + BinaryPrimitives.WriteDoubleLittleEndian(span[40..], 20.0); + BinaryPrimitives.WriteDoubleLittleEndian(span[48..], 30.0); + BinaryPrimitives.WriteDoubleLittleEndian(span[56..], 500.0); + }); + + var decoded = row.AsPart(); + Assert.Equal(101, decoded.Id); + Assert.Equal(102, decoded.PartAttrId); + Assert.Equal(103, decoded.PositioningAttrId); + Assert.Equal(104, decoded.StartPointId); + Assert.Equal(105, decoded.EndPointId); + Assert.Equal(106, decoded.GeometryTreeRootId); + Assert.Equal(107, decoded.CoordsysAttrId); + Assert.Equal(0, decoded.Reserved); + Assert.Equal(10.0, decoded.CoordsysX); + Assert.Equal(20.0, decoded.CoordsysY); + Assert.Equal(30.0, decoded.CoordsysZ); + Assert.Equal(500.0, decoded.Length); + } + + [Fact] + public void PointRowDecodesCorrectly() + { + var row = MakeRow(PointRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 777); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 4); + BinaryPrimitives.WriteDoubleLittleEndian(span[8..], 1234.5); + BinaryPrimitives.WriteDoubleLittleEndian(span[16..], -6789.0); + BinaryPrimitives.WriteDoubleLittleEndian(span[24..], 0.0); + }); + + var decoded = row.AsPoint(); + Assert.Equal(777, decoded.Id); + Assert.Equal(4, decoded.Visibility); + Assert.Equal(1234.5, decoded.X); + Assert.Equal(-6789.0, decoded.Y); + Assert.Equal(0.0, decoded.Z); + } + + [Fact] + public void RelationRowDecodesCorrectly() + { + var row = MakeRow(RelationRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 55); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 61000); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 11); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 22); + Encoding.UTF8.GetBytes("7b4b9794-f4b8-4040-b87b-3da518911e24\0").CopyTo(span[16..]); + BinaryPrimitives.WriteInt32LittleEndian(span[56..], 99); + }); + + var decoded = row.AsRelation(); + Assert.Equal(55, decoded.Id); + Assert.Equal(61000, decoded.Type); + Assert.Equal(11, decoded.Id1); + Assert.Equal(22, decoded.Id2); + Assert.Equal("7b4b9794-f4b8-4040-b87b-3da518911e24", decoded.Guid); + Assert.Equal(99, decoded.Flags); + } + + [Fact] + public void WeldingRowDecodesCorrectly() + { + var row = MakeRow(WeldingRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 1); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 2); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 3); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 4); + BinaryPrimitives.WriteInt32LittleEndian(span[16..], 5); + BinaryPrimitives.WriteInt32LittleEndian(span[20..], 6); + }); + + var decoded = row.AsWelding(); + Assert.Equal(1, decoded.Id); + Assert.Equal(2, decoded.CommonAttrId); + Assert.Equal(3, decoded.Seam1Id); + Assert.Equal(4, decoded.Seam2Id); + Assert.Equal(5, decoded.CompoundSeam1Id); + Assert.Equal(6, decoded.CompoundSeam2Id); + } + + [Fact] + public void BoltRowDecodesCorrectly() + { + var row = MakeRow(BoltRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 10); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 20); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 30); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 40); + BinaryPrimitives.WriteInt32LittleEndian(span[16..], 50); + BinaryPrimitives.WriteInt32LittleEndian(span[20..], 60); + }); + + var decoded = row.AsBolt(); + Assert.Equal(10, decoded.Id); + Assert.Equal(20, decoded.BoltAttrId); + Assert.Equal(30, decoded.PositioningAttrId); + Assert.Equal(40, decoded.FirstPointId); + Assert.Equal(50, decoded.SecondPointId); + Assert.Equal(60, decoded.PolygonId); + } + + [Fact] + public void ObjectPhaseRowDecodesCorrectly() + { + var row = MakeRow(ObjectPhaseRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 123); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 3); + }); + + var decoded = row.AsObjectPhase(); + Assert.Equal(123, decoded.ObjectId); + Assert.Equal(3, decoded.PhaseNumber); + } + + [Fact] + public void ObjectNumberingRowDecodesCorrectly() + { + var row = MakeRow(ObjectNumberingRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 500); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 600); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 700); + }); + + var decoded = row.AsObjectNumbering(); + Assert.Equal(500, decoded.Id); + Assert.Equal(600, decoded.NumberId); + Assert.Equal(700, decoded.NumberingGroup); + } + + [Fact] + public void ObjectModificationRowDecodesCorrectly() + { + var row = MakeRow(ObjectModificationRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 1); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 2); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 3); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 4); + BinaryPrimitives.WriteInt32LittleEndian(span[16..], 5); + }); + + var decoded = row.AsObjectModification(); + Assert.Equal(1, decoded.ModificationId); + Assert.Equal(2, decoded.Id); + Assert.Equal(3, decoded.ModType); + Assert.Equal(4, decoded.DependencyId); + Assert.Equal(5, decoded.ModTime); + } + + [Fact] + public void NumAttrAndStrAttrRowsDecodeCorrectly() + { + var numRow = MakeRow(NumAttrRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 1); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 2); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 3); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 4); + BinaryPrimitives.WriteInt32LittleEndian(span[16..], 5); + BinaryPrimitives.WriteInt32LittleEndian(span[20..], 6); + }); + var num = numRow.AsNumAttr(); + Assert.Equal(1, num.Id); + Assert.Equal(2, num.AttrId); + Assert.Equal(3, num.FatherId); + Assert.Equal(4, num.OwnerId); + Assert.Equal(5, num.Flags); + Assert.Equal(6, num.NameId); + + var strRow = MakeRow(StrAttrRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 10); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 20); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 30); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 40); + BinaryPrimitives.WriteInt32LittleEndian(span[16..], 50); + BinaryPrimitives.WriteInt32LittleEndian(span[20..], 60); + }); + var str = strRow.AsStrAttr(); + Assert.Equal(10, str.Id); + Assert.Equal(20, str.AttrId); + Assert.Equal(30, str.FatherId); + Assert.Equal(40, str.OwnerId); + Assert.Equal(50, str.Flags); + Assert.Equal(60, str.NameId); + } + + [Fact] + public void PartAttrRowDecodesCorrectly() + { + var row = MakeRow(PartAttrRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 1); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 2); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 0); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 4); + BinaryPrimitives.WriteInt32LittleEndian(span[16..], 1); + BinaryPrimitives.WriteInt32LittleEndian(span[20..], 0); + BinaryPrimitives.WriteInt32LittleEndian(span[24..], 2); + BinaryPrimitives.WriteInt32LittleEndian(span[28..], 0); + Encoding.UTF8.GetBytes("B\0").CopyTo(span[32..]); + Encoding.UTF8.GetBytes("BEAM\0").CopyTo(span[54..]); + Encoding.UTF8.GetBytes("HEA300\0").CopyTo(span[116..]); + BinaryPrimitives.WriteInt32LittleEndian(span[180..], 999); + Encoding.UTF8.GetBytes("A\0").CopyTo(span[184..]); + Encoding.UTF8.GetBytes("MAIN_BEAM\0").CopyTo(span[206..]); + Encoding.UTF8.GetBytes("S355J2\0").CopyTo(span[268..]); + Encoding.UTF8.GetBytes("PAINTED\0").CopyTo(span[300..]); + BinaryPrimitives.WriteInt32LittleEndian(span[364..], 7); + BinaryPrimitives.WriteInt32LittleEndian(span[368..], 2); + }); + + var decoded = row.AsPartAttr(); + Assert.Equal(1, decoded.Id); + Assert.Equal(2, decoded.ObjType); + Assert.Equal(0, decoded.FormType); + Assert.Equal(4, decoded.ObjClass); + Assert.Equal(1, decoded.AssStartNo); + Assert.Equal(0, decoded.Flags); + Assert.Equal(2, decoded.NPoints); + Assert.Equal(0, decoded.PartFlags); + Assert.Equal("B", decoded.Prefix); + Assert.Equal("BEAM", decoded.Name); + Assert.Equal("HEA300", decoded.Profile); + Assert.Equal(999, decoded.ParameterStringId); + Assert.Equal("A", decoded.AssemblyPosition); + Assert.Equal("MAIN_BEAM", decoded.AssemblyName); + Assert.Equal("S355J2", decoded.Material); + Assert.Equal("PAINTED", decoded.Finish); + Assert.Equal(7, decoded.AssemblyType); + Assert.Equal(2, decoded.PourPhase); + } + + [Fact] + public void AttrDefinitionRowsDecodeCorrectly() + { + var numAttrRow = MakeRow(NumAttrAttrRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 1); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 2); + BinaryPrimitives.WriteDoubleLittleEndian(span[8..], 42.5); + Encoding.UTF8.GetBytes("DIAMETER\0").CopyTo(span[16..]); + BinaryPrimitives.WriteInt32LittleEndian(span[40..], 99); + }); + var num = numAttrRow.AsNumAttrAttr(); + Assert.Equal(1, num.Id); + Assert.Equal(2, num.Type); + Assert.Equal(42.5, num.Value); + Assert.Equal("DIAMETER", num.Name); + Assert.Equal(99, num.PrivilegeId); + + var strAttrRow = MakeRow(StrAttrAttrRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 10); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 3); + Encoding.UTF8.GetBytes("COMMENT\0").CopyTo(span[8..]); + Encoding.UTF8.GetBytes("Important part\0").CopyTo(span[29..]); + BinaryPrimitives.WriteInt32LittleEndian(span[112..], 500); + BinaryPrimitives.WriteInt32LittleEndian(span[116..], 0); + }); + var str = strAttrRow.AsStrAttrAttr(); + Assert.Equal(10, str.Id); + Assert.Equal(3, str.Type); + Assert.Equal("COMMENT", str.Name); + Assert.Equal("Important part", str.Value); + Assert.Equal(500, str.ValueId); + Assert.Equal(0, str.PrivilegeId); + } + + [Fact] + public void StringPoolRowDecodesCorrectly() + { + var row = MakeRow(StringPoolRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 100); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], -1); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 12345); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 1); + Encoding.UTF8.GetBytes("Hello Tekla\0").CopyTo(span[16..]); + }); + + var decoded = row.AsStringPool(); + Assert.Equal(100, decoded.Id); + Assert.Equal(-1, decoded.NextId); + Assert.Equal(12345, decoded.HashId); + Assert.Equal(1, decoded.RefCount); + Assert.Equal("Hello Tekla", decoded.Text); + } + + [Fact] + public void NumberingRowsDecodeCorrectly() + { + var partNumRow = MakeRow(PartNumberingRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 1); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 0); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 100); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 5); + BinaryPrimitives.WriteInt32LittleEndian(span[16..], 0); + Encoding.UTF8.GetBytes("P-\0").CopyTo(span[20..]); + }); + var pnum = partNumRow.AsPartNumbering(); + Assert.Equal(1, pnum.Id); + Assert.Equal(0, pnum.SeriesId); + Assert.Equal(100, pnum.StartNumber); + Assert.Equal(5, pnum.FPos); + Assert.Equal(0, pnum.MultiFPos); + Assert.Equal("P-", pnum.Prefix); + + var assNumRow = MakeRow(AssemblyNumberingRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 2); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 0); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 200); + BinaryPrimitives.WriteInt32LittleEndian(span[12..], 10); + BinaryPrimitives.WriteInt32LittleEndian(span[16..], 0); + BinaryPrimitives.WriteInt32LittleEndian(span[20..], 0); + BinaryPrimitives.WriteInt32LittleEndian(span[24..], 0); + Encoding.UTF8.GetBytes("B-\0").CopyTo(span[28..]); + }); + var anum = assNumRow.AsAssemblyNumbering(); + Assert.Equal(2, anum.Id); + Assert.Equal(0, anum.SeriesId); + Assert.Equal(200, anum.StartNumber); + Assert.Equal(10, anum.FPos); + Assert.Equal("B-", anum.Prefix); + } + + [Fact] + public void BoltHolesRowsDecodeCorrectly() + { + var holeRow = MakeRow(BoltHoleRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 100); + BinaryPrimitives.WriteInt32LittleEndian(span[4..], 1); + BinaryPrimitives.WriteInt32LittleEndian(span[8..], 200); + BinaryPrimitives.WriteDoubleLittleEndian(span[16..], 15.0); + BinaryPrimitives.WriteDoubleLittleEndian(span[24..], 30.0); + BinaryPrimitives.WriteInt32LittleEndian(span[32..], 1); + BinaryPrimitives.WriteInt32LittleEndian(span[36..], 2); + BinaryPrimitives.WriteDoubleLittleEndian(span[40..], -5.0); + }); + var hole = holeRow.AsBoltHole(); + Assert.Equal(100, hole.BoltId); + Assert.Equal(1, hole.No); + Assert.Equal(200, hole.PartId); + Assert.Equal(15.0, hole.PointZ); + Assert.Equal(30.0, hole.Length); + Assert.Equal(1, hole.LongHoleDir); + Assert.Equal(2, hole.LongHoleType); + Assert.Equal(-5.0, hole.ShorteningLength); + + var ptsRow = MakeRow(BoltHolePointsRow.ExpectedRowSize, span => + { + BinaryPrimitives.WriteInt32LittleEndian(span[0..], 100); + BinaryPrimitives.WriteDoubleLittleEndian(span[8..], 1.0); + BinaryPrimitives.WriteDoubleLittleEndian(span[16..], 2.0); + BinaryPrimitives.WriteDoubleLittleEndian(span[24..], 3.0); + BinaryPrimitives.WriteDoubleLittleEndian(span[32..], 4.0); + BinaryPrimitives.WriteDoubleLittleEndian(span[40..], 5.0); + BinaryPrimitives.WriteDoubleLittleEndian(span[48..], 6.0); + BinaryPrimitives.WriteInt32LittleEndian(span[56..], 8); + }); + var pts = ptsRow.AsBoltHolePoints(); + Assert.Equal(100, pts.BoltId); + Assert.Equal(1.0, pts.IntPoint1X); + Assert.Equal(2.0, pts.IntPoint1Y); + Assert.Equal(3.0, pts.IntPoint1Z); + Assert.Equal(4.0, pts.IntPoint2X); + Assert.Equal(5.0, pts.IntPoint2Y); + Assert.Equal(6.0, pts.IntPoint2Z); + Assert.Equal(8, pts.NumHoles); + } + + [Fact] + public void BodyTooShortThrowsArgumentException() + { + var shortRow = MakeRow(10, _ => { }); + Assert.Throws(() => shortRow.AsObject()); + Assert.Throws(() => shortRow.AsPoint()); + Assert.Throws(() => shortRow.AsPart()); + } + + [Fact] + public void SectionRowSizeMismatchThrowsInvalidOperationException() + { + var row = MakeRow(32, _ => { }); + var section = MakeSection(32, 13, row); + + // Section has RowSize 32, but AsObjects expects 72 + Assert.Throws(() => section.AsObjects()); + Assert.Throws(() => section.AsParts()); + + // AsPoints matches 32 + var points = section.AsPoints(); + Assert.Single(points); + } + + [SkippableFact] + public void RealModelsDecodeKnownTableSections() + { + var files = TestFiles.FindAll(".db1", ".bak").Where(f => TdbFile.DetectKind(f) == TdbFileKind.Db1).ToList(); + if (files.Count == 0) throw TestFiles.Missing("model database files"); + + foreach (string file in files) + { + var doc = (Db1Document)TdbFile.Open(file); + var sections = doc.ReadTables(); + + foreach (var section in sections) + { + if (section.Rows.Count == 0) continue; + + switch (section.Key) + { + case ObjectRow.TableKey when section.RowSize == ObjectRow.ExpectedRowSize: + var objects = section.AsObjects(); + Assert.Equal(section.Rows.Count, objects.Count); + Assert.All(objects, o => Assert.NotEqual(Guid.Empty, o.Guid)); + break; + + case AssemblyRow.TableKey when section.RowSize == AssemblyRow.ExpectedRowSize: + var assemblies = section.AsAssemblies(); + Assert.Equal(section.Rows.Count, assemblies.Count); + Assert.All(assemblies, a => Assert.True(a.Id > 0 && a.Name != null)); + break; + + case PointRow.TableKey when section.RowSize == PointRow.ExpectedRowSize: + var points = section.AsPoints(); + Assert.Equal(section.Rows.Count, points.Count); + Assert.All(points, p => Assert.True(double.IsFinite(p.X) && double.IsFinite(p.Y) && double.IsFinite(p.Z))); + break; + + case PartRow.TableKey when section.RowSize == PartRow.ExpectedRowSize: + var parts = section.AsParts(); + Assert.Equal(section.Rows.Count, parts.Count); + Assert.All(parts, p => Assert.True(p.Id > 0 && p.StartPointId > 0 && p.EndPointId > 0)); + break; + + case CoordsysRow.TableKey when section.RowSize == CoordsysRow.ExpectedRowSize: + var csys = section.AsCoordsys(); + Assert.Equal(section.Rows.Count, csys.Count); + Assert.All(csys, c => Assert.True(double.IsFinite(c.X) && double.IsFinite(c.Y) && double.IsFinite(c.Z))); + break; + + case BoltRow.TableKey when section.RowSize == BoltRow.ExpectedRowSize: + var bolts = section.AsBolts(); + Assert.Equal(section.Rows.Count, bolts.Count); + Assert.All(bolts, b => Assert.True(b.Id > 0 && b.FirstPointId > 0 && b.SecondPointId > 0)); + break; + + case WeldingRow.TableKey when section.RowSize == WeldingRow.ExpectedRowSize: + var welds = section.AsWeldings(); + Assert.Equal(section.Rows.Count, welds.Count); + Assert.All(welds, w => Assert.True(w.Id > 0)); + break; + + case ObjectPhaseRow.TableKey when section.RowSize == ObjectPhaseRow.ExpectedRowSize: + var phases = section.AsObjectPhases(); + Assert.Equal(section.Rows.Count, phases.Count); + Assert.All(phases, ph => Assert.True(ph.ObjectId > 0 && ph.PhaseNumber >= 0)); + break; + + case RelationRow.TableKey when section.RowSize == RelationRow.ExpectedRowSize: + var rels = section.AsRelations(); + Assert.Equal(section.Rows.Count, rels.Count); + Assert.All(rels, r => Assert.True(r.Id > 0 && r.Guid != null)); + break; + + case NumAttrRow.TableKey when section.RowSize == NumAttrRow.ExpectedRowSize: + var numAttrs = section.AsNumAttrs(); + Assert.Equal(section.Rows.Count, numAttrs.Count); + Assert.All(numAttrs, n => Assert.True(n.Id > 0 && n.AttrId > 0)); + break; + + case StrAttrRow.TableKey when section.RowSize == StrAttrRow.ExpectedRowSize: + var strAttrs = section.AsStrAttrs(); + Assert.Equal(section.Rows.Count, strAttrs.Count); + Assert.All(strAttrs, s => Assert.True(s.Id > 0 && s.AttrId > 0)); + break; + + case PartAttrRow.TableKey when section.RowSize == PartAttrRow.ExpectedRowSize: + var partAttrs = section.AsPartAttrs(); + Assert.Equal(section.Rows.Count, partAttrs.Count); + Assert.All(partAttrs, pa => Assert.True(pa.Id > 0 && pa.Profile != null)); + break; + + case PartNumberingRow.TableKey when section.RowSize == PartNumberingRow.ExpectedRowSize: + var partNums = section.AsPartNumberings(); + Assert.Equal(section.Rows.Count, partNums.Count); + Assert.All(partNums, pn => Assert.True(pn.Id > 0 && pn.Prefix != null)); + break; + + case AssemblyNumberingRow.TableKey when section.RowSize == AssemblyNumberingRow.ExpectedRowSize: + var assNums = section.AsAssemblyNumberings(); + Assert.Equal(section.Rows.Count, assNums.Count); + Assert.All(assNums, an => Assert.True(an.Id > 0 && an.Prefix != null)); + break; + + case ObjectNumberingRow.TableKey when section.RowSize == ObjectNumberingRow.ExpectedRowSize: + var objNums = section.AsObjectNumberings(); + Assert.Equal(section.Rows.Count, objNums.Count); + Assert.All(objNums, on => Assert.True(on.Id > 0)); + break; + + case ObjectModificationRow.TableKey when section.RowSize == ObjectModificationRow.ExpectedRowSize: + var mods = section.AsObjectModifications(); + Assert.Equal(section.Rows.Count, mods.Count); + Assert.All(mods, m => Assert.True(m.ModificationId > 0 && m.Id > 0)); + break; + + case BoltHoleRow.TableKey when section.RowSize == BoltHoleRow.ExpectedRowSize: + var holes = section.AsBoltHoles(); + Assert.Equal(section.Rows.Count, holes.Count); + Assert.All(holes, h => Assert.True(h.BoltId > 0 && h.PartId > 0)); + break; + + case BoltHolePointsRow.TableKey when section.RowSize == BoltHolePointsRow.ExpectedRowSize: + var holePts = section.AsBoltHolePoints(); + Assert.Equal(section.Rows.Count, holePts.Count); + Assert.All(holePts, hp => Assert.True(hp.BoltId > 0 && hp.NumHoles > 0)); + break; + + case StrAttrAttrRow.TableKey when section.RowSize == StrAttrAttrRow.ExpectedRowSize: + var strDefs = section.AsStrAttrAttrs(); + Assert.Equal(section.Rows.Count, strDefs.Count); + Assert.All(strDefs, sd => Assert.True(sd.Id > 0 && sd.Name != null)); + break; + + case NumAttrAttrRow.TableKey when section.RowSize == NumAttrAttrRow.ExpectedRowSize: + var numDefs = section.AsNumAttrAttrs(); + Assert.Equal(section.Rows.Count, numDefs.Count); + Assert.All(numDefs, nd => Assert.True(nd.Id > 0 && nd.Name != null)); + break; + + case StringPoolRow.TableKey when section.RowSize == StringPoolRow.ExpectedRowSize: + var strPool = section.AsStringPool(); + Assert.Equal(section.Rows.Count, strPool.Count); + Assert.All(strPool, sp => Assert.True(sp.Id > 0 && sp.Text != null)); + break; + } + } + } + } +}