Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/components/base-components/NormalComponent/NormalComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1750,28 +1750,28 @@ export class NormalComponent<
this._validatePcbCoordinateReferences({
rawValue: pcbLeftEdgeX,
axis: "pcbX",
propertyNameForError: "pcbLeftEdgeX",
propertyName: "pcbLeftEdgeX",
})
}
if (pcbRightEdgeX !== undefined) {
this._validatePcbCoordinateReferences({
rawValue: pcbRightEdgeX,
axis: "pcbX",
propertyNameForError: "pcbRightEdgeX",
propertyName: "pcbRightEdgeX",
})
}
if (pcbTopEdgeY !== undefined) {
this._validatePcbCoordinateReferences({
rawValue: pcbTopEdgeY,
axis: "pcbY",
propertyNameForError: "pcbTopEdgeY",
propertyName: "pcbTopEdgeY",
})
}
if (pcbBottomEdgeY !== undefined) {
this._validatePcbCoordinateReferences({
rawValue: pcbBottomEdgeY,
axis: "pcbY",
propertyNameForError: "pcbBottomEdgeY",
propertyName: "pcbBottomEdgeY",
})
}
}
Expand Down Expand Up @@ -1826,10 +1826,14 @@ export class NormalComponent<
(props.pcbX !== undefined
? this._resolvePcbCoordinate(props.pcbX, "pcbX")
: pcbLeftEdgeX !== undefined
? this._resolvePcbCoordinate(pcbLeftEdgeX, "pcbX") +
? this._resolvePcbCoordinate(pcbLeftEdgeX, "pcbX", {
propertyName: "pcbLeftEdgeX",
}) +
componentWidth / 2
: pcbRightEdgeX !== undefined
? this._resolvePcbCoordinate(pcbRightEdgeX, "pcbX") -
? this._resolvePcbCoordinate(pcbRightEdgeX, "pcbX", {
propertyName: "pcbRightEdgeX",
}) -
componentWidth / 2
: undefined)

Expand All @@ -1838,10 +1842,14 @@ export class NormalComponent<
(props.pcbY !== undefined
? this._resolvePcbCoordinate(props.pcbY, "pcbY")
: pcbTopEdgeY !== undefined
? this._resolvePcbCoordinate(pcbTopEdgeY, "pcbY") -
? this._resolvePcbCoordinate(pcbTopEdgeY, "pcbY", {
propertyName: "pcbTopEdgeY",
}) -
componentHeight / 2
: pcbBottomEdgeY !== undefined
? this._resolvePcbCoordinate(pcbBottomEdgeY, "pcbY") +
? this._resolvePcbCoordinate(pcbBottomEdgeY, "pcbY", {
propertyName: "pcbBottomEdgeY",
}) +
componentHeight / 2
: undefined)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,21 +244,21 @@ export abstract class PrimitiveComponent<
this._validatePcbCoordinateReferences({
rawValue: rawPcbX,
axis: "pcbX",
propertyNameForError: "pcbX",
propertyName: "pcbX",
})
this._validatePcbCoordinateReferences({
rawValue: rawPcbY,
axis: "pcbY",
propertyNameForError: "pcbY",
propertyName: "pcbY",
})
}

protected _validatePcbCoordinateReferences(params: {
rawValue: unknown
axis: "pcbX" | "pcbY"
propertyNameForError?: string
propertyName?: string
}): void {
const { rawValue, axis, propertyNameForError = axis } = params
const { rawValue, axis, propertyName = axis } = params
if (typeof rawValue !== "string") return

const isNormalComponent = (this as any)._isNormalComponent === true
Expand All @@ -270,8 +270,8 @@ export abstract class PrimitiveComponent<
calcIdentifiers = extractCalcIdentifiers(rawValue)
} catch {
this._reportInvalidComponentPropertyError(
propertyNameForError,
`Invalid ${propertyNameForError} value for ${this.componentName}: Invalid calc() expression. expression="${rawValue}"`,
propertyName,
`Invalid ${propertyName} value for ${this.componentName}: Invalid calc() expression. expression="${rawValue}"`,
)
return
}
Expand All @@ -282,8 +282,8 @@ export abstract class PrimitiveComponent<

