Saturday, 15 February 2025

Procedural Generation of MegaCastle Castellated Symbols

In the previous post I described how I went about procedurally generating KiCad footprints for modules of different sizes and pin configurations.  That's all working great, but now I'd like the same for schematic symbols.

Like footprints, KiCad has a text-based format for symbols, and that format looks quite similar. The main difference is that while footprints each have their own file, symbols are are stored in a single library.  I'm not quite sure why, but that's how it is.

So where as with the footprints I just had to create the files in the library directory, for the symbols I'll have to read the existing symbol library and then insert them.  To do that I'll probably read the library file, and copy lines to a temporary file until I reach the end, removing the symbol if it's already in there in the process. Then at the end, instead of writing the final line, which is just a ")", I'll insert the new symbol entities. That way we can either insert or update the symbol.

One nice thing once I have this all done, is that I can make the symbols choose the correct footprint by default, to provide a small but welcome convenience.

Okay, it's taken a few hours to get it all right, but it's working now.  

One thing I found out in the process, is that if you corrupt a KiCad symbol library, you can't just re-load it. You have to quit and reload KiCad, before it will actually reload the library.  There are also some odd things with symbol order. It might expect them to be in alphabetical order or something in the file. But it all works okay.

Here's what the symbols look like for all the combinations of whether its the module or the bay to take a module, and whether its for the edge of a PCB and/or has a cut-out to allow a component zone on the rear of the module:








And of course it correctly populates which pins are actually present or not.

So now I really can get back to making the next module.


No comments:

Post a Comment