psychogenic / kicad-skip

kicad s-expression schematic/layout file manipulation
GNU Lesser General Public License v2.1
73 stars 7 forks source link

Schematic write formatting doesn't match KiCAD #5

Open shreekumar3d opened 1 week ago

shreekumar3d commented 1 week ago

I found this library useful to automatically update schematics. Thanks for this!

I track my changes on git, and try to track changes like code. When skip write schematics, it writes split lines. This formatting differs from KiCAD, which uses nested indentation. This makes it difficult to use the output directly with git diff - primarily to see whether the script did exactly what was intended.

E.g. a file written by skip on my system has this as the first line

(kicad_sch (version 20231120) (generator "eeschema") (generator_version "8.0") (uuid "012bf1cf-e3e3-4b01-8e23-6b37d758d0e3") (paper "A1") (lib_symbols

When KiCAD saves the file, I get

(kicad_sch
    (version 20231120)
    (generator "eeschema")
    (generator_version "8.0")
    (uuid "012bf1cf-e3e3-4b01-8e23-6b37d758d0e3")
    (paper "A1")
    (lib_symbols

The order of elements saved by both KiCAD and skip library seems to be same, but the difference is in the indentation and line formatting. To get clean git diffs, I now open the file generated by skip, and save the file. Then commit the schematic. Fixing the schematic write seems useful.

Fixing schematic write - writeTree() in sexp/util.py I suppose would resolve this. I see some comments there that I don't understand :) Any guidance appreciated. I can submit a PR.

shreekumar3d commented 1 week ago

From KiCAD's output, there may be some formatting gotchas. "xy" lists are clearly handled differently. One place I saw custom logic was when it split up "xy" lists in "polyline" into two lines.

EDIT: Looking at the source code of KiCAD, it is clear that logic specific to the type of symbols is involved (arc, circle, polygon, bezier etc). So making this happen would require the writeTree functionality to mirror KiCAD logic,