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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ API between minor releases.
their attributes.
- JSON output of the object model: `TeklaModel.ToJson()` and `WriteJson`, and `tdbio export model.db1 model.json`.
- IFC output of the object model: `TeklaModel.ToIfc()` and `WriteIfc` with `TeklaIfcExportOptions` (IFC2x3 and IFC4),
and `tdbio export model.db1 model.ifc`. Each straight part is approximated by a box.
and `tdbio export model.db1 model.ifc`. Each straight part is approximated by a box; a part whose end points are less
than 0.001 mm apart gets no body.
- The `TdbIO.Scene` package: `TdbSceneReader` turns the object model into an IOScene scene document in metres, and
`tdbio export model.db1 model.scene.json --scene-json` writes it.
- A standalone HTML viewer of the scene: `tdbio export model.db1 viewer.html`.
Expand Down
10 changes: 6 additions & 4 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ An option given twice is a usage error.

## Geometry warnings of an export

The IFC, scene JSON and HTML viewer exports draw every part as a rectangular proxy box between its start and end point,
The IFC, scene JSON and HTML viewer exports draw each part as a rectangular proxy box between its start and end point,
with the width and height estimated from the numbers in the profile name (150 mm × 150 mm when none are read). The
estimate does not check the profile. The meshes are not decoded Tekla solids and are not made for clash detection or
exact measurement. The warnings go to standard error, so the output on standard output or in the file holds none of
them, and they do not change the exit code. The JSON export of the object model has no geometry and no such warning.
estimate does not check the profile. A part with no start or end point, or with the two less than 0.001 mm apart, gets
no box; the IFC export keeps its element without a body. The meshes are not decoded Tekla solids and are not made for
clash detection or exact measurement. The warnings go to standard error, so the output on standard output or in the file
holds none of them, and they do not change the exit code. The JSON export of the object model has no geometry and no
such warning.

All three exports print one line with the number of proxy boxes. The scene JSON export (`--scene-json`) and the HTML
viewer export build the scene of the `TdbIO.Scene` adapter and then also print, each after a summary line, one line per
Expand Down
11 changes: 9 additions & 2 deletions src/TdbIO.Cli/Cli.cs
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,7 @@ private static IReadOnlyList<string> Export(TextWriter w, TdbDocument doc, Comma

if (cmd.Ifc)
{
// The parts the IFC output gives a body: those with both points.
int boxes = model.Parts.Count(part => part.StartPoint.HasValue && part.EndPoint.HasValue);
int boxes = model.Parts.Count(HasIfcBody);
if (boxes > 0) geometryWarnings.Add(ProxyBoxWarning(boxes, model.Parts.Count));
if (cmd.OutputPath != null)
{
Expand Down Expand Up @@ -476,6 +475,14 @@ private static IReadOnlyList<string> Export(TextWriter w, TdbDocument doc, Comma
return geometryWarnings;
}

// The parts the IFC output gives a body: those with two points at least 0.001 mm apart.
private static bool HasIfcBody(ModelPart part)
{
if (part.StartPoint is not { } start || part.EndPoint is not { } end) return false;
double dx = end.X - start.X, dy = end.Y - start.Y, dz = end.Z - start.Z;
return Math.Sqrt(dx * dx + dy * dy + dz * dz) >= 0.001;
}

// The warning lines for the geometry of a scene conversion: one line for the proxy boxes, then for the parts with
// fallback dimensions and for those without a usable geometry a summary line and one detail line per part, up to
// DetailLimit parts per kind. Without the 'tdbio: warning: ' prefix; the caller adds it when the lines are written to
Expand Down
23 changes: 16 additions & 7 deletions src/TdbIO/Ifc/IfcExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,11 @@ public static void Write(
{
var p0 = part.StartPoint.Value;
var p1 = part.EndPoint.Value;
int geomId = WritePartFacetedBrep(w, p0, p1, part.Profile, scale);
shapeRepId = w.Add("IFCSHAPEREPRESENTATION",
$"{StepWriter.Ref(bodySubContextId)},'Body','Brep',({StepWriter.Ref(geomId)})");
if (WritePartFacetedBrep(w, p0, p1, part.Profile, scale) is int geomId)
{
shapeRepId = w.Add("IFCSHAPEREPRESENTATION",
$"{StepWriter.Ref(bodySubContextId)},'Body','Brep',({StepWriter.Ref(geomId)})");
}
}

int? prodShapeId = shapeRepId.HasValue
Expand Down Expand Up @@ -350,18 +352,25 @@ private static int AddIfcProduct(
};
}

private static int WritePartFacetedBrep(StepWriter w, ModelPoint p0, ModelPoint p1, string profile, double scale)
// Null, with nothing written, when the axis is too short to give the box a direction.
private static int? WritePartFacetedBrep(StepWriter w, ModelPoint p0, ModelPoint p1, string profile, double scale)
{
// Vector along the beam axis
double dx = (p1.X - p0.X) * scale;
double dy = (p1.Y - p0.Y) * scale;
double dz = (p1.Z - p0.Z) * scale;
double len = Math.Sqrt(dx * dx + dy * dy + dz * dz);

// 0.001 mm in the units of the output: the same limit as in the scene export.
if (len < 0.001 * scale)
{
return null;
}

// Normalize direction
double dirX = len > 1e-6 ? dx / len : 1.0;
double dirY = len > 1e-6 ? dy / len : 0.0;
double dirZ = len > 1e-6 ? dz / len : 0.0;
double dirX = dx / len;
double dirY = dy / len;
double dirZ = dz / len;

// Find perpendicular vectors (u and v)
double perpX, perpY, perpZ;
Expand Down
53 changes: 53 additions & 0 deletions tests/TdbIO.Tests/CliTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,59 @@ public void Export_AcceptsFiniteCustomProfileWithSubstringsAcrossAllRenderers()
Assert.Contains("CUSTOM_NANO200", System.IO.File.ReadAllText(htmlPath));
}

[Fact]
public void Export_IfcCountsNoProxyBoxForAPartWithCoincidentEndPoints()
{
byte[] ObjectBytes(int id)
{
var bytes = new byte[ObjectRow.ExpectedRowSize];
BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(0), id);
BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(28), 1);
return bytes;
}

byte[] PointBytes(int id, double x)
{
var bytes = new byte[PointRow.ExpectedRowSize];
BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(0), id);
BinaryPrimitives.WriteDoubleLittleEndian(bytes.AsSpan(8), x);
return bytes;
}

byte[] PartBytes(int id, int startPoint, int endPoint)
{
var bytes = new byte[PartRow.ExpectedRowSize];
BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(0), id);
BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(12), startPoint);
BinaryPrimitives.WriteInt32LittleEndian(bytes.AsSpan(16), endPoint);
return bytes;
}

