T
tscircuit10mo ago
Seve

Recreate KiCad parsing library "kicad-skip"

I'd like to create a Typescript version of this library: https://www.youtube.com/watch?v=EP1GtsZ2VfM We can use it as a foundation to build a new tscircuit <> KiCad Importer/Exporter As we learned with the DSN Converter, it's important to have a really clean architecture for these libraries, not AI-generated slop code (AI can work inside a good architecture, but it often doesn't architect well) I think we can define a "class-based" system and mostly use a similar API of kicad-skip, that way there's less arguing about the API This project is very valuable and will open a lot of bounties, but needs to be architected. CC @Rishabh @shibo @Anas @Abse @Cascaderay @Niharika for thoughts
Psychogenic Technologies
YouTube
Kicad schematics and PCB Python scripting
Not just for layout anymore! Kicad schems can be easily edited or generated with this open source Python library, and I'll be doing both through the course of this short tutorial, including creating a simple LED grid design generator, to demonstrate some possibilities. The skip modules are designed to parse the kicad s-expression source files a...
No description
12 Replies
Seve
SeveOP10mo ago
We already have a AI-generated somewhat sloppy project here: https://github.com/tscircuit/kicad-converter/tree/main But I think like the DSN converter, it's quite hacky with long single files and no classes. I'm not a huge class guy but sometimes when you're parsing these formats it fits very naturally with classes. Moreover, I'm not completely sure we want to always go from kicad -> circuit json, we might want to go from kicad directly to tscircuit core. So it'd be useful to have something that is very good and just parsing and manipulating kicad.
GitHub
GitHub - tscircuit/kicad-converter
Contribute to tscircuit/kicad-converter development by creating an account on GitHub.
Rishabh
Rishabh10mo ago
This is quite interesting, specially the idea going directly from kicad to core. As experienced with dsn-converter where the code is hacky and messy with circuit json. If possible going to and fro directly from core would be quite a neat approach
Seve
SeveOP10mo ago
100%, also separating the S-Expression -> Class into it's own dedicated library/test suite to avoid having too many converters in the same place. The most annoying thing about not having circuit json is you don't get to do snapshot testing easily, but i think we may be able to work around that
Rishabh
Rishabh10mo ago
I think there will be some way to get around that, given that we can convert the kicad to our core specifics then we can easily generate the circuit json needed for snapshot testing
Seve
SeveOP10mo ago
yes i agree we could do that, but I think it might be simpler to have a library that replicates the library in the youtube video without having any tscircuit-specific code in it. This way we wouldn't have the additional complexity of converting to tscircuit inside the library. so i'm suggesting we have two libraries: * @tscircuit/kicad * @tscircuit/kicad-to-tscircuit Something like that
Rishabh
Rishabh10mo ago
So the @tscircuit/kicad libaray will be converting the S-Expression to Classes which are used in core, right?
Seve
SeveOP10mo ago
No, we'd be replicating the functionality of the youtube video inside @tscircuit/kicad. It would probably not need any tscircuit imports. @tscircuit/kicad-to-tscircuit would use @tscircuit/kicad and @tscircuit/core
Rishabh
Rishabh10mo ago
I see, makes sense now
Seve
SeveOP10mo ago
we'd need new classes in @tscircuit/kicad to create the functionality
Rishabh
Rishabh10mo ago
So, first we will be creating a libaray similar to skip just for typescript and then will later use that for tscircuit
Seve
SeveOP10mo ago
yep! Something like this:
export class Schematic extends SourceFile {
/** Collection of all symbols. */
symbols: Collection<Symbol>;

/** Collection of all wires. */
wires: Collection<Wire>;

/** Collection of all labels. */
labels: Collection<Label>;

/** Return all symbols whose reference matches a given RegExp. */
findSymbolsByReferenceRegex(regex: RegExp): Symbol[];

/** Return all symbols whose reference starts with the given prefix. */
findSymbolsByReferencePrefix(prefix: string): Symbol[];

/**
* Return *any* element (symbol, wire, label, etc.) in the bounding
* rectangle between two elements with an 'at' coordinate.
*/
betweenElements(e1: BaseElement, e2: BaseElement): BaseElement[];
}
export class Schematic extends SourceFile {
/** Collection of all symbols. */
symbols: Collection<Symbol>;

/** Collection of all wires. */
wires: Collection<Wire>;

/** Collection of all labels. */
labels: Collection<Label>;

/** Return all symbols whose reference matches a given RegExp. */
findSymbolsByReferenceRegex(regex: RegExp): Symbol[];

/** Return all symbols whose reference starts with the given prefix. */
findSymbolsByReferencePrefix(prefix: string): Symbol[];

/**
* Return *any* element (symbol, wire, label, etc.) in the bounding
* rectangle between two elements with an 'at' coordinate.
*/
betweenElements(e1: BaseElement, e2: BaseElement): BaseElement[];
}
So we could use classes to make sure that we have really detailed/useful object representations as opposed to interfaces in DSN converter which were a bit tricky to work with
Rishabh
Rishabh10mo ago
Gotcha!

Did you find this page helpful?