Skip to content

Commit 9c573b9

Browse files
Improve PCB calc coordinate error handling and edge-prop diagnostics (#2044)
1 parent 8a28618 commit 9c573b9

File tree

6 files changed

+206
-83
lines changed

6 files changed

+206
-83
lines changed

lib/components/base-components/NormalComponent/NormalComponent.ts

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,28 +1750,28 @@ export class NormalComponent<
17501750
this._validatePcbCoordinateReferences({
17511751
rawValue: pcbLeftEdgeX,
17521752
axis: "pcbX",
1753-
propertyNameForError: "pcbLeftEdgeX",
1753+
propertyName: "pcbLeftEdgeX",
17541754
})
17551755
}
17561756
if (pcbRightEdgeX !== undefined) {
17571757
this._validatePcbCoordinateReferences({
17581758
rawValue: pcbRightEdgeX,
17591759
axis: "pcbX",
1760-
propertyNameForError: "pcbRightEdgeX",
1760+
propertyName: "pcbRightEdgeX",
17611761
})
17621762
}
17631763
if (pcbTopEdgeY !== undefined) {
17641764
this._validatePcbCoordinateReferences({
17651765
rawValue: pcbTopEdgeY,
17661766
axis: "pcbY",
1767-
propertyNameForError: "pcbTopEdgeY",
1767+
propertyName: "pcbTopEdgeY",
17681768
})
17691769
}
17701770
if (pcbBottomEdgeY !== undefined) {
17711771
this._validatePcbCoordinateReferences({
17721772
rawValue: pcbBottomEdgeY,
17731773
axis: "pcbY",
1774-
propertyNameForError: "pcbBottomEdgeY",
1774+
propertyName: "pcbBottomEdgeY",
17751775
})
17761776
}
17771777
}
@@ -1826,10 +1826,14 @@ export class NormalComponent<
18261826
(props.pcbX !== undefined
18271827
? this._resolvePcbCoordinate(props.pcbX, "pcbX")
18281828
: pcbLeftEdgeX !== undefined
1829-
? this._resolvePcbCoordinate(pcbLeftEdgeX, "pcbX") +
1829+
? this._resolvePcbCoordinate(pcbLeftEdgeX, "pcbX", {
1830+
propertyName: "pcbLeftEdgeX",
1831+
}) +
18301832
componentWidth / 2
18311833
: pcbRightEdgeX !== undefined
1832-
? this._resolvePcbCoordinate(pcbRightEdgeX, "pcbX") -
1834+
? this._resolvePcbCoordinate(pcbRightEdgeX, "pcbX", {
1835+
propertyName: "pcbRightEdgeX",
1836+
}) -
18331837
componentWidth / 2
18341838
: undefined)
18351839

@@ -1838,10 +1842,14 @@ export class NormalComponent<
18381842
(props.pcbY !== undefined
18391843
? this._resolvePcbCoordinate(props.pcbY, "pcbY")
18401844
: pcbTopEdgeY !== undefined
1841-
? this._resolvePcbCoordinate(pcbTopEdgeY, "pcbY") -
1845+
? this._resolvePcbCoordinate(pcbTopEdgeY, "pcbY", {
1846+
propertyName: "pcbTopEdgeY",
1847+
}) -
18421848
componentHeight / 2
18431849
: pcbBottomEdgeY !== undefined
1844-
? this._resolvePcbCoordinate(pcbBottomEdgeY, "pcbY") +
1850+
? this._resolvePcbCoordinate(pcbBottomEdgeY, "pcbY", {
1851+
propertyName: "pcbBottomEdgeY",
1852+
}) +
18451853
componentHeight / 2
18461854
: undefined)
18471855

