Skip to content

Commit

Permalink
pinLabels support
Browse files Browse the repository at this point in the history
  • Loading branch information
seveibar committed Aug 26, 2024
1 parent 5310ed4 commit ee28f11
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 3 deletions.
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions lib/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ export { Net } from "./primitive-components/Net"
export { Trace } from "./primitive-components/Trace"
export { TraceHint } from "./primitive-components/TraceHint"
export { Group } from "./primitive-components/Group"
export { Chip } from "./normal-components/Chip"
76 changes: 75 additions & 1 deletion lib/components/normal-components/Chip.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,78 @@
import { NormalComponent } from "lib/components/base-components/NormalComponent"
import { chipProps } from "@tscircuit/props"
import { Port } from "../primitive-components/Port"
import type { BaseSymbolName } from "lib/utils/constants"

export class Chip extends NormalComponent<typeof chipProps> {}
export class Chip extends NormalComponent<typeof chipProps> {
get config() {
return {
zodProps: chipProps,
}
}

initPorts() {
super.initPorts()

const { _parsedProps: props } = this

if (props.pinLabels) {
for (const [pinNumber, label] of Object.entries(props.pinLabels)) {
console.log(this.children)
const port = this.selectOne(`port[pinNumber='${pinNumber}']`)
const ports = this.selectAll("port")
if (!port) {
throw new Error(
`Could not find port for pin number ${pinNumber} in chip ${this.getString()}`,
)
}
port.props.aliases.push(port.props.name)
port.props.name = label
}
}
}

doInitialSourceRender(): void {
const { db } = this.project!
const { _parsedProps: props } = this

const source_component = db.source_component.insert({
ftype: "simple_chip",
name: props.name,
manufacturer_part_number: props.manufacturerPartNumber,
supplier_part_numbers: props.supplierPartNumbers,
})

this.source_component_id = source_component.source_component_id!
}

doInitialSchematicComponentRender() {
const { db } = this.project!
const { _parsedProps: props } = this

const schematic_component = db.schematic_component.insert({
center: { x: props.schX ?? 0, y: props.schY ?? 0 },
rotation: props.schRotation ?? 0,
size: { width: 3, height: 4 }, // Default size, adjust as needed
source_component_id: this.source_component_id!,
symbol_name: "chip",
})

this.schematic_component_id = schematic_component.schematic_component_id
}

doInitialPcbComponentRender() {
const { db } = this.project!
const { _parsedProps: props } = this

const pcb_component = db.pcb_component.insert({
center: { x: props.pcbX ?? 0, y: props.pcbY ?? 0 },
width: 2, // Default width, adjust as needed
height: 3, // Default height, adjust as needed
layer: props.layer ?? "top",
rotation: props.rotation ?? 0,
source_component_id: this.source_component_id!,
})

this.pcb_component_id = pcb_component.pcb_component_id
}
}
4 changes: 4 additions & 0 deletions lib/components/primitive-components/Port.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ export class Port extends PrimitiveComponent<typeof portProps> {

getGlobalSchematicPosition(): { x: number; y: number } {
if (!this.schematicSymbolPortDef) {
return applyToPoint(this.parent!.computeSchematicGlobalTransform(), {
x: 0,
y: 0,
})
throw new Error(
`Could not find schematic symbol port for port ${this} so couldn't determine port position`,
)
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/selector-matching/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@ export function isMatchingSelector(
const match = condition.match(/\[(\w+)=['"]?(.+?)['"]?\]/)
if (!match) return true
const [, prop, value] = match
return component.props[prop] === value
return component.props[prop].toString() === value
})
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
},
"dependencies": {
"@tscircuit/infgrid-ijump-astar": "0.0.4",
"@tscircuit/props": "^0.0.37",
"@tscircuit/props": "^0.0.38",
"@tscircuit/soup": "^0.0.52",
"@tscircuit/soup-util": "0.0.18",
"footprinter": "^0.0.44",
Expand Down
50 changes: 50 additions & 0 deletions tests/components/normal-components/chip.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { it, expect } from "bun:test"
import { Project } from "lib/Project"
import { Chip } from "lib/components/normal-components/Chip"
import "lib/register-catalogue"

it("should create a Chip component with correct properties", () => {
const project = new Project()

project.add(
<board width="10mm" height="10mm">
<chip
name="U1"
footprint="soic8"
pinLabels={{
"1": "VCC",
}}
schPortArrangement={{
leftSize: 4,
rightSize: 4,
}}
/>
</board>,
)

project.render()

const chip = project.selectOne("chip") as Chip

expect(chip).not.toBeNull()
expect(chip.props.name).toBe("U1")

// Check if ports are created correctly
const ports = chip.children.filter((c) => c.componentName === "Port")
expect(ports).toHaveLength(8)

// Check specific ports
const vccPort = chip.selectOne("port[name='VCC']")
expect(vccPort).not.toBeNull()
expect(vccPort!.props.pinNumber).toBe(1)

const gndPort = chip.selectOne("port[name='GND']")
expect(gndPort).not.toBeNull()

Check failure on line 42 in tests/components/normal-components/chip.test.tsx

View workflow job for this annotation

GitHub Actions / test

error: expect(received).not.toBeNull()

Received: null at /home/runner/work/core/core/tests/components/normal-components/chip.test.tsx:42:23
expect(gndPort!.props.pinNumber).toBe(2)

// Test schematic rendering
expect(chip.schematic_component_id).not.toBeNull()

// Test PCB rendering
expect(chip.pcb_component_id).not.toBeNull()
})

0 comments on commit ee28f11

Please sign in to comment.