-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/pcb_keepout
- Loading branch information
Showing
9 changed files
with
130 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -179,3 +179,5 @@ dist | |
|
||
*.old.ts | ||
context | ||
|
||
test.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
tests/components/primitive-components/__snapshots__/silkscreen-text-pcb.snap.svg
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
32
tests/components/primitive-components/silkscreen-text.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |