Circuit-Synth Architecture
Overview
Circuit-synth is built around a JSON-centric architecture with modern kicad-sch-api integration where JSON serves as the canonical intermediate representation for all circuit data. This design enables seamless interoperability between Python circuit definitions and KiCad projects while maintaining full round-trip fidelity.
Core Data Flow
┌─────────────┐ ┌──────────┐ ┌─────────────┐
│ Python │ ───► │ JSON │ ───► │ KiCad │
│ Circuit │ │ (Central │ │ Files │
│ Code │ ◄─── │ Format) │ ◄─── │ (.kicad_*) │
└─────────────┘ └──────────┘ └─────────────┘
Python → JSON → KiCad
Python Circuit Definition: Engineers write circuit designs using Python classes (
Circuit,Component,Net)JSON Serialization: The
Circuit.to_dict()method converts the circuit hierarchy to JSONKiCad Generation: JSON is processed with hybrid architecture:
Legacy positioning system: Handles component placement and hierarchical structure
Modern kicad-sch-api: Professional schematic file generation via PyPI package (v0.3.4+)
Intelligent selection: Automatically chooses optimal approach per schematic type
KiCad → JSON → Python
KiCad Import: Parser reads
.kicad_proand.kicad_schfilesJSON Conversion: Circuit structure is extracted into circuit-synth JSON format
Python Generation:
json_to_python_projectcreates Python code from JSON
JSON as Canonical Format
JSON serves as the single source of truth for circuit data:
Hierarchical Structure: Preserves full circuit hierarchy with subcircuits
Complete Fidelity: All circuit information is maintained during conversions
Version Control Friendly: Human-readable text format ideal for Git
Language Agnostic: Can be consumed by any tool or language
JSON Structure
The JSON format represents circuits as nested hierarchies:
{
"name": "Main_Circuit",
"description": "Top-level circuit",
"components": {
"U1": {
"symbol": "MCU_Module:Arduino_UNO_R3",
"footprint": "Module:Arduino_UNO_R3",
"pins": [
{"pin_id": 0, "name": "~IOREF", "num": "1"},
{"pin_id": 1, "name": "~Reset", "num": "2"}
]
}
},
"nets": {
"VCC": [
{"component": "U1", "pin_id": 3},
{"component": "R1", "pin_id": 0}
]
},
"subcircuits": [
{
"name": "Power_Supply",
"description": "5V to 3.3V regulation",
"components": {},
"nets": {}
}
]
}
Key Elements
Components: Dictionary of components with symbols, footprints, and pins
Nets: Connectivity information mapping net names to component pins
Subcircuits: Nested circuit hierarchies for modular design
Metadata: Descriptions, annotations, and design intent
Implementation Details
Core Classes
NetlistExporter (
core/netlist_exporter.py)Handles
to_dict()conversion from Circuit objectsManages hierarchical traversal
Generates both JSON and KiCad netlists
JSON Loader (
io/json_loader.py)load_circuit_from_json_file(): Reads JSON filesload_circuit_from_dict(): Reconstructs Circuit objectsHandles net connectivity and pin mappings
JSON Encoder (
core/json_encoder.py)Custom encoder for circuit-synth types
Handles Enum serialization
Supports objects with
to_dict()methods
Workflow Integration
Direct JSON Export:
circuit.generate_json_netlist("my_circuit.json")
KiCad Project Generation (uses JSON internally):
circuit.generate_kicad_project("my_project")
Round-trip Conversion:
# Import from KiCad circuit = load_circuit_from_kicad("project.kicad_pro") # Modify in Python circuit.add_component(...) # Export back to KiCad circuit.generate_kicad_project("project_updated")
Performance Optimization
Benefits
Single Source of Truth: JSON serves as the definitive circuit representation
Tool Agnostic: Any tool can read/write the JSON format
Version Control: Text-based format works perfectly with Git
Extensibility: Easy to add new fields without breaking compatibility
Debugging: Human-readable format simplifies troubleshooting
Schema Documentation
For detailed JSON schema documentation including field descriptions, data types, and examples, see JSON_SCHEMA.md.
Future Considerations
Schema Validation: Implement JSON Schema validation using the documented schema
Streaming Support: Handle very large circuits efficiently
Binary Format: Optional binary encoding for performance
API Versioning: Maintain compatibility as format evolves