@twinfinity/printing 5.0.3-ci.28538-beta

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 (209) hide show
  1. package/README.md +208 -0
  2. package/dist/BuildInfo.d.ts +25 -0
  3. package/dist/BuildInfo.d.ts.map +1 -0
  4. package/dist/BuildInfo.js +15 -0
  5. package/dist/BuildInfo.js.map +1 -0
  6. package/dist/index.d.ts +7 -0
  7. package/dist/index.d.ts.map +1 -0
  8. package/dist/index.js +9 -0
  9. package/dist/index.js.map +1 -0
  10. package/dist/layout/analyzeTemplate.d.ts +29 -0
  11. package/dist/layout/analyzeTemplate.d.ts.map +1 -0
  12. package/dist/layout/analyzeTemplate.js +168 -0
  13. package/dist/layout/analyzeTemplate.js.map +1 -0
  14. package/dist/layout/clipUtils.d.ts +18 -0
  15. package/dist/layout/clipUtils.d.ts.map +1 -0
  16. package/dist/layout/clipUtils.js +165 -0
  17. package/dist/layout/clipUtils.js.map +1 -0
  18. package/dist/layout/colorConstants.d.ts +11 -0
  19. package/dist/layout/colorConstants.d.ts.map +1 -0
  20. package/dist/layout/colorConstants.js +14 -0
  21. package/dist/layout/colorConstants.js.map +1 -0
  22. package/dist/layout/elementKinds.d.ts +21 -0
  23. package/dist/layout/elementKinds.d.ts.map +1 -0
  24. package/dist/layout/elementKinds.js +17 -0
  25. package/dist/layout/elementKinds.js.map +1 -0
  26. package/dist/layout/exportDrawing.d.ts +143 -0
  27. package/dist/layout/exportDrawing.d.ts.map +1 -0
  28. package/dist/layout/exportDrawing.js +146 -0
  29. package/dist/layout/exportDrawing.js.map +1 -0
  30. package/dist/layout/filters.d.ts +33 -0
  31. package/dist/layout/filters.d.ts.map +1 -0
  32. package/dist/layout/filters.js +77 -0
  33. package/dist/layout/filters.js.map +1 -0
  34. package/dist/layout/geometryConstants.d.ts +8 -0
  35. package/dist/layout/geometryConstants.d.ts.map +1 -0
  36. package/dist/layout/geometryConstants.js +10 -0
  37. package/dist/layout/geometryConstants.js.map +1 -0
  38. package/dist/layout/index.d.ts +86 -0
  39. package/dist/layout/index.d.ts.map +1 -0
  40. package/dist/layout/index.js +363 -0
  41. package/dist/layout/index.js.map +1 -0
  42. package/dist/layout/itemBuilders.d.ts +29 -0
  43. package/dist/layout/itemBuilders.d.ts.map +1 -0
  44. package/dist/layout/itemBuilders.js +135 -0
  45. package/dist/layout/itemBuilders.js.map +1 -0
  46. package/dist/layout/labelsRender.d.ts +29 -0
  47. package/dist/layout/labelsRender.d.ts.map +1 -0
  48. package/dist/layout/labelsRender.js +109 -0
  49. package/dist/layout/labelsRender.js.map +1 -0
  50. package/dist/layout/legendConstants.d.ts +12 -0
  51. package/dist/layout/legendConstants.d.ts.map +1 -0
  52. package/dist/layout/legendConstants.js +12 -0
  53. package/dist/layout/legendConstants.js.map +1 -0
  54. package/dist/layout/legendRender.d.ts +13 -0
  55. package/dist/layout/legendRender.d.ts.map +1 -0
  56. package/dist/layout/legendRender.js +424 -0
  57. package/dist/layout/legendRender.js.map +1 -0
  58. package/dist/layout/legendRows.d.ts +26 -0
  59. package/dist/layout/legendRows.d.ts.map +1 -0
  60. package/dist/layout/legendRows.js +45 -0
  61. package/dist/layout/legendRows.js.map +1 -0
  62. package/dist/layout/legendUtils.d.ts +21 -0
  63. package/dist/layout/legendUtils.d.ts.map +1 -0
  64. package/dist/layout/legendUtils.js +110 -0
  65. package/dist/layout/legendUtils.js.map +1 -0
  66. package/dist/layout/metadata.d.ts +43 -0
  67. package/dist/layout/metadata.d.ts.map +1 -0
  68. package/dist/layout/metadata.js +191 -0
  69. package/dist/layout/metadata.js.map +1 -0
  70. package/dist/layout/model.d.ts +15 -0
  71. package/dist/layout/model.d.ts.map +1 -0
  72. package/dist/layout/model.js +310 -0
  73. package/dist/layout/model.js.map +1 -0
  74. package/dist/layout/options.d.ts +17 -0
  75. package/dist/layout/options.d.ts.map +1 -0
  76. package/dist/layout/options.js +2 -0
  77. package/dist/layout/options.js.map +1 -0
  78. package/dist/layout/presets.d.ts +23 -0
  79. package/dist/layout/presets.d.ts.map +1 -0
  80. package/dist/layout/presets.js +41 -0
  81. package/dist/layout/presets.js.map +1 -0
  82. package/dist/layout/qrRender.d.ts +13 -0
  83. package/dist/layout/qrRender.d.ts.map +1 -0
  84. package/dist/layout/qrRender.js +97 -0
  85. package/dist/layout/qrRender.js.map +1 -0
  86. package/dist/layout/renderDrawing.d.ts +62 -0
  87. package/dist/layout/renderDrawing.d.ts.map +1 -0
  88. package/dist/layout/renderDrawing.js +555 -0
  89. package/dist/layout/renderDrawing.js.map +1 -0
  90. package/dist/layout/renderUtils.d.ts +36 -0
  91. package/dist/layout/renderUtils.d.ts.map +1 -0
  92. package/dist/layout/renderUtils.js +117 -0
  93. package/dist/layout/renderUtils.js.map +1 -0
  94. package/dist/layout/sectionValidation.d.ts +3 -0
  95. package/dist/layout/sectionValidation.d.ts.map +1 -0
  96. package/dist/layout/sectionValidation.js +2 -0
  97. package/dist/layout/sectionValidation.js.map +1 -0
  98. package/dist/layout/templateVariables.d.ts +137 -0
  99. package/dist/layout/templateVariables.d.ts.map +1 -0
  100. package/dist/layout/templateVariables.js +295 -0
  101. package/dist/layout/templateVariables.js.map +1 -0
  102. package/dist/layout/typeGuards.d.ts +5 -0
  103. package/dist/layout/typeGuards.d.ts.map +1 -0
  104. package/dist/layout/typeGuards.js +9 -0
  105. package/dist/layout/typeGuards.js.map +1 -0
  106. package/dist/layout/types.d.ts +816 -0
  107. package/dist/layout/types.d.ts.map +1 -0
  108. package/dist/layout/types.js +2 -0
  109. package/dist/layout/types.js.map +1 -0
  110. package/dist/layout/utils.d.ts +9 -0
  111. package/dist/layout/utils.d.ts.map +1 -0
  112. package/dist/layout/utils.js +84 -0
  113. package/dist/layout/utils.js.map +1 -0
  114. package/dist/layout/validation.d.ts +7 -0
  115. package/dist/layout/validation.d.ts.map +1 -0
  116. package/dist/layout/validation.js +352 -0
  117. package/dist/layout/validation.js.map +1 -0
  118. package/dist/layout/validationHelpers.d.ts +15 -0
  119. package/dist/layout/validationHelpers.d.ts.map +1 -0
  120. package/dist/layout/validationHelpers.js +43 -0
  121. package/dist/layout/validationHelpers.js.map +1 -0
  122. package/dist/layout/validationUtils.d.ts +5 -0
  123. package/dist/layout/validationUtils.d.ts.map +1 -0
  124. package/dist/layout/validationUtils.js +5 -0
  125. package/dist/layout/validationUtils.js.map +1 -0
  126. package/dist/layout/viewportPrep.d.ts +47 -0
  127. package/dist/layout/viewportPrep.d.ts.map +1 -0
  128. package/dist/layout/viewportPrep.js +275 -0
  129. package/dist/layout/viewportPrep.js.map +1 -0
  130. package/dist/layout/viewportRender.d.ts +42 -0
  131. package/dist/layout/viewportRender.d.ts.map +1 -0
  132. package/dist/layout/viewportRender.js +337 -0
  133. package/dist/layout/viewportRender.js.map +1 -0
  134. package/dist/layout/viewportUtils.d.ts +9 -0
  135. package/dist/layout/viewportUtils.d.ts.map +1 -0
  136. package/dist/layout/viewportUtils.js +40 -0
  137. package/dist/layout/viewportUtils.js.map +1 -0
  138. package/dist/layout/warnings.d.ts +2 -0
  139. package/dist/layout/warnings.d.ts.map +1 -0
  140. package/dist/layout/warnings.js +4 -0
  141. package/dist/layout/warnings.js.map +1 -0
  142. package/dist/printToPdf.d.ts +38 -0
  143. package/dist/printToPdf.d.ts.map +1 -0
  144. package/dist/printToPdf.js +79 -0
  145. package/dist/printToPdf.js.map +1 -0
  146. package/dist/printToSvg.d.ts +11 -0
  147. package/dist/printToSvg.d.ts.map +1 -0
  148. package/dist/printToSvg.js +168 -0
  149. package/dist/printToSvg.js.map +1 -0
  150. package/dist/sections.d.ts +118 -0
  151. package/dist/sections.d.ts.map +1 -0
  152. package/dist/sections.js +839 -0
  153. package/dist/sections.js.map +1 -0
  154. package/dist/templates/default-template.json +137 -0
  155. package/dist/types.d.ts +54 -0
  156. package/dist/types.d.ts.map +1 -0
  157. package/dist/types.js +11 -0
  158. package/dist/types.js.map +1 -0
  159. package/package.json +47 -0
  160. package/src/BuildInfo.ts +41 -0
  161. package/src/index.ts +10 -0
  162. package/src/layout/analyzeTemplate.ts +218 -0
  163. package/src/layout/clipUtils.ts +193 -0
  164. package/src/layout/colorConstants.ts +16 -0
  165. package/src/layout/elementKinds.ts +35 -0
  166. package/src/layout/exportDrawing.ts +206 -0
  167. package/src/layout/filters.ts +80 -0
  168. package/src/layout/geometryConstants.ts +11 -0
  169. package/src/layout/index.ts +671 -0
  170. package/src/layout/itemBuilders.ts +159 -0
  171. package/src/layout/labelsRender.ts +170 -0
  172. package/src/layout/legendConstants.ts +11 -0
  173. package/src/layout/legendRender.ts +543 -0
  174. package/src/layout/legendRows.ts +62 -0
  175. package/src/layout/legendUtils.ts +144 -0
  176. package/src/layout/metadata.ts +210 -0
  177. package/src/layout/model.ts +372 -0
  178. package/src/layout/options.ts +17 -0
  179. package/src/layout/presets.ts +51 -0
  180. package/src/layout/qrRender.ts +126 -0
  181. package/src/layout/qrcode.d.ts +2 -0
  182. package/src/layout/renderDrawing.ts +710 -0
  183. package/src/layout/renderUtils.ts +138 -0
  184. package/src/layout/sectionValidation.ts +2 -0
  185. package/src/layout/templateVariables.ts +317 -0
  186. package/src/layout/typeGuards.ts +9 -0
  187. package/src/layout/types.ts +835 -0
  188. package/src/layout/utils.ts +85 -0
  189. package/src/layout/validation.ts +392 -0
  190. package/src/layout/validationHelpers.ts +59 -0
  191. package/src/layout/validationUtils.ts +6 -0
  192. package/src/layout/viewportPrep.ts +351 -0
  193. package/src/layout/viewportRender.ts +442 -0
  194. package/src/layout/viewportUtils.ts +50 -0
  195. package/src/layout/warnings.ts +3 -0
  196. package/src/printToPdf.ts +108 -0
  197. package/src/printToSvg.ts +188 -0
  198. package/src/sections.ts +1019 -0
  199. package/src/templates/default-template.json +137 -0
  200. package/src/types.ts +49 -0
  201. package/templates/default-a0-landscape.json +209 -0
  202. package/templates/default-a0-portrait.json +195 -0
  203. package/templates/default-a1-landscape.json +167 -0
  204. package/templates/default-a1-portrait.json +153 -0
  205. package/templates/default-a3-landscape.json +111 -0
  206. package/templates/default-a3-portrait.json +125 -0
  207. package/templates/default-a4-landscape.json +97 -0
  208. package/templates/default-a4-portrait.json +97 -0
  209. package/templates/template-schema.json +1395 -0
