Skip to content

Commit a59ab88

Browse files
authored
feat: add support for schematic box, path, text, and circle elements (#24)
* feat: add support for schematic box, path, text, and circle elements * up * up * up * runTscircuitCode * add test * update
1 parent a26848b commit a59ab88

4 files changed

Lines changed: 203 additions & 60 deletions

lib/generate-symbol-tsx.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ export const generateSymbolTsx = (
66
): string | null => {
77
const schematicArcs = su(circuitJson).schematic_arc.list()
88
const schematicLines = su(circuitJson).schematic_line.list()
9-
9+
const schematicPaths = su(circuitJson).schematic_path.list()
10+
const schematicTexts = su(circuitJson).schematic_text.list()
11+
const schematicCircles = su(circuitJson).schematic_circle.list()
12+
const schematicBoxes = su(circuitJson).schematic_box.list()
1013
const elementStrings: string[] = []
1114

1215
for (const arc of schematicArcs) {
@@ -47,6 +50,57 @@ export const generateSymbolTsx = (
4750
)
4851
}
4952

53+
for (const box of schematicBoxes) {
54+
const x = box.x ?? 0
55+
const y = box.y ?? 0
56+
const width = box.width ?? 0
57+
const height = box.height ?? 0
58+
const isDashed = box.is_dashed ?? false
59+
60+
elementStrings.push(
61+
`<schematicbox center={{ x: ${x}, y: ${y} }} width={${width}} height={${height}} isDashed={${isDashed}}/>`,
62+
)
63+
}
64+
65+
for (const path of schematicPaths) {
66+
const points = path.points ?? []
67+
const fillColor = path.fill_color ?? "red"
68+
const isFilled = path.is_filled ?? false
69+
70+
elementStrings.push(
71+
`<schematicpath points={${JSON.stringify(points)}} strokeColor="${fillColor}" fillColor="${fillColor}" isFilled={${isFilled}}/>`,
72+
)
73+
}
74+
75+
for (const text of schematicTexts) {
76+
const x = text.position?.x ?? 0
77+
const y = text.position?.y ?? 0
78+
const rawText = String(text.text ?? "")
79+
const escapedText = rawText.replace(/"/g, '\\"')
80+
const anchorAlignment = text.anchor ?? "center"
81+
const fontSize = text.font_size ?? 0.1
82+
const color = text.color ?? "black"
83+
const rotation = text.rotation ?? 0
84+
85+
elementStrings.push(
86+
`<schematictext text="${escapedText}" x={${x}} y={${y}} anchorAlignment="${anchorAlignment}" fontSize={${fontSize}} color="${color}" rotation={${rotation}} />`,
87+
)
88+
}
89+
90+
for (const circle of schematicCircles) {
91+
const x = circle.center?.x ?? 0
92+
const y = circle.center?.y ?? 0
93+
const radius = circle.radius ?? 0
94+
const strokeWidth = circle.stroke_width ?? 0.05
95+
const color = circle.color ?? "black"
96+
const isFilled = circle.is_filled ?? false
97+
const isDashed = circle.is_dashed ?? false
98+
99+
elementStrings.push(
100+
`<schematiccircle center={{ x: ${x}, y: ${y} }} radius={${radius}} strokeWidth={${strokeWidth}} color="${color}" isFilled={${isFilled}} isDashed={${isDashed}} />`,
101+
)
102+
}
103+
50104
if (elementStrings.length === 0) {
51105
return null
52106
}
Lines changed: 25 additions & 0 deletions
Loading

tests/test7-support-schematic-arc.test.tsx

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import { test, expect } from "bun:test"
2+
import { convertCircuitJsonToTscircuit } from "lib/index"
3+
import type { AnyCircuitElement } from "circuit-json"
4+
import { runTscircuitCode } from "tscircuit"
5+
import { convertCircuitJsonToSchematicSvg } from "circuit-to-svg"
6+
7+
const circuitJson: AnyCircuitElement[] = [
8+
{
9+
type: "schematic_arc",
10+
center: { x: 0, y: 0 },
11+
schematic_arc_id: "schematic_arc_id_1",
12+
radius: 1,
13+
schematic_component_id: "schematic_component_id_1",
14+
start_angle_degrees: 0,
15+
end_angle_degrees: 180,
16+
stroke_width: 0.05,
17+
color: "black",
18+
is_dashed: false,
19+
direction: "counterclockwise",
20+
},
21+
{
22+
type: "schematic_line",
23+
x1: 2,
24+
schematic_line_id: "schematic_line_id_1",
25+
schematic_component_id: "schematic_component_id_1",
26+
y1: 0,
27+
x2: 4,
28+
y2: 0,
29+
stroke_width: 0.05,
30+
color: "black",
31+
is_dashed: false,
32+
},
33+
{
34+
type: "schematic_box",
35+
schematic_component_id: "schematic_component_id_1",
36+
x: 0,
37+
y: 3,
38+
width: 2,
39+
height: 2,
40+
is_dashed: true,
41+
},
42+
{
43+
type: "schematic_path",
44+
schematic_path_id: "schematic_path_id_1",
45+
points: [
46+
{ x: 3, y: 3 },
47+
{ x: 4, y: 4 },
48+
],
49+
fill_color: "blue",
50+
},
51+
{
52+
type: "schematic_text",
53+
schematic_text_id: "schematic_text_id_1",
54+
text: "U1",
55+
position: { x: 0, y: -3 },
56+
anchor: "center",
57+
font_size: 0.2,
58+
color: "red",
59+
rotation: 45,
60+
},
61+
{
62+
type: "schematic_circle",
63+
center: { x: 3, y: -3 },
64+
schematic_circle_id: "schematic_circle_id_1",
65+
radius: 0.5,
66+
stroke_width: 0.05,
67+
color: "green",
68+
is_filled: true,
69+
is_dashed: true,
70+
},
71+
]
72+
73+
test("test7 comprehensive schematic symbol support", async () => {
74+
const tscircuit = convertCircuitJsonToTscircuit(circuitJson, {
75+
componentName: "U1",
76+
})
77+
78+
expect(tscircuit).toMatchInlineSnapshot(`
79+
"import { type ChipProps } from "tscircuit"
80+
export const U1 = (props: ChipProps) => (
81+
<chip
82+
symbol={<symbol>
83+
<schematicarc
84+
center={{ x: 0, y: 0 }}
85+
radius={1}
86+
startAngleDegrees={0}
87+
endAngleDegrees={180}
88+
strokeWidth={0.05}
89+
color="black"
90+
isDashed={false}
91+
direction="counterclockwise"
92+
/>
93+
<schematicline x1={2} y1={0} x2={4} y2={0} strokeWidth={0.05} color="black" isDashed={false}/>
94+
<schematicbox center={{ x: 0, y: 3 }} width={2} height={2} isDashed={true}/>
95+
<schematicpath points={[{"x":3,"y":3},{"x":4,"y":4}]} strokeColor="blue" fillColor="blue" isFilled={false}/>
96+
<schematictext text="U1" x={0} y={-3} anchorAlignment="center" fontSize={0.2} color="red" rotation={45} />
97+
<schematiccircle center={{ x: 3, y: -3 }} radius={0.5} strokeWidth={0.05} color="green" isFilled={true} isDashed={true} />
98+
</symbol>}
99+
{...props}
100+
/>
101+
)"
102+
`)
103+
const result = await runTscircuitCode(tscircuit)
104+
105+
expect(Array.isArray(result)).toBe(true)
106+
expect(result).not.toHaveLength(0)
107+
}, 15000)
108+
109+
test("test7 comprehensive schematic symbol support svg", async () => {
110+
const tscircuit = convertCircuitJsonToTscircuit(circuitJson, {
111+
componentName: "U1",
112+
})
113+
114+
const result = await runTscircuitCode(tscircuit)
115+
116+
const schematicSvg = convertCircuitJsonToSchematicSvg(
117+
result as AnyCircuitElement[],
118+
)
119+
await expect(schematicSvg).toMatchSvgSnapshot(
120+
import.meta.path,
121+
"comprehensive-schematic-symbol",
122+
)
123+
})

0 commit comments

Comments
 (0)