Skip to content

Commit

Permalink
Merge pull request #132 from tscircuit/cad-component
Browse files Browse the repository at this point in the history
create cad_component for any component with a footprint
  • Loading branch information
seveibar authored Oct 1, 2024
2 parents 3533a3f + f0bc99b commit 59af70e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 50 deletions.
115 changes: 65 additions & 50 deletions lib/components/base-components/NormalComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ import { createComponentsFromSoup } from "lib/utils/createComponentsFromSoup"
import { Net } from "../primitive-components/Net"
import { createNetsFromProps } from "lib/utils/components/createNetsFromProps"
import { getBoundsOfPcbComponents } from "lib/utils/get-bounds-of-pcb-components"
import type { CadModelProp } from "@tscircuit/props"
import type {
CadModelJscad,
CadModelObj,
CadModelProp,
CadModelStl,
} from "@tscircuit/props"
import { rotation } from "circuit-json"

const rotation3 = z.object({
Expand Down Expand Up @@ -442,59 +447,69 @@ export class NormalComponent<
doInitialCadModelRender(): void {
const { db } = this.root!
const { boardThickness = 0 } = this.root?._getBoard() ?? {}
const cadModel = this._parsedProps.cadModel as CadModelProp
const cadModel = this._parsedProps.cadModel as CadModelProp | undefined

if (cadModel) {
// Use post-layout bounds
const bounds = this._getPcbCircuitJsonBounds()
if (!this.pcb_component_id) return
if (!cadModel && !this.props.footprint) return

const pcb_component = db.pcb_component.get(this.pcb_component_id!)
// Use post-layout bounds
const bounds = this._getPcbCircuitJsonBounds()

if (typeof cadModel === "string") {
throw new Error("String cadModel not yet implemented")
}
const pcb_component = db.pcb_component.get(this.pcb_component_id!)

const rotationOffset = rotation3.parse({
x: 0,
y: 0,
z:
typeof cadModel.rotationOffset === "number"
? cadModel.rotationOffset
: 0,
...(typeof cadModel.rotationOffset === "object"
? (cadModel.rotationOffset ?? {})
: {}),
})

const cad_model = db.cad_component.insert({
// TODO z maybe depends on layer
position: {
x: bounds.center.x,
y: bounds.center.y,
z:
this.props.layer === "bottom"
? -boardThickness / 2
: boardThickness / 2,
},
rotation: {
x: rotationOffset.x,
y: (this.props.layer === "top" ? 0 : 180) + rotationOffset.y,
z:
(pcb_component?.rotation ?? 0) +
(this.props.layer === "bottom" ? 180 : 0) +
rotationOffset.z,
},
pcb_component_id: this.pcb_component_id!,
source_component_id: this.source_component_id!,
model_stl_url: "stlUrl" in cadModel ? cadModel.stlUrl : undefined,
model_obj_url: "objUrl" in cadModel ? cadModel.objUrl : undefined,
model_jscad: "jscad" in cadModel ? cadModel.jscad : undefined,

footprinter_string:
typeof this.props.footprint === "string" && !cadModel
? this.props.footprint
: undefined,
})
if (typeof cadModel === "string") {
throw new Error("String cadModel not yet implemented")
}

const rotationOffset = rotation3.parse({
x: 0,
y: 0,
z:
typeof cadModel?.rotationOffset === "number"
? cadModel.rotationOffset
: 0,
...(typeof cadModel?.rotationOffset === "object"
? (cadModel.rotationOffset ?? {})
: {}),
})

const cad_model = db.cad_component.insert({
// TODO z maybe depends on layer
position: {
x: bounds.center.x,
y: bounds.center.y,
z:
this.props.layer === "bottom"
? -boardThickness / 2
: boardThickness / 2,
},
rotation: {
x: rotationOffset.x,
y: (this.props.layer === "top" ? 0 : 180) + rotationOffset.y,
z:
(pcb_component?.rotation ?? 0) +
(this.props.layer === "bottom" ? 180 : 0) +
rotationOffset.z,
},
pcb_component_id: this.pcb_component_id!,
source_component_id: this.source_component_id!,
model_stl_url:
"stlUrl" in (cadModel ?? {})
? (cadModel as CadModelStl).stlUrl
: undefined,
model_obj_url:
"objUrl" in (cadModel ?? {})
? (cadModel as CadModelObj).objUrl
: undefined,
model_jscad:
"jscad" in (cadModel ?? {})
? (cadModel as CadModelJscad).jscad
: undefined,

footprinter_string:
typeof this.props.footprint === "string" && !cadModel
? this.props.footprint
: undefined,
})
}
}
1 change: 1 addition & 0 deletions tests/components/normal-components/chip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ it("should create a Chip component with cadModel prop", async () => {
circuit.render()

const cadComponents = circuit.db.cad_component.list()

expect(cadComponents).toHaveLength(1)
expect(cadComponents[0].position.x).toBeCloseTo(4)
expect(cadComponents[0].model_stl_url).toBe("https://example.com/chip.stl")
Expand Down
4 changes: 4 additions & 0 deletions tests/examples/example1.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ test("example1", async () => {
circuit.db.pcb_smtpad.list().map((smtpad) => smtpad.pcb_port_id),
).not.toContain(null)

// We should have a cad_component for each component
const cadComponents = circuit.db.cad_component.list()
expect(cadComponents).toHaveLength(4)

await expect(
circuit.getSvg({
view: "pcb",
Expand Down

0 comments on commit 59af70e

Please sign in to comment.