lib/components/base-components/PrimitiveComponent/PrimitiveComponent.ts

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -244,21 +244,21 @@ export abstract class PrimitiveComponent<
244244
this._validatePcbCoordinateReferences({
245245
rawValue: rawPcbX,
246246
axis: "pcbX",
247-
propertyNameForError: "pcbX",
247+
propertyName: "pcbX",
248248
})
249249
this._validatePcbCoordinateReferences({
250250
rawValue: rawPcbY,
251251
axis: "pcbY",
252-
propertyNameForError: "pcbY",
252+
propertyName: "pcbY",
253253
})
254254
}
255255

256256
protected _validatePcbCoordinateReferences(params: {
257257
rawValue: unknown
258258
axis: "pcbX" | "pcbY"
259-
propertyNameForError?: string
259+
propertyName?: string
260260
}): void {
261-
const { rawValue, axis, propertyNameForError = axis } = params
261+
const { rawValue, axis, propertyName = axis } = params
262262
if (typeof rawValue !== "string") return
263263

264264
const isNormalComponent = (this as any)._isNormalComponent === true
@@ -270,8 +270,8 @@ export abstract class PrimitiveComponent<
270270
calcIdentifiers = extractCalcIdentifiers(rawValue)
271271
} catch {
272272
this._reportInvalidComponentPropertyError(
273-
propertyNameForError,
274-
`Invalid ${propertyNameForError} value for ${this.componentName}: Invalid calc() expression. expression="${rawValue}"`,
273+
propertyName,
274+
`Invalid ${propertyName} value for ${this.componentName}: Invalid calc() expression. expression="${rawValue}"`,
275275
)
276276
return
277277
}
@@ -282,8 +282,8 @@ export abstract class PrimitiveComponent<
282282

