Kicad API: How to access location, size etc of footprint text

Where in the pcbnew object model do we get access to the position, size, visibility, orientation etc of the various texts associated with a footprint (aka “module”)? That includes the Reference, Value, and Properties (user-added to a symbol instance in EESchema).

I have a basic Action Plugin working which can iterate board.footprints(), use GetReference(), GetFPIDAsString, GetValue() and GetProperties() and write these to a text file.

However, these are all basically strings, and don’t include attributes for the text’s position, size etc. I can’t find where those attributes are stored, and how they are associated with the footprints.

Clues? Thanks!

With footprint.GraphicalItems() you can get all footprint drawing items (inckuding text). You can get only text items by filtering them with if type(item) is pcbnew.FP_TEXT:. Then you can get position and orientation of the text with GetPosition() and GetTextAngle(). But IIRC the position is relative to the center of the footprint so you have to account for this if you want to get absolute position. And I forgot how is with the orientation. And finally you have to be careful if the footprint is flipped.

Footprint class has Reference() and Value() methods that return FP_TEXT.

1 Like

Thanks MitjaN and @qu1ck, that got me going on the right track.

However one oddity – I noticed that the FP_TEXT items returned by footprint.Reference() and Value() do not appear in the GraphicalItems collection. Though there is a ${REFERENCE} FP_TEXT in GraphicalItems, but this is the one on the Fab layer, as opposed to Reference, which is the one on the Silkscreen layer.

Yeah, that’s by design. Value and Reference are madatory properties of footprint. Everything extra is added to GraphicalItems. Reference on fab layer is also extra.

2 Likes

When was this changed? It used to be that GraphicalItems returned all FP_TEXT items. Including Value and Reference text items on Silkscreen layers.

This also explains why some FP text items were not handled properly in my plugins (though no bug was reported until now)

I’m pretty sure that’s been the case since v5 at least.

Yeah, your’re right. I got tripped (again) by the “Keep upright” text option. If checked and footprint only slightly tilted in the right way, the text looks upside down. So that is why I thought my plugins failed to position text.

With Keep upright
image
Without Keep upright
image

Anyhow, this is how I acctually get footprint ext items (including Reference and Value):

def get_footprint_text_items(footprint):
    list_of_items = [footprint.Reference(), footprint.Value()]

    footprint_items = footprint.GraphicalItems()
    for item in footprint_items:
        if type(item) is pcbnew.FP_TEXT:
            list_of_items.append(item)
    return list_of_items
1 Like

That looks like a bug to me and it may be worth a bug report.
It’s just a small thing, but as long as nobody reports it, it’s unlikely to get fixed.

2 Likes