Fix: Kicad-to-python Sync Bug & Placement Algorithm
Hey everyone, let's dive into a frustrating bug that's been bugging a few of us using circuit-synth and its kicad-to-python sync tool. If you've been relying on this tool to keep your KiCad projects in sync with your Python code, you might have run into some trouble. Specifically, the tool generates Python code that's missing a crucial parameter, leading to errors and a broken workflow. We'll break down the issue, walk through how to reproduce it, and, most importantly, provide a fix so you can get back to creating awesome circuits. Let's get started, guys!
The Bug: Missing Parameter in Generated Code
The core of the problem lies in the kicad-to-python tool, which is supposed to automatically update your Python code based on changes in your KiCad schematic. The tool generates Python code, but it's missing a crucial parameter: placement_algorithm. This parameter is essential for how circuit-synth arranges components in your KiCad project. Without it, the generated code fails to execute correctly. This results in an AttributeError during the KiCad project generation. The specific error message is 'ConnectionAnalyzer' object has no attribute 'get_placement_order'. This is because the missing placement_algorithm causes the code to try and use get_placement_order(), which is not available, breaking the sync workflow. This essentially breaks the bidirectional link between your Python code and your KiCad project, which can be super annoying if you're frequently updating both.
The Impact of this Bug
The impact of this bug is pretty significant. The inability to sync changes seamlessly means more manual work for you. Every time you run the kicad-to-python sync, you'll need to manually edit the generated Python code to add the missing placement_algorithm parameter. This extra step can be time-consuming and prone to errors. Without a fix, you will need to manually fix the code after every sync operation. This will cause friction in your workflow. The entire purpose of using the kicad-to-python tool is to make your life easier by automating the synchronization process. The bug defeats the purpose of the tools.
Version Affected
This bug has been identified in circuit-synth version 0.8.30. It's always a good idea to check for updates and bug fixes, but as of this version, the issue exists. We are looking forward to a fix, hopefully soon!
Steps to Reproduce the Bug
To see this bug in action, follow these steps. Don't worry, it's pretty straightforward, so you can easily replicate the issue and confirm that you're experiencing the same problem. This will help you understand the problem and test the fix. Now let's jump right into the steps to reproduce the bug, so you can see it firsthand!
-
Create a Simple Circuit and Generate KiCad Project: Start by crafting a basic circuit in Python using the
circuit-synthlibrary. Here's a quick example of a voltage divider:from circuit_synth import Component, Net, circuit @circuit(name="VoltageDivider") def voltage_divider(): # Create simple voltage divider circuit vin = Net('VIN_5V') vout = Net('VOUT_2V5') gnd = Net('GND') r1 = Component(symbol="Device:R", ref="R", value="10k", footprint="Resistor_SMD:R_0603_1608Metric") r2 = Component(symbol="Device:R", ref="R", value="10k", footprint="Resistor_SMD:R_0603_1608Metric") r1[1] += vin r1[2] += vout r2[1] += vout r2[2] += gnd if __name__ == "__main__": circuit_obj = voltage_divider() circuit_obj.generate_kicad_project( project_name="VoltageDivider", placement_algorithm="hierarchical", generate_pcb=True ) -
Run the Circuit: Execute the Python script. This will generate the KiCad project files in a designated folder. Verify the files are created correctly.
-
Edit the Schematic: Open the KiCad project (e.g.,
VoltageDivider/VoltageDivider.kicad_pro) and make some changes to the schematic. You can add a new component, move existing ones, or change the wiring. Save your changes. -
Run
kicad-to-pythonSync: Now, it's time to run thekicad-to-pythoncommand. The command should be run in your terminal. Here's what it should look like:uv run kicad-to-python VoltageDivider/VoltageDivider.kicad_pro voltage_divider.py --backup -
Run the Generated Python File: The sync tool will generate a new Python file. Try running this file. You should use a command like this:
uv run python voltage_divider.py -
Observe the Error: You should see the
AttributeErrorwe discussed earlier, indicating that theplacement_algorithmparameter is missing and causing the sync to fail. The error message will show something like this:AttributeError: 'ConnectionAnalyzer' object has no attribute 'get_placement_order'
This completes the steps to reproduce the bug. If you have been following along, you're now seeing the bug for yourself!
Root Cause: The Missing Parameter
The root cause is a straightforward oversight in the code that kicad-to-python generates. Specifically, the tool is missing the placement_algorithm parameter when calling the generate_kicad_project() function. In circuit_synth/tools/utilities/python_code_generator.py, the generated code looks something like this:
circuit.generate_kicad_project(project_name=