kicad-to-circuit-json 0.0.1
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/LICENSE +21 -0
- package/README.md +123 -0
- package/dist/index.d.ts +173 -0
- package/dist/index.js +1111 -0
- package/package.json +34 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 tscircuit Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# kicad-to-circuit-json
|
|
2
|
+
|
|
3
|
+
Convert KiCad schematic and PCB files to Circuit JSON format.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install kicad-to-circuit-json
|
|
9
|
+
# or
|
|
10
|
+
bun install kicad-to-circuit-json
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { KicadToCircuitJsonConverter } from "kicad-to-circuit-json"
|
|
17
|
+
import fs from "fs"
|
|
18
|
+
|
|
19
|
+
// Create a converter instance
|
|
20
|
+
const converter = new KicadToCircuitJsonConverter()
|
|
21
|
+
|
|
22
|
+
// Add KiCad files
|
|
23
|
+
const pcbContent = fs.readFileSync("path/to/file.kicad_pcb", "utf-8")
|
|
24
|
+
const schContent = fs.readFileSync("path/to/file.kicad_sch", "utf-8")
|
|
25
|
+
|
|
26
|
+
converter.addFile("example.kicad_pcb", pcbContent)
|
|
27
|
+
converter.addFile("example.kicad_sch", schContent)
|
|
28
|
+
|
|
29
|
+
// Run the conversion
|
|
30
|
+
converter.runUntilFinished()
|
|
31
|
+
|
|
32
|
+
// Get the Circuit JSON output
|
|
33
|
+
const circuitJson = converter.getOutput()
|
|
34
|
+
console.log(JSON.stringify(circuitJson, null, 2))
|
|
35
|
+
|
|
36
|
+
// Get diagnostics
|
|
37
|
+
console.log("Warnings:", converter.getWarnings())
|
|
38
|
+
console.log("Stats:", converter.getStats())
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Architecture
|
|
42
|
+
|
|
43
|
+
The converter uses a staged pipeline architecture that mirrors the circuit-json-to-kicad converter:
|
|
44
|
+
|
|
45
|
+
### Schematic Pipeline
|
|
46
|
+
|
|
47
|
+
1. **InitializeSchematicContextStage** - Sets up coordinate transformations (KiCad → Circuit JSON)
|
|
48
|
+
2. **CollectLibrarySymbolsStage** - Extracts symbols and creates `source_component` + `schematic_component` entries
|
|
49
|
+
3. **CollectSchematicTracesStage** - Converts wires and junctions to `schematic_trace` elements
|
|
50
|
+
|
|
51
|
+
### PCB Pipeline
|
|
52
|
+
|
|
53
|
+
1. **InitializePcbContextStage** - Sets up PCB coordinate transformations
|
|
54
|
+
2. **CollectNetsStage** - Builds net number to name mappings
|
|
55
|
+
3. **CollectFootprintsStage** - Converts footprints to `pcb_component` with pads/holes
|
|
56
|
+
4. **CollectTracesStage** - Converts segments to `pcb_trace` elements
|
|
57
|
+
5. **CollectViasStage** - Converts vias to `pcb_via` elements
|
|
58
|
+
6. **CollectGraphicsStage** - Extracts board outline and silkscreen graphics
|
|
59
|
+
|
|
60
|
+
## Coordinate Transformations
|
|
61
|
+
|
|
62
|
+
The converter handles coordinate system differences between KiCad and Circuit JSON:
|
|
63
|
+
|
|
64
|
+
- **Schematic**: `scale(1/15, -1/15)` with translation (inverse of CJ→KiCad transform)
|
|
65
|
+
- **PCB**: `scale(1, -1)` with translation
|
|
66
|
+
|
|
67
|
+
## Supported Features
|
|
68
|
+
|
|
69
|
+
### Schematic
|
|
70
|
+
- ✅ Symbols/Components
|
|
71
|
+
- ✅ Symbol ports/pins
|
|
72
|
+
- ✅ Wires/traces
|
|
73
|
+
- ✅ Junctions
|
|
74
|
+
- ✅ Component properties (Reference, Value)
|
|
75
|
+
- ⚠️ Net labels (partial)
|
|
76
|
+
- ⚠️ Power symbols (partial)
|
|
77
|
+
|
|
78
|
+
### PCB
|
|
79
|
+
- ✅ Footprints/Components
|
|
80
|
+
- ✅ SMD pads
|
|
81
|
+
- ✅ Through-hole pads (plated holes)
|
|
82
|
+
- ✅ NPTH holes
|
|
83
|
+
- ✅ Traces/Segments
|
|
84
|
+
- ✅ Vias
|
|
85
|
+
- ✅ Board outline (Edge.Cuts)
|
|
86
|
+
- ✅ Silkscreen graphics
|
|
87
|
+
- ✅ Net mappings
|
|
88
|
+
|
|
89
|
+
## MVP Limitations
|
|
90
|
+
|
|
91
|
+
This is an MVP implementation. Some limitations include:
|
|
92
|
+
|
|
93
|
+
- Component sizes are estimated (not derived from actual symbol geometry)
|
|
94
|
+
- Port positions are simplified (not fully transformed relative to component)
|
|
95
|
+
- Trace routing is basic (segments grouped by net/layer)
|
|
96
|
+
- Some pad shapes may not map perfectly
|
|
97
|
+
- Power symbols and net labels need enhancement
|
|
98
|
+
- Multi-sheet schematics not fully tested
|
|
99
|
+
|
|
100
|
+
## Development
|
|
101
|
+
|
|
102
|
+
```bash
|
|
103
|
+
# Install dependencies
|
|
104
|
+
bun install
|
|
105
|
+
|
|
106
|
+
# Build
|
|
107
|
+
bun run build
|
|
108
|
+
|
|
109
|
+
# Type check
|
|
110
|
+
bunx tsc --noEmit
|
|
111
|
+
|
|
112
|
+
# Test
|
|
113
|
+
bun test
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
## Related Projects
|
|
117
|
+
|
|
118
|
+
- [circuit-json-to-kicad](https://github.com/tscircuit/circuit-json-to-kicad) - Convert Circuit JSON to KiCad (reverse direction)
|
|
119
|
+
- [circuit-to-svg](https://github.com/tscircuit/circuit-to-svg) - Render Circuit JSON as SVG
|
|
120
|
+
|
|
121
|
+
## License
|
|
122
|
+
|
|
123
|
+
MIT
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { CircuitJsonUtilObjects } from '@tscircuit/circuit-json-util';
|
|
2
|
+
import { KicadPcb, KicadSch } from 'kicadts';
|
|
3
|
+
import { Matrix } from 'transformation-matrix';
|
|
4
|
+
|
|
5
|
+
interface ConverterContext {
|
|
6
|
+
db: CircuitJsonUtilObjects;
|
|
7
|
+
kicadPcb?: KicadPcb;
|
|
8
|
+
kicadSch?: KicadSch;
|
|
9
|
+
k2cMatSch?: Matrix;
|
|
10
|
+
k2cMatPcb?: Matrix;
|
|
11
|
+
netNumToName?: Map<number, string>;
|
|
12
|
+
footprintUuidToComponentId?: Map<string, string>;
|
|
13
|
+
symbolUuidToComponentId?: Map<string, string>;
|
|
14
|
+
warnings?: string[];
|
|
15
|
+
stats?: {
|
|
16
|
+
components?: number;
|
|
17
|
+
pads?: number;
|
|
18
|
+
vias?: number;
|
|
19
|
+
traces?: number;
|
|
20
|
+
labels?: number;
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Base class for converter stages that process KiCad data into Circuit JSON.
|
|
25
|
+
* Each stage performs a specific transformation step and can run iteratively.
|
|
26
|
+
*/
|
|
27
|
+
declare abstract class ConverterStage {
|
|
28
|
+
protected ctx: ConverterContext;
|
|
29
|
+
protected MAX_ITERATIONS: number;
|
|
30
|
+
protected iterationCount: number;
|
|
31
|
+
finished: boolean;
|
|
32
|
+
constructor(ctx: ConverterContext);
|
|
33
|
+
/**
|
|
34
|
+
* Perform one step of the conversion process.
|
|
35
|
+
* Returns true if the stage has more work to do, false if finished.
|
|
36
|
+
*/
|
|
37
|
+
abstract step(): boolean;
|
|
38
|
+
/**
|
|
39
|
+
* Run this stage until completion or max iterations reached.
|
|
40
|
+
*/
|
|
41
|
+
runUntilFinished(): void;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
declare class KicadToCircuitJsonConverter {
|
|
45
|
+
fsMap: Record<string, string>;
|
|
46
|
+
ctx?: ConverterContext;
|
|
47
|
+
currentStageIndex: number;
|
|
48
|
+
pipeline?: ConverterStage[];
|
|
49
|
+
get currentStage(): ConverterStage | undefined;
|
|
50
|
+
addFile(filePath: string, content: string): void;
|
|
51
|
+
_findFileWithExtension(extension: string): string | null;
|
|
52
|
+
initializePipeline(): void;
|
|
53
|
+
step(): boolean;
|
|
54
|
+
runUntilFinished(): void;
|
|
55
|
+
getOutput(): any[];
|
|
56
|
+
getOutputString(): string;
|
|
57
|
+
getWarnings(): string[];
|
|
58
|
+
getStats(): {
|
|
59
|
+
components?: number;
|
|
60
|
+
pads?: number;
|
|
61
|
+
vias?: number;
|
|
62
|
+
traces?: number;
|
|
63
|
+
labels?: number;
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* InitializeSchematicContextStage sets up the coordinate transformation
|
|
69
|
+
* from KiCad schematic space to Circuit JSON space.
|
|
70
|
+
*
|
|
71
|
+
* KiCad→CJ schematic transform (inverse of CJ→KiCad):
|
|
72
|
+
* - CJ→KiCad used: translate(KICAD_CENTER) ∘ scale(15, -15) ∘ translate(-center)
|
|
73
|
+
* - KiCad→CJ uses: translate(center) ∘ scale(1/15, -1/15) ∘ translate(-KICAD_CENTER)
|
|
74
|
+
*/
|
|
75
|
+
declare class InitializeSchematicContextStage extends ConverterStage {
|
|
76
|
+
step(): boolean;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* CollectLibrarySymbolsStage extracts KiCad schematic symbols and creates:
|
|
81
|
+
* - source_component entries (with ftype inferred from library id)
|
|
82
|
+
* - schematic_component entries with positions
|
|
83
|
+
* - schematic_port entries for each pin
|
|
84
|
+
*/
|
|
85
|
+
declare class CollectLibrarySymbolsStage extends ConverterStage {
|
|
86
|
+
private processedSymbols;
|
|
87
|
+
step(): boolean;
|
|
88
|
+
private processSymbol;
|
|
89
|
+
private getProperty;
|
|
90
|
+
private inferFtype;
|
|
91
|
+
private getRotation;
|
|
92
|
+
private estimateSize;
|
|
93
|
+
private createPorts;
|
|
94
|
+
private inferPinDirection;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* CollectSchematicTracesStage converts KiCad schematic wires and junctions
|
|
99
|
+
* into Circuit JSON schematic_trace elements.
|
|
100
|
+
*/
|
|
101
|
+
declare class CollectSchematicTracesStage extends ConverterStage {
|
|
102
|
+
step(): boolean;
|
|
103
|
+
private processWire;
|
|
104
|
+
private processJunction;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* InitializePcbContextStage sets up the coordinate transformation
|
|
109
|
+
* from KiCad PCB space to Circuit JSON space.
|
|
110
|
+
*
|
|
111
|
+
* KiCad→CJ PCB transform (inverse of CJ→KiCad):
|
|
112
|
+
* - CJ→KiCad used: translate(100, 100) ∘ scale(1, -1)
|
|
113
|
+
* - KiCad→CJ uses: scale(1, -1) ∘ translate(-100, -100)
|
|
114
|
+
*/
|
|
115
|
+
declare class InitializePcbContextStage extends ConverterStage {
|
|
116
|
+
step(): boolean;
|
|
117
|
+
private calculateBoardCenter;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* CollectNetsStage builds a mapping from KiCad net numbers to meaningful net names.
|
|
122
|
+
* Prefers KiCad's actual net names, falls back to "Net-<n>" for unnamed nets.
|
|
123
|
+
*/
|
|
124
|
+
declare class CollectNetsStage extends ConverterStage {
|
|
125
|
+
step(): boolean;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* CollectFootprintsStage converts KiCad footprints into Circuit JSON pcb_components,
|
|
130
|
+
* along with their associated pads (SMT, plated holes, NPTH) and silkscreen text.
|
|
131
|
+
*/
|
|
132
|
+
declare class CollectFootprintsStage extends ConverterStage {
|
|
133
|
+
private processedFootprints;
|
|
134
|
+
step(): boolean;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* CollectTracesStage converts KiCad PCB segments (traces) into Circuit JSON pcb_trace elements.
|
|
139
|
+
* Each segment becomes its own trace with a simple 2-point route.
|
|
140
|
+
*/
|
|
141
|
+
declare class CollectTracesStage extends ConverterStage {
|
|
142
|
+
step(): boolean;
|
|
143
|
+
private createTraceFromSegment;
|
|
144
|
+
private mapLayer;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* CollectViasStage converts KiCad vias into Circuit JSON pcb_via elements.
|
|
149
|
+
*/
|
|
150
|
+
declare class CollectViasStage extends ConverterStage {
|
|
151
|
+
step(): boolean;
|
|
152
|
+
private processVia;
|
|
153
|
+
private mapLayer;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* CollectGraphicsStage processes KiCad graphics elements:
|
|
158
|
+
* - gr_line on Edge.Cuts → pcb_board.outline
|
|
159
|
+
* - gr_text on silk layers → pcb_silkscreen_text
|
|
160
|
+
* - gr_line on silk layers → pcb_silkscreen_path
|
|
161
|
+
*/
|
|
162
|
+
declare class CollectGraphicsStage extends ConverterStage {
|
|
163
|
+
step(): boolean;
|
|
164
|
+
private createBoardOutline;
|
|
165
|
+
private createSilkscreenPath;
|
|
166
|
+
private createSilkscreenText;
|
|
167
|
+
private mapLayer;
|
|
168
|
+
private pointsEqual;
|
|
169
|
+
private calculateWidth;
|
|
170
|
+
private calculateHeight;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export { CollectFootprintsStage, CollectGraphicsStage, CollectLibrarySymbolsStage, CollectNetsStage, CollectSchematicTracesStage, CollectTracesStage, CollectViasStage, type ConverterContext, ConverterStage, InitializePcbContextStage, InitializeSchematicContextStage, KicadToCircuitJsonConverter };
|