-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathBoard.ts
More file actions
732 lines (626 loc) · 22.4 KB
/
Board.ts
File metadata and controls
732 lines (626 loc) · 22.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
import {
runAllNetlistChecks,
runAllPinSpecificationChecks,
runAllPlacementChecks,
runAllRoutingChecks,
} from "@tscircuit/checks"
import { getBoundsFromPoints } from "@tscircuit/math-utils"
import { boardProps } from "@tscircuit/props"
import type { AnyCircuitElement, PcbBoard } from "circuit-json"
import { type Matrix, compose, translate } from "transformation-matrix"
import { getDescendantSubcircuitIds } from "../../utils/autorouting/getAncestorSubcircuitIds"
import { getBoardCenterFromAnchor } from "../../utils/boards/get-board-center-from-anchor"
import { inflateCircuitJson } from "../../utils/circuit-json/inflate-circuit-json"
import { NormalComponent } from "../base-components/NormalComponent/NormalComponent"
import type { RenderPhase } from "../base-components/Renderable"
import { Group } from "../primitive-components/Group/Group"
import type { SubcircuitI } from "../primitive-components/Group/Subcircuit/SubcircuitI"
import { Subcircuit_doInitialRenderIsolatedSubcircuits } from "../primitive-components/Group/Subcircuit/Subcircuit_doInitialRenderIsolatedSubcircuits"
import { Subcircuit_getSubcircuitPropHash } from "../primitive-components/Group/Subcircuit_getSubcircuitPropHash"
import type { BoardI } from "./BoardI"
import { jlcMinTolerances } from "../../utils/pcb/jlc-manufacturing-tolerances"
const MIN_EFFECTIVE_BORDER_RADIUS_MM = 0.01
const getRoundedRectOutline = (
width: number,
height: number,
radius: number,
) => {
const w2 = width / 2
const h2 = height / 2
const r = Math.min(radius, w2, h2)
if (r < MIN_EFFECTIVE_BORDER_RADIUS_MM) {
return [
{ x: -w2, y: -h2 },
{ x: w2, y: -h2 },
{ x: w2, y: h2 },
{ x: -w2, y: h2 },
]
}
const maxArcLengthPerSegment = 0.1 // mm
const segments = Math.max(
1,
Math.ceil(((Math.PI / 2) * r) / maxArcLengthPerSegment),
)
const step = Math.PI / 2 / segments
const outline: { x: number; y: number }[] = []
outline.push({ x: -w2 + r, y: -h2 })
outline.push({ x: w2 - r, y: -h2 })
for (let i = 1; i <= segments; i++) {
const theta = -Math.PI / 2 + i * step
outline.push({
x: w2 - r + r * Math.cos(theta),
y: -h2 + r + r * Math.sin(theta),
})
}
outline.push({ x: w2, y: h2 - r })
for (let i = 1; i <= segments; i++) {
const theta = 0 + i * step
outline.push({
x: w2 - r + r * Math.cos(theta),
y: h2 - r + r * Math.sin(theta),
})
}
outline.push({ x: -w2 + r, y: h2 })
for (let i = 1; i <= segments; i++) {
const theta = Math.PI / 2 + i * step
outline.push({
x: -w2 + r + r * Math.cos(theta),
y: h2 - r + r * Math.sin(theta),
})
}
outline.push({ x: -w2, y: -h2 + r })
for (let i = 1; i <= segments; i++) {
const theta = Math.PI + i * step
outline.push({
x: -w2 + r + r * Math.cos(theta),
y: -h2 + r + r * Math.sin(theta),
})
}
return outline
}
export class Board
extends Group<typeof boardProps>
implements BoardI, SubcircuitI
{
pcb_board_id: string | null = null
source_board_id: string | null = null
_drcChecksComplete = false
_drcChecksInProgress = false
_connectedSchematicPortPairs = new Set<string>()
_panelPositionOffset: { x: number; y: number } | null = null
get isSubcircuit() {
return true
}
get isGroup() {
return true
}
get config() {
return {
componentName: "Board",
zodProps: boardProps,
}
}
get boardThickness() {
return this._parsedProps.thickness ?? 1.4
}
/**
* Get all available layers for the board
*/
get allLayers() {
const layerCount = this._parsedProps.layers ?? 2
if (layerCount === 1) {
return ["top"] as const
}
if (layerCount === 4) {
return ["top", "bottom", "inner1", "inner2"] as const
}
return ["top", "bottom"] as const
}
_getSubcircuitLayerCount(): number {
return this._parsedProps.layers ?? 2
}
override _computePcbGlobalTransformBeforeLayout(): Matrix {
// If we have a panel-computed offset, incorporate it into the transform
if (this._panelPositionOffset) {
// Get parent transform (typically the Panel)
const parentTransform =
this.parent?._computePcbGlobalTransformBeforeLayout?.() ??
({ a: 1, b: 0, c: 0, d: 1, e: 0, f: 0 } as Matrix)
// Compose parent transform with panel offset translation
// The panel offset is relative to the panel center
return compose(
parentTransform,
translate(this._panelPositionOffset.x, this._panelPositionOffset.y),
)
}
// Otherwise, fall back to the default behavior
return super._computePcbGlobalTransformBeforeLayout()
}
_getBoardCalcVariables(): Record<string, number> {
const { _parsedProps: props } = this
const isAutoSized =
(props.width == null || props.height == null) && !props.outline
if (isAutoSized) return {}
const dbBoard = this.pcb_board_id
? this.root?.db.pcb_board.get(this.pcb_board_id)
: null
let width = dbBoard?.width ?? props.width
let height = dbBoard?.height ?? props.height
if ((width == null || height == null) && props.outline?.length) {
const outlineBounds = getBoundsFromPoints(props.outline)
if (outlineBounds) {
width ??= outlineBounds.maxX - outlineBounds.minX
height ??= outlineBounds.maxY - outlineBounds.minY
}
}
const { pcbX, pcbY } = this.getResolvedPcbPositionProp()
const center = dbBoard?.center ?? {
x: pcbX + (props.outlineOffsetX ?? 0),
y: pcbY + (props.outlineOffsetY ?? 0),
}
const resolvedWidth = width ?? 0
const resolvedHeight = height ?? 0
return {
"board.minX": center.x - resolvedWidth / 2,
"board.maxX": center.x + resolvedWidth / 2,
"board.minY": center.y - resolvedHeight / 2,
"board.maxY": center.y + resolvedHeight / 2,
}
}
doInitialPcbBoardAutoSize(): void {
if (this.root?.pcbDisabled) return
if (!this.pcb_board_id) return
const { db } = this.root!
const { _parsedProps: props } = this
// Use global position to properly handle boards inside panels
const globalPos = this._getGlobalPcbPositionBeforeLayout()
const pcbBoard = db.pcb_board.get(this.pcb_board_id!)
// If the board is already sized (from props or circuitJson) or has an outline, don't autosize
if (
(pcbBoard?.width && pcbBoard?.height) ||
(pcbBoard?.outline && pcbBoard.outline.length > 0)
)
return
let minX = Infinity
let minY = Infinity
let maxX = -Infinity
let maxY = -Infinity
// Get all PCB components and groups from the database
const descendantIds = getDescendantSubcircuitIds(db, this.subcircuit_id!)
const allowedSubcircuitIds = new Set([this.subcircuit_id, ...descendantIds])
const allPcbComponents = db.pcb_component
.list()
.filter(
(c) => c.subcircuit_id && allowedSubcircuitIds.has(c.subcircuit_id),
)
const allPcbGroups = db.pcb_group
.list()
.filter(
(g) => g.subcircuit_id && allowedSubcircuitIds.has(g.subcircuit_id),
)
let hasComponents = false
const updateBounds = (
center: { x: number; y: number },
width: number,
height: number,
) => {
if (width === 0 || height === 0) return
hasComponents = true
minX = Math.min(minX, center.x - width / 2)
minY = Math.min(minY, center.y - height / 2)
maxX = Math.max(maxX, center.x + width / 2)
maxY = Math.max(maxY, center.y + height / 2)
}
// Process all PCB components
for (const pcbComponent of allPcbComponents) {
updateBounds(pcbComponent.center, pcbComponent.width, pcbComponent.height)
}
// Process all PCB groups (for nested subcircuits)
for (const pcbGroup of allPcbGroups) {
let width = pcbGroup.width ?? 0
let height = pcbGroup.height ?? 0
// If the group has an outline, calculate width and height from outline
if (pcbGroup.outline && pcbGroup.outline.length > 0) {
const bounds = getBoundsFromPoints(pcbGroup.outline)
if (bounds) {
width = bounds.maxX - bounds.minX
height = bounds.maxY - bounds.minY
}
}
updateBounds(pcbGroup.center, width, height)
}
if (props.boardAnchorPosition) {
const { x, y } = props.boardAnchorPosition
minX = Math.min(minX, x)
minY = Math.min(minY, y)
maxX = Math.max(maxX, x)
maxY = Math.max(maxY, y)
}
// Add padding around components
const padding = 2
// Use dimensions of 0,0 when no components are found
const computedWidth = hasComponents ? maxX - minX + padding * 2 : 0
const computedHeight = hasComponents ? maxY - minY + padding * 2 : 0
// Center the board around the components or use global position for empty boards
const center = {
x: hasComponents
? (minX + maxX) / 2 + (props.outlineOffsetX ?? 0)
: (props.outlineOffsetX ?? 0) + globalPos.x,
y: hasComponents
? (minY + maxY) / 2 + (props.outlineOffsetY ?? 0)
: (props.outlineOffsetY ?? 0) + globalPos.y,
}
// by the user while auto-calculating the missing one.
const finalWidth = props.width ?? computedWidth
const finalHeight = props.height ?? computedHeight
let outline = props.outline as { x: number; y: number }[] | undefined
if (
!outline &&
props.borderRadius != null &&
finalWidth > 0 &&
finalHeight > 0
) {
outline = getRoundedRectOutline(
finalWidth,
finalHeight,
props.borderRadius,
)
}
const update: Record<string, unknown> = {
width: finalWidth,
height: finalHeight,
center,
}
if (outline) {
update.outline = outline.map((point) => ({
x: point.x + (props.outlineOffsetX ?? 0),
y: point.y + (props.outlineOffsetY ?? 0),
}))
}
db.pcb_board.update(this.pcb_board_id, update)
}
// Recompute autosize after child components update (e.g., async footprints)
updatePcbBoardAutoSize(): void {
// Reuse the same logic as initial autosize; it is idempotent
this.doInitialPcbBoardAutoSize()
}
/**
* Update the board information silkscreen text if platform config is set and
* the project name, version, or url is set.
*/
private _addBoardInformationToSilkscreen() {
const platform = this.root?.platform
if (!platform?.printBoardInformationToSilkscreen) return
const pcbBoard = this.root!.db.pcb_board.get(this.pcb_board_id!)
if (!pcbBoard) return
const boardInformation: string[] = []
if (platform.projectName) boardInformation.push(platform.projectName)
if (platform.version) boardInformation.push(`v${platform.version}`)
if (platform.url) boardInformation.push(platform.url)
if (boardInformation.length === 0) return
const text = boardInformation.join("\n")
const marginX = 0.25
const marginY = 1
const position = {
x: pcbBoard.center.x + pcbBoard.width! / 2 - marginX,
y: pcbBoard.center.y - pcbBoard.height! / 2 + marginY,
}
this.root!.db.pcb_silkscreen_text.insert({
pcb_component_id: this.pcb_board_id!,
layer: "top",
font: "tscircuit2024",
font_size: 0.45,
text,
ccw_rotation: 0,
anchor_alignment: "bottom_right",
anchor_position: position,
})
}
doInitialSourceRender() {
// Check for nested boards (boards inside this board at any depth)
const nestedBoard = this.getDescendants().find(
(d) => d.lowercaseComponentName === "board",
)
if (nestedBoard) {
throw new Error(
`Nested boards are not supported: found board "${nestedBoard.name}" inside board "${this.name}"`,
)
}
super.doInitialSourceRender()
const { db } = this.root!
const source_board = db.source_board.insert({
source_group_id: this.source_group_id!,
title: this.props.title || this.props.name,
})
this.source_board_id = source_board.source_board_id
}
/**
* Computes a hash of this board's props and children for caching.
* Position/identity props are excluded so identical boards at
* different locations share the same hash.
*/
getSubcircuitPropHash(): string {
return Subcircuit_getSubcircuitPropHash(this)
}
/**
* Render this board in isolation if _subcircuitCachingEnabled is set.
* This phase runs before InflateSubcircuitCircuitJson to prepare the
* isolated circuit JSON that will be inflated.
*/
doInitialRenderIsolatedSubcircuits(): void {
Subcircuit_doInitialRenderIsolatedSubcircuits(this)
}
doInitialInflateSubcircuitCircuitJson() {
const isolatedJson = this._isolatedCircuitJson
if (isolatedJson) {
this._isInflatedFromCircuitJson = true
this._isolatedCircuitJson = null
inflateCircuitJson(this, isolatedJson, [])
return
}
const { circuitJson, children } = this._parsedProps
if (circuitJson) {
this._isInflatedFromCircuitJson = true
}
inflateCircuitJson(this, circuitJson, children)
}
doInitialPcbComponentRender(): void {
if (this.root?.pcbDisabled) return
const { db } = this.root!
const { _parsedProps: props } = this
const circuitJsonElements = props.circuitJson
const pcbBoardFromCircuitJson = circuitJsonElements?.find(
(elm) => elm.type === "pcb_board",
)
// Initialize with minimal dimensions if not provided
// They will be updated in PcbBoardAutoSize phase
let computedWidth = props.width ?? pcbBoardFromCircuitJson?.width ?? 0
let computedHeight = props.height ?? pcbBoardFromCircuitJson?.height ?? 0
// Use global position to properly handle boards inside panels
const globalPos = this._getGlobalPcbPositionBeforeLayout()
let center = {
x: globalPos.x + (props.outlineOffsetX ?? 0),
y: globalPos.y + (props.outlineOffsetY ?? 0),
}
const { boardAnchorPosition, boardAnchorAlignment } = props
if (boardAnchorPosition) {
center = getBoardCenterFromAnchor({
boardAnchorPosition,
boardAnchorAlignment: boardAnchorAlignment ?? "center",
width: computedWidth,
height: computedHeight,
})
}
// Compute width and height from outline if not provided
if (props.outline) {
const xValues = props.outline.map((point) => point.x)
const yValues = props.outline.map((point) => point.y)
const minX = Math.min(...xValues)
const maxX = Math.max(...xValues)
const minY = Math.min(...yValues)
const maxY = Math.max(...yValues)
computedWidth = maxX - minX
computedHeight = maxY - minY
}
let outline = props.outline
if (
!outline &&
props.borderRadius != null &&
computedWidth > 0 &&
computedHeight > 0
) {
outline = getRoundedRectOutline(
computedWidth,
computedHeight,
props.borderRadius,
)
}
// Calculate outline translation to center it on the board position
// This is ONLY needed for boards inside panels/subpanels, where the board
// position is offset from the origin by the panel layout
let outlineTranslation = { x: 0, y: 0 }
if (
outline &&
outline.length > 0 &&
(this.parent?.lowercaseComponentName === "panel" ||
this.parent?.lowercaseComponentName === "subpanel")
) {
// Find the center of the original outline
const outlineBounds = getBoundsFromPoints(outline)
if (outlineBounds) {
const outlineCenterX = (outlineBounds.minX + outlineBounds.maxX) / 2
const outlineCenterY = (outlineBounds.minY + outlineBounds.maxY) / 2
// Translation needed to move outline center to board center
outlineTranslation = {
x: center.x - outlineCenterX,
y: center.y - outlineCenterY,
}
}
}
const subcircuitProps = this.getSubcircuit()._parsedProps
const pcb_board = db.pcb_board.insert({
source_board_id: this.source_board_id,
center,
thickness: this.boardThickness,
num_layers: this.allLayers.length,
width: computedWidth!,
height: computedHeight!,
outline: outline?.map((point) => ({
x: point.x + (props.outlineOffsetX ?? 0) + outlineTranslation.x,
y: point.y + (props.outlineOffsetY ?? 0) + outlineTranslation.y,
})),
material: props.material,
min_trace_width:
subcircuitProps.minTraceWidth ?? jlcMinTolerances.min_trace_width,
min_via_hole_diameter:
subcircuitProps.minViaHoleDiameter ??
jlcMinTolerances.min_via_hole_diameter,
min_via_pad_diameter:
subcircuitProps.minViaPadDiameter ??
jlcMinTolerances.min_via_pad_diameter,
min_via_to_via_clearance:
subcircuitProps.minViaToViaClearance ??
jlcMinTolerances.min_via_to_via_clearance,
min_trace_to_pad_clearance:
subcircuitProps.minTraceToPadClearance ??
jlcMinTolerances.min_trace_to_pad_clearance,
min_pad_to_pad_clearance:
subcircuitProps.minPadToPadClearance ??
jlcMinTolerances.min_pad_to_pad_clearance,
} as Omit<PcbBoard, "type" | "pcb_board_id">)
this.pcb_board_id = pcb_board.pcb_board_id!
// Add board information silkscreen text
this._addBoardInformationToSilkscreen()
}
removePcbComponentRender(): void {
const { db } = this.root!
if (!this.pcb_board_id) return
db.pcb_board.delete(this.pcb_board_id!)
this.pcb_board_id = null
}
doInitialPcbDesignRuleChecks() {
if (this.root?.pcbDisabled) return
super.doInitialPcbDesignRuleChecks()
this.updatePcbDesignRuleChecks()
}
updatePcbDesignRuleChecks() {
const { db } = this.root!
const routingDisabled =
this.root?.pcbRoutingDisabled ||
this.getInheritedProperty("routingDisabled")
const pcbDisabled = this.root?.pcbDisabled
const drcChecksDisabled =
this.root?.platform?.drcChecksDisabled ??
this.getInheritedProperty("drcChecksDisabled")
const netlistDrcChecksDisabled =
this.root?.platform?.netlistDrcChecksDisabled ??
this.getInheritedProperty("netlistDrcChecksDisabled")
const pinSpecificationDrcChecksDisabled = this.getInheritedProperty(
"pinSpecificationDrcChecksDisabled",
)
const placementDrcChecksDisabled =
this.root?.platform?.placementDrcChecksDisabled ??
this.getInheritedProperty("placementDrcChecksDisabled")
const routingDrcChecksDisabled =
this.root?.platform?.routingDrcChecksDisabled ??
this.getInheritedProperty("routingDrcChecksDisabled")
const shouldRunNetlistChecks =
!drcChecksDisabled && !netlistDrcChecksDisabled
const shouldRunPinSpecificationChecks =
!drcChecksDisabled && !pinSpecificationDrcChecksDisabled
const shouldRunPlacementChecks =
!drcChecksDisabled && !pcbDisabled && !placementDrcChecksDisabled
const shouldRunRoutingChecks =
!drcChecksDisabled &&
!pcbDisabled &&
!routingDisabled &&
!routingDrcChecksDisabled
// If async trace routing is still in progress anywhere in this board subtree,
// wait so routing DRC sees final routed traces and doesn't mark DRC complete early.
if (
shouldRunRoutingChecks &&
this._hasIncompleteAsyncEffectsInSubtreeForPhase("PcbTraceRender")
)
return
// Routing checks should only wait for child subcircuits when there are
// traces that actually need routing. Otherwise placement/netlist DRC can run.
const hasTracesToRoute = this._hasTracesToRoute()
if (
shouldRunRoutingChecks &&
hasTracesToRoute &&
!this._areChildSubcircuitsRouted()
)
return
// Only run once after all configured checks are complete.
if (this._drcChecksComplete || this._drcChecksInProgress) return
const runDrcChecks = async (circuitJson: AnyCircuitElement[]) => {
const checksToRun: Promise<AnyCircuitElement[]>[] = []
if (shouldRunRoutingChecks) {
checksToRun.push(
runAllRoutingChecks(circuitJson) as Promise<AnyCircuitElement[]>,
)
}
if (shouldRunPlacementChecks) {
checksToRun.push(
runAllPlacementChecks(circuitJson) as Promise<AnyCircuitElement[]>,
)
}
if (shouldRunNetlistChecks) {
checksToRun.push(
runAllNetlistChecks(circuitJson) as Promise<AnyCircuitElement[]>,
)
}
if (shouldRunPinSpecificationChecks) {
checksToRun.push(
runAllPinSpecificationChecks(circuitJson) as Promise<
AnyCircuitElement[]
>,
)
}
const checkResults = await Promise.all(checksToRun)
db.insertAll(checkResults.flat())
}
const subcircuit = db.subtree({ subcircuit_id: this.subcircuit_id })
const subcircuitCircuitJson = subcircuit.toArray()
this._drcChecksInProgress = true
this._queueAsyncEffect("board:drc-checks", async () => {
try {
await runDrcChecks(subcircuitCircuitJson)
this._drcChecksComplete = true
} finally {
this._drcChecksInProgress = false
}
})
}
override _emitRenderLifecycleEvent(
phase: RenderPhase,
startOrEnd: "start" | "end",
) {
super._emitRenderLifecycleEvent(phase, startOrEnd)
if (startOrEnd === "start") {
this.root?.emit("board:renderPhaseStarted", {
renderId: this._renderId,
phase,
})
}
}
_repositionOnPcb(position: { x: number; y: number }): void {
const { db } = this.root!
const pcbBoard = this.pcb_board_id
? db.pcb_board.get(this.pcb_board_id)
: null
const oldPos = pcbBoard?.center
if (!oldPos) {
if (this.pcb_board_id) {
db.pcb_board.update(this.pcb_board_id, { center: position })
}
return
}
const deltaX = position.x - oldPos.x
const deltaY = position.y - oldPos.y
if (Math.abs(deltaX) < 1e-6 && Math.abs(deltaY) < 1e-6) {
return
}
if (this.pcb_board_id) {
db.pcb_board.update(this.pcb_board_id, { center: position })
if (pcbBoard?.outline) {
const outlineBounds = getBoundsFromPoints(pcbBoard.outline)
if (outlineBounds) {
const oldOutlineCenter = {
x: (outlineBounds.minX + outlineBounds.maxX) / 2,
y: (outlineBounds.minY + outlineBounds.maxY) / 2,
}
const outlineDeltaX = position.x - oldOutlineCenter.x
const outlineDeltaY = position.y - oldOutlineCenter.y
const newOutline = pcbBoard.outline.map((p) => ({
x: p.x + outlineDeltaX,
y: p.y + outlineDeltaY,
}))
db.pcb_board.update(this.pcb_board_id, {
outline: newOutline,
})
}
}
}
}
}