From 6d000a5703b935c6daaa1c75b542fe0959b53cb7 Mon Sep 17 00:00:00 2001 From: mie Date: Mon, 16 Nov 2026 17:03:21 +0300 Subject: [PATCH 1/2] print: stop a joined wall end flat at the partner near face Port rvt-rs #1474 / RE-488 to PlanPrinter.CollectLevel: a butt join (JoinType 0) to another wall of the level cuts back the overshoot past the partner near face. Orthogonal joins only; mutual end-to-end L corners, mitre joins, roof-joined walls and ambiguous/off-level ends keep the stored line; only shortening, never extension. Tests: PlanWallJoinCutbackTests (5 synthetic); fixture check on 2022/2025 projects shows every joined end there is a mutual L corner, so their plans are unchanged. --- CHANGELOG.md | 2 + docs/printing.md | 2 +- src/RvtIO/Print/PlanPrinter.cs | 82 ++++++++- tests/RvtIO.Tests/PlanWallJoinCutbackTests.cs | 156 ++++++++++++++++++ 4 files changed, 239 insertions(+), 3 deletions(-) create mode 100644 tests/RvtIO.Tests/PlanWallJoinCutbackTests.cs diff --git a/CHANGELOG.md b/CHANGELOG.md index 92288cad..dbe9732f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -134,6 +134,8 @@ The text of a sheet prints too: a text note's RTF string (class 0x0F83 block) is - Export report and diagnostics: `streams[].unpackedBytes` (`StreamRecord.UnpackedBytes`) records the inflated byte size of every decoded stream alongside `rawBytes`, and `export.geometry` (`exact`, `approximated`, `boxOnly`, `none`) and `export.elements[]` (`GeometryQualityClass`, `ElementGeometryQuality`, `IfcExporter.ExportedElementQualities`) report the geometry quality class of each exported element in IFC, compact JSON and full JSON exports with totals that sum to `export.exported`. (Closes #96) - Adds `--ifc-timestamp ` (an ISO-8601 timestamp or Unix epoch seconds; `IfcExporter.Timestamp` in the library) to write a fixed UTC timestamp into the STEP `FILE_NAME` header so repeated IFC exports of the same input are byte-for-byte identical, and adds CI-runnable corruption and fuzz tests over synthetic `.rvt` containers and inner streams with execution-time and memory-allocation bounds alongside full Windows, Linux and macOS CI test matrices. (Closes #39) +- Printing: a plan stops a wall flat at the near face of the wall it joins (rvt-rs #1474, RE-488): a butt join (JoinType 0) to another wall of the level cuts back the overshoot past the partner's near face, orthogonal joins only, mutual end-to-end L corners, mitre joins, roof-joined walls and ambiguous ends keep the stored line. New tests: `PlanWallJoinCutbackTests` (5, synthetic T/L/mitre/roof-joined/start-end); on the 2022 and 2025 fixture projects every joined end is a mutual L corner, so no printed plan changes there. + ### Changed - Reading: a profile roof's own area is no longer computed from the faces of its geometry (`ProfileRoof` step history and G-element graph, read for the current IFC profile only). Since the current profile writes the stored host area as `ProjectedArea`, as Revit's exports do, nothing read it. Measured on six project files of Revit 2020, 2022 and 2024: the IFC of both profiles is identical. diff --git a/docs/printing.md b/docs/printing.md index eb33d764..dc0e9c28 100644 --- a/docs/printing.md +++ b/docs/printing.md @@ -163,7 +163,7 @@ stays empty. | Content | Source | |---|---| -| Walls | the stored location line, drawn as two sides its stored width apart, the stretch between them poche (filled grey) with end caps (`WALL_ATTR_WIDTH_PARAM` of the wall or its type; 100 mm when neither stores one) | +| Walls | the stored location line, drawn as two sides its stored width apart, the stretch between them poche (filled grey) with end caps (`WALL_ATTR_WIDTH_PARAM` of the wall or its type; 100 mm when neither stores one); a stopping end joined to another wall (butt join) is cut back flat at the partner's near face instead of running through it, as in rvt-rs #1474 and RE-488 (orthogonal joins only; mutual end-to-end L corners, mitre joins, roof-joined walls and ambiguous ends keep the stored line) | | Openings | the cut loop of a hosted door or window, transformed to the model, leaves its stretch of wall out; the wall is drawn in the pieces between its openings, with a jamb at every cut, and a window gets a light line across the opening | | Columns | the stored bounding box footprint, as a filled rectangle | | Grids | the stored location lines, clipped to the extent of the level (a grid line can run far beyond the building), with a white bubble and the grid name at both ends | diff --git a/src/RvtIO/Print/PlanPrinter.cs b/src/RvtIO/Print/PlanPrinter.cs index 5e2cee00..dc7a964f 100644 --- a/src/RvtIO/Print/PlanPrinter.cs +++ b/src/RvtIO/Print/PlanPrinter.cs @@ -208,11 +208,86 @@ public static PdfDocument RenderViews(RvtDocument document, IReadOnlyList v return pdf; } + /// Shortens the ends of walls that stop at a joined wall, drawing them flat at the partner's near face + /// instead of through it (rvt-rs #1474 and RE-488): a butt join (JoinType 0, m_wallJoinType) whose end is not shared + /// with the partner's own end (a T stop, not an L corner both walls end at), on a wall with no top attachment and a + /// partner drawn on the same level with a straight location line. Only orthogonal joins move, only the overshoot past + /// the near face is taken back (never extended), and at most to a stub of the wall's own width. Mitre joins and + /// ambiguous ends (no single butt join, a partner off the level or without a line) keep the stored location line. + internal static List<(int Id, double X1, double Y1, double X2, double Y2, double Width)> CutBackJoinedEnds( + List<(int Id, double X1, double Y1, double X2, double Y2, double Width)> walls, + IReadOnlyDictionary elements) + { + if (walls.Count == 0) return walls; + var byId = new Dictionary(); + for (int i = 0; i < walls.Count; i++) byId[walls[i].Id] = i; + var result = walls.ToList(); + for (int i = 0; i < result.Count; i++) + { + var wall = result[i]; + if (!elements.TryGetValue(wall.Id, out var element)) continue; + if (element.SerializedWallJoinedToTop == true) continue; + if (element.SerializedWallEndJoins is not { Count: > 0 } joins) continue; + double dx = wall.X2 - wall.X1, dy = wall.Y2 - wall.Y1; + double length = Math.Sqrt(dx * dx + dy * dy); + if (length < 1e-6) continue; + double ux = dx / length, uy = dy / length; + for (int end = 0; end < 2; end++) + { + var candidates = joins.Where(join => join.End == end && join.JoinType == 0).ToList(); + if (candidates.Count != 1) continue; + var join = candidates[0]; + if (!byId.TryGetValue(join.ElementId, out int partnerIndex) || partnerIndex == i) continue; + var partner = result[partnerIndex]; + if (!elements.TryGetValue(partner.Id, out var partnerElement)) continue; + double pdx = partner.X2 - partner.X1, pdy = partner.Y2 - partner.Y1; + double partnerLength = Math.Sqrt(pdx * pdx + pdy * pdy); + if (partnerLength < 1e-6) continue; + double vx = pdx / partnerLength, vy = pdy / partnerLength; + // Orthogonal butt joins only: an angled join keeps the stored line. + if (Math.Abs(ux * vx + uy * vy) > 0.02) continue; + // An L corner both walls end at keeps the overlap: the partner names this wall back at one of its ends. + bool mutualEnd = partnerElement.SerializedWallEndJoins?.Any(partnerJoin => + partnerJoin.ElementId == wall.Id && partnerJoin.JoinedEnd is 0 or 1 && partnerJoin.JoinType == 0) == true; + if (mutualEnd && join.JoinedEnd is 0 or 1) continue; + // The near face of the partner on the side of this end: the face a point just inside + // the wall stands on (an end exactly on the partner's axis faces both sides at once). + double nx = -vy, ny = vx; + double ex = end == 0 ? wall.X1 : wall.X2, ey = end == 0 ? wall.Y1 : wall.Y2; + double s = (ex - partner.X1) * nx + (ey - partner.Y1) * ny; + double denom = ux * nx + uy * ny; + if (Math.Abs(denom) < 0.98) continue; + double probe = s - denom * 1e-3; + if (Math.Abs(probe) < 1e-9) continue; + double facing = Math.Sign(probe) * partner.Width / 2; + double t = (facing - s) / denom; + // The end must stand on the partner's span, and only an overshoot past the face is taken back. + double along = (ex - partner.X1) * vx + (ey - partner.Y1) * vy; + if (along < -partner.Width / 2 - 1 || along > partnerLength + partner.Width / 2 + 1) continue; + if (t >= -1) continue; + double newLength = length + t; + if (newLength < Math.Max(wall.Width, 10)) continue; + // t < 0 shortens the wall: the start moves forward along +u, the end moves back. + double newX = end == 0 ? ex - ux * t : ex + ux * t; + double newY = end == 0 ? ey - uy * t : ey + uy * t; + result[i] = end == 0 ? (wall.Id, newX, newY, wall.X2, wall.Y2, wall.Width) + : (wall.Id, wall.X1, wall.Y1, newX, newY, wall.Width); + wall = result[i]; + dx = wall.X2 - wall.X1; dy = wall.Y2 - wall.Y1; + length = Math.Sqrt(dx * dx + dy * dy); + if (length < 1e-6) break; + ux = dx / length; uy = dy / length; + } + } + return result; + } + /// Collects what the plan of a level draws: its walls (cut at the openings of their doors and windows), /// columns and grids, and the extents they span in model millimetres. Null when the level has nothing to draw. internal static PlanContent? CollectLevel(RvtDocument document, int levelId) { var walls = new List<(int Id, double X1, double Y1, double X2, double Y2, double Width)>(); + var wallElements = new Dictionary(); var columns = new List<(double X, double Y, double Width, double Height)>(); double minX = double.MaxValue, minY = double.MaxValue, maxX = double.MinValue, maxY = double.MinValue; void Grow(double x, double y) @@ -232,7 +307,7 @@ void Grow(double x, double y) double x2 = line.EndPoint[0] * FeetToMillimetres, y2 = line.EndPoint[1] * FeetToMillimetres; double width = WidthOf(element) ?? 100; walls.Add((element.Id, x1, y1, x2, y2, width)); - Grow(x1, y1); Grow(x2, y2); + wallElements[element.Id] = element; continue; } @@ -243,10 +318,13 @@ void Grow(double x, double y) double x = box[0] * FeetToMillimetres, y = box[1] * FeetToMillimetres; double width = (box[3] - box[0]) * FeetToMillimetres, height = (box[4] - box[1]) * FeetToMillimetres; columns.Add((x, y, width, height)); - Grow(x, y); Grow(x + width, y + height); } } + walls = CutBackJoinedEnds(walls, wallElements); + foreach (var wall in walls) { Grow(wall.X1, wall.Y1); Grow(wall.X2, wall.Y2); } + foreach (var column in columns) { Grow(column.X, column.Y); Grow(column.X + column.Width, column.Y + column.Height); } + if (walls.Count == 0 && columns.Count == 0) return null; // Openings: the cut loop of a hosted door or window, transformed to the model, gives the stretch of its wall to diff --git a/tests/RvtIO.Tests/PlanWallJoinCutbackTests.cs b/tests/RvtIO.Tests/PlanWallJoinCutbackTests.cs new file mode 100644 index 00000000..4d1fa577 --- /dev/null +++ b/tests/RvtIO.Tests/PlanWallJoinCutbackTests.cs @@ -0,0 +1,156 @@ +using RvtIO.BuiltIns; +using RvtIO.Model; +using RvtIO.Parsing; +using RvtIO.Print; +using Xunit; + +namespace RvtIO.Tests; + +/// The plan stops a wall flat at the near face of the wall it joins (rvt-rs #1474, RE-488): +/// a butt join (JoinType 0) to another wall of the level, orthogonal, mutual end-to-end L corners and +/// roof-joined, mitre and ambiguous ends left out. +public class PlanWallJoinCutbackTests +{ + private static void Add(Element element, BuiltInParameter id, StorageType storage, object value) => + element.Parameters.Add(BipHelpers.MakeBipParam((int)id, id.ToString(), storage, value)); + + private static Element Level(int id, string name, double elevationFeet) + { + var level = new Element { Id = id, Name = name, CategoryId = (int)BuiltInCategory.OST_Levels }; + Add(level, BuiltInParameter.LEVEL_ELEV, StorageType.Double, elevationFeet); + return level; + } + + private static Element Wall(int id, int levelId, double x1, double y1, double x2, double y2, + double widthFeet = 0.656168, bool roofJoined = false, + IReadOnlyList? joins = null) + { + var wall = new Element + { + Id = id, + CategoryId = (int)BuiltInCategory.OST_Walls, + Location = new LocationCurve + { + CurveType = "Line", + StartPoint = [x1, y1, 0], + EndPoint = [x2, y2, 0], + }, + }; + Add(wall, BuiltInParameter.LEVEL_PARAM, StorageType.ElementId, levelId); + Add(wall, BuiltInParameter.WALL_ATTR_WIDTH_PARAM, StorageType.Double, widthFeet); + if (roofJoined) wall.SerializedWallJoinedToTop = true; + if (joins != null) wall.SerializedWallEndJoins = joins; + return wall; + } + + private static double Feet(double feet) => feet * 304.8; + + [Fact] + public void StoppingEnd_IsCutAtThePartnerNearFace() + { + // Wall 600 runs into the middle of the vertical wall 601 (a T stop). + var document = RvtDocument.FromInfo(new RevitFileInfo + { + Elements = + [ + Level(500, "Level 1", 0), + Wall(600, 500, 0, 0, 10, 0, joins: [new TfBlockDeserializer.WallEndJoin(1, 601, 2, 0)]), + Wall(601, 500, 10, -5, 10, 5), + ], + }); + + var content = PlanPrinter.CollectLevel(document, 500); + + Assert.NotNull(content); + var wall = Assert.Single(content!.Walls, w => w.Id == 600); + // Partner half width 100 mm back from x = 10 ft. + Assert.Equal(Feet(0), wall.X1, 3); + Assert.Equal(Feet(10) - 100, wall.X2, 3); + Assert.Equal(Feet(0), wall.Y1, 3); + Assert.Equal(Feet(0), wall.Y2, 3); + } + + [Fact] + public void MutualEndJoin_KeepsTheOverlap() + { + // An L corner both walls end at: each names the other's end. + var document = RvtDocument.FromInfo(new RevitFileInfo + { + Elements = + [ + Level(500, "Level 1", 0), + Wall(600, 500, 0, 0, 10, 0, joins: [new TfBlockDeserializer.WallEndJoin(1, 601, 0, 0)]), + Wall(601, 500, 10, 0, 10, 10, joins: [new TfBlockDeserializer.WallEndJoin(0, 600, 1, 0)]), + ], + }); + + var content = PlanPrinter.CollectLevel(document, 500); + + Assert.NotNull(content); + var wall = Assert.Single(content!.Walls, w => w.Id == 600); + Assert.Equal(Feet(10), wall.X2, 3); + } + + [Fact] + public void MitreJoin_KeepsTheStoredLine() + { + var document = RvtDocument.FromInfo(new RevitFileInfo + { + Elements = + [ + Level(500, "Level 1", 0), + Wall(600, 500, 0, 0, 10, 0, joins: [new TfBlockDeserializer.WallEndJoin(1, 601, 2, 1)]), + Wall(601, 500, 10, -5, 10, 5), + ], + }); + + var content = PlanPrinter.CollectLevel(document, 500); + + Assert.NotNull(content); + var wall = Assert.Single(content!.Walls, w => w.Id == 600); + Assert.Equal(Feet(10), wall.X2, 3); + } + + [Fact] + public void RoofJoinedWall_KeepsTheStoredLine() + { + var document = RvtDocument.FromInfo(new RevitFileInfo + { + Elements = + [ + Level(500, "Level 1", 0), + Wall(600, 500, 0, 0, 10, 0, roofJoined: true, + joins: [new TfBlockDeserializer.WallEndJoin(1, 601, 2, 0)]), + Wall(601, 500, 10, -5, 10, 5), + ], + }); + + var content = PlanPrinter.CollectLevel(document, 500); + + Assert.NotNull(content); + var wall = Assert.Single(content!.Walls, w => w.Id == 600); + Assert.Equal(Feet(10), wall.X2, 3); + } + + [Fact] + public void StartEnd_IsCutToo() + { + // The wall runs from inside the partner outwards: its start is the stopping end. + var document = RvtDocument.FromInfo(new RevitFileInfo + { + Elements = + [ + Level(500, "Level 1", 0), + Wall(600, 500, 10, 0, 20, 0, joins: [new TfBlockDeserializer.WallEndJoin(0, 601, 2, 0)]), + Wall(601, 500, 10, -5, 10, 5), + ], + }); + + var content = PlanPrinter.CollectLevel(document, 500); + + Assert.NotNull(content); + var wall = Assert.Single(content!.Walls, w => w.Id == 600); + Assert.Equal(Feet(10) + 100, wall.X1, 3); + Assert.Equal(Feet(20), wall.X2, 3); + } +} From ae4acb3d691c6ee9f420c2b8d53d84253e8d14ff Mon Sep 17 00:00:00 2001 From: mie Date: Mon, 16 Nov 2026 18:30:38 +0300 Subject: [PATCH 2/2] print: cut joined wall ends to the retained-side near face Review #252: an end past the partner axis kept the far face (end stayed at 3050 instead of 2900) and starts moved to the axis (3000) instead of the face (3100). Choose the near face from the retained wall side (direction sign per end) and solve the +u displacement with per-end overshoot (end shortens on -t, start on +t). Offset tests cover both sides of the axis for starts and ends. --- src/RvtIO/Print/PlanPrinter.cs | 30 ++++++++++------- tests/RvtIO.Tests/PlanWallJoinCutbackTests.cs | 32 +++++++++++++++++++ 2 files changed, 51 insertions(+), 11 deletions(-) diff --git a/src/RvtIO/Print/PlanPrinter.cs b/src/RvtIO/Print/PlanPrinter.cs index dc7a964f..bc6381e3 100644 --- a/src/RvtIO/Print/PlanPrinter.cs +++ b/src/RvtIO/Print/PlanPrinter.cs @@ -250,26 +250,34 @@ public static PdfDocument RenderViews(RvtDocument document, IReadOnlyList v bool mutualEnd = partnerElement.SerializedWallEndJoins?.Any(partnerJoin => partnerJoin.ElementId == wall.Id && partnerJoin.JoinedEnd is 0 or 1 && partnerJoin.JoinType == 0) == true; if (mutualEnd && join.JoinedEnd is 0 or 1) continue; - // The near face of the partner on the side of this end: the face a point just inside - // the wall stands on (an end exactly on the partner's axis faces both sides at once). + // The near face of the partner on the side the wall comes from: travelling along + // the wall into the join first meets that face. For the end (tip in +u) the wall + // approaches along +u, so the retained interior lies behind (-denom side); for the + // start the wall leaves along +u, so the interior lies ahead (+denom side). A probe + // just inside the tip fails once the tip has crossed the partner axis, choosing the + // far face instead. double nx = -vy, ny = vx; double ex = end == 0 ? wall.X1 : wall.X2, ey = end == 0 ? wall.Y1 : wall.Y2; double s = (ex - partner.X1) * nx + (ey - partner.Y1) * ny; double denom = ux * nx + uy * ny; if (Math.Abs(denom) < 0.98) continue; - double probe = s - denom * 1e-3; - if (Math.Abs(probe) < 1e-9) continue; - double facing = Math.Sign(probe) * partner.Width / 2; + double side = end == 0 ? Math.Sign(denom) : -Math.Sign(denom); + if (side == 0) continue; + double facing = side * partner.Width / 2; + // Displacement along +u that plants the tip on the face. The start moves forward + // (+t shortens), the end moves back (-t shortens); only an overshoot past the face + // is taken back, never an extension, and at least a millimetre. double t = (facing - s) / denom; - // The end must stand on the partner's span, and only an overshoot past the face is taken back. + // The tip must stand on the partner's span, and only an overshoot past the face + // is taken back, never an extension, and at least a millimetre. double along = (ex - partner.X1) * vx + (ey - partner.Y1) * vy; if (along < -partner.Width / 2 - 1 || along > partnerLength + partner.Width / 2 + 1) continue; - if (t >= -1) continue; - double newLength = length + t; + bool overshoot = end == 0 ? t >= 1 : t <= -1; + if (!overshoot) continue; + double newLength = end == 0 ? length - t : length + t; if (newLength < Math.Max(wall.Width, 10)) continue; - // t < 0 shortens the wall: the start moves forward along +u, the end moves back. - double newX = end == 0 ? ex - ux * t : ex + ux * t; - double newY = end == 0 ? ey - uy * t : ey + uy * t; + double newX = ex + ux * t; + double newY = ey + uy * t; result[i] = end == 0 ? (wall.Id, newX, newY, wall.X2, wall.Y2, wall.Width) : (wall.Id, wall.X1, wall.Y1, newX, newY, wall.Width); wall = result[i]; diff --git a/tests/RvtIO.Tests/PlanWallJoinCutbackTests.cs b/tests/RvtIO.Tests/PlanWallJoinCutbackTests.cs index 4d1fa577..0e29dce0 100644 --- a/tests/RvtIO.Tests/PlanWallJoinCutbackTests.cs +++ b/tests/RvtIO.Tests/PlanWallJoinCutbackTests.cs @@ -153,4 +153,36 @@ public void StartEnd_IsCutToo() Assert.Equal(Feet(10) + 100, wall.X1, 3); Assert.Equal(Feet(20), wall.X2, 3); } + + private static double CutEnd(double x1, double x2, int end, bool checkStart) + { + var source = new Element + { + Id = 600, + SerializedWallEndJoins = [new TfBlockDeserializer.WallEndJoin(end, 601, 2, 0)], + }; + var partner = new Element { Id = 601 }; + var walls = new List<(int Id, double X1, double Y1, double X2, double Y2, double Width)> + { + (600, x1, 0, x2, 0, 200), + (601, 3000, -1000, 3000, 1000, 200), + }; + var result = PlanPrinter.CutBackJoinedEnds( + walls, new Dictionary { [600] = source, [601] = partner })[0]; + return checkStart ? result.X1 : result.X2; + } + + [Fact] + public void OffsetEnds_AreCutToTheNearFaceOnBothSides() + { + // Vertical partner centred at x=3000, width 200: the near face is 2900 for a wall + // ending from the west and 3100 for a wall starting to the east. The on-axis + // controls already pass through CollectLevel above. + Assert.Equal(2900, CutEnd(0, 3000, 1, checkStart: false), 3); + Assert.Equal(2900, CutEnd(0, 3050, 1, checkStart: false), 3); + Assert.Equal(2900, CutEnd(0, 2950, 1, checkStart: false), 3); + Assert.Equal(3100, CutEnd(3000, 6000, 0, checkStart: true), 3); + Assert.Equal(3100, CutEnd(2950, 6000, 0, checkStart: true), 3); + Assert.Equal(3100, CutEnd(3050, 6000, 0, checkStart: true), 3); + } }