bimdown-cli 0.3.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (151) hide show
  1. package/README.md +24 -3
  2. package/dist/chunk-4SKM4RHY.js +142 -0
  3. package/dist/chunk-4SKM4RHY.js.map +1 -0
  4. package/dist/chunk-4YHVOWS4.js +255 -0
  5. package/dist/chunk-4YHVOWS4.js.map +1 -0
  6. package/dist/chunk-AYAXRYGP.js +2129 -0
  7. package/dist/chunk-AYAXRYGP.js.map +1 -0
  8. package/dist/chunk-EC5MR5ZH.js +648 -0
  9. package/dist/chunk-EC5MR5ZH.js.map +1 -0
  10. package/dist/chunk-HS3PO2LB.js +487 -0
  11. package/dist/chunk-HS3PO2LB.js.map +1 -0
  12. package/dist/chunk-IQJA3EFO.js +179 -0
  13. package/dist/chunk-IQJA3EFO.js.map +1 -0
  14. package/dist/chunk-JY6KWRDB.js +80 -0
  15. package/dist/chunk-JY6KWRDB.js.map +1 -0
  16. package/dist/chunk-KWSGJUMZ.js +2501 -0
  17. package/dist/chunk-KWSGJUMZ.js.map +1 -0
  18. package/dist/chunk-LWYIPR64.js +35 -0
  19. package/dist/chunk-LWYIPR64.js.map +1 -0
  20. package/dist/chunk-OBY3UYCO.js +44 -0
  21. package/dist/chunk-OBY3UYCO.js.map +1 -0
  22. package/dist/chunk-XC6WHJRL.js +29 -0
  23. package/dist/chunk-XC6WHJRL.js.map +1 -0
  24. package/dist/chunk-XQU52JKB.js +187 -0
  25. package/dist/chunk-XQU52JKB.js.map +1 -0
  26. package/dist/clash-load-D3LNQR6G.js +941 -0
  27. package/dist/clash-load-D3LNQR6G.js.map +1 -0
  28. package/dist/cli.js +431 -0
  29. package/dist/cli.js.map +1 -0
  30. package/dist/dehydrate-ZHAGOI7Q.js +90 -0
  31. package/dist/dehydrate-ZHAGOI7Q.js.map +1 -0
  32. package/dist/diff-T2KOK2WR.js +89 -0
  33. package/dist/diff-T2KOK2WR.js.map +1 -0
  34. package/dist/engine-2TPWJVEQ.js +12 -0
  35. package/dist/engine-2TPWJVEQ.js.map +1 -0
  36. package/dist/hydrate-V24H7IF3.js +673 -0
  37. package/dist/hydrate-V24H7IF3.js.map +1 -0
  38. package/dist/import-ifc-RCZSXDA6.js +5324 -0
  39. package/dist/import-ifc-RCZSXDA6.js.map +1 -0
  40. package/dist/info-QC7YXWNX.js +92 -0
  41. package/dist/info-QC7YXWNX.js.map +1 -0
  42. package/dist/init-VWDML3UC.js +112 -0
  43. package/dist/init-VWDML3UC.js.map +1 -0
  44. package/dist/lib.d.ts +492 -0
  45. package/dist/lib.js +6528 -0
  46. package/dist/lib.js.map +1 -0
  47. package/dist/mep-W4HJI363.js +25 -0
  48. package/dist/mep-W4HJI363.js.map +1 -0
  49. package/dist/merge-ESOPDSA5.js +413 -0
  50. package/dist/merge-ESOPDSA5.js.map +1 -0
  51. package/dist/publish-3TAXQCUV.js +80 -0
  52. package/dist/publish-3TAXQCUV.js.map +1 -0
  53. package/dist/render-UEWYHPN6.js +1807 -0
  54. package/dist/render-UEWYHPN6.js.map +1 -0
  55. package/dist/resolve-topology-AQ2RO3W3.js +338 -0
  56. package/dist/resolve-topology-AQ2RO3W3.js.map +1 -0
  57. package/dist/spec/csv-schema/annotation/dimension.yaml +77 -0
  58. package/dist/spec/csv-schema/annotation/tag.yaml +44 -0
  59. package/dist/spec/csv-schema/annotation/text_note.yaml +52 -0
  60. package/dist/spec/csv-schema/architecture/ceiling.yaml +8 -1
  61. package/dist/spec/csv-schema/architecture/column.yaml +5 -1
  62. package/dist/spec/csv-schema/architecture/curtain_wall.yaml +45 -5
  63. package/dist/spec/csv-schema/architecture/door.yaml +5 -0
  64. package/dist/spec/csv-schema/architecture/furniture.yaml +28 -0
  65. package/dist/spec/csv-schema/architecture/opening.yaml +27 -6
  66. package/dist/spec/csv-schema/architecture/railing.yaml +35 -0
  67. package/dist/spec/csv-schema/architecture/roof.yaml +34 -1
  68. package/dist/spec/csv-schema/architecture/slab.yaml +19 -0
  69. package/dist/spec/csv-schema/architecture/space.yaml +11 -1
  70. package/dist/spec/csv-schema/architecture/stair.yaml +49 -0
  71. package/dist/spec/csv-schema/architecture/stair_landing.yaml +36 -0
  72. package/dist/spec/csv-schema/architecture/stair_run.yaml +31 -0
  73. package/dist/spec/csv-schema/architecture/wall.yaml +14 -1
  74. package/dist/spec/csv-schema/architecture/window.yaml +19 -0
  75. package/dist/spec/csv-schema/base/element.yaml +20 -11
  76. package/dist/spec/csv-schema/base/furniture_params.yaml +60 -0
  77. package/dist/spec/csv-schema/base/hosted_element.yaml +10 -2
  78. package/dist/spec/csv-schema/base/mep_connectable.yaml +23 -0
  79. package/dist/spec/csv-schema/base/mep_design.yaml +24 -0
  80. package/dist/spec/csv-schema/base/mep_insulated.yaml +9 -0
  81. package/dist/spec/csv-schema/base/mep_system_tag.yaml +12 -0
  82. package/dist/spec/csv-schema/base/point_element.yaml +1 -0
  83. package/dist/spec/csv-schema/base/section_profile.yaml +5 -0
  84. package/dist/spec/csv-schema/base/spatial_line_element.yaml +4 -2
  85. package/dist/spec/csv-schema/base/structural_section_profile.yaml +50 -0
  86. package/dist/spec/csv-schema/base/vertical_span.yaml +9 -3
  87. package/dist/spec/csv-schema/base/wall_bounded.yaml +21 -0
  88. package/dist/spec/csv-schema/grid.yaml +5 -0
  89. package/dist/spec/csv-schema/mep/cable_tray.yaml +10 -3
  90. package/dist/spec/csv-schema/mep/circuit.yaml +70 -0
  91. package/dist/spec/csv-schema/mep/conduit.yaml +2 -2
  92. package/dist/spec/csv-schema/mep/connector.yaml +112 -0
  93. package/dist/spec/csv-schema/mep/duct.yaml +10 -3
  94. package/dist/spec/csv-schema/mep/equipment.yaml +18 -1
  95. package/dist/spec/csv-schema/mep/mep_node.yaml +91 -2
  96. package/dist/spec/csv-schema/mep/mep_system.yaml +21 -0
  97. package/dist/spec/csv-schema/mep/pipe.yaml +27 -3
  98. package/dist/spec/csv-schema/mep/terminal.yaml +22 -1
  99. package/dist/spec/csv-schema/model_group.yaml +40 -0
  100. package/dist/spec/csv-schema/sheet/sheet.yaml +50 -0
  101. package/dist/spec/csv-schema/sheet/sheet_viewport.yaml +86 -0
  102. package/dist/spec/csv-schema/structure/beam.yaml +29 -1
  103. package/dist/spec/csv-schema/structure/beam_system.yaml +29 -0
  104. package/dist/spec/csv-schema/structure/brace.yaml +24 -1
  105. package/dist/spec/csv-schema/structure/foundation.yaml +40 -7
  106. package/dist/spec/csv-schema/structure/structure_column.yaml +19 -1
  107. package/dist/spec/csv-schema/structure/structure_slab.yaml +32 -0
  108. package/dist/spec/csv-schema/structure/structure_wall.yaml +22 -0
  109. package/dist/spec/csv-schema/types/beam_type.yaml +11 -0
  110. package/dist/spec/csv-schema/types/ceiling_type.yaml +6 -0
  111. package/dist/spec/csv-schema/types/column_type.yaml +12 -0
  112. package/dist/spec/csv-schema/types/curtain_wall_type.yaml +32 -0
  113. package/dist/spec/csv-schema/types/door_type.yaml +27 -0
  114. package/dist/spec/csv-schema/types/element_type.yaml +21 -0
  115. package/dist/spec/csv-schema/types/fenestration.yaml +16 -0
  116. package/dist/spec/csv-schema/types/foundation_type.yaml +36 -0
  117. package/dist/spec/csv-schema/types/furniture_type.yaml +11 -0
  118. package/dist/spec/csv-schema/types/layered_type.yaml +13 -0
  119. package/dist/spec/csv-schema/types/railing_type.yaml +40 -0
  120. package/dist/spec/csv-schema/types/roof_type.yaml +7 -0
  121. package/dist/spec/csv-schema/types/slab_type.yaml +9 -0
  122. package/dist/spec/csv-schema/types/type_layer.yaml +47 -0
  123. package/dist/spec/csv-schema/types/wall_type.yaml +6 -0
  124. package/dist/spec/csv-schema/types/window_type.yaml +31 -0
  125. package/dist/spec/csv-schema/underlay.yaml +73 -0
  126. package/dist/spec/csv-schema/view.yaml +36 -0
  127. package/dist/spec/duckdb-strategy.md +4 -3
  128. package/dist/spec/geojson-schema/readme.md +380 -0
  129. package/dist/spec/mep-port-conventions.md +190 -0
  130. package/dist/spec/migration-guide.md +1 -1
  131. package/dist/spec/project_metadata.schema.yaml +52 -4
  132. package/dist/spec/readme.md +356 -97
  133. package/dist/spec/starter-types/global/beam_type.csv +5 -0
  134. package/dist/spec/starter-types/global/ceiling_type.csv +3 -0
  135. package/dist/spec/starter-types/global/column_type.csv +5 -0
  136. package/dist/spec/starter-types/global/curtain_wall_type.csv +2 -0
  137. package/dist/spec/starter-types/global/door_type.csv +6 -0
  138. package/dist/spec/starter-types/global/foundation_type.csv +7 -0
  139. package/dist/spec/starter-types/global/furniture_type.csv +11 -0
  140. package/dist/spec/starter-types/global/railing_type.csv +5 -0
  141. package/dist/spec/starter-types/global/roof_type.csv +3 -0
  142. package/dist/spec/starter-types/global/slab_type.csv +4 -0
  143. package/dist/spec/starter-types/global/type_layer.csv +40 -0
  144. package/dist/spec/starter-types/global/wall_type.csv +8 -0
  145. package/dist/spec/starter-types/global/window_type.csv +6 -0
  146. package/dist/spec/svg-schema/readme.md +47 -1
  147. package/package.json +17 -5
  148. package/dist/index.js +0 -3366
  149. package/dist/index.js.map +0 -1
  150. package/dist/spec/csv-schema/base/mep_connected_segment.yaml +0 -17
  151. package/dist/spec/csv-schema/base/mep_system.yaml +0 -7
