Skip to content

Commit 9b5ed6e

Browse files
Merge pull request #12 from MustafaMulla29/feat/support-simple-route-json
Added support for uploading SimpleRouteJson
2 parents 4bdc1b9 + a1f2798 commit 9b5ed6e

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

assets/simple-route-json.json

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
{
2+
"layerCount": 2,
3+
"minTraceWidth": 0.5,
4+
"obstacles": [
5+
{
6+
"type": "rect",
7+
"layers": ["top", "bottom"],
8+
"center": { "x": 50, "y": 50 },
9+
"width": 10,
10+
"height": 20,
11+
"connectedTo": ["net1", "net2"]
12+
}
13+
],
14+
"connections": [
15+
{
16+
"name": "connection1",
17+
"pointsToConnect": [
18+
{ "x": 0, "y": 0, "layer": "top" },
19+
{ "x": 10, "y": 10, "layer": "top" }
20+
]
21+
}
22+
],
23+
"bounds": {
24+
"minX": 0,
25+
"maxX": 100,
26+
"minY": 0,
27+
"maxY": 100
28+
},
29+
"traces": [
30+
{
31+
"type": "pcb_trace",
32+
"pcb_trace_id": "trace1",
33+
"route": [
34+
{
35+
"route_type": "wire",
36+
"x": 0,
37+
"y": 0,
38+
"width": 0.5,
39+
"layer": "top"
40+
},
41+
{
42+
"route_type": "via",
43+
"x": 10,
44+
"y": 10,
45+
"to_layer": "bottom",
46+
"from_layer": "top"
47+
}
48+
]
49+
}
50+
]
51+
}

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
"typescript": "^5.0.0"
2424
},
2525
"dependencies": {
26+
"@tscircuit/core": "^0.0.331",
27+
"circuit-json": "^0.0.144",
2628
"zustand": "^5.0.3"
2729
}
2830
}

src/App.tsx

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
import { useCallback } from "react"
22
import { useStore } from "./store"
33
import { CircuitJsonPreview } from "@tscircuit/runframe"
4+
import type { AnyCircuitElement } from "circuit-json"
5+
import type { SimpleRouteJson } from "@tscircuit/core"
46

57
export const App = () => {
68
const circuitJson = useStore((s) => s.circuitJson)
79
const setCircuitJson = useStore((s) => s.setCircuitJson)
810
const reset = useStore((s) => s.reset)
911

12+
const convertSimpleRouteJsonToCircuitJson = (simpleRouteJson: SimpleRouteJson): AnyCircuitElement[] => {
13+
const circuitJson: AnyCircuitElement[] = []
14+
15+
for (const connection of simpleRouteJson.connections) {
16+
const trace: AnyCircuitElement = {
17+
type: "pcb_trace",
18+
pcb_trace_id: connection.name,
19+
route: connection.pointsToConnect.map(point => ({
20+
route_type: "wire",
21+
x: point.x,
22+
y: point.y,
23+
layer: point.layer as "top" | "bottom" | "inner1" | "inner2" | "inner3" | "inner4" | "inner5" | "inner6",
24+
width: simpleRouteJson.minTraceWidth,
25+
})),
26+
}
27+
circuitJson.push(trace)
28+
}
29+
30+
return circuitJson
31+
}
32+
1033
const handleDrop = useCallback(
1134
(e: React.DragEvent) => {
1235
e.preventDefault()
@@ -16,7 +39,12 @@ export const App = () => {
1639
reader.onload = (e) => {
1740
try {
1841
const json = JSON.parse(e.target?.result as string)
19-
setCircuitJson(json)
42+
if (json.connections && json.minTraceWidth !== undefined) {
43+
const circuitJson = convertSimpleRouteJsonToCircuitJson(json)
44+
setCircuitJson(circuitJson)
45+
} else {
46+
setCircuitJson(json)
47+
}
2048
} catch (err) {
2149
console.error("Failed to parse JSON:", err)
2250
}
@@ -35,7 +63,12 @@ export const App = () => {
3563
reader.onload = (e) => {
3664
try {
3765
const json = JSON.parse(e.target?.result as string)
38-
setCircuitJson(json)
66+
if (json.connections && json.minTraceWidth !== undefined) {
67+
const circuitJson = convertSimpleRouteJsonToCircuitJson(json)
68+
setCircuitJson(circuitJson)
69+
} else {
70+
setCircuitJson(json)
71+
}
3972
} catch (err) {
4073
console.error("Failed to parse JSON:", err)
4174
}

0 commit comments

Comments
 (0)