Export footprints as images (by script)

Short version: Is there any way how to export footprint as image from a script?

Long version: I’ve mismatched a footprint and had to do a lot of wiring to fix the PCB. So I was thinking about creating a script, that would export all used footprints in a PCB as images and I would go through them one by one to verify that they are ok.
I was thinking about grouping the same footprints, and maybe some sort of “verified” field in eeschema. For example the footprint and selected eeschema fields would be hashed and you would need to enter the hash into another eeschema field to mark the footprint as verified… But first I would need to export the footprints in a format that would be useful to check it.

For me, the only ‘useful’ way is to have the Dimensions for Footrpint’s parts and PCB.

A few ways to do it but, Kicad’s not a Drawing/Drafting/CAD program so, you’d spend a good deal of time getting accurate Dim’s.

For me, it’s best done in FreeCAD using it’s StepUp Workbench and TechDraw Workbench… Quick example below

Dimensions from the board below Just grabbed some features to Dim for example… No Script…

Sound interesting, but I need it automated or I will not do it :slight_smile:

All I would need is something like this:


And print it probably twice: 1:1 and scaled with some sort of measure alongside it. So I could open the datasheet or take the physical part and confirm that it is a match.

Use the measuring Tools in Kicad’s PCB editor. Can turn On/Off snapping… and can print…

Yeah, that would be the manual way. But I am trying to automate everything, ideally using CI. So the less manual work (aka prone to error) the better…

kicad-cli fp export svg ...
kicad-cli comes with kicad v7

I did look into it however I was not able to find some detailed documentation. It looks great however it seems to export only a footprint from a library:

kicad-cli fp export svg -o out/a.svg test/Displays/displays.pretty/

produces this:
image

this is what I would need however I would like to use footprints from PCB. And according to (a very brief) help message there is no way to specify project/PCB file and export a single (or all) footprints. The thing is that someone could edit the footprint in the PCB - which I would discourage but that is one thing that this script should show…

So as far I understand it now I have two ways how to do it:

  1. use python to copy every footprint from PCB to new temporary board file and plot it
  2. use python or some KiCad files parsing to extract footprints into temporary library and export it.

That is why I did ask in the forums to see if I am not missing anything obvious.

I did find the python function ExportFootprintsToLibrary ( KiCad Pcbnew Python Scripting: pcbnew Namespace Reference ) however there is no documentation, only:

def pcbnew.ExportFootprintsToLibrary ( * *args* )

ExportFootprintsToLibrary(bool aStoreInNewLib, wxString aLibName=wxEmptyString, wxString aLibPath=None) -> bool

Definition at line 9261 of file pcbnew.py.

I tried to call it

board = pcbnew.LoadBoard(project_file)
pcbnew.ExportFootprintsToLibrary(True,"test")

but it did not do anything. I also do not understand why it is function of pcbnew class and not of board class.
I will try to look at it more as it seems that this function in combination with kicad-cli fp export svg could be the way…

You are not missing anything. But it’s fairly simple to write a script to export footprints one by one either into a lib or into a pcb.

Probably because it’s implemented as window method and not board method and needs a valid pcbnew window. It probably won’t help you with automating your export if that’s the case.

Try running it from kicad scripting console instead of standalone python script.

Great to hear. Can you point me in the right direction? I was not able to find a python object for a library. I have a code that traverses through all the footprints, prints their details, etc… but I have not found any class for footprint library.

There are FootprintLibCreate() and FootprintSave() in pcbnew module. Granted I have not tested them.

Thanks, they seem to work. They are a little bit cumbersome and the documentation says nothing on how to use them but I managed to get it working by trial and error :slight_smile:

Here is the proof-of-conecpt version of the code if someone is interested

    board = pcbnew.LoadBoard(project_file)
    libname='testlib'
    pcbnew.FootprintLibCreate(libname)    
    for footprint in board.GetFootprints():
            ref=footprint.GetReference()
            footprint.SetFPIDAsString(ref)
            footprint.SetReference("**REF")
            pcbnew.FootprintSave(libname,footprint)

And just for reference, the FootprintLibCreate fails with I/O error if the directory testlib already exists…

1 Like

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