@@ -0,0 +1,1807 @@
1
+ #!/usr/bin/env node
2
+ import {
3
+ centroid,
4
+ roofInputFor,
5
+ signedArea,
6
+ solveRoof
7
+ } from "./chunk-HS3PO2LB.js";
8
+ import {
9
+ STAIR_CUT_HEIGHT,
10
+ arcPath,
11
+ cellKinds,
12
+ curtainEmbedSpan,
13
+ embedPointAt,
14
+ furniturePlan,
15
+ gridLines,
16
+ loadStairs,
17
+ parseDimensionRef,
18
+ parsePanels,
19
+ parseSlopeArrow,
20
+ railPlan,
21
+ resolveFurniture,
22
+ runDir,
23
+ runPlan,
24
+ walkingLine,
25
+ winderGeometry
26
+ } from "./chunk-AYAXRYGP.js";
27
+ import {
28
+ UNIT_TO_M,
29
+ projectUnits
30
+ } from "./chunk-LWYIPR64.js";
31
+ import {
32
+ effectiveRows
33
+ } from "./chunk-XQU52JKB.js";
34
+ import {
35
+ parseGeoJsonFile
36
+ } from "./chunk-4SKM4RHY.js";
37
+ import {
38
+ GEOJSON_FILE_NAMES
39
+ } from "./chunk-4YHVOWS4.js";
40
+ import {
41
+ readCsv
42
+ } from "./chunk-JY6KWRDB.js";
43
+ import {
44
+ discoverLayout
45
+ } from "./chunk-OBY3UYCO.js";
46
+
47
+ // src/render.ts
48
+ import { existsSync as existsSync4 } from "fs";
49
+ import { dirname as dirname2, join as join4 } from "path";
50
+
51
+ // src/annotations.ts
52
+ import { existsSync } from "fs";
53
+ import { join } from "path";
54
+ var sub = (a, b) => ({ x: a.x - b.x, y: a.y - b.y });
55
+ var add = (a, b) => ({ x: a.x + b.x, y: a.y + b.y });
56
+ var mul = (a, k) => ({ x: a.x * k, y: a.y * k });
57
+ var dot = (a, b) => a.x * b.x + a.y * b.y;
58
+ var len = (a) => Math.hypot(a.x, a.y);
59
+ var mid = (a, b) => ({ x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 });
60
+ function projectOntoLine(p, a, b) {
61
+ const d = sub(b, a);
62
+ const l2 = dot(d, d);
63
+ if (l2 < 1e-12) return a;
64
+ return add(a, mul(d, dot(sub(p, a), d) / l2));
65
+ }
66
+ function shoelace(v) {
67
+ let s = 0;
68
+ for (let i = 0; i < v.length; i++) {
69
+ const a = v[i], b = v[(i + 1) % v.length];
70
+ s += a.x * b.y - b.x * a.y;
71
+ }
72
+ return s / 2;
73
+ }
74
+ function centroid2(v) {
75
+ const a = shoelace(v);
76
+ if (Math.abs(a) < 1e-10) return mul(v.reduce((s, p) => add(s, p), { x: 0, y: 0 }), 1 / v.length);
77
+ let cx = 0, cy = 0;
78
+ for (let i = 0; i < v.length; i++) {
79
+ const p = v[i], q = v[(i + 1) % v.length];
80
+ const c = p.x * q.y - q.x * p.y;
81
+ cx += (p.x + q.x) * c;
82
+ cy += (p.y + q.y) * c;
83
+ }
84
+ return { x: cx / (6 * a), y: cy / (6 * a) };
85
+ }
86
+ function csvRows(dir, table) {
87
+ const p = join(dir, `${table}.csv`);
88
+ if (!existsSync(p)) return [];
89
+ try {
90
+ return readCsv(p).rows;
91
+ } catch {
92
+ return [];
93
+ }
94
+ }
95
+ function indexLevelElements(levelPath, globalPath) {
96
+ const out = /* @__PURE__ */ new Map();
97
+ for (const dir of [globalPath, levelPath]) {
98
+ for (const [table, fileBase] of Object.entries(GEOJSON_FILE_NAMES)) {
99
+ if (table === "space") continue;
100
+ const path = join(dir, `${fileBase}.geojson`);
101
+ if (!existsSync(path)) continue;
102
+ const attrsById = new Map(csvRows(dir, table).map((r) => [r.id, r]));
103
+ let fc;
104
+ try {
105
+ fc = parseGeoJsonFile(path);
106
+ } catch {
107
+ continue;
108
+ }
109
+ for (const f2 of fc.features) {
110
+ const id = String(f2.properties?.id ?? "");
111
+ if (!id) continue;
112
+ const attrs = attrsById.get(id) ?? { id };
113
+ const g = f2.geometry;
114
+ if (g.type === "LineString" && g.coordinates.length >= 2) {
115
+ const a = g.coordinates[0], b = g.coordinates[g.coordinates.length - 1];
116
+ const t = parseFloat(attrs.thickness ?? "");
117
+ out.set(id, { kind: "line", table, start: { x: a[0], y: a[1] }, end: { x: b[0], y: b[1] }, thickness: Number.isFinite(t) ? t : 0, attrs });
118
+ } else if (g.type === "Point") {
119
+ out.set(id, { kind: "point", table, pos: { x: g.coordinates[0], y: g.coordinates[1] }, attrs });
120
+ } else if (g.type === "Polygon" && g.coordinates[0]?.length >= 3) {
121
+ const ring = g.coordinates[0].map((c) => ({ x: c[0], y: c[1] }));
122
+ const last = ring[ring.length - 1];
123
+ if (last.x === ring[0].x && last.y === ring[0].y) ring.pop();
124
+ out.set(id, { kind: "polygon", table, vertices: ring, attrs });
125
+ }
126
+ }
127
+ }
128
+ }
129
+ for (const r of csvRows(globalPath, "grid")) {
130
+ const [sx, sy, ex, ey] = [r.start_x, r.start_y, r.end_x, r.end_y].map(Number);
131
+ if ([sx, sy, ex, ey].every(Number.isFinite)) {
132
+ out.set(r.id, { kind: "line", table: "grid", start: { x: sx, y: sy }, end: { x: ex, y: ey }, thickness: 0, attrs: r });
133
+ }
134
+ }
135
+ for (const table of ["door", "window"]) {
136
+ for (const r of csvRows(levelPath, table)) {
137
+ const host = out.get(r.host_id ?? "");
138
+ const pos = parseFloat(r.position ?? "");
139
+ const width = parseFloat(r.width ?? "");
140
+ if (!host || host.kind !== "line" || !Number.isFinite(pos) || !Number.isFinite(width)) continue;
141
+ const d = sub(host.end, host.start);
142
+ const l = len(d);
143
+ if (l < 1e-9) continue;
144
+ const u = mul(d, 1 / l);
145
+ const c = add(host.start, mul(u, pos));
146
+ out.set(r.id, { kind: "opening", table, start: sub(c, mul(u, width / 2)), end: add(c, mul(u, width / 2)), thickness: host.thickness, attrs: r });
147
+ }
148
+ }
149
+ const polys = /* @__PURE__ */ new Map();
150
+ const netAreas = /* @__PURE__ */ new Map();
151
+ const spacePath = join(levelPath, "space.geojson");
152
+ if (existsSync(spacePath)) {
153
+ try {
154
+ for (const f2 of parseGeoJsonFile(spacePath).features) {
155
+ if (f2.geometry.type !== "Polygon") continue;
156
+ const ring = f2.geometry.coordinates[0].map((c) => ({ x: c[0], y: c[1] }));
157
+ const last = ring[ring.length - 1];
158
+ if (ring.length && last.x === ring[0].x && last.y === ring[0].y) ring.pop();
159
+ polys.set(String(f2.properties.id), ring);
160
+ if (typeof f2.properties.net_area === "number") netAreas.set(String(f2.properties.id), f2.properties.net_area);
161
+ }
162
+ } catch {
163
+ }
164
+ }
165
+ for (const r of csvRows(levelPath, "space")) {
166
+ const x = parseFloat(r.x ?? ""), y = parseFloat(r.y ?? "");
167
+ if (!Number.isFinite(x) || !Number.isFinite(y)) continue;
168
+ const vertices = polys.get(r.id);
169
+ out.set(r.id, { kind: "space", table: "space", pos: { x, y }, vertices, area: netAreas.get(r.id) ?? (vertices ? Math.abs(shoelace(vertices)) : void 0), attrs: r });
170
+ }
171
+ return out;
172
+ }
173
+ function elementAnchor(el) {
174
+ switch (el.kind) {
175
+ case "line":
176
+ case "opening":
177
+ return mid(el.start, el.end);
178
+ case "point":
179
+ case "space":
180
+ return el.pos;
181
+ case "polygon":
182
+ return centroid2(el.vertices);
183
+ }
184
+ }
185
+ function resolveDimensionPoint(ref, stored, index) {
186
+ const r = ref.trim();
187
+ if (!r) return stored;
188
+ const { id, anchor } = parseDimensionRef(r);
189
+ const el = index.get(id);
190
+ if (!el) return stored;
191
+ if (el.kind === "line" || el.kind === "opening") {
192
+ if (anchor === "start") return el.start;
193
+ if (anchor === "end") return el.end;
194
+ if (anchor === "center") return mid(el.start, el.end);
195
+ if (el.kind === "opening") return mid(el.start, el.end);
196
+ let a = el.start, b = el.end;
197
+ if (anchor === "left" || anchor === "right") {
198
+ const d = sub(b, a);
199
+ const l = len(d);
200
+ if (l > 1e-9) {
201
+ const n = mul({ x: -d.y / l, y: d.x / l }, (anchor === "left" ? 1 : -1) * el.thickness / 2);
202
+ a = add(a, n);
203
+ b = add(b, n);
204
+ }
205
+ }
206
+ return projectOntoLine(stored, a, b);
207
+ }
208
+ return elementAnchor(el);
209
+ }
210
+ function dimensionGeometry(p1, p2, direction, offset) {
211
+ let u;
212
+ if (direction !== null && Number.isFinite(direction)) {
213
+ const a = direction * Math.PI / 180;
214
+ u = { x: Math.cos(a), y: Math.sin(a) };
215
+ } else {
216
+ const d = sub(p2, p1);
217
+ const l = len(d);
218
+ if (l < 1e-9) return null;
219
+ u = mul(d, 1 / l);
220
+ }
221
+ const n = { x: -u.y, y: u.x };
222
+ const signed = dot(sub(p2, p1), u);
223
+ const d1 = add(p1, mul(n, offset));
224
+ const d2 = add(d1, mul(u, signed));
225
+ return { d1, d2, value: Math.abs(signed), u, n };
226
+ }
227
+ function formatDimension(value, units) {
228
+ if (units === "m") return String(Math.round(value * 1e3));
229
+ if (units === "mm") return String(Math.round(value));
230
+ const totalIn = units === "ft" ? value * 12 : value;
231
+ let ft = Math.floor(totalIn / 12);
232
+ let inch = Math.round((totalIn - ft * 12) * 4) / 4;
233
+ if (inch >= 12) {
234
+ ft += 1;
235
+ inch -= 12;
236
+ }
237
+ return ft > 0 ? `${ft}'-${inch}"` : `${inch}"`;
238
+ }
239
+ function formatArea(area, units) {
240
+ return `${area.toFixed(units === "mm" ? 0 : 2)} ${units}\xB2`;
241
+ }
242
+ var fmt = (n) => Number.isFinite(n) ? Number(n.toFixed(3)).toString() : "0";
243
+ var esc = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
244
+ function sizeText(el, units) {
245
+ const a = el.attrs;
246
+ const f2 = (v) => {
247
+ const n = parseFloat(v ?? "");
248
+ return Number.isFinite(n) ? formatDimension(n, units) : "";
249
+ };
250
+ if (a.width && a.height) return `${f2(a.width)}\xD7${f2(a.height)}`;
251
+ if (a.size_x && a.size_y) return `${f2(a.size_x)}\xD7${f2(a.size_y)}`;
252
+ if (a.thickness) return f2(a.thickness);
253
+ if (a.width) return f2(a.width);
254
+ return "";
255
+ }
256
+ function tagLines(el, content, units) {
257
+ const a = el.attrs;
258
+ const number = (a.number ?? "").trim();
259
+ const name = (a.name ?? "").trim();
260
+ const area = el.kind === "space" && el.area !== void 0 ? formatArea(el.area, units) : "";
261
+ switch (content || "auto") {
262
+ case "number":
263
+ return [number || a.id];
264
+ case "name":
265
+ return [name || number || a.id];
266
+ case "type":
267
+ return [(a.type_name ?? a.type_id ?? a.type ?? "").trim() || sizeText(el, units) || a.id];
268
+ case "size":
269
+ return [sizeText(el, units) || a.id];
270
+ case "area":
271
+ return [area || "\u2014"];
272
+ default:
273
+ if (el.kind === "space") return [name, number, area].filter(Boolean).length ? [name, number, area].filter(Boolean) : [a.id];
274
+ if (el.table === "door" || el.table === "window") return [number || sizeText(el, units) || a.id];
275
+ return [number || name || a.id];
276
+ }
277
+ }
278
+ function defaultTagPosition(el, m) {
279
+ if (el.kind === "opening") {
280
+ const d = sub(el.end, el.start);
281
+ const l = len(d) || 1;
282
+ const n = { x: -d.y / l, y: d.x / l };
283
+ return add(mid(el.start, el.end), mul(n, el.thickness / 2 + 0.35 * m));
284
+ }
285
+ return elementAnchor(el);
286
+ }
287
+ function renderAnnotations(levelPath, globalPath, units) {
288
+ const parts = [];
289
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
290
+ const grow = (p, r = 0) => {
291
+ minX = Math.min(minX, p.x - r);
292
+ maxX = Math.max(maxX, p.x + r);
293
+ minY = Math.min(minY, p.y - r);
294
+ maxY = Math.max(maxY, p.y + r);
295
+ };
296
+ const dims = csvRows(levelPath, "dimension");
297
+ const notes = csvRows(levelPath, "text_note");
298
+ const tags = csvRows(levelPath, "tag");
299
+ if (dims.length + notes.length + tags.length === 0) return { parts, bounds: null };
300
+ const m = 1 / (UNIT_TO_M[units] ?? 1);
301
+ const index = indexLevelElements(levelPath, globalPath);
302
+ const ink = "#1d3557";
303
+ const text2 = (p, s, size, angleDeg = 0, weight = 400) => {
304
+ const tf = `translate(${fmt(p.x)},${fmt(p.y)}) scale(1,-1) rotate(${fmt(-angleDeg)})`;
305
+ return ` <text transform="${tf}" font-size="${fmt(size)}" font-family="sans-serif" font-weight="${weight}" fill="${ink}" text-anchor="middle" dominant-baseline="central">${esc(s)}</text>`;
306
+ };
307
+ for (const r of dims) {
308
+ const stored1 = { x: Number(r.start_x), y: Number(r.start_y) };
309
+ const stored2 = { x: Number(r.end_x), y: Number(r.end_y) };
310
+ if (![stored1.x, stored1.y, stored2.x, stored2.y].every(Number.isFinite)) continue;
311
+ const p1 = resolveDimensionPoint(r.start_ref ?? "", stored1, index);
312
+ const p2 = resolveDimensionPoint(r.end_ref ?? "", stored2, index);
313
+ const dirRaw = (r.direction ?? "").trim();
314
+ const g = dimensionGeometry(p1, p2, dirRaw === "" ? null : Number(dirRaw), Number(r.offset) || 0);
315
+ if (!g) continue;
316
+ const sw = 0.02 * m, tick = 0.12 * m, over = 0.1 * m, gap = 0.08 * m;
317
+ const ext = (p, d) => {
318
+ const v = sub(d, p);
319
+ const l = len(v);
320
+ if (l < gap) return "";
321
+ const dir = mul(v, 1 / l);
322
+ const a = add(p, mul(dir, gap)), b = add(d, mul(dir, over));
323
+ return ` <line x1="${fmt(a.x)}" y1="${fmt(a.y)}" x2="${fmt(b.x)}" y2="${fmt(b.y)}" stroke="${ink}" stroke-width="${fmt(sw)}" />`;
324
+ };
325
+ const slash = mul(add(g.u, g.n), tick / Math.SQRT2);
326
+ const ticks = [g.d1, g.d2].map((d) => {
327
+ const a = sub(d, slash), b = add(d, slash);
328
+ return ` <line x1="${fmt(a.x)}" y1="${fmt(a.y)}" x2="${fmt(b.x)}" y2="${fmt(b.y)}" stroke="${ink}" stroke-width="${fmt(sw * 2)}" />`;
329
+ });
330
+ let angle = Math.atan2(g.u.y, g.u.x) * 180 / Math.PI;
331
+ if (angle > 90.01 || angle <= -89.99) angle += 180;
332
+ const label = (r.text ?? "").trim() || formatDimension(g.value, units);
333
+ const up = { x: -Math.sin(angle * Math.PI / 180), y: Math.cos(angle * Math.PI / 180) };
334
+ const tp = add(mid(g.d1, g.d2), mul(up, 0.16 * m));
335
+ parts.push(
336
+ ` <g data-annotation="dimension" id="${esc(r.id)}">`,
337
+ ext(p1, g.d1),
338
+ ext(p2, g.d2),
339
+ ` <line x1="${fmt(g.d1.x)}" y1="${fmt(g.d1.y)}" x2="${fmt(g.d2.x)}" y2="${fmt(g.d2.y)}" stroke="${ink}" stroke-width="${fmt(sw)}" />`,
340
+ ...ticks,
341
+ text2(tp, label, 0.22 * m, angle),
342
+ " </g>"
343
+ );
344
+ for (const p of [p1, p2, g.d1, g.d2, tp]) grow(p, 0.2 * m);
345
+ }
346
+ for (const r of notes) {
347
+ const p = { x: Number(r.x), y: Number(r.y) };
348
+ if (!Number.isFinite(p.x) || !Number.isFinite(p.y) || !r.text) continue;
349
+ const size = Number(r.paper_size) ? Number(r.paper_size) * 0.1 * m : Number(r.size) || 0.25 * m;
350
+ const rot = Number(r.rotation) || 0;
351
+ const lines = r.text.split("\\n");
352
+ const a = rot * Math.PI / 180;
353
+ parts.push(` <g data-annotation="text_note" id="${esc(r.id)}">`);
354
+ lines.forEach((line, i) => {
355
+ const dy = ((lines.length - 1) / 2 - i) * size * 1.25;
356
+ const q = { x: p.x - Math.sin(a) * dy, y: p.y + Math.cos(a) * dy };
357
+ parts.push(text2(q, line, size, rot));
358
+ });
359
+ parts.push(" </g>");
360
+ grow(p, size * Math.max(...lines.map((l) => l.length)) * 0.6);
361
+ }
362
+ for (const r of tags) {
363
+ const host = index.get(r.host_id ?? "");
364
+ if (!host) continue;
365
+ const pinned = (r.x ?? "") !== "" && (r.y ?? "") !== "";
366
+ const pos = pinned ? { x: Number(r.x), y: Number(r.y) } : defaultTagPosition(host, m);
367
+ if (!Number.isFinite(pos.x) || !Number.isFinite(pos.y)) continue;
368
+ const lines = tagLines(host, r.content ?? "", units);
369
+ const room = host.kind === "space";
370
+ const size = (room ? 0.24 : 0.2) * m;
371
+ const w = Math.max(...lines.map((l) => l.length)) * size * 0.62 + 0.16 * m;
372
+ const h = lines.length * size * 1.25 + 0.08 * m;
373
+ parts.push(` <g data-annotation="tag" id="${esc(r.id)}">`);
374
+ const anchor = elementAnchor(host);
375
+ if (pinned && len(sub(anchor, pos)) > 0.6 * m) {
376
+ parts.push(` <line x1="${fmt(anchor.x)}" y1="${fmt(anchor.y)}" x2="${fmt(pos.x)}" y2="${fmt(pos.y)}" stroke="${ink}" stroke-width="${fmt(0.015 * m)}" />`);
377
+ }
378
+ if (!room) {
379
+ parts.push(` <rect x="${fmt(pos.x - w / 2)}" y="${fmt(pos.y - h / 2)}" width="${fmt(w)}" height="${fmt(h)}" rx="${fmt(0.04 * m)}" fill="white" stroke="${ink}" stroke-width="${fmt(0.015 * m)}" />`);
380
+ }
381
+ lines.forEach((line, i) => {
382
+ const dy = ((lines.length - 1) / 2 - i) * size * 1.25;
383
+ parts.push(text2({ x: pos.x, y: pos.y + dy }, line, i === 0 && room ? size * 1.1 : size, 0, i === 0 ? 600 : 400));
384
+ });
385
+ parts.push(" </g>");
386
+ grow(pos, Math.max(w, h));
387
+ }
388
+ return { parts: parts.filter(Boolean), bounds: Number.isFinite(minX) ? { minX, minY, maxX, maxY } : null };
389
+ }
390
+
391
+ // src/furniture/render.ts
392
+ import { existsSync as existsSync2 } from "fs";
393
+ import { dirname, join as join2 } from "path";
394
+
395
+ // src/furniture/svg.ts
396
+ var STROKE = "#4b5563";
397
+ var LINE = 0.012;
398
+ var FILLS = { paper: "#ffffff", tone: "#e5e7eb", dark: "#4b5563" };
399
+ function fmt2(n) {
400
+ if (!isFinite(n)) return "0";
401
+ return Number(n.toFixed(4)).toString();
402
+ }
403
+ function esc2(s) {
404
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
405
+ }
406
+ function paint(fill, dash, k = 1) {
407
+ const f2 = fill ? FILLS[fill] : "none";
408
+ const d = dash ? ` stroke-dasharray="${fmt2(0.06 * k)},${fmt2(0.04 * k)}"` : "";
409
+ return `fill="${f2}" stroke="${STROKE}" stroke-width="${fmt2(LINE * k)}"${d}`;
410
+ }
411
+ function primToSvg(p, k = 1) {
412
+ const paint_ = (fill, dash) => paint(fill, dash, k);
413
+ switch (p.k) {
414
+ case "rect":
415
+ return `<rect x="${fmt2(p.x)}" y="${fmt2(p.y)}" width="${fmt2(p.w)}" height="${fmt2(p.h)}"${p.r ? ` rx="${fmt2(p.r)}"` : ""} ${paint_(p.fill, p.dash)} />`;
416
+ case "circle":
417
+ return `<circle cx="${fmt2(p.cx)}" cy="${fmt2(p.cy)}" r="${fmt2(p.r)}" ${paint_(p.fill, p.dash)} />`;
418
+ case "ellipse":
419
+ return `<ellipse cx="${fmt2(p.cx)}" cy="${fmt2(p.cy)}" rx="${fmt2(p.rx)}" ry="${fmt2(p.ry)}" ${paint_(p.fill, p.dash)} />`;
420
+ case "line":
421
+ return `<line x1="${fmt2(p.x1)}" y1="${fmt2(p.y1)}" x2="${fmt2(p.x2)}" y2="${fmt2(p.y2)}" ${paint_(void 0, p.dash)} />`;
422
+ case "poly": {
423
+ const pts3 = p.pts.map(([x, y]) => `${fmt2(x)},${fmt2(y)}`).join(" ");
424
+ return p.closed ? `<polygon points="${pts3}" ${paint_(p.fill, p.dash)} />` : `<polyline points="${pts3}" ${paint_(void 0, p.dash)} />`;
425
+ }
426
+ case "arc":
427
+ return `<path d="${arcPath(p)}" ${paint_(void 0, p.dash)} />`;
428
+ case "text":
429
+ return `<text transform="translate(${fmt2(p.x)},${fmt2(p.y)}) scale(1,-1)" font-size="${fmt2(p.size)}" font-family="sans-serif" fill="${STROKE}" text-anchor="middle" dominant-baseline="central">${esc2(p.text)}</text>`;
430
+ }
431
+ }
432
+ function furnitureToSvg(id, attrs, x, y, rotationDeg, upm = 1) {
433
+ const r = resolveFurniture(attrs, upm);
434
+ const body = furniturePlan(r).map((p) => primToSvg(p, upm)).join("");
435
+ const rot = rotationDeg ? ` rotate(${fmt2(rotationDeg)})` : "";
436
+ const svg = ` <g id="${esc2(id)}" transform="translate(${fmt2(x)},${fmt2(y)})${rot}">${body}</g>`;
437
+ const a = rotationDeg * Math.PI / 180;
438
+ const c = Math.abs(Math.cos(a)), s = Math.abs(Math.sin(a));
439
+ const hx = (r.size.w * c + r.size.d * s) / 2;
440
+ const hy = (r.size.w * s + r.size.d * c) / 2;
441
+ return { svg, bounds: { minX: x - hx, minY: y - hy, maxX: x + hx, maxY: y + hy } };
442
+ }
443
+
444
+ // src/furniture/render.ts
445
+ function renderFurniture(levelPath) {
446
+ const geoPath = join2(levelPath, "furniture.geojson");
447
+ if (!existsSync2(geoPath)) return [];
448
+ let fc;
449
+ try {
450
+ fc = parseGeoJsonFile(geoPath);
451
+ } catch {
452
+ return [];
453
+ }
454
+ const csvPath = join2(levelPath, "furniture.csv");
455
+ const rows = /* @__PURE__ */ new Map();
456
+ if (existsSync2(csvPath)) for (const row of effectiveRows(dirname(levelPath), "furniture", readCsv(csvPath).rows)) rows.set(row.id, row);
457
+ const upm = 1 / (UNIT_TO_M[projectUnits(dirname(levelPath))] ?? 1);
458
+ const out = [];
459
+ for (const f2 of fc.features) {
460
+ if (f2.geometry.type !== "Point") continue;
461
+ const id = String(f2.properties.id ?? "");
462
+ if (!id) continue;
463
+ const [x, y] = f2.geometry.coordinates;
464
+ const rot = typeof f2.properties.rotation === "number" ? f2.properties.rotation : 0;
465
+ const r = furnitureToSvg(id, rows.get(id) ?? {}, x, y, rot, upm);
466
+ out.push({ tableName: "furniture", ...r });
467
+ }
468
+ return out;
469
+ }
470
+
471
+ // src/render-mep.ts
472
+ import { existsSync as existsSync3 } from "fs";
473
+ import { join as join3 } from "path";
474
+ var MEP_RENDER_TABLES = /* @__PURE__ */ new Set(["duct", "pipe", "cable_tray", "conduit", "equipment", "terminal", "mep_node"]);
475
+ var CURVES = ["duct", "pipe", "cable_tray", "conduit"];
476
+ var DEFAULT_COLOR = { duct: "#00b4d8", pipe: "#06d6a0", cable_tray: "#ffd166", conduit: "#ffd166", equipment: "#e63946", terminal: "#f77f00", mep_node: "#888888" };
477
+ var SYSTEM_COLORS = {
478
+ SA: "#3b82f6",
479
+ RA: "#60a5fa",
480
+ EA: "#a855f7",
481
+ OA: "#22d3ee",
482
+ GE: "#7c3aed",
483
+ CHWS: "#06b6d4",
484
+ CHWR: "#0891b2",
485
+ HWS: "#f97316",
486
+ HWR: "#ea580c",
487
+ CWS: "#10b981",
488
+ CWR: "#059669",
489
+ RF: "#ec4899",
490
+ SF: "#fb923c",
491
+ DW: "#1d4ed8",
492
+ DCW: "#3b82f6",
493
+ DHW: "#dc2626",
494
+ DHWR: "#b91c1c",
495
+ SS: "#92400e",
496
+ VENT: "#a16207",
497
+ SD: "#0369a1",
498
+ GAS: "#facc15",
499
+ DHWS: "#dc2626",
500
+ SAN: "#92400e",
501
+ V: "#a16207",
502
+ G: "#facc15",
503
+ CD: "#14b8a6",
504
+ FP: "#ef4444",
505
+ PWR: "#eab308",
506
+ LTG: "#f59e0b",
507
+ NP: "#fde047",
508
+ EM: "#f43f5e",
509
+ LV: "#eab308",
510
+ HV: "#ca8a04",
511
+ DATA: "#22c55e",
512
+ FA: "#ef4444",
513
+ SEC: "#a3a3a3"
514
+ };
515
+ function hashColor(s) {
516
+ let h = 0;
517
+ for (let i = 0; i < s.length; i++) h = h * 31 + s.charCodeAt(i) >>> 0;
518
+ return `hsl(${h % 360}, 60%, 50%)`;
519
+ }
520
+ var f3 = (n) => Number.isFinite(n) ? Number(n.toFixed(3)).toString() : "0";
521
+ var pts = (ps) => ps.map((p) => `${f3(p.x)},${f3(p.y)}`).join(" ");
522
+ var esc3 = (s) => s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
523
+ var num = (v, d = 0) => {
524
+ const n = parseFloat(String(v ?? ""));
525
+ return Number.isFinite(n) ? n : d;
526
+ };
527
+ function load(dirs) {
528
+ const curves = [];
529
+ const hosts = [];
530
+ const ports = [];
531
+ for (const d of dirs) {
532
+ for (const t of [...CURVES, "equipment", "terminal", "mep_node"]) {
533
+ const csvPath = join3(d, `${t}.csv`), geoPath = join3(d, `${t}.geojson`);
534
+ if (!existsSync3(csvPath) || !existsSync3(geoPath)) continue;
535
+ const rows = new Map(readCsv(csvPath).rows.map((r) => [r.id, r]));
536
+ let feats = [];
537
+ try {
538
+ feats = parseGeoJsonFile(geoPath).features;
539
+ } catch {
540
+ continue;
541
+ }
542
+ for (const f2 of feats) {
543
+ const id = String(f2.properties?.id ?? "");
544
+ const row = rows.get(id);
545
+ if (!row) continue;
546
+ const g = f2.geometry;
547
+ if (CURVES.includes(t) && g.type === "LineString") {
548
+ const c = g.coordinates;
549
+ const a = c[0], b = c[c.length - 1];
550
+ const round = (row.shape || (t === "pipe" || t === "conduit" ? "round" : "rect")) === "round";
551
+ const w = num(row.size_x, t === "duct" ? 0.3 : t === "pipe" ? 0.1 : t === "cable_tray" ? 0.2 : 0.05);
552
+ curves.push({ id, table: t, a: [a[0], a[1], a[2] ?? 0], b: [b[0], b[1], b[2] ?? 0], row, w, h: round ? w : num(row.size_y, w), round });
553
+ } else if (g.type === "Point") {
554
+ const c = g.coordinates;
555
+ const size = Array.isArray(f2.properties?.size) ? f2.properties.size : [];
556
+ hosts.push({ id, table: t, pos: [c[0], c[1], c[2] ?? 0], rot: num(f2.properties?.rotation), size, row });
557
+ }
558
+ }
559
+ }
560
+ const cp = join3(d, "connector.csv");
561
+ if (existsSync3(cp)) for (const r of readCsv(cp).rows) ports.push({ hostId: r.host_id, row: r });
562
+ }
563
+ return { curves, hosts, ports };
564
+ }
565
+ function systemColors(globalDir) {
566
+ const out = /* @__PURE__ */ new Map();
567
+ const p = join3(globalDir, "mep_system.csv");
568
+ if (!existsSync3(p)) return out;
569
+ for (const r of readCsv(p).rows) if (r.system_type && r.color) out.set(r.system_type.trim(), r.color.trim());
570
+ return out;
571
+ }
572
+ var sub2 = (a, b) => [a[0] - b[0], a[1] - b[1], a[2] - b[2]];
573
+ var len3 = (a) => Math.hypot(a[0], a[1], a[2]);
574
+ var norm3 = (a) => {
575
+ const l = len3(a) || 1;
576
+ return [a[0] / l, a[1] / l, a[2] / l];
577
+ };
578
+ var dot3 = (a, b) => a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
579
+ function text(x, y, dir, size, color, s, weight = 500) {
580
+ let ang = dir ? Math.atan2(dir.y, dir.x) * 180 / Math.PI : 0;
581
+ if (ang > 90.01) ang -= 180;
582
+ if (ang < -89.99) ang += 180;
583
+ return `<text x="0" y="0" font-size="${f3(size)}" font-family="sans-serif" font-weight="${weight}" fill="${color}" text-anchor="middle" dominant-baseline="central" transform="translate(${f3(x)},${f3(y)}) rotate(${f3(ang)}) scale(1,-1)">${esc3(s)}</text>`;
584
+ }
585
+ function sizeLabel(c) {
586
+ const mm = (v) => Math.round(v * 1e3);
587
+ const sys = (c.row.system_type || "").trim();
588
+ let size;
589
+ if (c.round) {
590
+ const d = mm(c.w);
591
+ const dn = c.table === "pipe" ? [15, 20, 25, 32, 40, 50, 65, 80, 100, 125, 150, 200, 250, 300].find((n) => Math.abs(n - d) <= Math.max(2, n * 0.12)) : void 0;
592
+ size = dn ? `DN${dn}` : `\xD8${d}`;
593
+ } else size = `${mm(c.w)}\xD7${mm(c.h)}${c.table === "duct" && c.row.shape === "oval" ? " oval" : ""}`;
594
+ return sys ? `${sys} ${size}` : size;
595
+ }
596
+ function renderMep(dirs, globalDir) {
597
+ const { curves, hosts, ports } = load(dirs);
598
+ if (curves.length === 0 && hosts.length === 0) return [];
599
+ const projectColors = systemColors(globalDir);
600
+ const colorOf = (sys, table) => {
601
+ const s = (sys || "").split(",")[0].trim();
602
+ if (!s) return DEFAULT_COLOR[table] ?? "#666";
603
+ return projectColors.get(s) ?? SYSTEM_COLORS[s] ?? hashColor(s);
604
+ };
605
+ const out = [];
606
+ const push = (tableName, svg, ps, pad = 0) => {
607
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
608
+ for (const p of ps) {
609
+ minX = Math.min(minX, p.x - pad);
610
+ minY = Math.min(minY, p.y - pad);
611
+ maxX = Math.max(maxX, p.x + pad);
612
+ maxY = Math.max(maxY, p.y + pad);
613
+ }
614
+ out.push({ tableName, svg, bounds: { minX, minY, maxX, maxY } });
615
+ };
616
+ const hostById = new Map(hosts.map((h) => [h.id, h]));
617
+ const legsByNode = /* @__PURE__ */ new Map();
618
+ for (const c of curves) {
619
+ for (const ref of [c.row.from, c.row.to]) {
620
+ const hid = (ref || "").split(":")[0];
621
+ const h = hid && !ref.includes(":") ? hostById.get(hid) : void 0;
622
+ if (!h || h.table !== "mep_node" || (h.row.kind || "").trim()) continue;
623
+ const da = Math.hypot(c.a[0] - h.pos[0], c.a[1] - h.pos[1]) + Math.abs(c.a[2] - h.pos[2]);
624
+ const db = Math.hypot(c.b[0] - h.pos[0], c.b[1] - h.pos[1]) + Math.abs(c.b[2] - h.pos[2]);
625
+ const side = da <= db ? "a" : "b";
626
+ const far = side === "a" ? c.b : c.a;
627
+ const dir = norm3(sub2(far, h.pos));
628
+ const list = legsByNode.get(h.id) ?? [];
629
+ if (list.some((l) => l.curve === c)) continue;
630
+ list.push({ curve: c, side, dir, trim: 0, width: c.w });
631
+ legsByNode.set(h.id, list);
632
+ }
633
+ }
634
+ const trimOf = /* @__PURE__ */ new Map();
635
+ const untagged = /* @__PURE__ */ new Set();
636
+ const sameRun = (a, b) => a.table === b.table && (a.row.system_type || "") === (b.row.system_type || "") && Math.abs(a.w - b.w) < 1e-6 && Math.abs(a.h - b.h) < 1e-6;
637
+ const markStraight = (x, y) => {
638
+ if (x.side === "a" && sameRun(x.curve, y.curve)) untagged.add(x.curve.id);
639
+ if (y.side === "a" && sameRun(x.curve, y.curve)) untagged.add(y.curve.id);
640
+ };
641
+ const fittingSvg = [];
642
+ for (const [nodeId, legs] of legsByNode) {
643
+ const node = hostById.get(nodeId);
644
+ const c = { x: node.pos[0], y: node.pos[1] };
645
+ const color = colorOf(node.row.system_type || legs[0].curve.row.system_type || "", legs[0].curve.table);
646
+ const sw = legs[0].curve.table === "duct" || legs[0].curve.table === "cable_tray" ? 0.025 : 0.02;
647
+ const horiz = (l) => Math.abs(l.dir[2]) < 0.5;
648
+ const pd = (l) => {
649
+ const hh = Math.hypot(l.dir[0], l.dir[1]) || 1;
650
+ return { x: l.dir[0] / hh, y: l.dir[1] / hh };
651
+ };
652
+ const curveLen = (l) => len3(sub2(l.curve.b, l.curve.a));
653
+ const clamp = (l, t) => Math.max(0, Math.min(t, curveLen(l) * 0.45));
654
+ const collar = (l) => Math.max(0.025, 0.08 * Math.max(l.curve.w, l.curve.h));
655
+ const band = (a, u, L, W) => {
656
+ const n2 = { x: -u.y, y: u.x }, hw = W / 2, b = { x: a.x + u.x * L, y: a.y + u.y * L };
657
+ return [{ x: a.x + n2.x * hw, y: a.y + n2.y * hw }, { x: b.x + n2.x * hw, y: b.y + n2.y * hw }, { x: b.x - n2.x * hw, y: b.y - n2.y * hw }, { x: a.x - n2.x * hw, y: a.y - n2.y * hw }];
658
+ };
659
+ const poly = (ps, kind2) => `<polygon data-mep="${kind2}" points="${pts(ps)}" fill="${color}" fill-opacity="0.13" stroke="${color}" stroke-width="${sw}" />`;
660
+ let kind = "coupling";
661
+ let svg = "";
662
+ const allPs = [c];
663
+ const n = legs.length;
664
+ const hint = (node.row.fitting || "").trim();
665
+ if (n === 2 && dot3(legs[0].dir, legs[1].dir) < -0.985) {
666
+ const [a, b] = legs;
667
+ markStraight(a, b);
668
+ if (Math.abs(a.curve.w - b.curve.w) > 1e-3 || Math.abs(a.curve.h - b.curve.h) > 1e-3 || a.curve.round !== b.curve.round) {
669
+ kind = a.curve.round === b.curve.round ? "reducer" : "transition";
670
+ const L = Math.max(0.1, Math.max(Math.abs(a.curve.w - b.curve.w), Math.abs(a.curve.h - b.curve.h), 0.05) / 2 / Math.tan(20 * Math.PI / 180));
671
+ a.trim = clamp(a, L / 2);
672
+ b.trim = clamp(b, L / 2);
673
+ if (horiz(a) && horiz(b)) {
674
+ const da = pd(a), db = pd(b), nn = { x: -da.y, y: da.x };
675
+ const pa = { x: c.x + da.x * a.trim, y: c.y + da.y * a.trim }, pb = { x: c.x + db.x * b.trim, y: c.y + db.y * b.trim };
676
+ const q = [
677
+ { x: pa.x + nn.x * a.width / 2, y: pa.y + nn.y * a.width / 2 },
678
+ { x: pb.x + nn.x * b.width / 2, y: pb.y + nn.y * b.width / 2 },
679
+ { x: pb.x - nn.x * b.width / 2, y: pb.y - nn.y * b.width / 2 },
680
+ { x: pa.x - nn.x * a.width / 2, y: pa.y - nn.y * a.width / 2 }
681
+ ];
682
+ svg = poly(q, kind);
683
+ allPs.push(...q);
684
+ }
685
+ }
686
+ } else if (n === 2) {
687
+ kind = "elbow";
688
+ const [a, b] = legs;
689
+ const defl = Math.PI - Math.acos(Math.max(-1, Math.min(1, dot3(a.dir, b.dir))));
690
+ const W = Math.max(a.curve.w, b.curve.w);
691
+ let R = (a.curve.table === "pipe" ? 1.5 : a.curve.table === "conduit" ? 4 : 1) * W;
692
+ const tanH = Math.tan(defl / 2);
693
+ const maxT = Math.min(curveLen(a), curveLen(b)) * 0.45;
694
+ if (R * tanH > maxT) R = maxT / tanH;
695
+ if (R >= W / 2 + 1e-4) {
696
+ a.trim = b.trim = R * tanH;
697
+ if (horiz(a) && horiz(b)) {
698
+ const da = pd(a), db = pd(b);
699
+ const bx = da.x + db.x, by = da.y + db.y, bl = Math.hypot(bx, by) || 1;
700
+ const o = { x: c.x + bx / bl * R / Math.cos(defl / 2), y: c.y + by / bl * R / Math.cos(defl / 2) };
701
+ const ta = { x: c.x + da.x * a.trim, y: c.y + da.y * a.trim }, tb = { x: c.x + db.x * b.trim, y: c.y + db.y * b.trim };
702
+ const a0 = Math.atan2(ta.y - o.y, ta.x - o.x), a1 = Math.atan2(tb.y - o.y, tb.x - o.x);
703
+ let sweep = a1 - a0;
704
+ while (sweep > Math.PI) sweep -= 2 * Math.PI;
705
+ while (sweep < -Math.PI) sweep += 2 * Math.PI;
706
+ const arc = (r) => Array.from({ length: 13 }, (_, i) => ({ x: o.x + Math.cos(a0 + sweep * i / 12) * r, y: o.y + Math.sin(a0 + sweep * i / 12) * r }));
707
+ const outer = arc(R + W / 2), inner = arc(Math.max(R - W / 2, 0)).reverse();
708
+ svg = poly([...outer, ...inner], kind);
709
+ allPs.push(...outer);
710
+ } else {
711
+ const h = horiz(a) ? a : horiz(b) ? b : null;
712
+ if (h) {
713
+ const q = band(c, pd(h), h.trim, h.width);
714
+ svg = poly(q, kind);
715
+ allPs.push(...q);
716
+ }
717
+ }
718
+ }
719
+ } else if (n >= 3) {
720
+ let best = null, bd = 1;
721
+ for (let i = 0; i < n; i++) for (let j = i + 1; j < n; j++) {
722
+ const d = dot3(legs[i].dir, legs[j].dir);
723
+ if (d < bd) {
724
+ bd = d;
725
+ best = [i, j];
726
+ }
727
+ }
728
+ const main = best && bd < -0.985 ? best : null;
729
+ if (main) markStraight(legs[main[0]], legs[main[1]]);
730
+ if (main && n === 3) {
731
+ const br = legs.find((_, i) => i !== main[0] && i !== main[1]);
732
+ const m1 = legs[main[0]], m2 = legs[main[1]];
733
+ const big = m1.curve.w * m1.curve.h >= m2.curve.w * m2.curve.h ? m1 : m2;
734
+ const tap = hint === "tap" || hint !== "tee" && br.curve.table === "duct" && (br.curve.round && !big.curve.round || br.curve.w <= 0.5 * big.curve.w);
735
+ const angled = Math.abs(dot3(br.dir, m1.dir)) > 0.3;
736
+ kind = tap ? "tap" : angled || hint === "wye" ? "wye" : "tee";
737
+ const mainExt = Math.abs(br.dir[2]) > 0.5 ? big.curve.h : big.curve.w;
738
+ if (tap) br.trim = clamp(br, mainExt / 2);
739
+ else {
740
+ m1.trim = clamp(m1, br.curve.w / 2 + collar(m1));
741
+ m2.trim = clamp(m2, br.curve.w / 2 + collar(m2));
742
+ br.trim = clamp(br, mainExt / 2 + collar(br));
743
+ }
744
+ } else {
745
+ kind = n === 4 && main ? "cross" : n === 3 ? "wye" : "manifold";
746
+ for (const l of legs) {
747
+ let ext = 0;
748
+ for (const o of legs) if (o !== l) ext = Math.max(ext, o.curve.w);
749
+ l.trim = clamp(l, ext / 2 + collar(l));
750
+ }
751
+ }
752
+ for (const l of legs) {
753
+ if (l.trim <= 1e-4 || !horiz(l)) continue;
754
+ const q = band(c, pd(l), l.trim, l.width);
755
+ svg += poly(q, kind);
756
+ allPs.push(...q);
757
+ }
758
+ if (kind === "tap") svg += `<circle data-mep="tap" cx="${f3(c.x)}" cy="${f3(c.y)}" r="0.001" fill="none" />`;
759
+ } else if (n === 1) {
760
+ kind = hint === "open" ? "open" : "cap";
761
+ if (kind === "cap" && horiz(legs[0])) {
762
+ const d = pd(legs[0]);
763
+ const q = band({ x: c.x - d.x * 0.02, y: c.y - d.y * 0.02 }, d, 0.02, legs[0].width * 1.06);
764
+ svg = poly(q, kind);
765
+ allPs.push(...q);
766
+ }
767
+ }
768
+ for (const l of legs) if (l.trim > 0) trimOf.set(`${l.curve.id}:${l.side}`, l.trim);
769
+ if (svg) fittingSvg.push({ node, svg, ps: allPs });
770
+ }
771
+ for (const c of curves) {
772
+ const color = colorOf(c.row.system_type || "", c.table);
773
+ const sw = c.table === "duct" || c.table === "cable_tray" ? 0.025 : 0.02;
774
+ const d = sub2(c.b, c.a);
775
+ const horLen = Math.hypot(d[0], d[1]);
776
+ if (horLen < 1e-3) {
777
+ const joinsRun = (ref) => {
778
+ if (!ref) return false;
779
+ const [hid, port] = ref.split(":");
780
+ const legs = legsByNode.get(hid);
781
+ if (legs) return legs.some((l) => Math.abs(l.dir[2]) < 0.5);
782
+ return !!port && hostById.has(hid);
783
+ };
784
+ const lowerIsA = c.a[2] <= c.b[2];
785
+ const lowerJoins = joinsRun(lowerIsA ? c.row.from : c.row.to), upperJoins = joinsRun(lowerIsA ? c.row.to : c.row.from);
786
+ const up = lowerJoins && !upperJoins ? true : upperJoins && !lowerJoins ? false : c.b[2] >= c.a[2];
787
+ const x = c.a[0], y = c.a[1], hw2 = c.w / 2, hh = c.h / 2;
788
+ const kind = up ? "riser-up" : "riser-down";
789
+ const rx = c.table === "duct" && c.row.shape === "oval" ? Math.min(hw2, hh) : 0;
790
+ const shape = c.round ? `<circle cx="${f3(x)}" cy="${f3(y)}" r="${f3(hw2)}" fill="${color}" fill-opacity="0.13" stroke="${color}" stroke-width="${sw}" />` : `<rect x="${f3(x - hw2)}" y="${f3(y - hh)}" width="${f3(c.w)}" height="${f3(c.h)}"${rx ? ` rx="${f3(rx)}"` : ""} fill="${color}" fill-opacity="0.13" stroke="${color}" stroke-width="${sw}" />`;
791
+ const k = c.round ? 0.7071 : 1;
792
+ const d1 = `<line x1="${f3(x - hw2 * k)}" y1="${f3(y - hh * k)}" x2="${f3(x + hw2 * k)}" y2="${f3(y + hh * k)}" stroke="${color}" stroke-width="${sw * 0.8}" />`;
793
+ const d2 = `<line x1="${f3(x - hw2 * k)}" y1="${f3(y + hh * k)}" x2="${f3(x + hw2 * k)}" y2="${f3(y - hh * k)}" stroke="${color}" stroke-width="${sw * 0.8}" />`;
794
+ push(c.table, ` <g id="${c.id}" data-mep="${kind}">${shape}${d1}${up ? d2 : ""}</g>`, [{ x, y }], Math.max(hw2, hh));
795
+ continue;
796
+ }
797
+ const u = { x: d[0] / horLen, y: d[1] / horLen }, n = { x: -u.y, y: u.x };
798
+ const ta = trimOf.get(`${c.id}:a`) ?? 0, tb = trimOf.get(`${c.id}:b`) ?? 0;
799
+ const L3 = len3(d);
800
+ const pa = { x: c.a[0] + u.x * ta * horLen / L3, y: c.a[1] + u.y * ta * horLen / L3 };
801
+ const pb = { x: c.b[0] - u.x * tb * horLen / L3, y: c.b[1] - u.y * tb * horLen / L3 };
802
+ const hw = c.w / 2;
803
+ const q = [{ x: pa.x + n.x * hw, y: pa.y + n.y * hw }, { x: pb.x + n.x * hw, y: pb.y + n.y * hw }, { x: pb.x - n.x * hw, y: pb.y - n.y * hw }, { x: pa.x - n.x * hw, y: pa.y - n.y * hw }];
804
+ let svg = `<polygon points="${pts(q)}" fill="${color}" fill-opacity="0.13" stroke="${color}" stroke-width="${sw}" />`;
805
+ const ins = num(c.row.insulation_thickness);
806
+ if (ins > 0) {
807
+ for (const s of [1, -1]) {
808
+ const o = s * (hw + ins);
809
+ svg += `<line x1="${f3(pa.x + n.x * o)}" y1="${f3(pa.y + n.y * o)}" x2="${f3(pb.x + n.x * o)}" y2="${f3(pb.y + n.y * o)}" stroke="${color}" stroke-width="${sw * 0.6}" stroke-dasharray="0.08,0.05" />`;
810
+ }
811
+ }
812
+ if ((c.row.flexible || "").toLowerCase() === "true") {
813
+ const L = Math.hypot(pb.x - pa.x, pb.y - pa.y), step = Math.max(0.08, Math.min(0.2, hw));
814
+ const zz = [];
815
+ for (let s = 0, k = 0; s <= L + 1e-9; s += step, k++) zz.push({ x: pa.x + u.x * Math.min(s, L) + n.x * (k % 2 ? 1 : -1) * hw * 0.85, y: pa.y + u.y * Math.min(s, L) + n.y * (k % 2 ? 1 : -1) * hw * 0.85 });
816
+ svg += `<polyline points="${pts(zz)}" fill="none" stroke="${color}" stroke-width="${sw * 0.6}" />`;
817
+ }
818
+ const label = untagged.has(c.id) ? "" : sizeLabel(c);
819
+ const font = c.table === "duct" || c.table === "cable_tray" ? 0.2 : 0.15;
820
+ const slope = (c.b[2] - c.a[2]) / horLen;
821
+ const tag = label + (c.table === "pipe" && Math.abs(slope) >= 1e-3 ? ` i=${(Math.abs(slope) * 100).toFixed(1)}%` : "");
822
+ if (label && horLen >= Math.max(1, tag.length * font * 0.62 + 0.3)) {
823
+ const m = { x: (c.a[0] + c.b[0]) / 2 + n.x * (hw + ins + font * 0.9), y: (c.a[1] + c.b[1]) / 2 + n.y * (hw + ins + font * 0.9) };
824
+ svg += text(m.x, m.y, u, font, color, tag);
825
+ }
826
+ if (c.table === "pipe" && Math.abs(slope) >= 1e-3 && horLen > 0.6) {
827
+ const down = slope < 0 ? 1 : -1, L = Math.min(0.6, horLen * 0.3), off = -(hw + ins + 0.12);
828
+ const mc = { x: (c.a[0] + c.b[0]) / 2 + n.x * off, y: (c.a[1] + c.b[1]) / 2 + n.y * off };
829
+ const a = { x: mc.x - u.x * down * L / 2, y: mc.y - u.y * down * L / 2 }, b = { x: mc.x + u.x * down * L / 2, y: mc.y + u.y * down * L / 2 };
830
+ const bk = { x: b.x - u.x * down * 0.08, y: b.y - u.y * down * 0.08 };
831
+ svg += `<g data-mep="slope"><line x1="${f3(a.x)}" y1="${f3(a.y)}" x2="${f3(b.x)}" y2="${f3(b.y)}" stroke="${color}" stroke-width="0.015" /><polygon points="${pts([b, { x: bk.x + n.x * 0.04, y: bk.y + n.y * 0.04 }, { x: bk.x - n.x * 0.04, y: bk.y - n.y * 0.04 }])}" fill="${color}" /></g>`;
832
+ }
833
+ push(c.table, ` <g id="${c.id}">${svg}</g>`, q, ins);
834
+ }
835
+ for (const f2 of fittingSvg) push("mep_node", ` <g id="${f2.node.id}">${f2.svg}</g>`, f2.ps);
836
+ const portOf = /* @__PURE__ */ new Map();
837
+ for (const p of ports) {
838
+ const l = portOf.get(p.hostId) ?? [];
839
+ l.push(p.row);
840
+ portOf.set(p.hostId, l);
841
+ }
842
+ for (const h of hosts) {
843
+ if (h.table === "mep_node") {
844
+ const kind = (h.row.kind || "").trim();
845
+ if (!kind) continue;
846
+ const leg = curves.find((c) => [c.row.from, c.row.to].some((r) => (r || "").split(":")[0] === h.id));
847
+ const color2 = colorOf(h.row.system_type || leg?.row.system_type || "", leg?.table ?? "pipe");
848
+ const W = leg ? leg.w : 0.05;
849
+ const L = leg?.table === "duct" ? kind === "fire_damper" ? 0.3 : 0.2 : Math.max(0.15, Math.min(0.5, W * 3));
850
+ const H = Math.max(kind.includes("damper") ? W : W * 1.6, 0.09);
851
+ const svg2 = accessorySvg(kind, L, H, color2);
852
+ push("mep_node", ` <g id="${h.id}" data-mep="${kind}" transform="translate(${f3(h.pos[0])},${f3(h.pos[1])}) rotate(${f3(h.rot)})">${svg2}</g>`, [{ x: h.pos[0], y: h.pos[1] }], L / 2);
853
+ continue;
854
+ }
855
+ const w = num(h.size[0], 0.3), d = num(h.size[1], w);
856
+ const type = (h.table === "equipment" ? h.row.equipment_type : h.row.terminal_type) || "other";
857
+ const sys = (h.row.system_type || "").split(",")[0].trim();
858
+ const color = sys ? colorOf(sys, h.table) : TERMINAL_COLORS[type] ?? DEFAULT_COLOR[h.table];
859
+ const sym = h.table === "equipment" ? equipmentSvg(type, w, d, color) : terminalSvg(type, w, d, color);
860
+ let svg = `<g transform="translate(${f3(h.pos[0])},${f3(h.pos[1])}) rotate(${f3(h.rot)})">${sym}</g>`;
861
+ if (h.table === "equipment") {
862
+ const tag = h.row.number || EQUIPMENT_ABBR[type] || "EQ";
863
+ svg += text(h.pos[0], h.pos[1], null, Math.max(0.14, Math.min(0.3, Math.min(w, d) * 0.3)), color, tag, 600);
864
+ } else if (num(h.row.flow) > 0 && /diffuser|grille/.test(type)) {
865
+ svg += text(h.pos[0], h.pos[1] - d / 2 - 0.12, null, 0.12, color, `${Math.round(num(h.row.flow))} L/s`, 600);
866
+ }
867
+ push(h.table, ` <g id="${h.id}" data-mep="${type}">${svg}</g>`, [{ x: h.pos[0], y: h.pos[1] }], Math.max(w, d) / 2);
868
+ }
869
+ return out;
870
+ }
871
+ var TERMINAL_COLORS = {
872
+ light_fixture: "#c99a06",
873
+ emergency_light: "#c99a06",
874
+ power_outlet: "#a16207",
875
+ switch: "#a16207",
876
+ junction_box: "#a16207",
877
+ data_outlet: "#15803d",
878
+ fire_alarm_device: "#dc2626",
879
+ sprinkler_head: "#dc2626"
880
+ };
881
+ var EQUIPMENT_ABBR = {
882
+ ahu: "AHU",
883
+ fcu: "FCU",
884
+ vav: "VAV",
885
+ chiller: "CH",
886
+ boiler: "B",
887
+ cooling_tower: "CT",
888
+ fan: "F",
889
+ pump: "P",
890
+ coil: "C",
891
+ transformer: "TX",
892
+ panelboard: "PNL",
893
+ generator: "GEN",
894
+ water_heater: "WH",
895
+ tank: "T",
896
+ other: "EQ"
897
+ };
898
+ function ln(a, b, c, sw) {
899
+ return `<line x1="${f3(a.x)}" y1="${f3(a.y)}" x2="${f3(b.x)}" y2="${f3(b.y)}" stroke="${c}" stroke-width="${f3(sw)}" />`;
900
+ }
901
+ function terminalSvg(type, w, d, c) {
902
+ const hw = w / 2, hd = d / 2, r = Math.min(hw, hd), sw = Math.max(0.012, Math.min(w, d) * 0.04);
903
+ const fill = `fill="${c}" fill-opacity="0.15" stroke="${c}" stroke-width="${f3(sw)}"`;
904
+ const rect = (x0 = -hw, y0 = -hd, ww = w, dd = d) => `<rect x="${f3(x0)}" y="${f3(y0)}" width="${f3(ww)}" height="${f3(dd)}" ${fill} />`;
905
+ const circ = (rr = r) => `<circle cx="0" cy="0" r="${f3(rr)}" ${fill} />`;
906
+ const X = ln({ x: -hw, y: -hd }, { x: hw, y: hd }, c, sw) + ln({ x: -hw, y: hd }, { x: hw, y: -hd }, c, sw);
907
+ switch (type) {
908
+ case "supply_air_diffuser":
909
+ return rect() + rect(-hw * 0.55, -hd * 0.55, w * 0.55, d * 0.55) + X;
910
+ case "return_air_grille":
911
+ return rect() + ln({ x: -hw, y: -hd }, { x: hw, y: hd }, c, sw);
912
+ case "exhaust_air_grille":
913
+ return rect() + ln({ x: -hw, y: hd }, { x: hw, y: -hd }, c, sw);
914
+ case "sprinkler_head": {
915
+ const rr = Math.max(r, 0.06);
916
+ return circ(rr) + ln({ x: -rr * 1.5, y: 0 }, { x: rr * 1.5, y: 0 }, c, sw) + ln({ x: 0, y: -rr * 1.5 }, { x: 0, y: rr * 1.5 }, c, sw);
917
+ }
918
+ case "light_fixture":
919
+ return Math.abs(w - d) < 1e-3 && w <= 0.35 ? circ() + ln({ x: -r * 0.7, y: -r * 0.7 }, { x: r * 0.7, y: r * 0.7 }, c, sw) + ln({ x: -r * 0.7, y: r * 0.7 }, { x: r * 0.7, y: -r * 0.7 }, c, sw) : rect() + X;
920
+ case "emergency_light":
921
+ return rect() + `<polygon points="${f3(-hw)},${f3(-hd)} ${f3(hw)},${f3(-hd)} ${f3(hw)},${f3(hd)}" fill="${c}" />`;
922
+ case "power_outlet":
923
+ return circ() + ln({ x: -r * 0.5, y: -r * 0.9 }, { x: -r * 0.5, y: r * 0.9 }, c, sw) + ln({ x: r * 0.5, y: -r * 0.9 }, { x: r * 0.5, y: r * 0.9 }, c, sw);
924
+ case "switch":
925
+ return circ() + text(0, 0, null, r * 1.3, c, "S", 700);
926
+ case "water_closet":
927
+ return rect(-hw, -hd, w, d * 0.28) + `<ellipse cx="0" cy="${f3(d * 0.12)}" rx="${f3(hw * 0.8)}" ry="${f3(d * 0.36)}" ${fill} />`;
928
+ case "lavatory":
929
+ case "sink":
930
+ return rect() + `<ellipse cx="0" cy="0" rx="${f3(hw * 0.7)}" ry="${f3(hd * 0.6)}" fill="none" stroke="${c}" stroke-width="${f3(sw)}" />`;
931
+ case "shower":
932
+ return rect() + X;
933
+ case "floor_drain":
934
+ return circ() + ln({ x: -r * 0.6, y: 0 }, { x: r * 0.6, y: 0 }, c, sw) + ln({ x: 0, y: -r * 0.6 }, { x: 0, y: r * 0.6 }, c, sw);
935
+ default:
936
+ return rect();
937
+ }
938
+ }
939
+ function equipmentSvg(type, w, d, c) {
940
+ const hw = w / 2, hd = d / 2, r = Math.min(hw, hd) * 0.6, sw = Math.max(0.015, Math.min(0.03, Math.min(w, d) * 0.03));
941
+ let s = `<rect x="${f3(-hw)}" y="${f3(-hd)}" width="${f3(w)}" height="${f3(d)}" fill="${c}" fill-opacity="0.15" stroke="${c}" stroke-width="${f3(sw)}" />`;
942
+ if (type === "pump" || type === "fan" || type === "cooling_tower") s += `<circle cx="0" cy="0" r="${f3(r)}" fill="none" stroke="${c}" stroke-width="${f3(sw)}" />`;
943
+ if (type === "pump") s += `<polygon points="${f3(-r * 0.5)},${f3(-r * 0.6)} ${f3(-r * 0.5)},${f3(r * 0.6)} ${f3(r * 0.7)},0" fill="${c}" fill-opacity="0.6" />`;
944
+ if (type === "panelboard") s += `<polygon points="${f3(-hw)},${f3(-hd)} ${f3(hw)},${f3(-hd)} ${f3(hw)},${f3(hd)}" fill="${c}" fill-opacity="0.55" />`;
945
+ if (type === "water_heater" || type === "tank") s += `<circle cx="0" cy="0" r="${f3(Math.min(hw, hd) * 0.92)}" fill="none" stroke="${c}" stroke-width="${f3(sw)}" />`;
946
+ return s;
947
+ }
948
+ function accessorySvg(kind, L, H, c) {
949
+ const hl = L / 2, hh = H / 2, sw = Math.max(0.012, Math.min(0.025, H * 0.06));
950
+ const tri = (s, filled = false) => `<polygon points="${f3(s * hl)},${f3(-hh)} ${f3(s * hl)},${f3(hh)} 0,0" fill="${filled ? c : "white"}" stroke="${c}" stroke-width="${f3(sw)}" />`;
951
+ const bow = tri(-1) + tri(1);
952
+ switch (kind) {
953
+ case "ball_valve":
954
+ return bow + `<circle cx="0" cy="0" r="${f3(hh * 0.45)}" fill="${c}" />`;
955
+ case "globe_valve":
956
+ return bow + `<circle cx="0" cy="0" r="${f3(hh * 0.45)}" fill="white" stroke="${c}" stroke-width="${f3(sw)}" />`;
957
+ case "butterfly_valve":
958
+ return bow + ln({ x: 0, y: -hh * 1.2 }, { x: 0, y: hh * 1.2 }, c, sw * 1.4);
959
+ case "balancing_valve":
960
+ return bow + ln({ x: -hl * 0.8, y: -hh * 1.1 }, { x: hl * 0.8, y: hh * 1.1 }, c, sw);
961
+ case "check_valve":
962
+ return `<polygon points="${f3(-hl)},${f3(-hh)} ${f3(-hl)},${f3(hh)} ${f3(hl * 0.6)},0" fill="${c}" fill-opacity="0.75" stroke="${c}" stroke-width="${f3(sw)}" />` + ln({ x: hl * 0.6, y: -hh }, { x: hl * 0.6, y: hh }, c, sw * 1.4);
963
+ case "damper":
964
+ return `<rect x="${f3(-hl)}" y="${f3(-hh)}" width="${f3(L)}" height="${f3(H)}" fill="white" stroke="${c}" stroke-width="${f3(sw)}" />` + ln({ x: -hl * 0.6, y: -hh * 0.85 }, { x: hl * 0.6, y: hh * 0.85 }, c, sw * 1.3);
965
+ case "fire_damper":
966
+ return `<rect x="${f3(-hl)}" y="${f3(-hh)}" width="${f3(L)}" height="${f3(H)}" fill="white" stroke="${c}" stroke-width="${f3(sw)}" />` + ln({ x: -hl, y: -hh }, { x: hl, y: hh }, c, sw) + ln({ x: -hl, y: hh }, { x: hl, y: -hh }, c, sw) + text(0, hh + Math.max(0.12, H * 0.25), null, Math.max(0.12, Math.min(0.2, H * 0.35)), c, "FD", 700);
967
+ case "flow_meter":
968
+ return `<circle cx="0" cy="0" r="${f3(Math.max(hh, hl * 0.7))}" fill="white" stroke="${c}" stroke-width="${f3(sw)}" />` + text(0, 0, null, Math.max(hh, hl * 0.7), c, "M", 700);
969
+ case "strainer":
970
+ return ln({ x: -hl, y: 0 }, { x: hl, y: 0 }, c, sw) + ln({ x: -hl * 0.3, y: 0 }, { x: hl * 0.5, y: -hh }, c, sw);
971
+ default:
972
+ return bow;
973
+ }
974
+ }
975
+
976
+ // src/stair/render.ts
977
+ var STAIR = "#f4a261";
978
+ var RAILING = "#546e7a";
979
+ var f = (n) => Number(n.toFixed(4)).toString();
980
+ var pts2 = (p) => p.map((q) => `${f(q.x)},${f(q.y)}`).join(" ");
981
+ function boundsOf(points) {
982
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
983
+ for (const p of points) {
984
+ minX = Math.min(minX, p.x);
985
+ minY = Math.min(minY, p.y);
986
+ maxX = Math.max(maxX, p.x);
987
+ maxY = Math.max(maxY, p.y);
988
+ }
989
+ return { minX, minY, maxX, maxY };
990
+ }
991
+ function scaled(r, scale) {
992
+ return scale === 1 ? r : { ...r, startZ: r.startZ * scale, endZ: r.endZ * scale };
993
+ }
994
+ function stairSvg(s, scale, cutZ, k) {
995
+ if (s.asm.runs.length === 0) return null;
996
+ const parts = [];
997
+ const all = [];
998
+ for (const l of s.asm.landings) {
999
+ const above = l.top * scale > cutZ;
1000
+ parts.push(`<polygon points="${pts2(l.vertices)}" fill="rgba(244,162,97,0.15)" stroke="${STAIR}" stroke-width="${f(0.03 * k)}"${above ? ` stroke-dasharray="${f(0.12 * k)},${f(0.06 * k)}"` : ""} />`);
1001
+ all.push(...l.vertices);
1002
+ const w = winderGeometry(s.asm, l);
1003
+ for (const ln2 of w?.lines ?? []) {
1004
+ parts.push(`<line x1="${f(ln2.a.x)}" y1="${f(ln2.a.y)}" x2="${f(ln2.b.x)}" y2="${f(ln2.b.y)}" stroke="${STAIR}" stroke-width="${f(0.025 * k)}"${ln2.z * scale > cutZ ? ` stroke-dasharray="${f(0.1 * k)},${f(0.06 * k)}"` : ""} />`);
1005
+ }
1006
+ }
1007
+ for (const r0 of s.asm.runs) {
1008
+ const plan = runPlan(scaled(r0, scale), cutZ);
1009
+ parts.push(`<polygon points="${pts2(plan.outline)}" fill="rgba(244,162,97,0.15)" stroke="none" />`);
1010
+ for (const l of plan.lines) {
1011
+ parts.push(`<line x1="${f(l.a.x)}" y1="${f(l.a.y)}" x2="${f(l.b.x)}" y2="${f(l.b.y)}" stroke="${STAIR}" stroke-width="${f(0.025 * k)}"${l.above ? ` stroke-dasharray="${f(0.1 * k)},${f(0.06 * k)}"` : ""} />`);
1012
+ }
1013
+ if (plan.breakLine) parts.push(`<polyline points="${pts2(plan.breakLine)}" fill="none" stroke="${STAIR}" stroke-width="${f(0.03 * k)}" />`);
1014
+ all.push(...plan.outline);
1015
+ }
1016
+ const walk = walkingLine(s.asm);
1017
+ if (walk.length >= 2) {
1018
+ const a = walk[walk.length - 2], b = walk[walk.length - 1];
1019
+ const l = Math.hypot(b.x - a.x, b.y - a.y) || 1;
1020
+ const u = { x: (b.x - a.x) / l, y: (b.y - a.y) / l };
1021
+ const head = Math.min(0.3 * k, s.asm.runs[0].width * 0.3);
1022
+ const back = { x: b.x - u.x * head, y: b.y - u.y * head };
1023
+ parts.push(`<polyline points="${pts2([...walk.slice(0, -1), back])}" fill="none" stroke="${STAIR}" stroke-width="${f(0.025 * k)}" />`);
1024
+ parts.push(`<polygon points="${pts2([b, { x: back.x - u.y * head * 0.4, y: back.y + u.x * head * 0.4 }, { x: back.x + u.y * head * 0.4, y: back.y - u.x * head * 0.4 }])}" fill="${STAIR}" />`);
1025
+ parts.push(`<circle cx="${f(walk[0].x)}" cy="${f(walk[0].y)}" r="${f(0.06 * k)}" fill="${STAIR}" />`);
1026
+ const r0 = s.asm.runs[0];
1027
+ const off = Math.min(0.6 * k, r0.length * 0.25);
1028
+ const u0 = runDir(r0, 0);
1029
+ const lp = { x: walk[0].x + u0.x * off - u0.y * r0.width * 0.22, y: walk[0].y + u0.y * off + u0.x * r0.width * 0.22 };
1030
+ const up = r0.endZ >= r0.startZ;
1031
+ parts.push(`<text x="0" y="0" transform="translate(${f(lp.x)},${f(lp.y)}) scale(1,-1)" font-size="${f(0.25 * k)}" font-family="sans-serif" font-weight="bold" fill="${STAIR}" text-anchor="middle" dominant-baseline="central">${up ? "UP" : "DN"}</text>`);
1032
+ }
1033
+ return { tableName: "stair", svg: ` <g id="${s.id}">${parts.join("")}</g>`, bounds: boundsOf(all) };
1034
+ }
1035
+ function renderStairs(projectDir, levelId) {
1036
+ const k = 1 / UNIT_TO_M[projectUnits(projectDir)];
1037
+ const cutZ = STAIR_CUT_HEIGHT * k;
1038
+ const out = [];
1039
+ for (const d of loadStairs(projectDir)) {
1040
+ const here = d.dir === levelId || d.dir === "global";
1041
+ const repOn = (s) => {
1042
+ if (here) return 0;
1043
+ return s ? s.reps.findIndex((r) => r.index > 0 && r.levelId === levelId) : -1;
1044
+ };
1045
+ for (const s of d.stairs) {
1046
+ const i = repOn(s);
1047
+ const r = i >= 0 ? stairSvg(s, s.reps[i].scale, cutZ, k) : null;
1048
+ if (r) out.push(r);
1049
+ }
1050
+ for (const rl of d.railings) {
1051
+ const i = rl.host === "stair" ? repOn(d.stairs.find((s) => s.id === rl.row.host_id)) : here ? 0 : -1;
1052
+ const segs = i >= 0 ? rl.paths[i] : void 0;
1053
+ if (!segs || segs.length === 0) continue;
1054
+ const plan = railPlan(segs);
1055
+ if (plan.length < 2) continue;
1056
+ out.push({
1057
+ tableName: "railing",
1058
+ svg: ` <polyline id="${rl.id}" points="${pts2(plan)}" fill="none" stroke="${RAILING}" stroke-width="${f(0.05 * k)}" stroke-linejoin="miter" />`,
1059
+ bounds: boundsOf(plan)
1060
+ });
1061
+ }
1062
+ }
1063
+ return out;
1064
+ }
1065
+
1066
+ // src/render.ts
1067
+ var COLORS = {
1068
+ wall: { stroke: "#1a1a2e" },
1069
+ structure_wall: { stroke: "#4a4e69" },
1070
+ column: { stroke: "#2b2d42", fill: "#2b2d42" },
1071
+ structure_column: { stroke: "#6c757d", fill: "#6c757d" },
1072
+ slab: { stroke: "#adb5bd", fill: "rgba(173,181,189,0.2)" },
1073
+ structure_slab: { stroke: "#868e96", fill: "rgba(134,142,150,0.2)" },
1074
+ space: { stroke: "#3a86ff", fill: "rgba(58,134,255,0.15)" },
1075
+ room_separator: { stroke: "#adb5bd" },
1076
+ door: { stroke: "#e63946" },
1077
+ window: { stroke: "#2a9d8f" },
1078
+ stair: { stroke: "#f4a261", fill: "rgba(244,162,97,0.2)" },
1079
+ beam: { stroke: "#9b5de5" },
1080
+ brace: { stroke: "#9b5de5" },
1081
+ foundation: { stroke: "#8d6e63", fill: "rgba(141,110,99,0.15)" },
1082
+ opening: { stroke: "#ff8a65", fill: "#ffffff" },
1083
+ roof: { stroke: "#8d6e63", fill: "rgba(141,110,99,0.06)" },
1084
+ grid: { stroke: "#ef476f" },
1085
+ duct: { stroke: "#00b4d8" },
1086
+ pipe: { stroke: "#48bfe3" },
1087
+ cable_tray: { stroke: "#90be6d" },
1088
+ conduit: { stroke: "#43aa8b" },
1089
+ equipment: { stroke: "#f94144", fill: "rgba(249,65,68,0.15)" },
1090
+ terminal: { stroke: "#f3722c", fill: "rgba(243,114,44,0.15)" }
1091
+ };
1092
+ var DEFAULT_COLOR2 = { stroke: "#666" };
1093
+ var DEFAULT_STROKE_WIDTH = {
1094
+ wall: 0.2,
1095
+ structure_wall: 0.2,
1096
+ curtain_wall: 0.1,
1097
+ room_separator: 0.05,
1098
+ stair: 1,
1099
+ ramp: 1,
1100
+ railing: 0.05,
1101
+ duct: 0.3,
1102
+ pipe: 0.1,
1103
+ cable_tray: 0.2,
1104
+ conduit: 0.05
1105
+ };
1106
+ var OVERHEAD_LINE_TABLES = /* @__PURE__ */ new Set(["beam", "brace"]);
1107
+ var OVERHEAD_STROKE_WIDTH = 0.04;
1108
+ var OVERHEAD_DASH = "0.3,0.15";
1109
+ var BELOW_DASH = "0.25,0.12";
1110
+ var GRID_BUBBLE_R = 0.5;
1111
+ var RENDER_ORDER = [
1112
+ "foundation",
1113
+ "slab",
1114
+ "structure_slab",
1115
+ "roof",
1116
+ "opening",
1117
+ "beam",
1118
+ "brace",
1119
+ "wall",
1120
+ "structure_wall",
1121
+ "room_separator",
1122
+ "curtain_wall",
1123
+ "column",
1124
+ "structure_column",
1125
+ "stair",
1126
+ "ramp",
1127
+ "railing",
1128
+ "duct",
1129
+ "pipe",
1130
+ "cable_tray",
1131
+ "conduit",
1132
+ "equipment",
1133
+ "terminal",
1134
+ "mep_node",
1135
+ "door",
1136
+ "window",
1137
+ "space"
1138
+ ];
1139
+ var CSV_ONLY_TABLES = /* @__PURE__ */ new Set(["door", "window", "space"]);
1140
+ var STAIR_TABLES = /* @__PURE__ */ new Set(["stair", "stair_run", "stair_landing", "railing"]);
1141
+ function renderLevel(projectDir, levelId, opts = {}) {
1142
+ const layout = discoverLayout(projectDir);
1143
+ const gridSvg = opts.grids === false ? [] : renderGrids(layout.globalDir);
1144
+ const known = levelElevations(layout.globalDir);
1145
+ const levelDir = layout.levelDirs.find((d) => d.name === levelId) ?? (known.has(levelId) ? { name: levelId, path: join4(projectDir, levelId) } : void 0);
1146
+ if (!levelDir) {
1147
+ const ids = [.../* @__PURE__ */ new Set([...layout.levelDirs.map((d) => d.name), ...known.keys()])];
1148
+ throw new Error(`Level "${levelId}" not found. Available: ${ids.join(", ")}`);
1149
+ }
1150
+ const sources = [
1151
+ { path: layout.globalDir },
1152
+ { path: levelDir.path }
1153
+ ];
1154
+ const wallById = /* @__PURE__ */ new Map();
1155
+ const csvThicknesses = /* @__PURE__ */ new Map();
1156
+ for (const src of sources) {
1157
+ indexWalls(src.path, wallById);
1158
+ for (const t of ["wall", "structure_wall", "curtain_wall"]) {
1159
+ const m = readThicknessFromCsv(src.path, t);
1160
+ if (m.size > 0) {
1161
+ const existing = csvThicknesses.get(t) ?? /* @__PURE__ */ new Map();
1162
+ for (const [k, v] of m) existing.set(k, v);
1163
+ csvThicknesses.set(t, existing);
1164
+ }
1165
+ }
1166
+ }
1167
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
1168
+ const elements = [];
1169
+ for (const tableName of RENDER_ORDER) {
1170
+ if (CSV_ONLY_TABLES.has(tableName) || STAIR_TABLES.has(tableName)) continue;
1171
+ if (MEP_RENDER_TABLES.has(tableName)) {
1172
+ if (tableName === "duct") elements.push(...renderMep(sources.map((s) => s.path), layout.globalDir));
1173
+ continue;
1174
+ }
1175
+ const fileBase = GEOJSON_FILE_NAMES[tableName];
1176
+ if (!fileBase) continue;
1177
+ for (const src of sources) {
1178
+ const path = join4(src.path, `${fileBase}.geojson`);
1179
+ if (!existsSync4(path)) continue;
1180
+ let fc;
1181
+ try {
1182
+ fc = parseGeoJsonFile(path);
1183
+ } catch {
1184
+ continue;
1185
+ }
1186
+ const thicknessMap = csvThicknesses.get(tableName);
1187
+ const rows = SIZED_TABLES.has(tableName) ? readRows(src.path, tableName) : void 0;
1188
+ for (const feat of fc.features) {
1189
+ if (tableName === "curtain_wall") elements.push(...embedMasks(feat, wallById, projectDir));
1190
+ const rendered = renderFeature(tableName, feat, thicknessMap, rows?.get(String(feat.properties?.id ?? "")));
1191
+ if (rendered) elements.push(rendered);
1192
+ }
1193
+ }
1194
+ }
1195
+ elements.push(...renderStairs(projectDir, levelId));
1196
+ elements.push(...renderFurniture(levelDir.path));
1197
+ elements.push(...renderShaftsFrom(layout.levelDirs, levelDir.name, levelElevations(layout.globalDir)));
1198
+ for (const t of ["door", "window", "opening"]) {
1199
+ elements.push(...renderHostedFromCsv(levelDir.path, t, wallById));
1200
+ }
1201
+ elements.push(...renderSpaces(levelDir.path));
1202
+ if (elements.length > 0) elements.unshift(...gridSvg);
1203
+ const annotations = renderAnnotations(levelDir.path, layout.globalDir, projectUnits(projectDir));
1204
+ if (annotations.bounds) {
1205
+ elements.push({ tableName: "annotation", svg: annotations.parts.join("\n"), bounds: annotations.bounds });
1206
+ }
1207
+ for (const e of elements) {
1208
+ minX = Math.min(minX, e.bounds.minX);
1209
+ minY = Math.min(minY, e.bounds.minY);
1210
+ maxX = Math.max(maxX, e.bounds.maxX);
1211
+ maxY = Math.max(maxY, e.bounds.maxY);
1212
+ }
1213
+ if (elements.length === 0) {
1214
+ return '<?xml version="1.0" encoding="utf-8"?>\n<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10">\n <text x="5" y="5" text-anchor="middle" font-size="1">No geometry found</text>\n</svg>';
1215
+ }
1216
+ const pad = Math.max(maxX - minX, maxY - minY) * 0.05;
1217
+ const vbX = minX - pad;
1218
+ const vbY = -(maxY + pad);
1219
+ const vbW = maxX - minX + 2 * pad;
1220
+ const vbH = maxY - minY + 2 * pad;
1221
+ const parts = [
1222
+ '<?xml version="1.0" encoding="utf-8"?>',
1223
+ `<svg xmlns="http://www.w3.org/2000/svg" viewBox="${fmt3(vbX)} ${fmt3(vbY)} ${fmt3(vbW)} ${fmt3(vbH)}">`,
1224
+ ` <rect x="${fmt3(vbX)}" y="${fmt3(vbY)}" width="${fmt3(vbW)}" height="${fmt3(vbH)}" fill="white" />`,
1225
+ ' <g transform="scale(1,-1)">'
1226
+ ];
1227
+ for (const e of elements) parts.push(e.svg);
1228
+ parts.push(" </g>", "</svg>");
1229
+ return parts.join("\n");
1230
+ }
1231
+ var SIZED_TABLES = /* @__PURE__ */ new Set(["column", "structure_column", "foundation", "roof", "curtain_wall", "structure_slab", "slab"]);
1232
+ function renderFeature(tableName, feat, thicknessMap, row) {
1233
+ const id = String(feat.properties?.id ?? "");
1234
+ if (!id) return null;
1235
+ if (tableName === "foundation") return renderFoundation(id, feat, row ?? {});
1236
+ if (tableName === "roof" && feat.geometry.type === "Polygon") return renderRoof(id, feat, row ?? {});
1237
+ if (tableName === "curtain_wall" && feat.geometry.type === "LineString" && !feat.properties.arc) {
1238
+ return renderCurtainWall(id, feat, thicknessMap?.get(id) ?? DEFAULT_STROKE_WIDTH.curtain_wall, row ?? {});
1239
+ }
1240
+ if ((tableName === "column" || tableName === "structure_column") && feat.geometry.type === "Point") {
1241
+ return renderColumn(tableName, id, feat, row ?? {});
1242
+ }
1243
+ const color = COLORS[tableName] ?? DEFAULT_COLOR2;
1244
+ const isOverhead = OVERHEAD_LINE_TABLES.has(tableName);
1245
+ const dashArray = isOverhead ? OVERHEAD_DASH : tableName === "room_separator" ? "0.2,0.1" : void 0;
1246
+ const g = feat.geometry;
1247
+ switch (g.type) {
1248
+ case "LineString": {
1249
+ if (g.coordinates.length < 2) return null;
1250
+ const a = g.coordinates[0], b = g.coordinates[g.coordinates.length - 1];
1251
+ const sw = isOverhead ? OVERHEAD_STROKE_WIDTH : thicknessMap?.get(id) ?? DEFAULT_STROKE_WIDTH[tableName] ?? 0.1;
1252
+ const arc = feat.properties.arc;
1253
+ const d = arc ? `M ${fmt3(a[0])},${fmt3(a[1])} A ${fmt3(arc.radius)},${fmt3(arc.radius)} 0 ${arc.large_arc ? 1 : 0},${arc.sweep ? 1 : 0} ${fmt3(b[0])},${fmt3(b[1])}` : `M ${fmt3(a[0])},${fmt3(a[1])} L ${fmt3(b[0])},${fmt3(b[1])}`;
1254
+ const dash = dashArray ? ` stroke-dasharray="${dashArray}"` : "";
1255
+ const cap = isOverhead ? "butt" : "square";
1256
+ const svg = ` <path id="${id}" d="${d}" stroke="${color.stroke}" stroke-width="${fmt3(sw)}" stroke-linecap="${cap}" fill="none"${dash} />`;
1257
+ const hw = sw / 2;
1258
+ return {
1259
+ tableName,
1260
+ svg,
1261
+ bounds: { minX: Math.min(a[0], b[0]) - hw, minY: Math.min(a[1], b[1]) - hw, maxX: Math.max(a[0], b[0]) + hw, maxY: Math.max(a[1], b[1]) + hw }
1262
+ };
1263
+ }
1264
+ case "Point": {
1265
+ const [x, y] = g.coordinates;
1266
+ const fill = color.fill ?? color.stroke;
1267
+ const r = 0.15;
1268
+ const svg = ` <circle id="${id}" cx="${fmt3(x)}" cy="${fmt3(y)}" r="${r}" fill="${fill}" stroke="${color.stroke}" stroke-width="0.02" />`;
1269
+ return { tableName, svg, bounds: { minX: x - r, minY: y - r, maxX: x + r, maxY: y + r } };
1270
+ }
1271
+ case "Polygon": {
1272
+ const ring = g.coordinates[0];
1273
+ if (!ring || ring.length < 3) return null;
1274
+ const fill = color.fill ?? "none";
1275
+ let svg = ` <path id="${id}" d="${polygonPath(g.coordinates)}" fill-rule="evenodd" fill="${fill}" stroke="${color.stroke}" stroke-width="0.05" />`;
1276
+ if (tableName === "opening") svg += "\n" + crossLines(ring, color.stroke);
1277
+ const span = tableName === "structure_slab" ? parseFloat(row?.span_direction ?? "") : NaN;
1278
+ if (Number.isFinite(span)) svg += "\n" + spanArrow(id, ring, span, color.stroke);
1279
+ const slope = tableName === "slab" || tableName === "structure_slab" ? parseSlopeArrow(row?.slope_arrow) : null;
1280
+ if (slope && typeof slope !== "string") svg += "\n" + slopeArrowSvg(id, slope, color.stroke);
1281
+ return { tableName, svg, bounds: ringBounds(ring) };
1282
+ }
1283
+ }
1284
+ return null;
1285
+ }
1286
+ function embedMasks(feat, wallById, projectDir) {
1287
+ const g = feat.geometry;
1288
+ if (g.type !== "LineString" || g.coordinates.length < 2) return [];
1289
+ const upm = 1 / UNIT_TO_M[projectUnits(projectDir)];
1290
+ if ((Number(feat.properties.base_offset) || 0) > 1.2 * upm) return [];
1291
+ const a = g.coordinates[0], b = g.coordinates[g.coordinates.length - 1];
1292
+ const cw = { start: { x: a[0], y: a[1] }, end: { x: b[0], y: b[1] }, arc: feat.properties.arc };
1293
+ const out = [];
1294
+ for (const w of wallById.values()) {
1295
+ if (w.table !== "wall" && w.table !== "structure_wall") continue;
1296
+ const span = curtainEmbedSpan(w, w.thickness, cw, 5e-3 * upm);
1297
+ if (!span) continue;
1298
+ const n = w.arc ? Math.max(2, Math.ceil((span.hi - span.lo) / (0.15 * upm))) : 1;
1299
+ const pts3 = [];
1300
+ for (let i = 0; i <= n; i++) pts3.push(embedPointAt(w, span.lo + (span.hi - span.lo) * i / n));
1301
+ const sw = w.thickness + 0.01 * upm;
1302
+ const xs = pts3.map((p) => p.x), ys = pts3.map((p) => p.y);
1303
+ out.push({
1304
+ tableName: "curtain_wall",
1305
+ svg: pts3.length === 2 ? ` <line x1="${fmt3(pts3[0].x)}" y1="${fmt3(pts3[0].y)}" x2="${fmt3(pts3[1].x)}" y2="${fmt3(pts3[1].y)}" stroke="white" stroke-width="${fmt3(sw)}" stroke-linecap="butt" />` : ` <polyline points="${pts3.map((p) => `${fmt3(p.x)},${fmt3(p.y)}`).join(" ")}" fill="none" stroke="white" stroke-width="${fmt3(sw)}" stroke-linecap="butt" />`,
1306
+ bounds: { minX: Math.min(...xs), minY: Math.min(...ys), maxX: Math.max(...xs), maxY: Math.max(...ys) }
1307
+ });
1308
+ }
1309
+ return out;
1310
+ }
1311
+ var CURTAIN_STROKE = "#2f8fb8";
1312
+ var CURTAIN_FILL = "#d6eaf8";
1313
+ function renderCurtainWall(id, feat, width, row) {
1314
+ const c = feat.geometry.coordinates;
1315
+ if (c.length < 2) return null;
1316
+ const a = { x: c[0][0], y: c[0][1] }, b = { x: c[c.length - 1][0], y: c[c.length - 1][1] };
1317
+ const len2 = Math.hypot(b.x - a.x, b.y - a.y);
1318
+ if (len2 < 1e-6) return null;
1319
+ const u = { x: (b.x - a.x) / len2, y: (b.y - a.y) / len2 };
1320
+ const n = { x: -u.y, y: u.x };
1321
+ const at = (t, s = 0) => [a.x + u.x * t + n.x * s, a.y + u.y * t + n.y * s];
1322
+ const xs = [0, ...gridLines(row.v_grids, row.v_grid_count, row.v_spacing, len2), len2];
1323
+ const rowsN = gridLines(row.u_grids, row.u_grid_count, row.u_spacing, Number.MAX_VALUE).length + 1;
1324
+ const kinds = cellKinds(parsePanels(row.panels).rules, xs.length - 1, rowsN);
1325
+ const mw = num2(row.mullion_width) || 0.05;
1326
+ const hw = width / 2;
1327
+ const parts = [];
1328
+ const pts3 = [];
1329
+ const isDoor = (i) => kinds[i][0] === "door" || kinds[i][0] === "double_door";
1330
+ for (let i = 0; i < xs.length - 1; ) {
1331
+ if (isDoor(i)) {
1332
+ i++;
1333
+ continue;
1334
+ }
1335
+ let j = i;
1336
+ while (j + 1 < xs.length - 1 && !isDoor(j + 1)) j++;
1337
+ const ring = [at(xs[i], hw), at(xs[j + 1], hw), at(xs[j + 1], -hw), at(xs[i], -hw)];
1338
+ pts3.push(...ring);
1339
+ parts.push(`<polygon points="${pointsAttr(ring)}" fill="${CURTAIN_FILL}" stroke="${CURTAIN_STROKE}" stroke-width="0.02" />`);
1340
+ i = j + 1;
1341
+ }
1342
+ const tick = Math.max(hw * 1.6, 0.08);
1343
+ const d = xs.map((x) => {
1344
+ const p = at(x, tick), q = at(x, -tick);
1345
+ pts3.push(p, q);
1346
+ return `M ${fmt3(p[0])},${fmt3(p[1])} L ${fmt3(q[0])},${fmt3(q[1])}`;
1347
+ });
1348
+ parts.push(`<path d="${d.join(" ")}" stroke="${CURTAIN_STROKE}" stroke-width="0.03" fill="none" />`);
1349
+ for (let i = 0; i < xs.length - 1; i++) {
1350
+ if (!isDoor(i)) continue;
1351
+ const x0 = xs[i] + mw / 2, x1 = xs[i + 1] - mw / 2;
1352
+ if (x1 - x0 < 0.05) continue;
1353
+ const leaves = kinds[i][0] === "double_door" ? [{ hinge: x0, dir: 1, l: (x1 - x0) / 2 }, { hinge: x1, dir: -1, l: (x1 - x0) / 2 }] : [{ hinge: x0, dir: 1, l: x1 - x0 }];
1354
+ for (const lf of leaves) {
1355
+ const arc = [];
1356
+ for (let k = 0; k <= 12; k++) {
1357
+ const th = Math.PI / 2 * (1 - k / 12);
1358
+ arc.push(at(lf.hinge + lf.dir * lf.l * Math.cos(th), lf.l * Math.sin(th)));
1359
+ }
1360
+ pts3.push(...arc);
1361
+ const h = at(lf.hinge);
1362
+ parts.push(`<path d="M ${fmt3(h[0])},${fmt3(h[1])} L ${fmt3(arc[0][0])},${fmt3(arc[0][1])}" stroke="${COLORS.door.stroke}" stroke-width="0.03" fill="none" />`);
1363
+ parts.push(`<polyline points="${pointsAttr(arc)}" stroke="${COLORS.door.stroke}" stroke-width="0.015" fill="none" />`);
1364
+ }
1365
+ }
1366
+ const svg = ` <g id="${id}">${parts.join("")}</g>`;
1367
+ return { tableName: "curtain_wall", svg, bounds: ringBounds(pts3) };
1368
+ }
1369
+ function polygonPath(rings) {
1370
+ return rings.filter((r) => r.length >= 3).map((r) => `M ${r.map((p) => `${fmt3(p[0])},${fmt3(p[1])}`).join(" L ")} Z`).join(" ");
1371
+ }
1372
+ function ringBounds(ring) {
1373
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
1374
+ for (const p of ring) {
1375
+ minX = Math.min(minX, p[0]);
1376
+ minY = Math.min(minY, p[1]);
1377
+ maxX = Math.max(maxX, p[0]);
1378
+ maxY = Math.max(maxY, p[1]);
1379
+ }
1380
+ return { minX, minY, maxX, maxY };
1381
+ }
1382
+ function crossLines(ring, stroke) {
1383
+ const b = ringBounds(ring);
1384
+ return ` <path d="M ${fmt3(b.minX)},${fmt3(b.minY)} L ${fmt3(b.maxX)},${fmt3(b.maxY)} M ${fmt3(b.maxX)},${fmt3(b.minY)} L ${fmt3(b.minX)},${fmt3(b.maxY)}" stroke="${stroke}" stroke-width="0.03" fill="none" />`;
1385
+ }
1386
+ var num2 = (v) => {
1387
+ const n = parseFloat(v ?? "");
1388
+ return Number.isFinite(n) && n > 0 ? n : NaN;
1389
+ };
1390
+ var pointsAttr = (pts3) => pts3.map((p) => `${fmt3(p[0])},${fmt3(p[1])}`).join(" ");
1391
+ function spanArrow(id, ring, deg, stroke) {
1392
+ let a = 0, cx = 0, cy = 0;
1393
+ for (let i = 0; i < ring.length; i++) {
1394
+ const p = ring[i], q = ring[(i + 1) % ring.length];
1395
+ const c2 = p[0] * q[1] - q[0] * p[1];
1396
+ a += c2;
1397
+ cx += (p[0] + q[0]) * c2;
1398
+ cy += (p[1] + q[1]) * c2;
1399
+ }
1400
+ const c = Math.abs(a) > 1e-9 ? [cx / (3 * a), cy / (3 * a)] : ring[0];
1401
+ const r = deg * Math.PI / 180, ux = Math.cos(r), uy = Math.sin(r), nx = -uy, ny = ux;
1402
+ let lo = Infinity, hi = -Infinity;
1403
+ for (const p of ring) {
1404
+ const t = (p[0] - c[0]) * ux + (p[1] - c[1]) * uy;
1405
+ lo = Math.min(lo, t);
1406
+ hi = Math.max(hi, t);
1407
+ }
1408
+ const half = Math.max(0.5, Math.min(2, (hi - lo) * 0.2));
1409
+ const p1 = [c[0] - ux * half, c[1] - uy * half], p2 = [c[0] + ux * half, c[1] + uy * half];
1410
+ const h = 0.25;
1411
+ const head = (t, d2) => `M ${fmt3(t[0] - d2 * ux * h + nx * h * 0.5)},${fmt3(t[1] - d2 * uy * h + ny * h * 0.5)} L ${fmt3(t[0])},${fmt3(t[1])} L ${fmt3(t[0] - d2 * ux * h - nx * h * 0.5)},${fmt3(t[1] - d2 * uy * h - ny * h * 0.5)}`;
1412
+ const d = `M ${fmt3(p1[0])},${fmt3(p1[1])} L ${fmt3(p2[0])},${fmt3(p2[1])} ${head(p2, 1)} ${head(p1, -1)} M ${fmt3(c[0] - nx * 0.3)},${fmt3(c[1] - ny * 0.3)} L ${fmt3(c[0] + nx * 0.3)},${fmt3(c[1] + ny * 0.3)}`;
1413
+ return ` <path id="${id}-span" class="span-direction" d="${d}" stroke="${stroke}" stroke-width="0.03" fill="none" />`;
1414
+ }
1415
+ function slopeArrowSvg(id, a, stroke) {
1416
+ const dx = a.head.x - a.tail.x, dy = a.head.y - a.tail.y;
1417
+ const l = Math.hypot(dx, dy) || 1;
1418
+ const ux = dx / l, uy = dy / l, nx = -uy, ny = ux;
1419
+ const h = Math.min(0.3, l * 0.3);
1420
+ const t = a.head;
1421
+ const d = `M ${fmt3(a.tail.x)},${fmt3(a.tail.y)} L ${fmt3(t.x)},${fmt3(t.y)} M ${fmt3(t.x - ux * h + nx * h * 0.45)},${fmt3(t.y - uy * h + ny * h * 0.45)} L ${fmt3(t.x)},${fmt3(t.y)} L ${fmt3(t.x - ux * h - nx * h * 0.45)},${fmt3(t.y - uy * h - ny * h * 0.45)}`;
1422
+ return ` <path id="${id}-slope" class="slope-arrow" d="${d}" stroke="${stroke}" stroke-width="0.03" fill="none" />
1423
+ <circle cx="${fmt3(a.tail.x)}" cy="${fmt3(a.tail.y)}" r="0.06" fill="${stroke}" />`;
1424
+ }
1425
+ function sectionRings(row, sx, sy) {
1426
+ const shape = row.shape ?? "";
1427
+ const t = num2(row.thickness), fl = num2(row.flange), wb = num2(row.web);
1428
+ const hw = sx / 2, hd = sy / 2;
1429
+ const box = (w, d) => [[-w, -d], [w, -d], [w, d], [-w, d]];
1430
+ const circle = (rad) => Array.from({ length: 32 }, (_, i) => [Math.cos(i / 32 * 2 * Math.PI) * rad, Math.sin(i / 32 * 2 * Math.PI) * rad]);
1431
+ switch (shape) {
1432
+ case "rect_hollow": {
1433
+ const w = Number.isFinite(t) ? t : Math.min(sx, sy) * 0.05;
1434
+ return [box(hw, hd), box(hw - w, hd - w)];
1435
+ }
1436
+ case "round_hollow": {
1437
+ const w = Number.isFinite(t) ? t : sx * 0.04;
1438
+ return [circle(hw), circle(hw - w)];
1439
+ }
1440
+ case "i_shape": {
1441
+ const f2 = Number.isFinite(fl) ? fl : Math.min(sy * 0.15, 0.025), e = (Number.isFinite(wb) ? wb : Math.min(sx * 0.1, 0.015)) / 2;
1442
+ return [[
1443
+ [-hw, -hd],
1444
+ [hw, -hd],
1445
+ [hw, -hd + f2],
1446
+ [e, -hd + f2],
1447
+ [e, hd - f2],
1448
+ [hw, hd - f2],
1449
+ [hw, hd],
1450
+ [-hw, hd],
1451
+ [-hw, hd - f2],
1452
+ [-e, hd - f2],
1453
+ [-e, -hd + f2],
1454
+ [-hw, -hd + f2]
1455
+ ]];
1456
+ }
1457
+ case "c_shape": {
1458
+ const f2 = Number.isFinite(fl) ? fl : Math.min(sy * 0.15, 0.012), e = Number.isFinite(wb) ? wb : Math.min(sx * 0.2, 8e-3);
1459
+ return [[[-hw, -hd], [hw, -hd], [hw, -hd + f2], [-hw + e, -hd + f2], [-hw + e, hd - f2], [hw, hd - f2], [hw, hd], [-hw, hd]]];
1460
+ }
1461
+ case "l_shape": {
1462
+ const w = Number.isFinite(t) ? t : Math.min(sx, sy) * 0.15;
1463
+ return [[[-hw, -hd], [hw, -hd], [hw, -hd + w], [-hw + w, -hd + w], [-hw + w, hd], [-hw, hd]]];
1464
+ }
1465
+ }
1466
+ return [box(hw, hd)];
1467
+ }
1468
+ function rotatedRect(x, y, w, d, rotDeg) {
1469
+ const a = rotDeg * Math.PI / 180, c = Math.cos(a), s = Math.sin(a);
1470
+ return [[-w / 2, -d / 2], [w / 2, -d / 2], [w / 2, d / 2], [-w / 2, d / 2]].map(([u, v]) => [x + u * c - v * s, y + u * s + v * c]);
1471
+ }
1472
+ function renderRoof(id, feat, row) {
1473
+ const rings = feat.geometry.coordinates;
1474
+ const ring = rings[0];
1475
+ if (!ring || ring.length < 3) return null;
1476
+ const color = COLORS.roof;
1477
+ const outline = ring.map((p) => ({ x: p[0], y: p[1] }));
1478
+ const parts = [` <path id="${id}" d="${polygonPath(rings)}" fill-rule="evenodd" fill="${color.fill}" stroke="${color.stroke}" stroke-width="0.05" />`];
1479
+ let sol = null;
1480
+ try {
1481
+ sol = solveRoof(roofInputFor(outline, row));
1482
+ } catch {
1483
+ sol = null;
1484
+ }
1485
+ if (sol) {
1486
+ for (const l of sol.lines) {
1487
+ const dash = l.kind === "valley" ? ' stroke-dasharray="0.25,0.12"' : "";
1488
+ const w = l.kind === "ridge" ? 0.05 : 0.035;
1489
+ parts.push(` <path d="M ${fmt3(l.a.x)},${fmt3(l.a.y)} L ${fmt3(l.b.x)},${fmt3(l.b.y)}" stroke="${color.stroke}" stroke-width="${w}" fill="none"${dash} />`);
1490
+ }
1491
+ for (const ar of slopeArrows(sol)) {
1492
+ const dx = ar.to.x - ar.from.x, dy = ar.to.y - ar.from.y;
1493
+ const len2 = Math.hypot(dx, dy) || 1;
1494
+ const ux = dx / len2, uy = dy / len2, head = Math.min(0.25, len2 * 0.35);
1495
+ const bx = ar.to.x - ux * head, by = ar.to.y - uy * head;
1496
+ parts.push(` <path d="M ${fmt3(ar.from.x)},${fmt3(ar.from.y)} L ${fmt3(bx)},${fmt3(by)}" stroke="${color.stroke}" stroke-width="0.025" fill="none" />`);
1497
+ parts.push(` <polygon points="${fmt3(ar.to.x)},${fmt3(ar.to.y)} ${fmt3(bx - uy * head * 0.45)},${fmt3(by + ux * head * 0.45)} ${fmt3(bx + uy * head * 0.45)},${fmt3(by - ux * head * 0.45)}" fill="${color.stroke}" />`);
1498
+ const tx = ar.from.x - uy * 0.25, ty = ar.from.y + ux * 0.25;
1499
+ parts.push(` <text x="${fmt3(tx)}" y="${fmt3(-ty)}" transform="scale(1,-1)" font-size="0.25" text-anchor="middle" dominant-baseline="central" fill="${color.stroke}" font-family="sans-serif">${escapeXml(ar.label)}</text>`);
1500
+ }
1501
+ }
1502
+ return { tableName: "roof", svg: parts.join("\n"), bounds: ringBounds(ring) };
1503
+ }
1504
+ function slopeArrows(sol) {
1505
+ const best = /* @__PURE__ */ new Map();
1506
+ for (const pc of sol.pieces) {
1507
+ const pl = sol.planes[pc.plane];
1508
+ if (Math.hypot(pl.a, pl.b) < 1e-6) continue;
1509
+ const area = Math.abs(signedArea(pc.poly));
1510
+ const cur = best.get(pc.plane);
1511
+ if (!cur || area > cur.area) best.set(pc.plane, { area, poly: pc.poly });
1512
+ }
1513
+ const out = [];
1514
+ for (const [plane, { area, poly }] of best) {
1515
+ if (area < 0.05) continue;
1516
+ const pl = sol.planes[plane];
1517
+ const g = Math.hypot(pl.a, pl.b);
1518
+ const ux = -pl.a / g, uy = -pl.b / g;
1519
+ const c = centroid(poly);
1520
+ const len2 = Math.max(0.3, Math.min(1.2, Math.sqrt(area) * 0.35));
1521
+ const deg = Math.atan(g) * 180 / Math.PI;
1522
+ const label = deg < 5 ? `${(g * 100).toFixed(1).replace(/\.0$/, "")}%` : `${Math.round(deg * 10) / 10}\xB0`;
1523
+ out.push({ from: { x: c.x - ux * len2 / 2, y: c.y - uy * len2 / 2 }, to: { x: c.x + ux * len2 / 2, y: c.y + uy * len2 / 2 }, label });
1524
+ }
1525
+ return out;
1526
+ }
1527
+ function levelElevations(globalDir) {
1528
+ const out = /* @__PURE__ */ new Map();
1529
+ const p = join4(globalDir, "level.csv");
1530
+ if (!existsSync4(p)) return out;
1531
+ for (const row of readCsv(p).rows) if (row.id) out.set(row.id, Number(row.elevation) || 0);
1532
+ return out;
1533
+ }
1534
+ function renderShaftsFrom(levelDirs, levelId, elev) {
1535
+ const here = elev.get(levelId);
1536
+ if (here === void 0) return [];
1537
+ const out = [];
1538
+ const color = COLORS.opening;
1539
+ for (const d of levelDirs) {
1540
+ if (d.name === levelId) continue;
1541
+ const rows = readRows(d.path, "opening");
1542
+ const shafts = [...rows.values()].filter((r) => r.top_level_id);
1543
+ if (shafts.length === 0) continue;
1544
+ const path = join4(d.path, `${GEOJSON_FILE_NAMES.opening}.geojson`);
1545
+ if (!existsSync4(path)) continue;
1546
+ let fc;
1547
+ try {
1548
+ fc = parseGeoJsonFile(path);
1549
+ } catch {
1550
+ continue;
1551
+ }
1552
+ for (const row of shafts) {
1553
+ const base = elev.get(d.name), top = elev.get(row.top_level_id);
1554
+ if (base === void 0 || top === void 0) continue;
1555
+ const z0 = base + (Number(row.base_offset) || 0), z1 = top + (Number(row.top_offset) || 0);
1556
+ if (!(here > z0 + 1e-6 && here <= z1 + 1e-6)) continue;
1557
+ const feat = fc.features.find((f2) => String(f2.properties?.id) === row.id);
1558
+ if (!feat || feat.geometry.type !== "Polygon") continue;
1559
+ const ring = feat.geometry.coordinates[0];
1560
+ if (!ring || ring.length < 3) continue;
1561
+ const svg = ` <path id="${row.id}" d="${polygonPath(feat.geometry.coordinates)}" fill="${color.fill}" stroke="${color.stroke}" stroke-width="0.05" />
1562
+ ${crossLines(ring, color.stroke)}`;
1563
+ out.push({ tableName: "opening", svg, bounds: ringBounds(ring) });
1564
+ }
1565
+ }
1566
+ return out;
1567
+ }
1568
+ function renderColumn(tableName, id, feat, row) {
1569
+ const color = COLORS[tableName] ?? DEFAULT_COLOR2;
1570
+ const [x, y] = feat.geometry.coordinates;
1571
+ const sx = num2(row.size_x) || 0.3;
1572
+ const sy = num2(row.size_y) || sx;
1573
+ const fill = color.fill ?? color.stroke;
1574
+ if ((row.shape ?? "") === "round") {
1575
+ const r = sx / 2;
1576
+ const svg2 = ` <circle id="${id}" cx="${fmt3(x)}" cy="${fmt3(y)}" r="${fmt3(r)}" fill="${fill}" stroke="${color.stroke}" stroke-width="0.02" />`;
1577
+ return { tableName, svg: svg2, bounds: { minX: x - r, minY: y - r, maxX: x + r, maxY: y + r } };
1578
+ }
1579
+ const rot = typeof feat.properties.rotation === "number" ? feat.properties.rotation : 0;
1580
+ const shape = row.shape ?? "";
1581
+ if (shape && shape !== "rect" && tableName === "structure_column") {
1582
+ const a = rot * Math.PI / 180, c = Math.cos(a), s = Math.sin(a);
1583
+ const rings = sectionRings(row, sx, shape === "round_hollow" ? sx : sy).map((r) => r.map(([u, v]) => [x + u * c - v * s, y + u * s + v * c]));
1584
+ const svg2 = ` <path id="${id}" d="${polygonPath(rings)}" fill-rule="evenodd" fill="${fill}" stroke="${color.stroke}" stroke-width="0.02" />`;
1585
+ return { tableName, svg: svg2, bounds: ringBounds(rings[0]) };
1586
+ }
1587
+ const pts3 = rotatedRect(x, y, sx, sy, rot);
1588
+ const svg = ` <polygon id="${id}" points="${pointsAttr(pts3)}" fill="${fill}" stroke="${color.stroke}" stroke-width="0.02" />`;
1589
+ return { tableName, svg, bounds: ringBounds(pts3) };
1590
+ }
1591
+ function renderFoundation(id, feat, row) {
1592
+ const color = COLORS.foundation;
1593
+ const style = `fill="${color.fill}" stroke="${color.stroke}" stroke-width="0.05" stroke-dasharray="${BELOW_DASH}"`;
1594
+ const g = feat.geometry;
1595
+ if (g.type === "Point") {
1596
+ const [x, y] = g.coordinates;
1597
+ const isPile = row.kind === "pile";
1598
+ const w = num2(row.width) || num2(row.size_x) || (isPile ? 0.6 : 1.5);
1599
+ const l = num2(row.length) || num2(row.size_y);
1600
+ if (isPile && !Number.isFinite(l)) {
1601
+ const r = w / 2;
1602
+ return {
1603
+ tableName: "foundation",
1604
+ svg: ` <circle id="${id}" cx="${fmt3(x)}" cy="${fmt3(y)}" r="${fmt3(r)}" ${style} />`,
1605
+ bounds: { minX: x - r, minY: y - r, maxX: x + r, maxY: y + r }
1606
+ };
1607
+ }
1608
+ const rot = typeof feat.properties.rotation === "number" ? feat.properties.rotation : 0;
1609
+ const pts3 = rotatedRect(x, y, w, Number.isFinite(l) ? l : w, rot);
1610
+ return { tableName: "foundation", svg: ` <polygon id="${id}" points="${pointsAttr(pts3)}" ${style} />`, bounds: ringBounds(pts3) };
1611
+ }
1612
+ if (g.type === "LineString") {
1613
+ if (g.coordinates.length < 2) return null;
1614
+ const a = g.coordinates[0], b = g.coordinates[g.coordinates.length - 1];
1615
+ const dx = b[0] - a[0], dy = b[1] - a[1];
1616
+ const len2 = Math.hypot(dx, dy);
1617
+ if (len2 < 1e-6) return null;
1618
+ const hw = (num2(row.width) || 0.8) / 2;
1619
+ const nx = -dy / len2 * hw, ny = dx / len2 * hw;
1620
+ const pts3 = [[a[0] + nx, a[1] + ny], [b[0] + nx, b[1] + ny], [b[0] - nx, b[1] - ny], [a[0] - nx, a[1] - ny]];
1621
+ return { tableName: "foundation", svg: ` <polygon id="${id}" points="${pointsAttr(pts3)}" ${style} />`, bounds: ringBounds(pts3) };
1622
+ }
1623
+ if (g.type === "Polygon") {
1624
+ const ring = g.coordinates[0];
1625
+ if (!ring || ring.length < 3) return null;
1626
+ return {
1627
+ tableName: "foundation",
1628
+ svg: ` <path id="${id}" d="${polygonPath(g.coordinates)}" fill-rule="evenodd" ${style} />`,
1629
+ bounds: ringBounds(ring)
1630
+ };
1631
+ }
1632
+ return null;
1633
+ }
1634
+ function renderGrids(globalDir) {
1635
+ const path = join4(globalDir, "grid.csv");
1636
+ if (!existsSync4(path)) return [];
1637
+ const color = COLORS.grid.stroke;
1638
+ const out = [];
1639
+ for (const row of readCsv(path).rows) {
1640
+ const x1 = parseFloat(row.start_x ?? ""), y1 = parseFloat(row.start_y ?? "");
1641
+ const x2 = parseFloat(row.end_x ?? ""), y2 = parseFloat(row.end_y ?? "");
1642
+ if (![x1, y1, x2, y2].every(Number.isFinite) || x1 === x2 && y1 === y2) continue;
1643
+ const label = escapeXml(row.number || row.id || "");
1644
+ const r = GRID_BUBBLE_R;
1645
+ const svg = [
1646
+ ` <line id="${row.id}" x1="${fmt3(x1)}" y1="${fmt3(y1)}" x2="${fmt3(x2)}" y2="${fmt3(y2)}" stroke="${color}" stroke-width="0.03" stroke-dasharray="0.6,0.15,0.05,0.15" />`,
1647
+ ` <circle cx="${fmt3(x1)}" cy="${fmt3(y1)}" r="${r}" fill="#ffffff" stroke="${color}" stroke-width="0.04" />`,
1648
+ ` <text x="${fmt3(x1)}" y="${fmt3(y1)}" font-size="0.45" font-weight="bold" fill="${color}" text-anchor="middle" dominant-baseline="central" transform="scale(1,-1) translate(0,${fmt3(-2 * y1)})">${label}</text>`
1649
+ ].join("\n");
1650
+ out.push({
1651
+ tableName: "grid",
1652
+ svg,
1653
+ bounds: {
1654
+ minX: Math.min(x1 - r, x2),
1655
+ minY: Math.min(y1 - r, y2),
1656
+ maxX: Math.max(x1 + r, x2),
1657
+ maxY: Math.max(y1 + r, y2)
1658
+ }
1659
+ });
1660
+ }
1661
+ return out;
1662
+ }
1663
+ function readRows(dirPath, tableName) {
1664
+ const out = /* @__PURE__ */ new Map();
1665
+ const csvPath = join4(dirPath, `${tableName}.csv`);
1666
+ if (!existsSync4(csvPath)) return out;
1667
+ for (const row of effectiveRows(dirname2(dirPath), tableName, readCsv(csvPath).rows)) if (row.id) out.set(row.id, row);
1668
+ return out;
1669
+ }
1670
+ function indexWalls(dirPath, out) {
1671
+ for (const t of ["wall", "curtain_wall", "structure_wall"]) {
1672
+ const path = join4(dirPath, `${GEOJSON_FILE_NAMES[t]}.geojson`);
1673
+ if (!existsSync4(path)) continue;
1674
+ let fc;
1675
+ try {
1676
+ fc = parseGeoJsonFile(path);
1677
+ } catch {
1678
+ continue;
1679
+ }
1680
+ const thicknessMap = readThicknessFromCsv(dirPath, t);
1681
+ for (const f2 of fc.features) {
1682
+ if (f2.geometry.type !== "LineString" || f2.geometry.coordinates.length < 2) continue;
1683
+ const id = String(f2.properties.id ?? "");
1684
+ if (!id) continue;
1685
+ const a = f2.geometry.coordinates[0], b = f2.geometry.coordinates[f2.geometry.coordinates.length - 1];
1686
+ out.set(id, {
1687
+ id,
1688
+ table: t,
1689
+ arc: f2.properties.arc,
1690
+ start: { x: a[0], y: a[1] },
1691
+ end: { x: b[0], y: b[1] },
1692
+ thickness: thicknessMap.get(id) ?? 0.2
1693
+ });
1694
+ }
1695
+ }
1696
+ }
1697
+ function readThicknessFromCsv(dirPath, tableName) {
1698
+ const out = /* @__PURE__ */ new Map();
1699
+ const csvPath = join4(dirPath, `${tableName}.csv`);
1700
+ if (!existsSync4(csvPath)) return out;
1701
+ for (const row of effectiveRows(dirname2(dirPath), tableName, readCsv(csvPath).rows)) {
1702
+ const t = parseFloat(row.thickness ?? "");
1703
+ if (!isNaN(t)) out.set(row.id, t);
1704
+ }
1705
+ return out;
1706
+ }
1707
+ function renderHostedFromCsv(levelPath, tableName, wallById) {
1708
+ const csvPath = join4(levelPath, `${tableName}.csv`);
1709
+ if (!existsSync4(csvPath)) return [];
1710
+ const rows = effectiveRows(dirname2(levelPath), tableName, readCsv(csvPath).rows);
1711
+ const color = COLORS[tableName] ?? DEFAULT_COLOR2;
1712
+ const out = [];
1713
+ for (const row of rows) {
1714
+ const hostId = row.host_id;
1715
+ const pos = parseFloat(row.position ?? "");
1716
+ const width = parseFloat(row.width ?? "0");
1717
+ if (!hostId || isNaN(pos) || isNaN(width)) continue;
1718
+ const wall = wallById.get(hostId);
1719
+ if (!wall) continue;
1720
+ const dx = wall.end.x - wall.start.x;
1721
+ const dy = wall.end.y - wall.start.y;
1722
+ const len2 = Math.sqrt(dx * dx + dy * dy);
1723
+ if (len2 === 0) continue;
1724
+ const ux = dx / len2, uy = dy / len2;
1725
+ const cx = wall.start.x + ux * pos;
1726
+ const cy = wall.start.y + uy * pos;
1727
+ const half = width / 2;
1728
+ const x1 = cx - ux * half, y1 = cy - uy * half;
1729
+ const x2 = cx + ux * half, y2 = cy + uy * half;
1730
+ const sw = tableName === "door" ? 0.22 : tableName === "opening" ? 0.24 : 0.18;
1731
+ const svg = ` <line id="${row.id}" x1="${fmt3(x1)}" y1="${fmt3(y1)}" x2="${fmt3(x2)}" y2="${fmt3(y2)}" stroke="${color.stroke}" stroke-width="${sw}" />`;
1732
+ out.push({
1733
+ tableName,
1734
+ svg,
1735
+ bounds: { minX: Math.min(x1, x2), minY: Math.min(y1, y2), maxX: Math.max(x1, x2), maxY: Math.max(y1, y2) }
1736
+ });
1737
+ }
1738
+ return out;
1739
+ }
1740
+ function renderSpaces(levelPath) {
1741
+ const csvPath = join4(levelPath, "space.csv");
1742
+ if (!existsSync4(csvPath)) return [];
1743
+ const csv = readCsv(csvPath);
1744
+ const color = COLORS.space ?? DEFAULT_COLOR2;
1745
+ const out = [];
1746
+ const nameMap = /* @__PURE__ */ new Map();
1747
+ for (const row of csv.rows) nameMap.set(row.id, row.name ?? row.id ?? "");
1748
+ const tagPath = join4(levelPath, "tag.csv");
1749
+ if (existsSync4(tagPath)) {
1750
+ for (const t of readCsv(tagPath).rows) if (t.host_id) nameMap.set(t.host_id, "");
1751
+ }
1752
+ const geomPath = join4(levelPath, "space.geojson");
1753
+ if (existsSync4(geomPath)) {
1754
+ try {
1755
+ const fc = parseGeoJsonFile(geomPath);
1756
+ for (const f2 of fc.features) {
1757
+ if (f2.geometry.type !== "Polygon") continue;
1758
+ const ring = f2.geometry.coordinates[0];
1759
+ if (!ring || ring.length < 3) continue;
1760
+ const id = String(f2.properties.id ?? "");
1761
+ const pts3 = ring.map((p) => `${fmt3(p[0])},${fmt3(p[1])}`).join(" ");
1762
+ let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
1763
+ let cx = 0, cy = 0;
1764
+ for (const p of ring) {
1765
+ minX = Math.min(minX, p[0]);
1766
+ minY = Math.min(minY, p[1]);
1767
+ maxX = Math.max(maxX, p[0]);
1768
+ maxY = Math.max(maxY, p[1]);
1769
+ cx += p[0];
1770
+ cy += p[1];
1771
+ }
1772
+ cx /= ring.length;
1773
+ cy /= ring.length;
1774
+ const name = nameMap.get(id) ?? id;
1775
+ const label = name ? `
1776
+ <text x="${fmt3(cx)}" y="${fmt3(cy)}" font-size="0.4" fill="${color.stroke}" text-anchor="middle" dominant-baseline="central" transform="scale(1,-1) translate(0,${fmt3(-2 * cy)})">${escapeXml(name)}</text>` : "";
1777
+ const svg = ` <polygon id="${id}" points="${pts3}" fill="${color.fill ?? "none"}" stroke="${color.stroke}" stroke-width="0.03" />${label}`;
1778
+ out.push({ tableName: "space", svg, bounds: { minX, minY, maxX, maxY } });
1779
+ }
1780
+ if (out.length > 0) return out;
1781
+ } catch {
1782
+ }
1783
+ }
1784
+ for (const row of csv.rows) {
1785
+ const x = parseFloat(row.x ?? "");
1786
+ const y = parseFloat(row.y ?? "");
1787
+ if (isNaN(x) || isNaN(y)) continue;
1788
+ const name = row.name ?? row.id ?? "";
1789
+ const svg = ` <circle id="${row.id}" cx="${fmt3(x)}" cy="${fmt3(y)}" r="0.15" fill="${color.fill ?? "none"}" stroke="${color.stroke}" stroke-width="0.03" />
1790
+ <text x="${fmt3(x)}" y="${fmt3(y)}" font-size="0.4" fill="${color.stroke}" text-anchor="middle" dominant-baseline="central" transform="scale(1,-1) translate(0,${fmt3(-2 * y)})">${escapeXml(name)}</text>`;
1791
+ out.push({ tableName: "space", svg, bounds: { minX: x - 1, minY: y - 1, maxX: x + 1, maxY: y + 1 } });
1792
+ }
1793
+ return out;
1794
+ }
1795
+ function fmt3(n) {
1796
+ if (!isFinite(n)) return "0";
1797
+ return Number(n.toFixed(3)).toString();
1798
+ }
1799
+ function escapeXml(s) {
1800
+ return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;");
1801
+ }
1802
+ export {
1803
+ renderLevel,
1804
+ slopeArrowSvg,
1805
+ spanArrow
1806
+ };
1807
+ //# sourceMappingURL=render-UEWYHPN6.js.map