-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathSolderJumper.ts
More file actions
216 lines (194 loc) · 7.5 KB
/
SolderJumper.ts
File metadata and controls
216 lines (194 loc) · 7.5 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
import { NormalComponent } from "lib/components/base-components/NormalComponent"
import { solderjumperProps } from "@tscircuit/props"
import { Port } from "../primitive-components/Port"
import type { SchematicBoxDimensions } from "lib/utils/schematic/getAllDimensionsForSchematicBox"
export class SolderJumper<
PinLabels extends string = never,
> extends NormalComponent<typeof solderjumperProps, PinLabels> {
schematicDimensions: SchematicBoxDimensions | null = null
_getPinNumberFromBridgedPinName(pinName: string): number | null {
const port = this.selectOne(`port.${pinName}`, {
type: "port",
}) as Port | null
return port?._parsedProps.pinNumber ?? null
}
get defaultInternallyConnectedPinNames(): string[][] {
if (this._parsedProps.bridged) {
const pins = this.children
.filter((c) => c.componentName === "Port")
.map((p) => (p as Port).name)
return pins.length > 0 ? [pins] : []
}
return this._parsedProps.bridgedPins ?? []
}
get config() {
const props = this._parsedProps ?? this.props
let resolvedPinCount = props.pinCount
if (props.pinCount == null && !props.footprint) {
// If neither pinCount nor a footprint is given, assume two pins
resolvedPinCount = 2
}
if (props.pinCount == null) {
const nums = (props.bridgedPins ?? [])
.flat()
.map((p_str: string) => this._getPinNumberFromBridgedPinName(p_str))
.filter((n): n is number => n !== null)
const maxPinFromBridged = nums.length > 0 ? Math.max(...nums) : 0
const pinCountFromLabels = props.pinLabels
? Object.keys(props.pinLabels).length
: 0
const finalPinCount = Math.max(maxPinFromBridged, pinCountFromLabels)
// This logic is related to available solderjumper2, solderjumper3 symbols
if (finalPinCount === 2 || finalPinCount === 3) {
resolvedPinCount = finalPinCount as 2 | 3
}
// Fallback: infer from footprint pin count
if (
resolvedPinCount == null &&
props.footprint &&
[2, 3].includes(this.getPortsFromFootprint().length)
) {
resolvedPinCount = this.getPortsFromFootprint().length as 2 | 3
}
}
let symbolName = ""
if (resolvedPinCount) {
symbolName += `solderjumper${resolvedPinCount}`
} else {
// Fallback if pinCount couldn't be resolved (e.g. from non-numeric bridgedPins)
// This might lead to a generic box if "solderjumper" itself isn't a symbol.
symbolName = "solderjumper"
}
let bridgedPinNumbers: number[] = []
if (Array.isArray(props.bridgedPins) && props.bridgedPins.length > 0) {
// Normalize pin names (e.g., "pin1" to "1"), then get unique sorted numbers
bridgedPinNumbers = Array.from(
new Set(
(props.bridgedPins as string[][])
.flat()
.map((pinName) => this._getPinNumberFromBridgedPinName(pinName))
.filter((n): n is number => n !== null),
),
).sort((a, b) => a - b)
} else if (props.bridged && resolvedPinCount) {
bridgedPinNumbers = Array.from(
{ length: resolvedPinCount },
(_, i) => i + 1,
)
}
if (bridgedPinNumbers.length > 0) {
symbolName += `_bridged${bridgedPinNumbers.join("")}`
}
return {
schematicSymbolName: props.symbolName ?? symbolName,
componentName: "SolderJumper",
zodProps: solderjumperProps,
shouldRenderAsSchematicBox: true,
}
}
_getSchematicPortArrangement() {
const arrangement = super._getSchematicPortArrangement()
if (arrangement && Object.keys(arrangement).length > 0) return arrangement
let pinCount =
this._parsedProps.pinCount ??
(Array.isArray(this._parsedProps.pinLabels)
? this._parsedProps.pinLabels.length
: this._parsedProps.pinLabels
? Object.keys(this._parsedProps.pinLabels).length
: this.getPortsFromFootprint().length)
if (pinCount == null && !this._parsedProps.footprint) {
pinCount = 2
}
const direction = this._parsedProps.schDirection ?? "right"
return {
leftSize: direction === "left" ? pinCount : 0,
rightSize: direction === "right" ? pinCount : 0,
}
}
doInitialSourceRender(): void {
const { db } = this.root!
const { _parsedProps: props } = this
const { pcbX, pcbY } = this.getResolvedPcbPositionProp()
const source_component = db.source_component.insert({
ftype: "simple_chip", // TODO unknown or jumper
name: this.name,
manufacturer_part_number: props.manufacturerPartNumber,
supplier_part_numbers: props.supplierPartNumbers,
are_pins_interchangeable: true,
display_name: props.displayName,
})
this.source_component_id = source_component.source_component_id!
}
doInitialPcbComponentRender() {
if (this.root?.pcbDisabled) return
const { db } = this.root!
const { _parsedProps: props } = this
const { pcbX, pcbY } = this.getResolvedPcbPositionProp()
const globalTransformRotation = this.getGlobalTransformRotation()
const pcb_component = db.pcb_component.insert({
center: { x: pcbX, y: pcbY },
width: 2, // Default width, adjust as needed
height: 3, // Default height, adjust as needed
layer: props.layer ?? "top",
rotation: props.pcbRotation ?? globalTransformRotation,
insertion_direction: this._getPcbComponentInsertionDirection(
props.layer ?? "top",
globalTransformRotation,
),
source_component_id: this.source_component_id!,
subcircuit_id: this.getSubcircuit().subcircuit_id ?? undefined,
do_not_place: props.doNotPlace ?? false,
obstructs_within_bounds: props.obstructsWithinBounds ?? true,
metadata: props.kicadFootprintMetadata
? { kicad_footprint: props.kicadFootprintMetadata }
: undefined,
})
this.pcb_component_id = pcb_component.pcb_component_id
}
doInitialPcbTraceRender() {
const { db } = this.root!
const pcb_ports = db.pcb_port.list({
pcb_component_id: this.pcb_component_id,
})
const pinLabelToPortId: Record<string, string> = {}
// Map pin labels ("1", "2", etc.) to pcb_port_id
for (let i = 0; i < pcb_ports.length; i++) {
const port = pcb_ports[i]
const sourcePort = db.source_port.get(port.source_port_id)
let pinLabel = ""
if (typeof sourcePort?.pin_number === "number") {
pinLabel = sourcePort.pin_number.toString()
} else if (Array.isArray(sourcePort?.port_hints)) {
let matchedHint = sourcePort.port_hints.find((h: string) =>
/^(pin)?\d+$/.test(h),
)
if (matchedHint) {
if (/^pin\d+$/.test(matchedHint)) {
pinLabel = matchedHint.replace(/^pin/, "")
} else {
pinLabel = matchedHint
}
}
}
pinLabelToPortId[pinLabel] = port.pcb_port_id
}
const traces = db.pcb_trace.list({
pcb_component_id: this.pcb_component_id,
})
const updatePortId = (portId: string | undefined) => {
if (portId && typeof portId === "string" && portId.startsWith("{PIN")) {
const pin = portId.replace("{PIN", "").replace("}", "")
return pinLabelToPortId[pin] || portId
}
return portId
}
for (const trace of traces) {
if (!trace.route) continue
for (const segment of trace.route) {
if (segment.route_type !== "wire") continue
segment.start_pcb_port_id = updatePortId(segment.start_pcb_port_id)
segment.end_pcb_port_id = updatePortId(segment.end_pcb_port_id)
}
}
}
}