Skip to content

Commit

Permalink
Merge pull request #18 from tscircuit/jumper
Browse files Browse the repository at this point in the history
Fix Rotations for SMTPads and SilkscreenPaths, Fix Traces routing through other traces
  • Loading branch information
seveibar authored Sep 1, 2024
2 parents e693880 + 5adfe98 commit 955c921
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 16 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/components/base-components/NormalComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ export class NormalComponent<
width: 0,
height: 0,
layer: props.layer ?? "top",
rotation: props.rotation ?? 0,
rotation: props.pcbRotation ?? 0,
source_component_id: this.source_component_id!,
})
this.pcb_component_id = pcb_component.pcb_component_id
Expand Down
11 changes: 9 additions & 2 deletions lib/components/base-components/PrimitiveComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
applyToPoint,
compose,
identity,
rotate,
translate,
type Matrix,
} from "transformation-matrix"
Expand Down Expand Up @@ -97,8 +98,14 @@ export abstract class PrimitiveComponent<
* components
*/
computePcbPropsTransform(): Matrix {
// TODO rotations
return translate(this.props.pcbX ?? 0, this.props.pcbY ?? 0)
const { _parsedProps: props } = this

const matrix = compose(
translate(props.pcbX ?? 0, props.pcbY ?? 0),
rotate(((props.pcbRotation ?? 0) * Math.PI) / 180),
)

return matrix
}

/**
Expand Down
15 changes: 14 additions & 1 deletion lib/components/primitive-components/SilkscreenPath.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { silkscreenPathProps } from "@tscircuit/props"
import { PrimitiveComponent } from "../base-components/PrimitiveComponent"
import { applyToPoint } from "transformation-matrix"

export class SilkscreenPath extends PrimitiveComponent<
typeof silkscreenPathProps
Expand All @@ -23,10 +24,22 @@ export class SilkscreenPath extends PrimitiveComponent<
)
}

const transform = this.computePcbGlobalTransform()

const pcb_silkscreen_path = db.pcb_silkscreen_path.insert({
pcb_component_id: this.parent?.pcb_component_id!,
layer,
route: props.route,
route: props.route.map((p) => {
const transformedPosition = applyToPoint(transform, {
x: p.x,
y: p.y,
})
return {
...p,
x: transformedPosition.x,
y: transformedPosition.y,
}
}),
stroke_width: props.strokeWidth ?? 0.1,
})

Expand Down
10 changes: 7 additions & 3 deletions lib/components/primitive-components/SmtPad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { smtPadProps } from "@tscircuit/props"
import type { Port } from "./Port"
import type { RenderPhaseFn } from "../base-components/Renderable"
import type { PCBSMTPad } from "@tscircuit/soup"
import { decomposeTSR } from "transformation-matrix"

export class SmtPad extends PrimitiveComponent<typeof smtPadProps> {
pcb_smtpad_id: string | null = null
Expand Down Expand Up @@ -40,6 +41,9 @@ export class SmtPad extends PrimitiveComponent<typeof smtPadProps> {
const { _parsedProps: props } = this
if (!props.portHints) return
const position = this.getGlobalPcbPosition()
const decomposedMat = decomposeTSR(this.computePcbGlobalTransform())
const isRotated90 =
Math.abs(decomposedMat.rotation.angle * (180 / Math.PI) - 90) < 0.01
let pcb_smtpad: PCBSMTPad | null = null
if (props.shape === "circle") {
pcb_smtpad = db.pcb_smtpad.insert({
Expand All @@ -63,9 +67,9 @@ export class SmtPad extends PrimitiveComponent<typeof smtPadProps> {
layer: props.layer ?? "top",
shape: "rect",

// @ts-ignore: no idea why this is triggering
width: props.width,
height: props.height,
...(isRotated90
? { width: props.height, height: props.width }
: { width: props.width, height: props.height }),

port_hints: props.portHints.map((ph) => ph.toString()),

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"typescript": "^5.0.0"
},
"dependencies": {
"@tscircuit/infgrid-ijump-astar": "0.0.5",
"@tscircuit/infgrid-ijump-astar": "^0.0.6",
"@tscircuit/props": "^0.0.49",
"@tscircuit/soup": "^0.0.58",
"@tscircuit/soup-util": "0.0.18",
Expand Down
4 changes: 2 additions & 2 deletions tests/__snapshots__/example1.snap.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions tests/components/primitive-components/pcb-rotation.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { test, expect } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("pcb rotation", async () => {
const { project, logSoup } = getTestFixture()

project.add(
<board width={12} height={4}>
<resistor name="R1" pcbX={-5} footprint="soic8" resistance="1k" />
<resistor
name="R2"
pcbX={5}
footprint="soic8"
resistance="1k"
pcbRotation={90}
/>
</board>,
)

project.render()

expect(project.getCircuitJson()).toMatchPcbSnapshot(import.meta.dir)
})
16 changes: 12 additions & 4 deletions tests/examples/example1.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,42 @@ import { test, expect } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("example1", async () => {
const { project } = getTestFixture()
const { project, logSoup } = getTestFixture()
project.add(
<board width="10mm" height="10mm">
<chip
name="U1"
footprint="soic8"
pinLabels={{ pin1: "PWR", pin8: "GND" }}
pcbX={0}
pcbY={0}
/>
<resistor
name="R1"
footprint="0402"
resistance="10k"
pullupFor=".U1 port.2"
pullupTo="net.5v"
pcbX={4}
pcbY={0}
pcbRotation={90}
/>
<capacitor
name="C1"
capacitance="10uF"
footprint="0603"
decouplingFor=".U1 port.PWR"
decouplingTo="net.GND"
pcbX={-5}
pcbY={0}
pcbRotation={90}
/>
<jumper name="J1" footprint="pinrow4" />
<jumper pcbX={0} pcbY={-4} name="J1" footprint="pinrow4" />

{/* <trace from=".J1 pin.1" to=".U1 .PWR" />
<trace from=".J1 pin.1" to=".U1 .PWR" />
<trace from=".J1 pin.2" to=".U1 port.2" />
<trace from=".J1 pin.3" to=".U1 port.3" />
<trace from=".J1 pin.4" to=".U1 .GND" /> */}
<trace from=".J1 pin.4" to=".U1 .GND" />
</board>,
)

Expand Down
4 changes: 2 additions & 2 deletions tests/fixtures/get-test-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ export const getTestFixture = () => {

return {
project,
logSoup: async (lgn: string) => {
logSoup: async (nameOfTest: string) => {
if (process.env.CI) return
if (!project.rootComponent?.renderPhaseStates.SourceRender.initialized) {
project.render()
}
await logSoup(`core_${lgn}`, project.getSoup())
await logSoup(`core_${nameOfTest}`, project.getSoup())
},
}
}

0 comments on commit 955c921

Please sign in to comment.