altium-toolkit 1.1.23 → 1.1.25

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.
@@ -0,0 +1,313 @@
1
+ // SPDX-FileCopyrightText: 2026 André Fiedler
2
+ //
3
+ // SPDX-License-Identifier: GPL-3.0-or-later
4
+
5
+ /**
6
+ * Builds deterministic design-rule impact summaries for PCB review workflows.
7
+ */
8
+ export class PcbRuleImpactReportBuilder {
9
+ static SCHEMA = 'altium-toolkit.pcb.rule-impact.a1'
10
+
11
+ /**
12
+ * Builds a rule impact report from a parser root or PCB model.
13
+ * @param {object} input Parser root, PCB model, or options object.
14
+ * @returns {object}
15
+ */
16
+ static build(input = {}) {
17
+ const pcb = PcbRuleImpactReportBuilder.#pcb(input)
18
+ const rules = Array.isArray(pcb?.rules) ? pcb.rules : []
19
+ const rows = rules.map((rule) =>
20
+ PcbRuleImpactReportBuilder.#ruleRow(rule)
21
+ )
22
+
23
+ return {
24
+ schema: PcbRuleImpactReportBuilder.SCHEMA,
25
+ summary: PcbRuleImpactReportBuilder.#summary(rows),
26
+ primitiveCounts: PcbRuleImpactReportBuilder.#primitiveCounts(pcb),
27
+ rules: rows
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Resolves the PCB payload from a parser root or direct PCB model.
33
+ * @param {object} input Parser root or PCB model.
34
+ * @returns {object}
35
+ */
36
+ static #pcb(input) {
37
+ return input?.pcb || input || {}
38
+ }
39
+
40
+ /**
41
+ * Builds one public rule impact row.
42
+ * @param {object} rule Parsed design-rule row.
43
+ * @returns {object}
44
+ */
45
+ static #ruleRow(rule) {
46
+ const kind = PcbRuleImpactReportBuilder.#kind(rule)
47
+ const category = PcbRuleImpactReportBuilder.#category(rule, kind)
48
+
49
+ return PcbRuleImpactReportBuilder.#stripEmpty({
50
+ ruleIndex: PcbRuleImpactReportBuilder.#integerOrFallback(
51
+ rule?.ruleIndex,
52
+ 0
53
+ ),
54
+ name: rule?.name || '',
55
+ uniqueId: rule?.uniqueId || '',
56
+ kind,
57
+ category,
58
+ displayName:
59
+ rule?.ruleType?.displayName || rule?.displayName || kind,
60
+ enabled: rule?.enabled !== false,
61
+ priority: rule?.priority ?? undefined,
62
+ affectedFamilies: PcbRuleImpactReportBuilder.#affectedFamilies(
63
+ kind,
64
+ category
65
+ ),
66
+ scopes: PcbRuleImpactReportBuilder.#scopes(rule),
67
+ lengthConstraints:
68
+ PcbRuleImpactReportBuilder.#lengthConstraints(rule)
69
+ })
70
+ }
71
+
72
+ /**
73
+ * Builds top-level rule impact counters.
74
+ * @param {object[]} rows Rule impact rows.
75
+ * @returns {object}
76
+ */
77
+ static #summary(rows) {
78
+ const enabledRows = rows.filter((row) => row.enabled !== false)
79
+ const impactedFamilies = new Set(
80
+ enabledRows.flatMap((row) => row.affectedFamilies || [])
81
+ )
82
+
83
+ return {
84
+ ruleCount: rows.length,
85
+ enabledRuleCount: enabledRows.length,
86
+ disabledRuleCount: rows.length - enabledRows.length,
87
+ manufacturingRuleCount: enabledRows.filter(
88
+ (row) => row.category === 'manufacturing'
89
+ ).length,
90
+ scopedRuleCount: rows.filter((row) => row.scopes?.length).length,
91
+ impactedFamilyCount: impactedFamilies.size,
92
+ lengthConstraintCount: rows.reduce(
93
+ (total, row) =>
94
+ total + Number(row.lengthConstraints?.length || 0),
95
+ 0
96
+ )
97
+ }
98
+ }
99
+
100
+ /**
101
+ * Builds primitive-family counts available to report consumers.
102
+ * @param {object} pcb PCB model.
103
+ * @returns {object}
104
+ */
105
+ static #primitiveCounts(pcb) {
106
+ return Object.fromEntries(
107
+ [
108
+ 'pads',
109
+ 'vias',
110
+ 'tracks',
111
+ 'arcs',
112
+ 'fills',
113
+ 'regions',
114
+ 'polygons',
115
+ 'components'
116
+ ].map((family) => [
117
+ family,
118
+ Array.isArray(pcb?.[family]) ? pcb[family].length : 0
119
+ ])
120
+ )
121
+ }
122
+
123
+ /**
124
+ * Resolves a normalized rule kind.
125
+ * @param {object} rule Rule row.
126
+ * @returns {string}
127
+ */
128
+ static #kind(rule) {
129
+ return (
130
+ String(
131
+ rule?.ruleType?.kind ||
132
+ rule?.kind ||
133
+ rule?.ruleKind ||
134
+ 'unknown'
135
+ )
136
+ .trim()
137
+ .toLowerCase() || 'unknown'
138
+ )
139
+ }
140
+
141
+ /**
142
+ * Resolves a normalized rule category.
143
+ * @param {object} rule Rule row.
144
+ * @param {string} kind Normalized rule kind.
145
+ * @returns {string}
146
+ */
147
+ static #category(rule, kind) {
148
+ const explicit = String(
149
+ rule?.ruleType?.category || rule?.category || ''
150
+ )
151
+ .trim()
152
+ .toLowerCase()
153
+ if (explicit) {
154
+ return explicit
155
+ }
156
+
157
+ if (/paste|mask|silk|annular|testpoint/u.test(kind)) {
158
+ return 'manufacturing'
159
+ }
160
+ if (/width|length|routing|fanout|via/u.test(kind)) {
161
+ return 'routing'
162
+ }
163
+ if (/clearance|short|unrouted/u.test(kind)) {
164
+ return 'electrical'
165
+ }
166
+
167
+ return 'other'
168
+ }
169
+
170
+ /**
171
+ * Infers broad primitive families affected by one rule kind.
172
+ * @param {string} kind Rule kind.
173
+ * @param {string} category Rule category.
174
+ * @returns {string[]}
175
+ */
176
+ static #affectedFamilies(kind, category) {
177
+ if (/component/u.test(kind)) {
178
+ return ['components']
179
+ }
180
+ if (/paste/u.test(kind)) {
181
+ return ['pads']
182
+ }
183
+ if (/solder.*mask|mask.*expansion/u.test(kind)) {
184
+ return ['pads', 'vias']
185
+ }
186
+ if (/polygon|plane|thermal/u.test(kind)) {
187
+ return ['pads', 'polygons', 'vias']
188
+ }
189
+ if (/annular|fanout|via/u.test(kind)) {
190
+ return ['pads', 'vias']
191
+ }
192
+ if (/width/u.test(kind)) {
193
+ return ['arcs', 'tracks']
194
+ }
195
+ if (/length|routing|corner|topology/u.test(kind)) {
196
+ return ['arcs', 'tracks', 'vias']
197
+ }
198
+ if (/clearance|short/u.test(kind)) {
199
+ return ['arcs', 'fills', 'pads', 'regions', 'tracks', 'vias']
200
+ }
201
+ if (category === 'manufacturing') {
202
+ return ['pads', 'vias']
203
+ }
204
+
205
+ return []
206
+ }
207
+
208
+ /**
209
+ * Builds normalized scope rows for non-trivial rule scopes.
210
+ * @param {object} rule Rule row.
211
+ * @returns {object[]}
212
+ */
213
+ static #scopes(rule) {
214
+ return [
215
+ PcbRuleImpactReportBuilder.#scopeRow('scope1', rule?.scope1),
216
+ PcbRuleImpactReportBuilder.#scopeRow('scope2', rule?.scope2)
217
+ ].filter(Boolean)
218
+ }
219
+
220
+ /**
221
+ * Builds one normalized scope row.
222
+ * @param {string} side Scope side label.
223
+ * @param {object} scope Parsed scope expression.
224
+ * @returns {object | null}
225
+ */
226
+ static #scopeRow(side, scope) {
227
+ const expression = String(scope?.rawExpression || '').trim()
228
+ const predicate = String(scope?.predicate || '').trim()
229
+ const isAll = scope?.isAll === true || /^all$/iu.test(predicate)
230
+
231
+ if (!expression && !predicate) {
232
+ return null
233
+ }
234
+ if (isAll && !expression) {
235
+ return null
236
+ }
237
+
238
+ return PcbRuleImpactReportBuilder.#stripEmpty({
239
+ side,
240
+ expression,
241
+ predicate,
242
+ arguments: Array.isArray(scope?.arguments) ? scope.arguments : []
243
+ })
244
+ }
245
+
246
+ /**
247
+ * Extracts length-valued typed constraints.
248
+ * @param {object} rule Rule row.
249
+ * @returns {object[]}
250
+ */
251
+ static #lengthConstraints(rule) {
252
+ const source = Object.keys(rule?.typedConstraints || {}).length
253
+ ? rule.typedConstraints
254
+ : rule?.constraintValues || {}
255
+ const rows = []
256
+
257
+ for (const [name, value] of Object.entries(source || {})) {
258
+ if (!PcbRuleImpactReportBuilder.#isLengthConstraint(value)) {
259
+ continue
260
+ }
261
+ rows.push(
262
+ PcbRuleImpactReportBuilder.#stripEmpty({
263
+ name,
264
+ key: value.key,
265
+ raw: value.raw,
266
+ valueMil: value.valueMil,
267
+ valueMm: value.valueMm
268
+ })
269
+ )
270
+ }
271
+
272
+ return rows.sort((left, right) =>
273
+ left.name.localeCompare(right.name, undefined, { numeric: true })
274
+ )
275
+ }
276
+
277
+ /**
278
+ * Returns true when one parsed constraint carries length units.
279
+ * @param {object} value Parsed constraint value.
280
+ * @returns {boolean}
281
+ */
282
+ static #isLengthConstraint(value) {
283
+ return (
284
+ value?.type === 'length' ||
285
+ Number.isFinite(Number(value?.valueMil)) ||
286
+ Number.isFinite(Number(value?.valueMm))
287
+ )
288
+ }
289
+
290
+ /**
291
+ * Parses an integer with a fallback.
292
+ * @param {unknown} value Numeric candidate.
293
+ * @param {number} fallback Fallback value.
294
+ * @returns {number}
295
+ */
296
+ static #integerOrFallback(value, fallback) {
297
+ const number = Number(value)
298
+ return Number.isInteger(number) ? number : fallback
299
+ }
300
+
301
+ /**
302
+ * Removes undefined and blank-string values from a shallow object.
303
+ * @param {Record<string, unknown>} row Input row.
304
+ * @returns {Record<string, unknown>}
305
+ */
306
+ static #stripEmpty(row) {
307
+ return Object.fromEntries(
308
+ Object.entries(row).filter(
309
+ ([, value]) => value !== undefined && value !== ''
310
+ )
311
+ )
312
+ }
313
+ }