@@ -0,0 +1,62 @@
1
+ import type { LayoutTemplate, ViewportLineStyle, FillRule, SvgModelView, RenderDrawingInput, RenderDrawingResult, NormalizedProductItem } from './types';
2
+ interface SampledColor {
3
+ color: {
4
+ r: number;
5
+ g: number;
6
+ b: number;
7
+ };
8
+ alpha: number;
9
+ }
10
+ declare let _renderLayout: ((template: LayoutTemplate, data: any, models: Record<string, SvgModelView>, options?: any) => Promise<{
11
+ svg: string;
12
+ viewportScales: any;
13
+ warnings: string[];
14
+ }>) | null;
15
+ /** Called by index.ts to register renderLayout without a circular import. */
16
+ export declare function setRenderLayoutBinding(fn: NonNullable<typeof _renderLayout>): void;
17
+ /**
18
+ * High-level entry point that converts raw plane-section results and a layout
19
+ * template into a ready-to-print SVG string.
20
+ *
21
+ * Internalizes all data wiring (section → SVG, item extraction, color sampling,
22
+ * metadata building, legend/label data) that previously required ~350 lines of
23
+ * app-side boilerplate.
24
+ *
25
+ * ```ts
26
+ * const section = computePlaneSectionsForProducts(products, plane, { includeInvisible: true });
27
+ * const { svg, warnings } = await renderDrawing({
28
+ * template,
29
+ * sections: { main: section },
30
+ * options: {
31
+ * allowRemoteAssets: true,
32
+ * qrContext: { host, containerName, planeHeight },
33
+ * productFields: { idPath: "propertySets.BIP.spacenumber.value", ... },
34
+ * roomLabels: { labelPath: "BIP.spacename", classFilter: "space" },
35
+ * },
36
+ * });
37
+ * ```
38
+ */
39
+ export declare function renderDrawing(input: RenderDrawingInput): Promise<RenderDrawingResult>;
40
+ /** Built-in label style defaults when template body styles are partially set. */
41
+ export declare const BUILTIN_LABEL_DEFAULTS: {
42
+ readonly fill: "#374151";
43
+ readonly fontFamily: "\"Segoe UI\", \"Helvetica Neue\", Arial, sans-serif";
44
+ readonly fontWeight: "300";
45
+ };
46
+ /** Built-in defaults when neither lineStyles nor lineWidthMm/lineColor are set. */
47
+ export declare const BUILTIN_LINE_DEFAULTS: Required<ViewportLineStyle>;
48
+ export declare const BUILTIN_HIDDEN_DEFAULTS: Required<ViewportLineStyle>;
49
+ type LineCategory = 'cutLines' | 'visibleLines' | 'hiddenLines' | 'silhouettes' | 'featureEdges' | 'stairArrows' | 'outlineVisible' | 'outlineHidden';
50
+ /** Category-specific built-in defaults (merged over the global default). */
51
+ export declare const CATEGORY_BUILTIN_OVERRIDES: Partial<Record<LineCategory, Partial<ViewportLineStyle>>>;
52
+ export interface ResolvedLineStyles {
53
+ default: Required<ViewportLineStyle>;
54
+ byCategory: Record<LineCategory, Required<ViewportLineStyle>>;
55
+ }
56
+ /**
57
+ * Resolve fill color for an item based on viewport fill rules.
58
+ * Returns an RGBA tuple [r, g, b, a] (0–255) or undefined if no rule matches.
59
+ */
60
+ export declare function resolveFillColor(item: NormalizedProductItem, product: unknown, fills: FillRule[] | undefined, sampleColor: (product: unknown) => SampledColor | undefined): [number, number, number, number] | undefined;
61
+ export {};
62
+ //# sourceMappingURL=renderDrawing.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"renderDrawing.d.ts","sourceRoot":"/","sources":["layout/renderDrawing.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EACR,cAAc,EAEd,iBAAiB,EACjB,QAAQ,EACR,YAAY,EAEZ,kBAAkB,EAClB,mBAAmB,EACnB,qBAAqB,EAExB,MAAM,SAAS,CAAC;AASjB,UAAU,YAAY;IAClB,KAAK,EAAE;QAAE,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IAC3C,KAAK,EAAE,MAAM,CAAC;CACjB;AA6GD,QAAA,IAAI,aAAa,EACX,CAAC,CACG,QAAQ,EAAE,cAAc,EACxB,IAAI,EAAE,GAAG,EACT,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,YAAY,CAAC,EACpC,OAAO,CAAC,EAAE,GAAG,KACZ,OAAO,CAAC;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,cAAc,EAAE,GAAG,CAAC;IAAC,QAAQ,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC,CAAC,GACvE,IAAW,CAAC;AAElB,6EAA6E;AAC7E,wBAAgB,sBAAsB,CAAC,EAAE,EAAE,WAAW,CAAC,OAAO,aAAa,CAAC,GAAG,IAAI,CAElF;AAeD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAsB,aAAa,CAAC,KAAK,EAAE,kBAAkB,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAgQ3F;AAwGD,iFAAiF;AACjF,eAAO,MAAM,sBAAsB;;;;CAIzB,CAAC;AAMX,mFAAmF;AACnF,eAAO,MAAM,qBAAqB,EAAE,QAAQ,CAAC,iBAAiB,CAK7D,CAAC;AAEF,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,iBAAiB,CAK/D,CAAC;AAEF,KAAK,YAAY,GACX,UAAU,GACV,cAAc,GACd,aAAa,GACb,aAAa,GACb,cAAc,GACd,aAAa,GACb,gBAAgB,GAChB,eAAe,CAAC;AAEtB,4EAA4E;AAC5E,eAAO,MAAM,0BAA0B,EAAE,OAAO,CAAC,MAAM,CAAC,YAAY,EAAE,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAIhG,CAAC;AAEF,MAAM,WAAW,kBAAkB;IAC/B,OAAO,EAAE,QAAQ,CAAC,iBAAiB,CAAC,CAAC;IACrC,UAAU,EAAE,MAAM,CAAC,YAAY,EAAE,QAAQ,CAAC,iBAAiB,CAAC,CAAC,CAAC;CACjE;AAgED;;;GAGG;AACH,wBAAgB,gBAAgB,CAC5B,IAAI,EAAE,qBAAqB,EAC3B,OAAO,EAAE,OAAO,EAChB,KAAK,EAAE,QAAQ,EAAE,GAAG,SAAS,EAC7B,WAAW,EAAE,CAAC,OAAO,EAAE,OAAO,KAAK,YAAY,GAAG,SAAS,GAC5D,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAoB9C"}
@@ -0,0 +1,555 @@
1
+ import { sectionStateToModelView, computePolylinesCenter } from './itemBuilders';
2
+ import { buildMetadataMap, normalizePropertySets, resolveIfcClassName, resolveIfcClassId } from './metadata';
3
+ import { applyLineStyleToPaths } from './utils';
4
+ import { analyzeTemplate } from './analyzeTemplate';
5
+ import { matchesV2Filter } from './filters';
6
+ import { getProp } from './typeGuards';
7
+ // These were previously lazy-loaded via require() from @measurement-tools/* packages.
8
+ // Now they are part of @twinfinity/printing (this package) and @twinfinity/geometry.
9
+ import { buildSectionStateForEntries, resolveProductProperty } from '../sections';
10
+ // Wrap into the ExternalSections shape for backward compat with the rest of this file.
11
+ const _sections = {
12
+ buildSectionStateForEntries: (perProduct, options) => {
13
+ return buildSectionStateForEntries(perProduct, {
14
+ ...options,
15
+ fills: options.fills,
16
+ buildLabels: options.buildLabels,
17
+ planeNormal: options.planeNormal
18
+ });
19
+ }
20
+ };
21
+ function getSections() {
22
+ return _sections;
23
+ }
24
+ // sampleProductColorRgba: duck-type access to product mesh style color.
25
+ // buildRoomLabels: requires product-geometry which this package can't depend on.
26
+ // Consumers must provide buildRoomLabels via options if room labels are needed.
27
+ const _geometry = {
28
+ sampleProductColorRgba: (product) => {
29
+ const p = product;
30
+ const meshes = (p?.meshes ?? p?.productMeshes);
31
+ if (!meshes?.length)
32
+ return undefined;
33
+ const rgba = new Uint8Array(4);
34
+ for (const mesh of meshes) {
35
+ try {
36
+ if (typeof mesh.copyColorTo === 'function') {
37
+ mesh.copyColorTo(rgba, 0);
38
+ }
39
+ else if (typeof mesh.getColorComponent === 'function') {
40
+ rgba[0] = mesh.getColorComponent(0);
41
+ rgba[1] = mesh.getColorComponent(1);
42
+ rgba[2] = mesh.getColorComponent(2);
43
+ rgba[3] = mesh.getColorComponent(3);
44
+ }
45
+ else {
46
+ const style = mesh.style;
47
+ const c = style?.surfaceColor ?? style?.color;
48
+ if (c) {
49
+ // surfaceColor values may be 0–1 or 0–255; normalize to 0–1
50
+ const maxVal = Math.max(c[0], c[1], c[2]);
51
+ const scale = maxVal > 1 ? 1 / 255 : 1;
52
+ return {
53
+ color: { r: c[0] * scale, g: c[1] * scale, b: c[2] * scale },
54
+ alpha: (c[3] ?? (maxVal > 1 ? 255 : 1)) * scale
55
+ };
56
+ }
57
+ continue;
58
+ }
59
+ }
60
+ catch {
61
+ continue;
62
+ }
63
+ const [r, g, b, a] = rgba;
64
+ if (a === 0 && r === 0 && g === 0 && b === 0)
65
+ continue;
66
+ return { color: { r: r / 255, g: g / 255, b: b / 255 }, alpha: a / 255 };
67
+ }
68
+ return undefined;
69
+ },
70
+ buildRoomLabels: () => [] // Requires product-geometry — consumer provides via options
71
+ };
72
+ function getGeometry() {
73
+ return _geometry;
74
+ }
75
+ function getSampleColor() {
76
+ return _geometry.sampleProductColorRgba;
77
+ }
78
+ // Late-binding for renderLayout to break circular dependency.
79
+ // renderDrawing is imported by index.ts which defines renderLayout.
80
+ // The binding is set by index.ts after definition via setRenderLayoutBinding().
81
+ let _renderLayout = null;
82
+ /** Called by index.ts to register renderLayout without a circular import. */
83
+ export function setRenderLayoutBinding(fn) {
84
+ _renderLayout = fn;
85
+ }
86
+ function getRenderLayout() {
87
+ if (!_renderLayout) {
88
+ throw new Error('renderLayout not initialized. Ensure @twinfinity/printing index is imported before calling renderDrawing.');
89
+ }
90
+ return _renderLayout;
91
+ }
92
+ // ---------------------------------------------------------------------------
93
+ // Public API
94
+ // ---------------------------------------------------------------------------
95
+ /**
96
+ * High-level entry point that converts raw plane-section results and a layout
97
+ * template into a ready-to-print SVG string.
98
+ *
99
+ * Internalizes all data wiring (section → SVG, item extraction, color sampling,
100
+ * metadata building, legend/label data) that previously required ~350 lines of
101
+ * app-side boilerplate.
102
+ *
103
+ * ```ts
104
+ * const section = computePlaneSectionsForProducts(products, plane, { includeInvisible: true });
105
+ * const { svg, warnings } = await renderDrawing({
106
+ * template,
107
+ * sections: { main: section },
108
+ * options: {
109
+ * allowRemoteAssets: true,
110
+ * qrContext: { host, containerName, planeHeight },
111
+ * productFields: { idPath: "propertySets.BIP.spacenumber.value", ... },
112
+ * roomLabels: { labelPath: "BIP.spacename", classFilter: "space" },
113
+ * },
114
+ * });
115
+ * ```
116
+ */
117
+ export async function renderDrawing(input) {
118
+ const { options } = input;
119
+ const warnings = [];
120
+ // ---- Resolve template variables (if present) ----
121
+ // This happens FIRST to ensure all {{vars.*}} references are resolved before validation/rendering
122
+ let template = input.template;
123
+ if (template.variables) {
124
+ const { resolveTemplateValues, applyTemplateVariables } = await import('./templateVariables');
125
+ const { values, warnings: varWarnings } = resolveTemplateValues(template, options?.variables);
126
+ // Add variable resolution warnings to output
127
+ varWarnings.forEach((w) => warnings.push(`[Variables] ${w}`));
128
+ // Apply variables to template (substitute {{vars.*}} references)
129
+ template = applyTemplateVariables(template, values);
130
+ }
131
+ const renderLayout = getRenderLayout();
132
+ // ---- Collect viewport elements from template ----
133
+ const viewports = template.elements.filter((el) => el.kind === 'viewport');
134
+ if (!viewports.length) {
135
+ return { svg: '', warnings: ['No viewport elements in template.'] };
136
+ }
137
+ // ---- Fast path: pre-built models provided directly ----
138
+ if (input.models) {
139
+ // Build items: explicit viewportItems take priority, then auto-build from productEntries
140
+ let viewportsData;
141
+ if (options?.viewportItems) {
142
+ const emptyDefaults = Object.fromEntries(viewports.map((vp) => [vp.id, { items: [] }]));
143
+ viewportsData = { ...emptyDefaults, ...options.viewportItems };
144
+ }
145
+ else if (options?.productEntries?.length) {
146
+ // Auto-build items per viewport from raw product entries
147
+ const sampleColor = getSampleColor();
148
+ viewportsData = {};
149
+ for (const vp of viewports) {
150
+ viewportsData[vp.id] = {
151
+ items: buildItemsFromEntries(options.productEntries, vp.fills, sampleColor)
152
+ };
153
+ }
154
+ }
155
+ else {
156
+ viewportsData = Object.fromEntries(viewports.map((vp) => [vp.id, { items: [] }]));
157
+ }
158
+ const layoutData = { viewports: viewportsData };
159
+ // Early input analysis: warn about mismatches between template and provided data
160
+ const reqs = analyzeTemplate(template);
161
+ for (const vpId of reqs.viewportIds) {
162
+ if (!input.models[vpId]) {
163
+ warnings.push(`Template viewport '${vpId}' has no model — it will render empty.`);
164
+ }
165
+ }
166
+ const allItems = Object.values(viewportsData).flatMap((v) => v.items);
167
+ if (allItems.length > 0) {
168
+ for (const path of reqs.propertyPaths) {
169
+ const parts = path.split('.');
170
+ if (parts.length < 2)
171
+ continue;
172
+ const [setName, propName] = parts;
173
+ const hasIt = allItems.some((item) => item.propertySets?.[setName]?.[propName]?.value !== undefined);
174
+ if (!hasIt) {
175
+ warnings.push(`Template references property '${path}' but no items provide it.`);
176
+ }
177
+ }
178
+ }
179
+ if (reqs.usesRemoteAssets && !template.allowRemoteAssets && !options?.allowRemoteAssets) {
180
+ warnings.push('Template uses remote assets but allowRemoteAssets is not enabled.');
181
+ }
182
+ // Build metadata from product entries (same as standard path)
183
+ const metadataEntries = options?.productEntries ?? [];
184
+ const metadataById = buildMetadataMap(metadataEntries);
185
+ const result = await renderLayout(template, layoutData, input.models, {
186
+ allowRemoteAssets: template.allowRemoteAssets ?? options?.allowRemoteAssets,
187
+ qrContext: options?.qrContext,
188
+ metadataById
189
+ });
190
+ return {
191
+ svg: result.svg,
192
+ warnings: [...warnings, ...result.warnings]
193
+ };
194
+ }
195
+ // ---- Standard path: build models from section results ----
196
+ if (!input.sections) {
197
+ return { svg: '', warnings: ['Either sections or models must be provided.'] };
198
+ }
199
+ const { buildSectionStateForEntries } = getSections();
200
+ const { sampleProductColorRgba, buildRoomLabels } = getGeometry();
201
+ // Normalize sections: single SectionResult → map to all viewports
202
+ const isSingle = !!input.sections.perProduct;
203
+ const sections = isSingle
204
+ ? Object.fromEntries(viewports.map((vp) => [vp.id, input.sections]))
205
+ : input.sections;
206
+ // Auto-detect whether the template has its own labels elements.
207
+ // If so, do NOT embed labels in the section SVG (would cause duplicates).
208
+ const hasTemplateLabelElements = template.elements.some((el) => el.kind === 'labels');
209
+ const roomLabelCfg = hasTemplateLabelElements ? undefined : options?.roomLabels;
210
+ // ---- Build section state + model view per viewport ----
211
+ const models = {};
212
+ const entriesByViewport = {};
213
+ const sectionStateCache = new Map();
214
+ for (const vp of viewports) {
215
+ const sectionResult = sections[vp.id];
216
+ if (!sectionResult?.perProduct?.length) {
217
+ warnings.push(`No section data for viewport '${vp.id}'.`);
218
+ continue;
219
+ }
220
+ entriesByViewport[vp.id] = sectionResult.perProduct;
221
+ // Dedup: skip if already built for this viewport key
222
+ if (sectionStateCache.has(vp.id)) {
223
+ const cached = sectionStateCache.get(vp.id);
224
+ if (cached) {
225
+ models[vp.id] = sectionStateToModelView(cached);
226
+ }
227
+ continue;
228
+ }
229
+ const resolved = resolveLineStyles(vp);
230
+ // Estimate label font size from template style and section bounds
231
+ const labelFontSize = estimateLabelFontSize(template, vp, sectionResult.perProduct);
232
+ // Extract clip polygon from section results if clip config is present
233
+ let clipPolygon;
234
+ if (vp.clip?.where && vp.clip.class) {
235
+ const clipClass = Array.isArray(vp.clip.class) ? vp.clip.class : [vp.clip.class];
236
+ const clipEntries = sectionResult.perProduct.filter((entry) => {
237
+ const product = entry.product;
238
+ const matchesClass = clipClass.some((c) => product.class?.is?.(c));
239
+ if (!matchesClass)
240
+ return false;
241
+ // Match where condition using T8-aware property resolution
242
+ for (const [path, expected] of Object.entries(vp.clip.where)) {
243
+ const actualValue = resolveProductProperty(product, path);
244
+ if (actualValue === undefined)
245
+ return false;
246
+ if (actualValue !== expected)
247
+ return false;
248
+ }
249
+ return true;
250
+ });
251
+ if (clipEntries.length) {
252
+ clipPolygon = clipEntries.flatMap((entry) => (entry.polylines || []).map((pl) => (pl.points || []).map((p) => ({ x: p.x, z: p.z }))));
253
+ }
254
+ }
255
+ // Convert planeNormal array to object with x, y, z if present
256
+ let planeNormal = undefined;
257
+ if (vp.planeNormal && Array.isArray(vp.planeNormal) && vp.planeNormal.length === 3) {
258
+ planeNormal = { x: vp.planeNormal[0], y: vp.planeNormal[1], z: vp.planeNormal[2] };
259
+ }
260
+ const state = buildSectionStateForEntries(sectionResult.perProduct, {
261
+ strokeWidth: resolved.default.widthMm,
262
+ labelFontSize,
263
+ fills: vp.fills,
264
+ includeMeta: true,
265
+ includeLabels: !!roomLabelCfg,
266
+ buildLabels: roomLabelCfg
267
+ ? (products, fontSize) => buildRoomLabels(products, fontSize, {
268
+ labelPath: roomLabelCfg.labelPath,
269
+ classFilter: roomLabelCfg.classFilter
270
+ })
271
+ : undefined,
272
+ clipPolygon,
273
+ planeNormal
274
+ });
275
+ sectionStateCache.set(vp.id, state);
276
+ if (!state) {
277
+ warnings.push(`No geometry for viewport '${vp.id}'.`);
278
+ continue;
279
+ }
280
+ // Apply line style overrides from viewport config
281
+ let finalState = state;
282
+ const defaultStyle = resolved.default;
283
+ if (defaultStyle.color || defaultStyle.widthMm) {
284
+ const style = {};
285
+ if (defaultStyle.color)
286
+ style.stroke = defaultStyle.color;
287
+ if (defaultStyle.widthMm)
288
+ style.strokeWidth = defaultStyle.widthMm;
289
+ finalState = {
290
+ ...state,
291
+ paths: applyLineStyleToPaths(state.paths, style)
292
+ };
293
+ if (state.rawPaths) {
294
+ finalState.rawPaths = applyLineStyleToPaths(state.rawPaths, style);
295
+ }
296
+ }
297
+ const labelStyle = template.styles?.body
298
+ ? {
299
+ fill: template.styles.body.color ?? BUILTIN_LABEL_DEFAULTS.fill,
300
+ fontFamily: template.styles.body.fontFamily ?? BUILTIN_LABEL_DEFAULTS.fontFamily,
301
+ fontWeight: template.styles.body.fontWeight ?? BUILTIN_LABEL_DEFAULTS.fontWeight
302
+ }
303
+ : undefined;
304
+ models[vp.id] = sectionStateToModelView(finalState, labelStyle);
305
+ }
306
+ // ---- Build items per viewport ----
307
+ const viewportsData = {};
308
+ for (const vp of viewports) {
309
+ const entries = entriesByViewport[vp.id];
310
+ if (!entries) {
311
+ viewportsData[vp.id] = { items: [] };
312
+ continue;
313
+ }
314
+ viewportsData[vp.id] = { items: buildItemsFromEntries(entries, vp.fills, sampleProductColorRgba) };
315
+ }
316
+ // ---- Build metadata from all section entries ----
317
+ const allEntries = Object.values(entriesByViewport).flat();
318
+ const metadataById = buildMetadataMap(allEntries);
319
+ // ---- Assemble layout data ----
320
+ const layoutData = {
321
+ viewports: viewportsData
322
+ };
323
+ // ---- Render ----
324
+ const result = await renderLayout(template, layoutData, models, {
325
+ allowRemoteAssets: template.allowRemoteAssets ?? options?.allowRemoteAssets,
326
+ qrContext: options?.qrContext,
327
+ metadataById
328
+ });
329
+ return {
330
+ svg: result.svg,
331
+ warnings: [...warnings, ...result.warnings]
332
+ };
333
+ }
334
+ // ---------------------------------------------------------------------------
335
+ // Helpers
336
+ // ---------------------------------------------------------------------------
337
+ /**
338
+ * Build NormalizedProductItems from per-product entries.
339
+ *
340
+ * Shared between the standard path (plane sections) and the models path
341
+ * (slab sections / pre-built models with productEntries).
342
+ */
343
+ function buildItemsFromEntries(entries, fills, sampleColor) {
344
+ return entries.map((entry) => {
345
+ const product = entry.product;
346
+ const propertySets = normalizePropertySets(product);
347
+ const gid = getProp(product, 'gid') ?? getProp(product, 'id');
348
+ const center = computePolylinesCenter(entry);
349
+ const name = getProp(product, 'name');
350
+ const displayName = typeof name === 'string' && name.trim().length ? name.trim() : gid ? String(gid) : '-';
351
+ const item = {
352
+ gid: gid ? String(gid) : undefined,
353
+ ifcClass: resolveIfcClassName(product),
354
+ ifcClassId: resolveIfcClassId(product),
355
+ propertySets,
356
+ productId: gid ? String(gid) : '-',
357
+ productName: displayName,
358
+ area: null,
359
+ x: center.x,
360
+ z: center.z,
361
+ text: displayName,
362
+ polylines: entry.polylines
363
+ };
364
+ if (fills?.length && sampleColor) {
365
+ const color = resolveFillColor(item, product, fills, sampleColor);
366
+ if (color) {
367
+ item.color = color;
368
+ }
369
+ }
370
+ return item;
371
+ });
372
+ }
373
+ /**
374
+ * Estimate label font size in model units from the template body style and the
375
+ * approximate viewport-to-world scale ratio.
376
+ */
377
+ function estimateLabelFontSize(template, vp, perProduct) {
378
+ const templateFontSizeMm = template.styles?.body?.fontSizeMm ?? 3.5;
379
+ // Quick world-bounds scan
380
+ let minX = Infinity;
381
+ let maxX = -Infinity;
382
+ let minZ = Infinity;
383
+ let maxZ = -Infinity;
384
+ for (const entry of perProduct) {
385
+ if (!entry.polylines)
386
+ continue;
387
+ for (const polyline of entry.polylines) {
388
+ if (!polyline.points)
389
+ continue;
390
+ for (const pt of polyline.points) {
391
+ const px = getProp(pt, 'x');
392
+ const pz = getProp(pt, 'z');
393
+ if (Number.isFinite(px) && Number.isFinite(pz)) {
394
+ if (px < minX)
395
+ minX = px;
396
+ if (px > maxX)
397
+ maxX = px;
398
+ if (pz < minZ)
399
+ minZ = pz;
400
+ if (pz > maxZ)
401
+ maxZ = pz;
402
+ }
403
+ }
404
+ }
405
+ }
406
+ const worldWidth = maxX - minX;
407
+ const worldHeight = maxZ - minZ;
408
+ if (!Number.isFinite(worldWidth) || worldWidth <= 0 || !Number.isFinite(worldHeight) || worldHeight <= 0) {
409
+ return templateFontSizeMm; // Fallback: 1:1 estimate
410
+ }
411
+ // Approximate mm-per-model-unit from the viewport dimensions
412
+ const scaleX = vp.widthMm / worldWidth;
413
+ const scaleY = vp.heightMm / worldHeight;
414
+ const mmPerModelUnit = Math.min(scaleX, scaleY);
415
+ if (!Number.isFinite(mmPerModelUnit) || mmPerModelUnit <= 0) {
416
+ return templateFontSizeMm;
417
+ }
418
+ return templateFontSizeMm / mmPerModelUnit;
419
+ }
420
+ // ---------------------------------------------------------------------------
421
+ // Label style defaults
422
+ // ---------------------------------------------------------------------------
423
+ /** Built-in label style defaults when template body styles are partially set. */
424
+ export const BUILTIN_LABEL_DEFAULTS = {
425
+ fill: '#374151',
426
+ fontFamily: '"Segoe UI", "Helvetica Neue", Arial, sans-serif',
427
+ fontWeight: '300'
428
+ };
429
+ // ---------------------------------------------------------------------------
430
+ // Line style defaults & resolution
431
+ // ---------------------------------------------------------------------------
432
+ /** Built-in defaults when neither lineStyles nor lineWidthMm/lineColor are set. */
433
+ export const BUILTIN_LINE_DEFAULTS = {
434
+ widthMm: 0.35, // Reasonable default: 0.35mm line width (was 0.01mm - too thin!)
435
+ color: '#1f2937',
436
+ dash: '',
437
+ enabled: true
438
+ };
439
+ export const BUILTIN_HIDDEN_DEFAULTS = {
440
+ widthMm: 0.005,
441
+ color: '#9ca3af',
442
+ dash: '4 2',
443
+ enabled: true
444
+ };
445
+ /** Category-specific built-in defaults (merged over the global default). */
446
+ export const CATEGORY_BUILTIN_OVERRIDES = {
447
+ cutLines: { widthMm: 0.02 },
448
+ hiddenLines: BUILTIN_HIDDEN_DEFAULTS,
449
+ outlineHidden: BUILTIN_HIDDEN_DEFAULTS
450
+ };
451
+ /**
452
+ * Resolve the effective line style for every category.
453
+ *
454
+ * Priority: category entry in `lineStyles` → `lineStyles.default`
455
+ * → `lineWidthMm`/`lineColor` shorthand → built-in defaults.
456
+ */
457
+ function resolveLineStyles(vp) {
458
+ const ls = vp.lineStyles;
459
+ // Base default: lineStyles.default → shorthand → built-in
460
+ const base = {
461
+ ...BUILTIN_LINE_DEFAULTS,
462
+ ...(vp.lineWidthMm !== undefined ? { widthMm: vp.lineWidthMm } : {}),
463
+ ...(vp.lineColor !== undefined ? { color: vp.lineColor } : {}),
464
+ ...(ls?.default ?? {})
465
+ };
466
+ const categories = [
467
+ 'cutLines',
468
+ 'visibleLines',
469
+ 'hiddenLines',
470
+ 'silhouettes',
471
+ 'featureEdges',
472
+ 'stairArrows',
473
+ 'outlineVisible',
474
+ 'outlineHidden'
475
+ ];
476
+ // Category group mapping: individual categories inherit from their group style
477
+ const categoryGroups = {
478
+ cutLines: 'visibleLines',
479
+ visibleLines: 'visibleLines',
480
+ silhouettes: 'visibleLines',
481
+ featureEdges: 'visibleLines',
482
+ outlineVisible: 'visibleLines',
483
+ hiddenLines: 'hiddenLines',
484
+ outlineHidden: 'hiddenLines',
485
+ stairArrows: 'stairArrows'
486
+ };
487
+ const byCategory = {};
488
+ for (const cat of categories) {
489
+ const builtinOverride = CATEGORY_BUILTIN_OVERRIDES[cat];
490
+ const groupName = categoryGroups[cat];
491
+ const groupStyle = ls?.[groupName]; // Group style (visibleLines, hiddenLines, stairArrows)
492
+ const categoryOverride = ls?.[cat]; // Individual category settings (enable/disable)
493
+ byCategory[cat] = {
494
+ ...base,
495
+ ...(builtinOverride ?? {}),
496
+ ...(groupStyle ?? {}), // Apply group style first
497
+ ...(categoryOverride ?? {}) // Then individual overrides (enable/disable)
498
+ };
499
+ }
500
+ return { default: base, byCategory };
501
+ }
502
+ // ---------------------------------------------------------------------------
503
+ // Fill rule helpers
504
+ // ---------------------------------------------------------------------------
505
+ /**
506
+ * Resolve fill color for an item based on viewport fill rules.
507
+ * Returns an RGBA tuple [r, g, b, a] (0–255) or undefined if no rule matches.
508
+ */
509
+ export function resolveFillColor(item, product, fills, sampleColor) {
510
+ if (!fills?.length)
511
+ return undefined;
512
+ for (const rule of fills) {
513
+ if (!matchesV2Filter(item, rule.class, rule.where))
514
+ continue;
515
+ if (rule.fill === 'sample') {
516
+ const sampled = sampleColor(product);
517
+ if (sampled) {
518
+ return [
519
+ Math.round(sampled.color.r * 255),
520
+ Math.round(sampled.color.g * 255),
521
+ Math.round(sampled.color.b * 255),
522
+ Math.round(sampled.alpha * 255)
523
+ ];
524
+ }
525
+ }
526
+ else {
527
+ return parseHexToRgba(rule.fill);
528
+ }
529
+ break; // first matching rule wins
530
+ }
531
+ return undefined;
532
+ }
533
+ /**
534
+ * Parse a CSS hex color string to an RGBA tuple (0–255).
535
+ */
536
+ function parseHexToRgba(hex) {
537
+ const h = hex.replace(/^#/, '');
538
+ if (h.length === 3) {
539
+ return [parseInt(h[0] + h[0], 16), parseInt(h[1] + h[1], 16), parseInt(h[2] + h[2], 16), 255];
540
+ }
541
+ if (h.length === 6) {
542
+ return [parseInt(h.substring(0, 2), 16), parseInt(h.substring(2, 4), 16), parseInt(h.substring(4, 6), 16), 255];
543
+ }
544
+ if (h.length === 8) {
545
+ return [
546
+ parseInt(h.substring(0, 2), 16),
547
+ parseInt(h.substring(2, 4), 16),
548
+ parseInt(h.substring(4, 6), 16),
549
+ parseInt(h.substring(6, 8), 16)
550
+ ];
551
+ }
552
+ // Fallback: opaque black
553
+ return [0, 0, 0, 255];
554
+ }
555
+ //# sourceMappingURL=renderDrawing.js.map