circuit-json-to-kicad 0.0.1 → 0.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1 +1,175 @@
1
1
  # circuit-json-to-kicad
2
+
3
+ Convert [Circuit JSON](https://github.com/tscircuit/circuit-json) files to KiCad schematic (`.kicad_sch`) and PCB (`.kicad_pcb`) files.
4
+
5
+ Circuit JSON is an open-source file format for describing electronic circuits. This library enables you to generate KiCad-compatible files from Circuit JSON, allowing you to use KiCad's powerful PCB design tools with circuits defined in code.
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install circuit-json-to-kicad
11
+ # or
12
+ bun add circuit-json-to-kicad
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ### Converting to KiCad Schematic
18
+
19
+ ```typescript
20
+ import { CircuitJsonToKicadSchConverter } from "circuit-json-to-kicad"
21
+
22
+ // Assuming you have circuit JSON from tscircuit or another source
23
+ const circuitJson = /* your circuit JSON */
24
+
25
+ const converter = new CircuitJsonToKicadSchConverter(circuitJson)
26
+ converter.runUntilFinished()
27
+
28
+ // Get the KiCad schematic file content
29
+ const kicadSchContent = converter.getOutputString()
30
+
31
+ // Write to file
32
+ Bun.write("output.kicad_sch", kicadSchContent)
33
+ ```
34
+
35
+ ### Converting to KiCad PCB
36
+
37
+ ```typescript
38
+ import { CircuitJsonToKicadPcbConverter } from "circuit-json-to-kicad"
39
+
40
+ const circuitJson = /* your circuit JSON */
41
+
42
+ const converter = new CircuitJsonToKicadPcbConverter(circuitJson)
43
+ converter.runUntilFinished()
44
+
45
+ // Get the KiCad PCB file content
46
+ const kicadPcbContent = converter.getOutputString()
47
+
48
+ // Write to file
49
+ Bun.write("output.kicad_pcb", kicadPcbContent)
50
+ ```
51
+
52
+ ### Complete Example with tscircuit
53
+
54
+ ```typescript
55
+ import { Circuit } from "tscircuit"
56
+ import {
57
+ CircuitJsonToKicadSchConverter,
58
+ CircuitJsonToKicadPcbConverter,
59
+ } from "circuit-json-to-kicad"
60
+
61
+ // Define your circuit
62
+ const circuit = new Circuit()
63
+ circuit.add(
64
+ <board width="10mm" height="10mm">
65
+ <resistor name="R1" resistance="1k" footprint="0402" />
66
+ <capacitor
67
+ name="C1"
68
+ capacitance="1uF"
69
+ footprint="0603"
70
+ connections={{ pin1: "R1.pin2" }}
71
+ />
72
+ </board>,
73
+ )
74
+
75
+ await circuit.renderUntilSettled()
76
+
77
+ // Get Circuit JSON
78
+ const circuitJson = circuit.getCircuitJson()
79
+
80
+ // Convert to KiCad schematic
81
+ const schConverter = new CircuitJsonToKicadSchConverter(circuitJson)
82
+ schConverter.runUntilFinished()
83
+ Bun.write("output.kicad_sch", schConverter.getOutputString())
84
+
85
+ // Convert to KiCad PCB
86
+ const pcbConverter = new CircuitJsonToKicadPcbConverter(circuitJson)
87
+ pcbConverter.runUntilFinished()
88
+ Bun.write("output.kicad_pcb", pcbConverter.getOutputString())
89
+ ```
90
+
91
+ ## Features
92
+
93
+ - **Schematic Conversion**: Convert Circuit JSON to KiCad schematic files (`.kicad_sch`)
94
+ - Library symbols
95
+ - Component placement
96
+ - Wire routing
97
+ - Net connections
98
+
99
+ - **PCB Conversion**: Convert Circuit JSON to KiCad PCB files (`.kicad_pcb`)
100
+ - Footprint placement
101
+ - Trace routing
102
+ - Vias
103
+ - Board outlines and graphics
104
+ - Net assignments
105
+
106
+ ## Development
107
+
108
+ This project uses [Bun](https://bun.sh) as its runtime and package manager.
109
+
110
+ ### Prerequisites
111
+
112
+ - Bun (latest version)
113
+ - KiCad (for testing generated files)
114
+
115
+ ### Setup
116
+
117
+ ```bash
118
+ # Install dependencies
119
+ bun install
120
+
121
+ # Download KiCad demo files (used for reference)
122
+ bun run download-demos
123
+
124
+ # Run tests
125
+ bun test
126
+ ```
127
+
128
+ ### Testing
129
+
130
+ Tests generate visual PNG snapshots for comparison:
131
+
132
+ ```bash
133
+ # Run a specific test
134
+ bun test tests/sch/basics/basics01.test.tsx
135
+
136
+ # Run all tests
137
+ bun test
138
+
139
+ # Update snapshots (if needed)
140
+ BUN_UPDATE_SNAPSHOTS=1 bun test
141
+ ```
142
+
143
+ Generated debug output is saved to the `debug-output/` directory.
144
+
145
+ ### Project Structure
146
+
147
+ ```
148
+ lib/
149
+ ├── index.ts # Main exports
150
+ ├── types.ts # Shared types
151
+ ├── schematic/
152
+ │ ├── CircuitJsonToKicadSchConverter.ts # Schematic converter
153
+ │ └── stages/ # Conversion pipeline stages
154
+ └── pcb/
155
+ ├── CircuitJsonToKicadPcbConverter.ts # PCB converter
156
+ └── stages/ # Conversion pipeline stages
157
+ ```
158
+
159
+ ## About Circuit JSON
160
+
161
+ Circuit JSON is an open-source file format for representing electronic circuits. Learn more:
162
+
163
+ - [Circuit JSON on npm](https://www.npmjs.com/package/circuit-json)
164
+ - [Circuit JSON README](https://github.com/tscircuit/circuit-json)
165
+ - [tscircuit](https://github.com/tscircuit/tscircuit) - Create circuits using React/TSX
166
+
167
+ ## License
168
+
169
+ MIT License - see [LICENSE](./LICENSE) for details.
170
+
171
+ Some interoperability test artifacts are generated from assets by KiCad. See [KiCad licenses](https://www.kicad.org/about/licenses/) for more information.
172
+
173
+ ## Contributing
174
+
175
+ Contributions are welcome! Please feel free to submit issues or pull requests.
@@ -0,0 +1,74 @@
1
+ import { CircuitJson } from 'circuit-json';
2
+ import { CircuitJsonUtilObjects } from '@tscircuit/circuit-json-util';
3
+ import { KicadSch, KicadPcb } from 'kicadts';
4
+ import { Matrix } from 'transformation-matrix';
5
+
6
+ type SchematicPortId = string;
7
+ type SchematicTraceId = string;
8
+ type PcbPortId = string;
9
+ interface ConverterContext {
10
+ db: CircuitJsonUtilObjects;
11
+ circuitJson: CircuitJson;
12
+ kicadSch?: KicadSch;
13
+ kicadPcb?: KicadPcb;
14
+ /** Circuit JSON to KiCad schematic transformation matrix */
15
+ c2kMatSch?: Matrix;
16
+ /** Circuit JSON to KiCad PCB transformation matrix */
17
+ c2kMatPcb?: Matrix;
18
+ pinPositions?: Map<SchematicPortId, {
19
+ x: number;
20
+ y: number;
21
+ }>;
22
+ wireConnections?: Map<SchematicTraceId, SchematicPortId[]>;
23
+ pcbPadPositions?: Map<PcbPortId, {
24
+ x: number;
25
+ y: number;
26
+ }>;
27
+ pcbNetMap?: Map<string, number>;
28
+ }
29
+ declare abstract class ConverterStage<Input, Output> {
30
+ MAX_ITERATIONS: number;
31
+ iteration: number;
32
+ finished: boolean;
33
+ input: Input;
34
+ ctx: ConverterContext;
35
+ constructor(input: Input, ctx: ConverterContext);
36
+ step(): void;
37
+ _step(): void;
38
+ runUntilFinished(): void;
39
+ getOutput(): Output;
40
+ }
41
+
42
+ declare class CircuitJsonToKicadSchConverter {
43
+ ctx: ConverterContext;
44
+ pipeline: ConverterStage<CircuitJson, KicadSch>[];
45
+ currentStageIndex: number;
46
+ finished: boolean;
47
+ get currentStage(): ConverterStage<CircuitJson, KicadSch> | undefined;
48
+ constructor(circuitJson: CircuitJson);
49
+ step(): void;
50
+ runUntilFinished(): void;
51
+ getOutput(): KicadSch;
52
+ /**
53
+ * Get the output as a string
54
+ */
55
+ getOutputString(): string;
56
+ }
57
+
58
+ declare class CircuitJsonToKicadPcbConverter {
59
+ ctx: ConverterContext;
60
+ pipeline: ConverterStage<CircuitJson, KicadPcb>[];
61
+ currentStageIndex: number;
62
+ finished: boolean;
63
+ get currentStage(): ConverterStage<CircuitJson, KicadPcb> | undefined;
64
+ constructor(circuitJson: CircuitJson);
65
+ step(): void;
66
+ runUntilFinished(): void;
67
+ getOutput(): KicadPcb;
68
+ /**
69
+ * Get the output as a string
70
+ */
71
+ getOutputString(): string;
72
+ }
73
+
74
+ export { CircuitJsonToKicadPcbConverter, CircuitJsonToKicadSchConverter };