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
50 changes: 47 additions & 3 deletions docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ offset or line number. A warning never contains text from the file. Warnings are

| Type | Use |
| --- | --- |
| `Db1Document` | A model database (`.db1`, `xslib.db1` and their `.bak` copies). `Version` (`d.dd`), `ModelGuid`, `IdMap` (entries in file order, or null), `IdMapField65` and `IdMapField73` (raw numbers, null when the id map is not read), `Payload` (the whole unpacked file; offsets refer to it). `ReadTextSections()` and `ReadTables()` read on request and build new lists on each call. |
| `Db1Document` | A model database (`.db1`, `xslib.db1` and their `.bak` copies). `Version` (`d.dd`), `ModelGuid`, `IdMap` (entries in file order, or null), `IdMapField65` and `IdMapField73` (raw numbers, null when the id map is not read), `Payload` (the whole unpacked file; offsets refer to it). `ReadTextSections()` and `ReadTables()` read on request and build new lists on each call. `ReadModel()` decodes a connected, read-only object model graph (<see cref="TeklaModel"/>) of parts, assemblies, bolts, welds and attributes. |
| `IdMapEntry` | `Sequence`, `Id` (GUID) and `Value` (raw, meaning not known). |
| `TextSection` | A section of 54-byte text rows: `Offset`, `RowCount`, `Chains` (in the file order of their heads) and `OrphanRowIds` (rows that belong to no chain, reported instead of dropped). The links of all rows are checked, orphans included: a link to a missing id, a cycle or two rows linking to one row is an error. |
| `TextChain` | One text: `HeadId`, `HeadTag` and `HeadField13` (raw), `RowIds` (chain order), `Text` (the bytes as stored, not decoded). `IsSaveStamp` is true when the text starts with `Save:`; `SavedAt` is its date and time, or null. `RevealSaveText()` returns the revealed bytes of a save stamp: fields separated by `;`. Fields after the date and time can hold names of people or machines; do not log them. |
Expand All @@ -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` (raw, `.db1` only, else null), `RowSize`, `Descriptors` (raw), `RowsOffset` and `Rows`. Sections have no names; tell them apart by `Ordinal` and `RowSize`. `Rows` is a view over the data: a row is built when it is read. |
| `TableRow` | `Offset` (of the marker byte), `Marker`, `Body` (`RowSize` raw bytes), `A` and `B` (the two numbers after the body: signed 32-bit in `.db1` files, unsigned in DBV@ files). |
| `TableSection` | A section of fixed-size rows, from `Db1Document.ReadTables()` or `DbvDocument.Sections`. `Ordinal`, `Offset`, `End`, `Key` (raw, `.db1` only, else null), `RowSize`, `Descriptors` (raw), `RowsOffset` and `Rows`. Sections have no names; tell them apart by `Ordinal` and `RowSize`. `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`, `Body` (`RowSize` raw bytes), `A` and `B` (the two numbers after the body: signed 32-bit in `.db1` files, unsigned in DBV@ files). Typed decoders (`AsObject()`, `AsPart()`, `AsPoint()`, etc.) decode the row body into typed record structs. |
| `DbvDocument` | A DBV@ file: `Name`, `Prologue`, `ListHeader`, `GuidRecords`, `B`, `C`, `L1`, `L2`, `T`, `C9Records`, `Sections` and `Payload`. The numbers are raw; their names are positions, not meanings. |
| `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`. |
Expand All @@ -90,6 +90,50 @@ 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`. |

## Object model: parts, assemblies, bolts and welds

High-level connected object model produced by `Db1Document.ReadModel()`:

| Type | Use |
| --- | --- |
| `TeklaModel` | Decoded object graph: `Document`, `ModelGuid`, `Assemblies`, `Parts`, `Bolts`, `Welds`, `Objects`. Fast lookup methods: `FindObject(id/guid)`, `FindPart(id)`, `FindAssembly(id)`, `FindBolt(id)`, `FindWeld(id)`. |
| `ModelObject` | Base entity: `Id`, `Guid` (RFC 4122), `Type`, `Subtype`, `Phase`, `ParentId`, `AssemblyId`, `StringAttributes`, `NumericAttributes`, `GetStringAttribute(name)`, `GetNumericAttribute(name)`. |
| `ModelAssembly` | Assembly / cast unit: `Name`, `Prefix`, `StartNumber`, `PositionNumber`, `AssemblyType`, `Class`, `MainPart`, `Secondaries`, `Parts`, `Bolts`, `Welds`. |
| `ModelPart` | Structural part: `Name`, `Profile` (resolved with parameter strings), `Material`, `Prefix`, `StartNumber`, `PositionNumber`, `Class`, `Finish`, `Assembly`, `IsMainPart`, `StartPoint`, `EndPoint`, `Length`, `CoordinateSystem`, `Bolts`, `Welds`. |
| `ModelBolt` | Bolt group: `FirstPoint`, `SecondPoint`, `ConnectedParts`, `Holes`, `Positions`, `Assembly`. |
| `ModelWeld` | Structural weld: `CommonAttrId`, `Parent`, `Assembly`, `ConnectedParts`. |
| `ModelPoint` | 3D coordinate point in millimeters: `Id`, `X`, `Y`, `Z`. |
| `ModelCoordsys` | Local coordinate system origin and scale: `Id`, `X`, `Y`, `Z`, `Length`. |
| `ModelBoltHole` | Bolt hole penetration: `No`, `PartId`, `Part`, `PointZ`, `Length`, `LongHoleDir`, `LongHoleType`, `ShorteningLength`. |

## Files next to the model

| Type | Use |
Expand Down
4 changes: 4 additions & 0 deletions src/TdbIO/Db1Document.Tables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ public sealed partial class Db1Document
/// or bytes are left after the last section. The message names the section and the offset.</exception>
public IReadOnlyList<TableSection> ReadTables() =>
SectionWalker.Walk(Payload, SectionWalker.FindFirstSection(Payload.Span), 0, SectionMode.Db1, null);

/// <summary>Decodes the table sections into a connected, read-only object model of parts, assemblies, bolts, welds and attributes.</summary>
/// <returns>A new <see cref="TeklaModel"/> representing the decoded model object graph.</returns>
public TeklaModel ReadModel() => ModelBuilder.Build(this);
}
37 changes: 37 additions & 0 deletions src/TdbIO/Internal/Bytes.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Buffers.Binary;
using System.Text;

namespace TdbIO.Internal;

Expand Down Expand Up @@ -37,4 +38,40 @@ public static uint U32BE(ReadOnlySpan<byte> d, long at)
Need(at, 4, d.Length, "u32");
return BinaryPrimitives.ReadUInt32BigEndian(d[(int)at..]);
}

public static double F64(ReadOnlySpan<byte> d, long at)
{
Need(at, 8, d.Length, "f64");
return BinaryPrimitives.ReadDoubleLittleEndian(d[(int)at..]);
}

public static float F32(ReadOnlySpan<byte> d, long at)
{
Need(at, 4, d.Length, "f32");
return BinaryPrimitives.ReadSingleLittleEndian(d[(int)at..]);
}

public static Guid Guid(ReadOnlySpan<byte> 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<byte> 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);
}
}
}
Loading
Loading