Skip to content

Commit

Permalink
Merge pull request #103 from tscircuit/cadmodel
Browse files Browse the repository at this point in the history
initial implementation of cad model render
  • Loading branch information
seveibar authored Sep 21, 2024
2 parents d038c19 + ff51150 commit 11847f3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
Binary file modified bun.lockb
Binary file not shown.
30 changes: 30 additions & 0 deletions lib/components/base-components/NormalComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ 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"

export type PortMap<T extends string> = {
[K in T]: Port
Expand Down Expand Up @@ -416,4 +417,33 @@ export class NormalComponent<
this.add(newPort)
}
}

doInitialCadModelRender(): void {
const { db } = this.root!
const { _parsedProps: props } = this

if (props.cadModel) {
// Use post-layout bounds
const bounds = this._getPcbCircuitJsonBounds()

const cadModel: CadModelProp = props.cadModel

if (typeof cadModel === "string") {
throw new Error("String cadModel not yet implemented")
}

const cad_model = db.cad_component.insert({
// TODO z maybe depends on layer
position: { x: bounds.center.x, y: bounds.center.y, z: 0 },
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,

// TODO
// footprinter_string:
})
}
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@lume/kiwi": "^0.4.3",
"@tscircuit/infgrid-ijump-astar": "^0.0.21",
"@tscircuit/math-utils": "^0.0.4",
"@tscircuit/props": "^0.0.63",
"@tscircuit/props": "^0.0.65",
"@tscircuit/soup": "^0.0.73",
"@tscircuit/soup-util": "^0.0.28",
"circuit-json": "^0.0.77",
Expand Down
22 changes: 22 additions & 0 deletions tests/components/normal-components/chip.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,25 @@ it("should create a Chip component with correct properties", async () => {
convertCircuitJsonToSchematicSvg(circuit.getCircuitJson()),
).toMatchSvgSnapshot(import.meta.path)
})

it("should create a Chip component with cadModel prop", async () => {
const { circuit } = getTestFixture()

circuit.add(
<board width="10mm" height="10mm">
<chip
name="U1"
footprint="soic8"
cadModel={{
stlUrl: "https://example.com/chip.stl",
}}
/>
</board>,
)

circuit.render()

const cadComponents = circuit.db.cad_component.list()
expect(cadComponents).toHaveLength(1)
expect(cadComponents[0].model_stl_url).toBe("https://example.com/chip.stl")
})

0 comments on commit 11847f3

Please sign in to comment.