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
2 changes: 1 addition & 1 deletion docs/public-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ 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)`. |
| `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)`. JSON serialization: `ToJson(indented)` and `WriteJson(stream/writer, indented)`. |
| `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`. |
Expand Down
23 changes: 20 additions & 3 deletions src/TdbIO.Cli/Cli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ internal static class Cli
Usage: tdbio info <file> [--json] [--encoding ascii|utf-8|cp1251|cp1252]
tdbio tables <file> [--json]
tdbio text <file> [--json]
tdbio export <file> [--indent]
tdbio --help | -h | --version

Commands:
info The kind of the file and its main values.
tables The table sections of a model database (.db1) or a DBV@ file, one line each.
text The text chains of a model database (.db1), one line each. Save stamps show only their date.
export Decodes the object model of a model database (.db1) and writes it as JSON.

Options:
--json Write one JSON object instead of text.
--indent Format the JSON output with indentation (export only).
--encoding The encoding of a template or report, instead of detecting it (info only).

Warnings go to stderr and do not change the exit code.
Expand All @@ -34,7 +37,7 @@ text The text chains of a model database (.db1), one line each. Save stamps
private static readonly string[] TextColumns = ["section", "headId", "rows", "length", "stamp", "savedAt", "text"];
private static readonly string[] OrphanColumns = ["section", "id"];

private sealed record Command(string Name, string Path, bool Json, Encoding? Encoding);
private sealed record Command(string Name, string Path, bool Json, bool Indent, Encoding? Encoding);

// A usage error found while parsing or after the kind is known.
private sealed class UsageException(string message) : Exception(message);
Expand Down Expand Up @@ -74,6 +77,7 @@ internal static int Run(string[] args, TextWriter stdout, TextWriter stderr)
{
case "info": error = Info(output, doc, command.Json); break;
case "tables": Tables(output, doc, command.Json); break;
case "export": Export(output, doc, command.Indent); break;
default: Text(output, doc, command.Json); break;
}
stdout.Write(output.ToString());
Expand Down Expand Up @@ -103,11 +107,12 @@ private static Command Parse(string[] args)
{
if (args.Length == 0) throw new UsageException("No command given.");
string name = args[0];
if (name is not ("info" or "tables" or "text")) throw new UsageException($"Unknown command '{name}'.");
if (name is not ("info" or "tables" or "text" or "export")) throw new UsageException($"Unknown command '{name}'.");

string? path = null;
string? encodingName = null;
bool json = false;
bool indent = false;
for (int i = 1; i < args.Length; i++)
{
string arg = args[i];
Expand All @@ -116,6 +121,11 @@ private static Command Parse(string[] args)
if (json) throw new UsageException("Option '--json' is repeated.");
json = true;
}
else if (arg == "--indent" && name == "export")
{
if (indent) throw new UsageException("Option '--indent' is repeated.");
indent = true;
}
else if (arg == "--encoding" && name == "info")
{
if (encodingName != null) throw new UsageException("Option '--encoding' is repeated.");
Expand All @@ -128,7 +138,7 @@ private static Command Parse(string[] args)
else path = arg;
}
if (path == null) throw new UsageException("No file given.");
return new Command(name, path, json, encodingName == null ? null : EncodingOf(encodingName));
return new Command(name, path, json, indent, encodingName == null ? null : EncodingOf(encodingName));
}

// Strict encodings: a byte the encoding does not define is an input error, never replaced.
Expand Down Expand Up @@ -290,6 +300,13 @@ private static void Text(TextWriter w, TdbDocument doc, bool json)
}
}

private static void Export(TextWriter w, TdbDocument doc, bool indent)
{
if (doc is not Db1Document db1) throw new WrongKindException($"'export' needs a model database (.db1), not {doc.Kind}.");
var model = db1.ReadModel();
w.WriteLine(model.ToJson(indent));
}

// The number of blocks below the root and the deepest level (the root is level 0).
private static (int Blocks, int Depth) Measure(TemplateBlock block)
{
Expand Down
68 changes: 46 additions & 22 deletions src/TdbIO/Internal/ModelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ internal static TeklaModel BuildFromSections(Db1Document doc, IReadOnlyList<Tabl
// 1. Decoded active row collections (Marker == 0x0C excluded)
var objRows = GetActiveRows(FindSection(sections, ObjectRow.TableKey, ObjectRow.ExpectedRowSize), ObjectRow.ExpectedRowSize, r => r.AsObject());
var assRows = GetActiveRows(FindSection(sections, AssemblyRow.TableKey, AssemblyRow.ExpectedRowSize), AssemblyRow.ExpectedRowSize, r => r.AsAssembly());
var partRows = GetActiveRows(FindSection(sections, PartRow.TableKey, PartRow.ExpectedRowSize), PartRow.ExpectedRowSize, r => r.AsPart());
var partRows = GetActiveRows(FindSection(sections, PartRow.TableKey, PartRow.ExpectedRowSize), PartRow.ExpectedRowSize, r => (Row: r.AsPart(), Offset: r.Offset + 1));
var boltRows = GetActiveRows(FindSection(sections, BoltRow.TableKey, BoltRow.ExpectedRowSize), BoltRow.ExpectedRowSize, r => r.AsBolt());
var weldRows = GetActiveRows(FindSection(sections, WeldingRow.TableKey, WeldingRow.ExpectedRowSize), WeldingRow.ExpectedRowSize, r => r.AsWelding());
var ptRows = GetActiveRows(FindSection(sections, PointRow.TableKey, PointRow.ExpectedRowSize), PointRow.ExpectedRowSize, r => r.AsPoint());
var csysRows = GetActiveRows(FindSection(sections, CoordsysRow.TableKey, CoordsysRow.ExpectedRowSize), CoordsysRow.ExpectedRowSize, r => r.AsCoordsys());
var ptRows = GetActiveRows(FindSection(sections, PointRow.TableKey, PointRow.ExpectedRowSize), PointRow.ExpectedRowSize, r => (Row: r.AsPoint(), Offset: r.Offset + 1));
var csysRows = GetActiveRows(FindSection(sections, CoordsysRow.TableKey, CoordsysRow.ExpectedRowSize), CoordsysRow.ExpectedRowSize, r => (Row: r.AsCoordsys(), Offset: r.Offset + 1));
var paRows = GetActiveRows(FindSection(sections, PartAttrRow.TableKey, PartAttrRow.ExpectedRowSize), PartAttrRow.ExpectedRowSize, r => r.AsPartAttr());
var strPoolRows = GetActiveRows(FindSection(sections, StringPoolRow.TableKey, StringPoolRow.ExpectedRowSize), StringPoolRow.ExpectedRowSize, r => r.AsStringPool());
var numAttrRows = GetActiveRows(FindSection(sections, NumAttrRow.TableKey, NumAttrRow.ExpectedRowSize), NumAttrRow.ExpectedRowSize, r => r.AsNumAttr());
var strAttrRows = GetActiveRows(FindSection(sections, StrAttrRow.TableKey, StrAttrRow.ExpectedRowSize), StrAttrRow.ExpectedRowSize, r => r.AsStrAttr());
var numDefRows = GetActiveRows(FindSection(sections, NumAttrAttrRow.TableKey, NumAttrAttrRow.ExpectedRowSize), NumAttrAttrRow.ExpectedRowSize, r => r.AsNumAttrAttr());
var numDefRows = GetActiveRows(FindSection(sections, NumAttrAttrRow.TableKey, NumAttrAttrRow.ExpectedRowSize), NumAttrAttrRow.ExpectedRowSize, r => (Row: r.AsNumAttrAttr(), Offset: r.Offset + 1));
var strDefRows = GetActiveRows(FindSection(sections, StrAttrAttrRow.TableKey, StrAttrAttrRow.ExpectedRowSize), StrAttrAttrRow.ExpectedRowSize, r => r.AsStrAttrAttr());
var phaseRows = GetActiveRows(FindSection(sections, ObjectPhaseRow.TableKey, ObjectPhaseRow.ExpectedRowSize), ObjectPhaseRow.ExpectedRowSize, r => r.AsObjectPhase());
var partNumRows = GetActiveRows(FindSection(sections, PartNumberingRow.TableKey, PartNumberingRow.ExpectedRowSize), PartNumberingRow.ExpectedRowSize, r => r.AsPartNumbering());
var assNumRows = GetActiveRows(FindSection(sections, AssemblyNumberingRow.TableKey, AssemblyNumberingRow.ExpectedRowSize), AssemblyNumberingRow.ExpectedRowSize, r => r.AsAssemblyNumbering());
var objNumRows = GetActiveRows(FindSection(sections, ObjectNumberingRow.TableKey, ObjectNumberingRow.ExpectedRowSize), ObjectNumberingRow.ExpectedRowSize, r => r.AsObjectNumbering());
var holeRows = GetActiveRows(FindSection(sections, BoltHoleRow.TableKey, BoltHoleRow.ExpectedRowSize), BoltHoleRow.ExpectedRowSize, r => r.AsBoltHole());
var boltPtRows = GetActiveRows(FindSection(sections, BoltHolePointsRow.TableKey, BoltHolePointsRow.ExpectedRowSize), BoltHolePointsRow.ExpectedRowSize, r => r.AsBoltHolePoints());
var holeRows = GetActiveRows(FindSection(sections, BoltHoleRow.TableKey, BoltHoleRow.ExpectedRowSize), BoltHoleRow.ExpectedRowSize, r => (Row: r.AsBoltHole(), Offset: r.Offset + 1));
var boltPtRows = GetActiveRows(FindSection(sections, BoltHolePointsRow.TableKey, BoltHolePointsRow.ExpectedRowSize), BoltHolePointsRow.ExpectedRowSize, r => (Row: r.AsBoltHolePoints(), Offset: r.Offset + 1));

// 2. Primary lookups
var objects = new Dictionary<int, ObjectRow>(objRows.Count);
Expand Down Expand Up @@ -114,10 +114,10 @@ void TraverseChain(StringPoolRow head)
}

var points = new Dictionary<int, ModelPoint>(ptRows.Count);
foreach (var pt in ptRows) points[pt.Id] = new ModelPoint(pt.Id, pt.X, pt.Y, pt.Z);
foreach (var (pt, offset) in ptRows) points[pt.Id] = new ModelPoint(pt.Id, pt.X, pt.Y, pt.Z) { Offset = offset };

var coords = new Dictionary<int, ModelCoordsys>(csysRows.Count);
foreach (var cs in csysRows) coords[cs.Id] = new ModelCoordsys(cs.Id, cs.X, cs.Y, cs.Z, cs.Length);
foreach (var (cs, offset) in csysRows) coords[cs.Id] = new ModelCoordsys(cs.Id, cs.X, cs.Y, cs.Z, cs.Length) { Offset = offset };

var partAttrs = new Dictionary<int, PartAttrRow>(paRows.Count);
foreach (var pa in paRows) partAttrs[pa.Id] = pa;
Expand All @@ -135,10 +135,10 @@ void TraverseChain(StringPoolRow head)
foreach (var on in objNumRows) objNumberIds[on.Id] = on.NumberId;

// 3. User & System Attributes
var numDefs = new Dictionary<int, (string Name, double Value)>(numDefRows.Count);
foreach (var nd in numDefRows)
var numDefs = new Dictionary<int, (string Name, double Value, long Offset)>(numDefRows.Count);
foreach (var (nd, offset) in numDefRows)
if (!string.IsNullOrEmpty(nd.Name))
numDefs[nd.Id] = (nd.Name, nd.Value);
numDefs[nd.Id] = (nd.Name, nd.Value, offset + 8);

var strDefs = new Dictionary<int, (string Name, string Value)>(strDefRows.Count);
foreach (var sd in strDefRows)
Expand All @@ -151,13 +151,18 @@ void TraverseChain(StringPoolRow head)
}

var numAttrsByObj = new Dictionary<int, Dictionary<string, double>>();
var numAttrOffsetsByObj = new Dictionary<int, Dictionary<string, long>>();
foreach (var na in numAttrRows)
{
if (numDefs.TryGetValue(na.AttrId, out var def))
{
if (!numAttrsByObj.TryGetValue(na.FatherId, out var dict))
numAttrsByObj[na.FatherId] = dict = new Dictionary<string, double>(StringComparer.OrdinalIgnoreCase);
dict[def.Name] = def.Value;

if (!numAttrOffsetsByObj.TryGetValue(na.FatherId, out var offDict))
numAttrOffsetsByObj[na.FatherId] = offDict = new Dictionary<string, long>(StringComparer.OrdinalIgnoreCase);
offDict[def.Name] = def.Offset;
}
}

Expand Down Expand Up @@ -198,7 +203,10 @@ void TraverseChain(StringPoolRow head)
a.Type,
a.Class,
strAttrs,
numAttrs);
numAttrs)
{
NumericAttributeOffsets = numAttrOffsetsByObj.GetValueOrDefault(a.Id)
};

assemblies.Add(assembly);
assembliesById[a.Id] = assembly;
Expand All @@ -207,7 +215,7 @@ void TraverseChain(StringPoolRow head)
// 5. Build Parts
var parts = new List<ModelPart>(partRows.Count);
var partsById = new Dictionary<int, ModelPart>(partRows.Count);
foreach (var p in partRows)
foreach (var (p, pBodyOffset) in partRows)
{
var obj = objects.GetValueOrDefault(p.Id);
int phase = phases.GetValueOrDefault(p.Id);
Expand Down Expand Up @@ -256,15 +264,19 @@ void TraverseChain(StringPoolRow head)
p.Length,
csys,
strAttrs,
numAttrs);
numAttrs,
lengthOffset: pBodyOffset + 56)
{
NumericAttributeOffsets = numAttrOffsetsByObj.GetValueOrDefault(p.Id)
};

parts.Add(part);
partsById[p.Id] = part;
}

// 6. Build Bolts and Bolt Holes
var holesByBolt = new Dictionary<int, List<ModelBoltHole>>();
foreach (var h in holeRows)
foreach (var (h, offset) in holeRows)
{
if (!holesByBolt.TryGetValue(h.BoltId, out var list))
holesByBolt[h.BoltId] = list = new List<ModelBoltHole>();
Expand All @@ -278,16 +290,19 @@ void TraverseChain(StringPoolRow head)
h.Length,
h.LongHoleDir,
h.LongHoleType,
h.ShorteningLength));
h.ShorteningLength)
{
Offset = offset
});
}

var patternPtsByBolt = new Dictionary<int, List<ModelPoint>>();
foreach (var bp in boltPtRows)
foreach (var (bp, offset) in boltPtRows)
{
if (!patternPtsByBolt.TryGetValue(bp.BoltId, out var list))
patternPtsByBolt[bp.BoltId] = list = new List<ModelPoint>();
list.Add(new ModelPoint(0, bp.IntPoint1X, bp.IntPoint1Y, bp.IntPoint1Z));
list.Add(new ModelPoint(0, bp.IntPoint2X, bp.IntPoint2Y, bp.IntPoint2Z));
list.Add(new ModelPoint(0, bp.IntPoint1X, bp.IntPoint1Y, bp.IntPoint1Z) { Offset = offset });
list.Add(new ModelPoint(0, bp.IntPoint2X, bp.IntPoint2Y, bp.IntPoint2Z) { Offset = offset + 24 });
}

var bolts = new List<ModelBolt>(boltRows.Count);
Expand Down Expand Up @@ -316,7 +331,10 @@ void TraverseChain(StringPoolRow head)
holes,
positions,
strAttrs,
numAttrs);
numAttrs)
{
NumericAttributeOffsets = numAttrOffsetsByObj.GetValueOrDefault(b.Id)
};

var connectedParts = new List<ModelPart>();
foreach (var h in holes)
Expand Down Expand Up @@ -350,7 +368,10 @@ void TraverseChain(StringPoolRow head)
obj.AssemblyId > 0 ? obj.AssemblyId : null,
w.CommonAttrId,
strAttrs,
numAttrs);
numAttrs)
{
NumericAttributeOffsets = numAttrOffsetsByObj.GetValueOrDefault(w.Id)
};

welds.Add(weld);
weldsById[w.Id] = weld;
Expand Down Expand Up @@ -446,7 +467,10 @@ void TraverseChain(StringPoolRow head)
o.ParentId > 0 ? o.ParentId : null,
o.AssemblyId > 0 ? o.AssemblyId : null,
strAttrs,
numAttrs);
numAttrs)
{
NumericAttributeOffsets = numAttrOffsetsByObj.GetValueOrDefault(o.Id)
};
genericObjectsById[o.Id] = genObj;
}
}
Expand Down
Loading
Loading