string path = File("coincident.db1", Gzip(Db1TablesPayload(
Db1Section(null, 4, [1], (0x04, Counting(4), 1, 2)),
Db1Section(ObjectRow.TableKey, ObjectRow.ExpectedRowSize, [1, 1],
(0x04, ObjectBytes(101), 1, 2), (0x04, ObjectBytes(102), 2, 3)),
Db1Section(PointRow.TableKey, PointRow.ExpectedRowSize, [1, 1],
(0x04, PointBytes(201, 0), 1, 2), (0x04, PointBytes(202, 6000), 2, 3)),
Db1Section(PartRow.TableKey, PartRow.ExpectedRowSize, [1, 1],
(0x04, PartBytes(101, 201, 202), 1, 2), (0x04, PartBytes(102, 201, 201), 2, 3)))));

var (ifcExit, ifcOut, ifcErr) = Run("export", path, "--ifc");
Assert.Equal(Cli.ExitOk, ifcExit);
string warning = Assert.Single(Lines(ifcErr));
Assert.StartsWith("tdbio: warning: 1 of 2 parts uses rectangular proxy boxes", warning);
Assert.Single(Lines(ifcOut), line => line.Contains("IFCFACETEDBREP(", StringComparison.Ordinal));
// Both parts stay, each with its ID as the tag; only the first one refers to a shape.
string[] elements = Lines(ifcOut).Where(line => line.Contains("=IFCMEMBER(", StringComparison.Ordinal)).ToArray();
Assert.Equal(2, elements.Length);
Assert.Matches(@",'Part_101',\$,\$,#\d+,#\d+,'101'\);$", elements[0]);
Assert.Matches(@",'Part_102',\$,\$,#\d+,\$,'102'\);$", elements[1]);

var (sceneExit, _, sceneErr) = Run("export", path, "--scene-json");
Assert.Equal(Cli.ExitOk, sceneExit);
Assert.Equal(warning, Lines(sceneErr)[0]);
}

private static void AssertInputError(params string[] args)
{
var (exit, stdout, stderr) = Run(args);
Expand Down
23 changes: 23 additions & 0 deletions tests/TdbIO.Tests/IfcExportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,29 @@ public void Ifc_ExportGeometryFalse_OmitsProductDefinitionShape()
Assert.All(columns, c => Assert.Equal("$", c.Args[6]));
}

// An axis shorter than 0.001 mm gives no direction to build a box along, in either unit of the output.
[Theory]
[InlineData(0.0, true, 0)]
[InlineData(0.0005, true, 0)]
[InlineData(0.0005, false, 0)]
[InlineData(0.002, true, 1)]
[InlineData(0.002, false, 1)]
public void Ifc_PartWithCoincidentEndPoints_HasNoBody(double lengthMm, bool metres, int bodies)
{
var p0 = new ModelPoint(1, 100, 200, 300);
var p1 = new ModelPoint(2, 100, 200, 300 + lengthMm);
var part = new ModelPart(1, Guid.Empty, 1, 0, 1, null, null, "PART", "PL10", "S235", "", 1, 1, 1, "", p0, p1, lengthMm, null);
var doc = Db1Document.Create(Synthetic.Db1Payload([]));
var model = new TeklaModel(doc, Guid.Empty, [], [part], [], [], [part]);

var entities = ParseIfcEntities(model.ToIfc(new TeklaIfcExportOptions { ConvertMmToMetres = metres }));

Assert.Equal(bodies, entities.Values.Count(e => e.Type == "IFCFACETEDBREP"));
Assert.Equal(bodies, entities.Values.Count(e => e.Type == "IFCPRODUCTDEFINITIONSHAPE"));
var element = Assert.Single(entities.Values, e => e.Type is "IFCBEAM" or "IFCCOLUMN" or "IFCPLATE" or "IFCMEMBER");
Assert.Equal(bodies == 0, element.Args[6] == "$");
}

[Fact]
public void Ifc_Units_ConvertMmToMetres_ScalesLengthAndDeclaresUnit()
{
Expand Down
Loading