Checking footprint attibutes in python code

Is there a way to check footprint attributes (e.g., Normal/Normal+Insert/Virtual) in pcbnew Python code? I see class members for getting and setting attributes and flags of modules, but the actual attributes/flags appear to be under-documented in the scripting references I’ve found.

I have a terrible habit of forgetting to set the Normal+Insert attribute of new SMD footprints, and I need to proofread the .pos files for a board with a big BOM. I’m hoping I can write a Python function to simply list all of the reference designators with the Insert attribute unset, so I can sanity-check the list.

I came up with this code by trial and error. It would be preferable if defined the attribute values, but this code “seems to work”.

def print_attributes():
    """Print footprint attributes of all components.

    This function prints the reference, footprint and attributes
    of all board components, in CSV format. It can be used to
    check that attributes such as Normal+Insert or Virtual are
    properly set."""

    attrdict = {0: 'Normal',
                1: 'Normal+Insert',
                2: 'Virtual'}

    board = pcbnew.GetBoard()

    for part in board.GetModules():
        ref       = part.Reference().GetText()
        footprint = part.GetFPID().GetFootprintName()
        attr      = part.GetAttributes()

        if attr in attrdict:
            attrstring = attrdict[attr]
            attrstring = str(attr)

        print '{:s}, {:s}, {:s}'.format(ref, footprint, attrstring)

Nice script! The python api isn’t really documented at all it seems, but I think it’s pretty easy to just parse the kicad_pcb file anyway. What I needed to do was just make all parts smd, this little vim command does just that in case anybody else need it too:

:%s/(path .\+)/\0 (attr smd)/g
1 Like