283283
if (includesComponentVariable && !allowComponentVariables) {
284284
this._reportInvalidComponentPropertyError(
285-
propertyNameForError,
286-
`Invalid ${propertyNameForError} value for ${this.componentName}: component-relative calc references are not supported for footprint elements (${this.componentName}); ${propertyNameForError} will be ignored. expression="${rawValue}"`,
285+
propertyName,
286+
`Invalid ${propertyName} value for ${this.componentName}: component-relative calc references are not supported for footprint elements (${this.componentName}); ${propertyName} will be ignored. expression="${rawValue}"`,
287287
)
288288
}
289289
}
@@ -295,10 +295,16 @@ export abstract class PrimitiveComponent<
295295
allowBoardVariables?: boolean
296296
allowComponentVariables?: boolean
297297
componentVariables?: Record<string, number>
298+
propertyName?: string
298299
} = {},
299300
): number {
300301
if (rawValue == null) return 0
301-
if (typeof rawValue === "number") return rawValue
302+
const propertyName = options.propertyName ?? axis
303+
304+
if (typeof rawValue === "number") {
305+
if (Number.isFinite(rawValue)) return rawValue
306+
return 0
307+
}
302308
if (typeof rawValue !== "string") {
303309
throw new Error(
304310
`Invalid ${axis} value for ${this.componentName}: ${String(rawValue)}`,
@@ -320,19 +326,23 @@ export abstract class PrimitiveComponent<
320326
const boardVariables = board?._getBoardCalcVariables() ?? {}
321327

322328
if (includesBoardVariable && !board) {
323-
throw new Error(
324-
`Cannot resolve ${axis} for ${this.componentName}: no board found for board.* variables`,
329+
this._reportInvalidComponentPropertyError(
330+
propertyName,
331+
`Invalid ${propertyName} value for ${this.componentName}: no board found for board.* variables. expression="${rawValue}"`,
325332
)
333+
return 0
326334
}
327335

328336
if (
329337
includesBoardVariable &&
330338
board &&
331339
Object.keys(boardVariables).length === 0
332340
) {
333-
throw new Error(
334-
"Cannot do calculations based on board size when the board is auto-sized",
341+
this._reportInvalidComponentPropertyError(
342+
propertyName,
343+
`Invalid ${propertyName} value for ${this.componentName}: Cannot do calculations based on board size when the board is auto-sized. expression="${rawValue}"`,
335344
)
345+
return 0
336346
}
337347

338348
Object.assign(knownVariables, boardVariables)
@@ -374,9 +384,11 @@ export abstract class PrimitiveComponent<
374384
return evaluateCalcString(rawValue, { knownVariables })
375385
} catch (error) {
376386
const message = error instanceof Error ? error.message : String(error)
377-
throw new Error(
378-
`Invalid ${axis} value for ${this.componentName}: ${message}`,
387+
this._reportInvalidComponentPropertyError(
388+
propertyName,
389+
`Invalid ${propertyName} value for ${this.componentName}: ${message}. expression="${rawValue}"`,
379390
)
391+
return 0
380392
}
381393
}
382394

@@ -429,16 +441,28 @@ export abstract class PrimitiveComponent<
429441
)
430442
}
431443

432-
resolvePcbCoordinate(
433-
rawValue: unknown,
434-
axis: "pcbX" | "pcbY",
435-
options: {
436-
allowBoardVariables?: boolean
437-
allowComponentVariables?: boolean
438-
componentVariables?: Record<string, number>
439-
} = {},
440-
): number {
441-
return this._resolvePcbCoordinate(rawValue, axis, options)
444+
resolvePcbCoordinate(params: {
445+
rawValue: unknown
446+
axis: "pcbX" | "pcbY"
447+
allowBoardVariables?: boolean
448+
allowComponentVariables?: boolean
449+
componentVariables?: Record<string, number>
450+
propertyName?: string
451+
}): number {
452+
const {
453+
rawValue,
454+
axis,
455+
allowBoardVariables,
456+
allowComponentVariables,
457+
componentVariables,
458+
propertyName,
459+
} = params
460+
return this._resolvePcbCoordinate(rawValue, axis, {
461+
allowBoardVariables,
462+
allowComponentVariables,
463+
componentVariables,
464+
propertyName,
465+
})
442466
}
443467

444468
/**

lib/components/primitive-components/Group/Group_doInitialPcbCalcPlacementResolution.ts

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -166,66 +166,66 @@ export function Group_doInitialPcbCalcPlacementResolution(
166166
}
167167

168168
if (rawPcbX !== undefined) {
169-
const resolvedPcbX = component.resolvePcbCoordinate(rawPcbX, "pcbX", {
169+
const resolvedPcbX = component.resolvePcbCoordinate({
170+
rawValue: rawPcbX,
171+
axis: "pcbX",
170172
allowComponentVariables: true,
171173
componentVariables: componentVars,
172174
})
173175
component._resolvedPcbCalcOffsetX = resolvedPcbX
174176
nextCenter.x = resolvedPcbX
175177
} else if (rawPcbLeftEdgeX !== undefined) {
176-
const resolvedPcbLeftEdgeX = component.resolvePcbCoordinate(
177-
rawPcbLeftEdgeX,
178-
"pcbX",
179-
{
180-
allowComponentVariables: true,
181-
componentVariables: componentVars,
182-
},
183-
)
178+
const resolvedPcbLeftEdgeX = component.resolvePcbCoordinate({
179+
rawValue: rawPcbLeftEdgeX,
180+
axis: "pcbX",
181+
allowComponentVariables: true,
182+
componentVariables: componentVars,
183+
propertyName: "pcbLeftEdgeX",
184+
})
184185
const resolvedPcbX = resolvedPcbLeftEdgeX + componentWidth / 2
185186
component._resolvedPcbCalcOffsetX = resolvedPcbX
186187
nextCenter.x = resolvedPcbX
187188
} else if (rawPcbRightEdgeX !== undefined) {
188-
const resolvedPcbRightEdgeX = component.resolvePcbCoordinate(
189-
rawPcbRightEdgeX,
190-
"pcbX",
191-
{
192-
allowComponentVariables: true,
193-
componentVariables: componentVars,
194-
},
195-
)
189+
const resolvedPcbRightEdgeX = component.resolvePcbCoordinate({
190+
rawValue: rawPcbRightEdgeX,
191+
axis: "pcbX",
192+
allowComponentVariables: true,
193+
componentVariables: componentVars,
194+
propertyName: "pcbRightEdgeX",
195+
})
196196
const resolvedPcbX = resolvedPcbRightEdgeX - componentWidth / 2
197197
component._resolvedPcbCalcOffsetX = resolvedPcbX
198198
nextCenter.x = resolvedPcbX
199199
}
200200

201201
if (rawPcbY !== undefined) {
202-
const resolvedPcbY = component.resolvePcbCoordinate(rawPcbY, "pcbY", {
202+
const resolvedPcbY = component.resolvePcbCoordinate({
203+
rawValue: rawPcbY,
204+
axis: "pcbY",
203205
allowComponentVariables: true,
204206
componentVariables: componentVars,
205207
})
206208
component._resolvedPcbCalcOffsetY = resolvedPcbY
207209
nextCenter.y = resolvedPcbY
208210
} else if (rawPcbTopEdgeY !== undefined) {
209-
const resolvedPcbTopEdgeY = component.resolvePcbCoordinate(
210-
rawPcbTopEdgeY,
211-
"pcbY",
212-
{
213-
allowComponentVariables: true,
214-
componentVariables: componentVars,
215-
},
216-
)
211+
const resolvedPcbTopEdgeY = component.resolvePcbCoordinate({
212+
rawValue: rawPcbTopEdgeY,
213+
axis: "pcbY",
214+
allowComponentVariables: true,
215+
componentVariables: componentVars,
216+
propertyName: "pcbTopEdgeY",
217+
})
217218
const resolvedPcbY = resolvedPcbTopEdgeY - componentHeight / 2
218219
component._resolvedPcbCalcOffsetY = resolvedPcbY
219220
nextCenter.y = resolvedPcbY
220221
} else if (rawPcbBottomEdgeY !== undefined) {
221-
const resolvedPcbBottomEdgeY = component.resolvePcbCoordinate(
222-
rawPcbBottomEdgeY,
223-
"pcbY",
224-
{
225-
allowComponentVariables: true,
226-
componentVariables: componentVars,
227-
},
228-
)
222+
const resolvedPcbBottomEdgeY = component.resolvePcbCoordinate({
223+
rawValue: rawPcbBottomEdgeY,
224+
axis: "pcbY",
225+
allowComponentVariables: true,
226+
componentVariables: componentVars,
227+
propertyName: "pcbBottomEdgeY",
228+
})
229229
const resolvedPcbY = resolvedPcbBottomEdgeY + componentHeight / 2
230230
component._resolvedPcbCalcOffsetY = resolvedPcbY
231231
nextCenter.y = resolvedPcbY

lib/components/primitive-components/SilkscreenText.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ export class SilkscreenText extends PrimitiveComponent<
9797
isFlipped ? flipY() : identity(),
9898
),
9999
{
100-
x: this.resolvePcbCoordinate(
101-
resolvedPcbSxPcbX ?? props.pcbX ?? 0,
102-
"pcbX",
103-
),
104-
y: this.resolvePcbCoordinate(
105-
resolvedPcbSxPcbY ?? props.pcbY ?? 0,
106-
"pcbY",
107-
),
100+
x: this.resolvePcbCoordinate({
101+
rawValue: resolvedPcbSxPcbX ?? props.pcbX ?? 0,
102+
axis: "pcbX",
103+
}),
104+
y: this.resolvePcbCoordinate({
105+
rawValue: resolvedPcbSxPcbY ?? props.pcbY ?? 0,
106+
axis: "pcbY",
107+
}),
108108
},
109109
)
110110
: this._getGlobalPcbPositionBeforeLayout()

0 commit comments

Comments
 (0)