Seeking python library to read schematics and symbols

Does anyone know of a Python library that will allow me to get a schematic symbol for a given pcbnew footprint? I would have access to the pcbnew footprint in python (and thus any properties of the footprint as well). Using that information, I want to retrieve the associated schematic symbol and get it in a format that gives me the properties and positions of the lines/arcs/curves/circles that comprise the symbol.

Preferably a Python library that is kept up to date with the latest KiCAD releases.

Edit: it would also have to work with both Python2 and Python3 due to Windows versions of KiCAD currently being stuck at Python2.

Well, I just looked up the format and it looks like I can convert a library to a dictionary of “DRAW” elements with:

def importlib(self,filename):
    """Schematic [LIB_FILE_NAME] import lib file as a dict with drawing elements, keyed by symbol name"""
    filename = filename[0]
    libdict = {}
    output('reading file: {}'.format(filename))
    with codecs.open(filename, encoding='utf-8') as lib:
        fileiter = iter(lib)
        for line in fileiter:
            if line.startswith('#'):
                continue
            output('Line: {}'.format(line))
            words = line.split()
            if words[0] == 'DEF':
                key = words[1]
                for line in fileiter:
                    # if line.startswith('#'):
                        # continue
                    words = line.split()
                    if words[0] == 'DRAW':
                        drawdef = []
                        for line in fileiter:
                            # if line.startswith('#'):
                                # continue
                            if words[0] == 'ENDDRAW':
                                break
                            words = line.split()
                            if words[0] in 'ACTS':
                                drawdef.append(line)
                        if words[0] == 'ENDDRAW':
                            break
                
                libdict[key] = ''.join(drawdef)
    return libdict

Now I’ll work on converting the schematic library draw commands to DRAWSEGMENTs.

1 Like

Now converted to DRAWSEGMENTs on pcbnew (haven’t figured out how to deal with symbol variations yet):

image

The KiCommand command string (in development) to use for this is:

  • "C:\Program Files\KiCad\KiCad-5.1.6\share\kicad\library\4xxx.lib" importlib HEF4093B sindex escaped 1 libtogeom S,10000,Thickness,100000 split swap append newdrawing refresh

Keep in mind that schematics page and library formats have changed in 5.99. So I’d recommend against putting a lot of effort into parsing 5.1.6 schematic files.

But from what I’ve seen only the file format changed. The graphical primitives stayed the same.

2 Likes

Or keep your code modular enough to swap out the file format parsing code. With some cleverness you could have your code decide which parsing code to use based on the schematic file format version it finds when the code first interrogates the file header.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.