How to get all selected footprints using KiCAD pcbnew plugin Python API

In our previous post, we showed how to get all selected objects using the KiCAD python API using

get_current_selection_call.py
pcbnew.GetCurrentSelection()
example.txt

You can simply filter these entries to obtain just a list of selected footprints using either a for loop with inline filtering:

```python
for selected_object in pcbnew.GetCurrentSelection():
    if type(selected_object).__name__ == 'FOOTPRINT':
        print(selected_object.GetReference())
filter_selected_footprints.py
for selected_object in pcbnew.GetCurrentSelection():
    if type(selected_object).__name__ == 'FOOTPRINT':
        print(selected_object.GetReference())

or using a list comprehension:

example.py
selected_footprints: list[pcbnew.FOOTPRINT] = [
    footprint for footprint in pcbnew.GetCurrentSelection() if type(footprint).__name__ == 'FOOTPRINT'
]
selected_footprints_comp.py
selected_footprints: list[pcbnew.FOOTPRINT] = [
    footprint for footprint in pcbnew.GetCurrentSelection() if type(footprint).__name__ == 'FOOTPRINT'
]

Complete plugin example:

example.py
#!/usr/bin/env python
import pcbnew
import os

class SimplePlugin(pcbnew.ActionPlugin):
    def defaults(self):
        self.name = "Plugin Name as shown in Pcbnew: Tools->External Plugins"
        self.category = "A descriptive category name"
        self.description = "A description of the plugin and what it does"
        self.show_toolbar_button = False # Optional, defaults to False
        self.icon_file_name = os.path.join(os.path.dirname(__file__), 'simple_plugin.png') # Optional, defaults to ""

    def Run(self):
        board: pcbnew.BOARD = pcbnew.GetBoard()
        footprints: list[pcbnew.FOOTPRINT] = board.GetFootprints()
        
        # TODO Do something useful with [board]
        for selected_object in pcbnew.GetCurrentSelection():
            print(selected_object)

SimplePlugin().register() # Instantiate and register to Pcbnew
kicad_simple_plugin_get_selected_footprints.py
#!/usr/bin/env python
import pcbnew
import os

class SimplePlugin(pcbnew.ActionPlugin):
    def defaults(self):
        self.name = "Plugin Name as shown in Pcbnew: Tools->External Plugins"
        self.category = "A descriptive category name"
        self.description = "A description of the plugin and what it does"
        self.show_toolbar_button = False # Optional, defaults to False
        self.icon_file_name = os.path.join(os.path.dirname(__file__), 'simple_plugin.png') # Optional, defaults to ""

    def Run(self):
        board: pcbnew.BOARD = pcbnew.GetBoard()
        footprints: list[pcbnew.FOOTPRINT] = board.GetFootprints()
        
        # TODO Do something useful with [board]
        for selected_object in pcbnew.GetCurrentSelection():
            print(selected_object)

SimplePlugin().register() # Instantiate and register to Pcbnew

Example output (excerpt):

example.txt
D39
D32
D23
D37
D18
D34
D11
D15
output.txt
D39
D32
D23
D37
D18
D34
D11
D15

 


Check out similar posts by category: Electronics, KiCAD