Skip to content

Commit

Permalink
Merge pull request #105 from tscircuit/boardthick
Browse files Browse the repository at this point in the history
offset the 3d model based on board thickness
  • Loading branch information
seveibar authored Sep 22, 2024
2 parents 7ab0310 + 3b942a5 commit 1c8b9a2
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 10 deletions.
11 changes: 11 additions & 0 deletions lib/Project.ts → lib/Circuit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,17 @@ export class Circuit {
this.children.push(component)
}

/**
* Get the main board for this Circuit.
*/
_getBoard(): PrimitiveComponent & { boardThickness: number } {
return this.children.find(
(c) => c.componentName === "Board",
) as PrimitiveComponent & {
boardThickness: number
}
}

_guessRootComponent() {
if (this.firstChild) return
if (this.children.length === 1) {
Expand Down
16 changes: 13 additions & 3 deletions lib/components/base-components/NormalComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ export class NormalComponent<

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

if (props.cadModel) {
Expand All @@ -434,15 +435,24 @@ export class NormalComponent<

const cad_model = db.cad_component.insert({
// TODO z maybe depends on layer
position: { x: bounds.center.x, y: bounds.center.y, z: 0 },
position: {
x: bounds.center.x,
y: bounds.center.y,
z:
this.props.layer === "bottom"
? -boardThickness / 2
: boardThickness / 2,
},
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:
footprinter_string:
typeof this.props.footprint === "string" && !cadModel
? this.props.footprint
: undefined,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/components/base-components/PrimitiveComponent.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AnySoupElement, AnySourceComponent } from "@tscircuit/soup"
import type { Circuit } from "../../Project"
import type { Circuit } from "../../Circuit"
import type { ZodType } from "zod"
import { z } from "zod"
import { symbols, type SchSymbol, type BaseSymbolName } from "schematic-symbols"
Expand Down
5 changes: 5 additions & 0 deletions lib/components/normal-components/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export class Board extends Group<typeof boardProps> {
}
}

get boardThickness() {
const { _parsedProps: props } = this
return 1.4 // TODO use prop
}

doInitialPcbComponentRender(): void {
const { db } = this.root!
const { _parsedProps: props } = this
Expand Down
2 changes: 1 addition & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export * from "./components"
export * from "./Project"
export * from "./Circuit"

import "./register-catalogue"
import "./fiber/intrinsic-jsx"
2 changes: 1 addition & 1 deletion tests/components/base-components/select-methods.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { it, expect } from "bun:test"
import { Circuit } from "lib/Project"
import { Circuit } from "lib/Circuit"
import "lib/register-catalogue"

it("should correctly use selectAll and selectOne methods", () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/components/normal-components/chip-aliases.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { it, expect } from "bun:test"
import { Circuit } from "lib/Project"
import { Circuit } from "lib/Circuit"
import { Chip } from "lib/components/normal-components/Chip"
import "lib/register-catalogue"
import { getTestFixture } from "tests/fixtures/get-test-fixture"
Expand Down
2 changes: 1 addition & 1 deletion tests/components/normal-components/chip.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { it, expect } from "bun:test"
import { Circuit } from "lib/Project"
import { Circuit } from "lib/Circuit"
import { Chip } from "lib/components/normal-components/Chip"
import "lib/register-catalogue"
import { getTestFixture } from "tests/fixtures/get-test-fixture"
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/extend-expect-circuit-snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as fs from "node:fs"
import * as path from "node:path"
import looksSame from "looks-same"
import type { AnySoupElement } from "@tscircuit/soup"
import { Circuit } from "lib/Project"
import { Circuit } from "lib/Circuit"

async function saveSnapshotOfSoup({
soup,
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/get-test-fixture.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Circuit } from "lib/Project"
import { Circuit } from "lib/Circuit"
import { logSoup } from "@tscircuit/log-soup"
import "lib/register-catalogue"
import "./extend-expect-circuit-snapshot"
Expand Down

0 comments on commit 1c8b9a2

Please sign in to comment.