if (includesComponentVariable && !allowComponentVariables) {
this._reportInvalidComponentPropertyError(
propertyNameForError,
`Invalid ${propertyNameForError} value for ${this.componentName}: component-relative calc references are not supported for footprint elements (${this.componentName}); ${propertyNameForError} will be ignored. expression="${rawValue}"`,
propertyName,
`Invalid ${propertyName} value for ${this.componentName}: component-relative calc references are not supported for footprint elements (${this.componentName}); ${propertyName} will be ignored. expression="${rawValue}"`,
)
}
}
Expand All @@ -295,10 +295,16 @@ export abstract class PrimitiveComponent<
allowBoardVariables?: boolean
allowComponentVariables?: boolean
componentVariables?: Record<string, number>
propertyName?: string
} = {},
): number {
if (rawValue == null) return 0
if (typeof rawValue === "number") return rawValue
const propertyName = options.propertyName ?? axis

if (typeof rawValue === "number") {
if (Number.isFinite(rawValue)) return rawValue
return 0
}
if (typeof rawValue !== "string") {
throw new Error(
`Invalid ${axis} value for ${this.componentName}: ${String(rawValue)}`,
Expand All @@ -320,19 +326,23 @@ export abstract class PrimitiveComponent<
const boardVariables = board?._getBoardCalcVariables() ?? {}

if (includesBoardVariable && !board) {
throw new Error(
`Cannot resolve ${axis} for ${this.componentName}: no board found for board.* variables`,
this._reportInvalidComponentPropertyError(
propertyName,
`Invalid ${propertyName} value for ${this.componentName}: no board found for board.* variables. expression="${rawValue}"`,
)
return 0
}

if (
includesBoardVariable &&
board &&
Object.keys(boardVariables).length === 0
) {
throw new Error(
"Cannot do calculations based on board size when the board is auto-sized",
this._reportInvalidComponentPropertyError(
propertyName,
`Invalid ${propertyName} value for ${this.componentName}: Cannot do calculations based on board size when the board is auto-sized. expression="${rawValue}"`,
)
return 0
}

Object.assign(knownVariables, boardVariables)
Expand Down Expand Up @@ -374,9 +384,11 @@ export abstract class PrimitiveComponent<
return evaluateCalcString(rawValue, { knownVariables })
} catch (error) {
const message = error instanceof Error ? error.message : String(error)
throw new Error(
`Invalid ${axis} value for ${this.componentName}: ${message}`,
this._reportInvalidComponentPropertyError(
propertyName,
`Invalid ${propertyName} value for ${this.componentName}: ${message}. expression="${rawValue}"`,
)
return 0
}
}

Expand Down Expand Up @@ -429,16 +441,28 @@ export abstract class PrimitiveComponent<
)
}

resolvePcbCoordinate(
rawValue: unknown,
axis: "pcbX" | "pcbY",
options: {
allowBoardVariables?: boolean
allowComponentVariables?: boolean
componentVariables?: Record<string, number>
} = {},
): number {
return this._resolvePcbCoordinate(rawValue, axis, options)
resolvePcbCoordinate(params: {
rawValue: unknown
axis: "pcbX" | "pcbY"
allowBoardVariables?: boolean
allowComponentVariables?: boolean
componentVariables?: Record<string, number>
propertyName?: string
}): number {
const {
rawValue,
axis,
allowBoardVariables,
allowComponentVariables,
componentVariables,
propertyName,
} = params
return this._resolvePcbCoordinate(rawValue, axis, {
allowBoardVariables,
allowComponentVariables,
componentVariables,
propertyName,
})
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,66 +166,66 @@ export function Group_doInitialPcbCalcPlacementResolution(
}

if (rawPcbX !== undefined) {
const resolvedPcbX = component.resolvePcbCoordinate(rawPcbX, "pcbX", {
const resolvedPcbX = component.resolvePcbCoordinate({
rawValue: rawPcbX,
axis: "pcbX",
allowComponentVariables: true,
componentVariables: componentVars,
})
component._resolvedPcbCalcOffsetX = resolvedPcbX
nextCenter.x = resolvedPcbX
} else if (rawPcbLeftEdgeX !== undefined) {
const resolvedPcbLeftEdgeX = component.resolvePcbCoordinate(
rawPcbLeftEdgeX,
"pcbX",
{
allowComponentVariables: true,
componentVariables: componentVars,
},
)
const resolvedPcbLeftEdgeX = component.resolvePcbCoordinate({
rawValue: rawPcbLeftEdgeX,
axis: "pcbX",
allowComponentVariables: true,
componentVariables: componentVars,
propertyName: "pcbLeftEdgeX",
})
const resolvedPcbX = resolvedPcbLeftEdgeX + componentWidth / 2
component._resolvedPcbCalcOffsetX = resolvedPcbX
nextCenter.x = resolvedPcbX
} else if (rawPcbRightEdgeX !== undefined) {
const resolvedPcbRightEdgeX = component.resolvePcbCoordinate(
rawPcbRightEdgeX,
"pcbX",
{
allowComponentVariables: true,
componentVariables: componentVars,
},
)
const resolvedPcbRightEdgeX = component.resolvePcbCoordinate({
rawValue: rawPcbRightEdgeX,
axis: "pcbX",
allowComponentVariables: true,
componentVariables: componentVars,
propertyName: "pcbRightEdgeX",
})
const resolvedPcbX = resolvedPcbRightEdgeX - componentWidth / 2
component._resolvedPcbCalcOffsetX = resolvedPcbX
nextCenter.x = resolvedPcbX
}

if (rawPcbY !== undefined) {
const resolvedPcbY = component.resolvePcbCoordinate(rawPcbY, "pcbY", {
const resolvedPcbY = component.resolvePcbCoordinate({
rawValue: rawPcbY,
axis: "pcbY",
allowComponentVariables: true,
componentVariables: componentVars,
})
component._resolvedPcbCalcOffsetY = resolvedPcbY
nextCenter.y = resolvedPcbY
} else if (rawPcbTopEdgeY !== undefined) {
const resolvedPcbTopEdgeY = component.resolvePcbCoordinate(
rawPcbTopEdgeY,
"pcbY",
{
allowComponentVariables: true,
componentVariables: componentVars,
},
)
const resolvedPcbTopEdgeY = component.resolvePcbCoordinate({
rawValue: rawPcbTopEdgeY,
axis: "pcbY",
allowComponentVariables: true,
componentVariables: componentVars,
propertyName: "pcbTopEdgeY",
})
const resolvedPcbY = resolvedPcbTopEdgeY - componentHeight / 2
component._resolvedPcbCalcOffsetY = resolvedPcbY
nextCenter.y = resolvedPcbY
} else if (rawPcbBottomEdgeY !== undefined) {
const resolvedPcbBottomEdgeY = component.resolvePcbCoordinate(
rawPcbBottomEdgeY,
"pcbY",
{
allowComponentVariables: true,
componentVariables: componentVars,
},
)
const resolvedPcbBottomEdgeY = component.resolvePcbCoordinate({
rawValue: rawPcbBottomEdgeY,
axis: "pcbY",
allowComponentVariables: true,
componentVariables: componentVars,
propertyName: "pcbBottomEdgeY",
})
const resolvedPcbY = resolvedPcbBottomEdgeY + componentHeight / 2
component._resolvedPcbCalcOffsetY = resolvedPcbY
nextCenter.y = resolvedPcbY
Expand Down
16 changes: 8 additions & 8 deletions lib/components/primitive-components/SilkscreenText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ export class SilkscreenText extends PrimitiveComponent<
isFlipped ? flipY() : identity(),
),
{
x: this.resolvePcbCoordinate(
resolvedPcbSxPcbX ?? props.pcbX ?? 0,
"pcbX",
),
y: this.resolvePcbCoordinate(
resolvedPcbSxPcbY ?? props.pcbY ?? 0,
"pcbY",
),
x: this.resolvePcbCoordinate({
rawValue: resolvedPcbSxPcbX ?? props.pcbX ?? 0,
axis: "pcbX",
}),
y: this.resolvePcbCoordinate({
rawValue: resolvedPcbSxPcbY ?? props.pcbY ?? 0,
axis: "pcbY",
}),
},
)
: this._getGlobalPcbPositionBeforeLayout()
Expand Down
Loading
Loading