Skip to content

Commit

Permalink
Merge pull request #121 from tscircuit/cad-component-position
Browse files Browse the repository at this point in the history
Fix Cad Components Rendering at wrong position in 3d
  • Loading branch information
seveibar committed Sep 25, 2024
2 parents 23b5f8e + 13f681f commit a92af76
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 24 deletions.
24 changes: 24 additions & 0 deletions lib/components/base-components/NormalComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,30 @@ export class NormalComponent<
}
}

_getPcbCircuitJsonBounds(): {
center: { x: number; y: number }
bounds: { left: number; top: number; right: number; bottom: number }
width: number
height: number
} {
const { db } = this.root!
if (!this.pcb_component_id) return super._getPcbCircuitJsonBounds()

const pcb_component = db.pcb_component.get(this.pcb_component_id)!

return {
center: { x: pcb_component.center.x, y: pcb_component.center.y },
bounds: {
left: pcb_component.center.x - pcb_component.width / 2,
top: pcb_component.center.y - pcb_component.height / 2,
right: pcb_component.center.x + pcb_component.width / 2,
bottom: pcb_component.center.y + pcb_component.height / 2,
},
width: pcb_component.width,
height: pcb_component.height,
}
}

doInitialCadModelRender(): void {
const { db } = this.root!
const { boardThickness = 0 } = this.root?._getBoard() ?? {}
Expand Down
24 changes: 0 additions & 24 deletions lib/components/normal-components/Chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,4 @@ export class Chip<PinLabels extends string = never> extends NormalComponent<

this.pcb_component_id = pcb_component.pcb_component_id
}

_getPcbCircuitJsonBounds(): {
center: { x: number; y: number }
bounds: { left: number; top: number; right: number; bottom: number }
width: number
height: number
} {
const { db } = this.root!
if (!this.pcb_component_id) return super._getPcbCircuitJsonBounds()

const pcb_component = db.pcb_component.get(this.pcb_component_id)!

return {
center: { x: pcb_component.center.x, y: pcb_component.center.y },
bounds: {
left: pcb_component.center.x - pcb_component.width / 2,
top: pcb_component.center.y - pcb_component.height / 2,
right: pcb_component.center.x + pcb_component.width / 2,
bottom: pcb_component.center.y + pcb_component.height / 2,
},
width: pcb_component.width,
height: pcb_component.height,
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { test, expect } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("resistor CAD component position", () => {
const { project } = getTestFixture()

project.add(
<board width="10mm" height="10mm">
<resistor
resistance="1k"
cadModel={{
objUrl:
"https://modelcdn.tscircuit.com/easyeda_models/download?pn=C2889342",
}}
footprint="0402"
name="R1"
pcbX={3}
/>
</board>,
)

project.render()

const cadComponent = project.db.cad_component.list()[0]

expect(cadComponent).toBeDefined()
expect(cadComponent.position.x).toBeCloseTo(3, 1)
expect(cadComponent.position.y).toBeCloseTo(0, 1)
expect(cadComponent.position.z).toBeCloseTo(0.7, 1)
})

0 comments on commit a92af76

Please sign in to comment.