Python comparison/equivalency of pcbnew.FOOTPRINT instances

Problem Summary
In writing tests for my python scripts that generate and modify footprints using the KiCad 7.0.0 interface, I’ve encountered a hurdle in comparing footprints to each other. Specifically what I want to do is determine if 2 footprints are geometrically identical to find if a generated footprint is equivalent to a known good reference.

Problem Details
The complexity arises mainly from the fact that the order in which elements are listed in a footprint file are often unimportant and unpredictable (as is the case for essentially any vector format). This means that a naive file comparison will often give false negatives because it can’t equate a re-ordered list of elements.

The 2 solutions I’ve come up with for this are:

  • Generating an SVG and then rasterizing it - this isn’t ideal because it takes a long time to process and isn’t guaranteed to be correct (resolution, occlusion etc)
  • Creating some kind of sexpr system that loads everything, re-orders sections internally in a fixed way and then simply compares the text output. Haven’t actually implemented this one yet though.

Is there something already implemented for this in the KiCad source (maybe something like this function for a pad)? Is there some other better methodology that I haven’t listed above?

You can find code which compares footprints in drc_test_provider_library_parity.cpp, but it doesn’t have any magic bullets for the ordering issues. (It essentially performs your second solution of re-ordering them in a “known” order, but there’s still no guarantee that will work.)


There is also the CheckFootprint method in qa/pcbnew_utils/board_test_utils.cpp · master · KiCad / KiCad Source Code / kicad · GitLab which compares two footprints for equality, and is used for qa testing of importers. It also works by creating a canonical sorting of elements.