-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathChip.ts
More file actions
237 lines (205 loc) · 7.91 KB
/
Chip.ts
File metadata and controls
237 lines (205 loc) · 7.91 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
import { chipProps } from "@tscircuit/props"
import { pcb_component_invalid_layer_error } from "circuit-json"
import { NormalComponent } from "lib/components/base-components/NormalComponent"
import { type SchematicBoxDimensions } from "lib/utils/schematic/getAllDimensionsForSchematicBox"
import { Trace } from "lib/components/primitive-components/Trace/Trace"
import { Port } from "lib/components/primitive-components/Port"
import type { z } from "zod"
export class Chip<PinLabels extends string = never> extends NormalComponent<
typeof chipProps,
PinLabels
> {
schematicBoxDimensions: SchematicBoxDimensions | null = null
constructor(props: z.input<typeof chipProps>) {
super(props)
}
get config() {
return {
componentName: "Chip",
zodProps: chipProps,
shouldRenderAsSchematicBox: true,
}
}
initPorts(opts = {}): void {
// First, call the parent initPorts to create ports normally
super.initPorts(opts)
// Then, ensure that any pins referenced in externallyConnectedPins have ports created
const { _parsedProps: props } = this
if (props.externallyConnectedPins) {
const requiredPorts = new Set<string>()
// Collect all pin identifiers that need ports
for (const [pin1, pin2] of props.externallyConnectedPins) {
requiredPorts.add(pin1)
requiredPorts.add(pin2)
}
// Create ports for any missing pin identifiers
for (const pinIdentifier of requiredPorts) {
// Check if a port already exists that matches this identifier
const existingPort = this.children.find(
(child) =>
child instanceof Port && child.isMatchingAnyOf([pinIdentifier]),
)
if (!existingPort) {
// Try to parse as a numeric pin (e.g., "pin1" -> pinNumber: 1)
const pinMatch = pinIdentifier.match(/^pin(\d+)$/)
if (pinMatch) {
const pinNumber = parseInt(pinMatch[1])
this.add(
new Port({
pinNumber,
aliases: [pinIdentifier],
}),
)
} else {
// It's an alias like "VCC", "VDD", etc.
this.add(
new Port({
name: pinIdentifier,
aliases: [pinIdentifier],
}),
)
}
}
}
}
}
doInitialSchematicComponentRender(): void {
const { _parsedProps: props } = this
// Early return if noSchematicRepresentation is true
if (props?.noSchematicRepresentation === true) return
// Continue with normal schematic rendering
super.doInitialSchematicComponentRender()
}
doInitialSourceRender(): void {
const { db } = this.root!
const { _parsedProps: props } = this
const source_component = db.source_component.insert({
ftype: "simple_chip",
name: this.name,
manufacturer_part_number: props.manufacturerPartNumber,
supplier_part_numbers: props.supplierPartNumbers,
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 footprint = props.footprint ?? this._getImpliedFootprintString()
const hasFootprintChild = this.children.some(
(c) => c.componentName === "Footprint",
)
if (!footprint && !hasFootprintChild && !this._hasStartedFootprintUrlLoad) {
const footprint_error = db.pcb_missing_footprint_error.insert({
message: `No footprint specified for component: ${this.getString()}`,
source_component_id: `${this.source_component_id}`,
error_type: "pcb_missing_footprint_error",
})
this.pcb_missing_footprint_error_id =
footprint_error.pcb_missing_footprint_error_id
}
// Validate that components can only be placed on top or bottom layers
const componentLayer = props.layer ?? "top"
if (componentLayer !== "top" && componentLayer !== "bottom") {
const subcircuit = this.getSubcircuit()
const error = pcb_component_invalid_layer_error.parse({
type: "pcb_component_invalid_layer_error",
message: `Component cannot be placed on layer '${componentLayer}'. Components can only be placed on 'top' or 'bottom' layers.`,
source_component_id: this.source_component_id!,
layer: componentLayer,
subcircuit_id: subcircuit.subcircuit_id ?? undefined,
})
db.pcb_component_invalid_layer_error.insert(error)
// Still create the component but with 'top' as fallback to avoid cascading errors
}
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:
componentLayer === "top" || componentLayer === "bottom"
? componentLayer
: "top",
rotation: props.pcbRotation ?? globalTransformRotation,
insertion_direction: this._getPcbComponentInsertionDirection(
componentLayer,
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,
is_allowed_to_be_off_board: props.allowOffBoard ?? false,
metadata: props.kicadFootprintMetadata
? { kicad_footprint: props.kicadFootprintMetadata }
: undefined,
})
this.pcb_component_id = pcb_component.pcb_component_id
}
doInitialCreateTracesFromProps(): void {
const { _parsedProps: props } = this
if (props.externallyConnectedPins) {
for (const [pin1, pin2] of props.externallyConnectedPins) {
this.add(
new Trace({
from: `${this.getSubcircuitSelector()} > port.${pin1}`,
to: `${this.getSubcircuitSelector()} > port.${pin2}`,
}),
)
}
}
this._createTracesFromConnectionsProp()
}
doInitialSimulationRender() {
const { db } = this.root!
const { pinAttributes } = this.props as any
if (!pinAttributes) return
let powerPort: Port | null = null
let groundPort: Port | null = null
let voltage: number | undefined
const ports = this.selectAll("port") as Port[]
for (const port of ports) {
for (const alias of port.getNameAndAliases()) {
if (pinAttributes[alias]) {
const attributes = pinAttributes[alias]
if (attributes.providesPower) {
powerPort = port
voltage = attributes.providesVoltage
}
if (attributes.providesGround) {
groundPort = port
}
}
}
}
if (!powerPort || !groundPort || voltage === undefined) {
return
}
const powerSourcePort = db.source_port.get(powerPort.source_port_id!)
if (!powerSourcePort?.subcircuit_connectivity_map_key) return
const groundSourcePort = db.source_port.get(groundPort.source_port_id!)
if (!groundSourcePort?.subcircuit_connectivity_map_key) return
const powerNet = db.source_net.getWhere({
subcircuit_connectivity_map_key:
powerSourcePort.subcircuit_connectivity_map_key,
})
const groundNet = db.source_net.getWhere({
subcircuit_connectivity_map_key:
groundSourcePort.subcircuit_connectivity_map_key,
})
if (!powerNet || !groundNet) {
return
}
;(db as any).simulation_voltage_source.insert({
type: "simulation_voltage_source",
positive_source_port_id: powerPort.source_port_id!,
positive_source_net_id: powerNet.source_net_id,
negative_source_port_id: groundPort.source_port_id!,
negative_source_net_id: groundNet.source_net_id,
voltage: voltage,
})
}
}