diff --git a/assets/simple-route-json.json b/assets/simple-route-json.json new file mode 100644 index 0000000..71eaace --- /dev/null +++ b/assets/simple-route-json.json @@ -0,0 +1,51 @@ +{ + "layerCount": 2, + "minTraceWidth": 0.5, + "obstacles": [ + { + "type": "rect", + "layers": ["top", "bottom"], + "center": { "x": 50, "y": 50 }, + "width": 10, + "height": 20, + "connectedTo": ["net1", "net2"] + } + ], + "connections": [ + { + "name": "connection1", + "pointsToConnect": [ + { "x": 0, "y": 0, "layer": "top" }, + { "x": 10, "y": 10, "layer": "top" } + ] + } + ], + "bounds": { + "minX": 0, + "maxX": 100, + "minY": 0, + "maxY": 100 + }, + "traces": [ + { + "type": "pcb_trace", + "pcb_trace_id": "trace1", + "route": [ + { + "route_type": "wire", + "x": 0, + "y": 0, + "width": 0.5, + "layer": "top" + }, + { + "route_type": "via", + "x": 10, + "y": 10, + "to_layer": "bottom", + "from_layer": "top" + } + ] + } + ] + } \ No newline at end of file diff --git a/package.json b/package.json index 9b2a569..05929d5 100644 --- a/package.json +++ b/package.json @@ -23,6 +23,8 @@ "typescript": "^5.0.0" }, "dependencies": { + "@tscircuit/core": "^0.0.331", + "circuit-json": "^0.0.144", "zustand": "^5.0.3" } } diff --git a/src/App.tsx b/src/App.tsx index 3e637ea..3f526b3 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -1,12 +1,35 @@ import { useCallback } from "react" import { useStore } from "./store" import { CircuitJsonPreview } from "@tscircuit/runframe" +import type { AnyCircuitElement } from "circuit-json" +import type { SimpleRouteJson } from "@tscircuit/core" export const App = () => { const circuitJson = useStore((s) => s.circuitJson) const setCircuitJson = useStore((s) => s.setCircuitJson) const reset = useStore((s) => s.reset) + const convertSimpleRouteJsonToCircuitJson = (simpleRouteJson: SimpleRouteJson): AnyCircuitElement[] => { + const circuitJson: AnyCircuitElement[] = [] + + for (const connection of simpleRouteJson.connections) { + const trace: AnyCircuitElement = { + type: "pcb_trace", + pcb_trace_id: connection.name, + route: connection.pointsToConnect.map(point => ({ + route_type: "wire", + x: point.x, + y: point.y, + layer: point.layer as "top" | "bottom" | "inner1" | "inner2" | "inner3" | "inner4" | "inner5" | "inner6", + width: simpleRouteJson.minTraceWidth, + })), + } + circuitJson.push(trace) + } + + return circuitJson + } + const handleDrop = useCallback( (e: React.DragEvent) => { e.preventDefault() @@ -16,7 +39,12 @@ export const App = () => { reader.onload = (e) => { try { const json = JSON.parse(e.target?.result as string) - setCircuitJson(json) + if (json.connections && json.minTraceWidth !== undefined) { + const circuitJson = convertSimpleRouteJsonToCircuitJson(json) + setCircuitJson(circuitJson) + } else { + setCircuitJson(json) + } } catch (err) { console.error("Failed to parse JSON:", err) } @@ -35,7 +63,12 @@ export const App = () => { reader.onload = (e) => { try { const json = JSON.parse(e.target?.result as string) - setCircuitJson(json) + if (json.connections && json.minTraceWidth !== undefined) { + const circuitJson = convertSimpleRouteJsonToCircuitJson(json) + setCircuitJson(circuitJson) + } else { + setCircuitJson(json) + } } catch (err) { console.error("Failed to parse JSON:", err) }