Skip to content

Commit

Permalink
Merge branch 'main' into feat/pcb_keepout
Browse files Browse the repository at this point in the history
  • Loading branch information
imrishabh18 authored Sep 6, 2024
2 parents 2d00b1e + 20f8103 commit bbb90a0
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 3 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,5 @@ dist

*.old.ts
context

test.json
52 changes: 52 additions & 0 deletions docs/CREATING_NEW_COMPONENTS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Creating New Components

If you got an error like `Unsupported component type (not registered in @tscircuit/core catalogue)`, you'll probably need to create a new component.

## Steps to creating a new component

- Determine if the component is a normal component or a primitive component
- If it's a normal component, create a new file in `lib/components/normal-components`
- If it's a primitive component, create a new file in `lib/components/primitive-components`
- Add the component to the catalogue by exporting it from the `lib/components/index.ts` file
- Create a test for the component

## Getting Props for a Component

Component props are defined in `@tscircuit/props` like so:

```ts
import { silkscreenPathProps, type SilkscreenPathProps } from "@tscircuit/props"
```

Most components are defined the zod definition for their props.

## Implementing Render Phases

For a new component, you'll want to determine what render phases need to be implemented.

For something like a PCB primitive, you might only need to implement a phase like `doInitialPcbPrimitiveRender`. Look for a similar component to copy/understand what needs to be implemented. For example

## Writing a Test

Every new component should have a test written for it. This test should be in `tests/components/` and should be named `<component-name>.test.tsx`.

Usually a test for a new component has a very simple test definition like this:

```tsx
import { test, expect } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("<mycomponent />", () => {
const { circuit } = getTestFixture()

circuit.add(
<board width="10mm" height="10mm">
<mycomponent />
</board>
)

circuit.render()

expect(circuit).toMatchPcbSnapshot(import.meta.path)
})
```
1 change: 1 addition & 0 deletions lib/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export { SilkscreenPath } from "./primitive-components/SilkscreenPath"
export { PlatedHole } from "./primitive-components/PlatedHole"
export { Constraint } from "./primitive-components/Constraint"
export { Hole } from "./primitive-components/Hole"
export { SilkscreenText } from "./primitive-components/SilkscreenText"
export { Keepout } from "./primitive-components/Keepout"
25 changes: 25 additions & 0 deletions lib/components/primitive-components/SilkscreenText.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { PrimitiveComponent } from "../base-components/PrimitiveComponent"
import { silkscreenTextProps } from "@tscircuit/props"

export class SilkscreenText extends PrimitiveComponent<
typeof silkscreenTextProps
> {
doInitialPcbPrimitiveRender(): void {
const { db } = this.root!
const { _parsedProps: props } = this
const container = this.getPrimitiveContainer()!

db.pcb_silkscreen_text.insert({
anchor_alignment: props.anchorAlignment,
anchor_position: {
x: props.pcbX ?? 0,
y: props.pcbY ?? 0,
},
font: props.font ?? "tscircuit2024",
font_size: props.fontSize ?? 1,
layer: "top",
text: props.text ?? "",
pcb_component_id: container.pcb_component_id!,
})
}
}
2 changes: 1 addition & 1 deletion lib/fiber/create-instance-from-react-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const hostConfig: HostConfig<
)
}
throw new Error(
`Unsupported component type (not registered in @tscircuit/core catalogue): ${type}`,
`Unsupported component type (not registered in @tscircuit/core catalogue): "${type}" See CREATING_NEW_COMPONENTS.md`,
)
}

Expand Down
6 changes: 5 additions & 1 deletion lib/register-catalogue.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import * as Components from "./components"
import { extendCatalogue } from "./fiber/catalogue"

// Register all components, generally you don't need to manually
// register a component, as long as it's exported from lib/components
// it'll automatically be registered!
extendCatalogue(Components)

// Aliases
// Aliases (only when class name is different than the name of the component)
extendCatalogue({
Bug: Components.Chip,
})
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@tscircuit/core",
"module": "index.ts",
"type": "module",
"version": "0.0.37",
"version": "0.0.39",
"types": "dist/index.d.ts",
"main": "dist/index.js",
"files": [
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 32 additions & 0 deletions tests/components/primitive-components/silkscreen-text.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { test, expect } from "bun:test"
import { getTestFixture } from "tests/fixtures/get-test-fixture"

test("SilkscreenText rendering", () => {
const { project } = getTestFixture()

project.add(
<board width="10mm" height="10mm">
<silkscreentext
text="Test Text"
pcbX={2}
pcbY={3}
fontSize={1.5}
anchorAlignment="center"
/>
</board>,
)

project.render()

const silkscreenTexts = project.db.pcb_silkscreen_text.list()

expect(silkscreenTexts.length).toBe(1)
expect(silkscreenTexts[0].text).toBe("Test Text")
expect(silkscreenTexts[0].anchor_position.x).toBe(2)
expect(silkscreenTexts[0].anchor_position.y).toBe(3)
expect(silkscreenTexts[0].font_size).toBe(1.5)
expect(silkscreenTexts[0].anchor_alignment).toBe("center")

Bun.write("test.json", JSON.stringify(silkscreenTexts, null, 2))
expect(project).toMatchPcbSnapshot(import.meta.path)
})

0 comments on commit bbb90a0

Please sign in to comment.