如何使用 KiCAD pcbnew 插件 Python API 获取所有选中的对象

当你在 KiCAD 的 Python API 中有 FOOTPRINT 对象或封装列表时(例如来自 board.GetFootprints()),你可以使用以下命令获取它们的参考设计ators 如 C11R4

get_current_selection.py
pcbnew.GetCurrentSelection():

注意不仅封装可能被选中,其他对象如形状、过孔、焊盘等也可能被选中。

完整插件示例:

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

class SimplePlugin(pcbnew.ActionPlugin):
    def defaults(self):
        self.name = "Pcbnew 中显示的插件名称:Tools->External Plugins"
        self.category = "描述性类别名称"
        self.description = "插件及其功能的描述"
        self.show_toolbar_button = False # 可选,默认为 False
        self.icon_file_name = os.path.join(os.path.dirname(__file__), 'simple_plugin.png') # 可选,默认为 ""

    def Run(self):
        board: pcbnew.BOARD = pcbnew.GetBoard()
        footprints: list[pcbnew.FOOTPRINT] = board.GetFootprints()

        for selected_object in pcbnew.GetCurrentSelection():
            # TODO 用 selected_object 做些有用的事
            print(selected_object)

SimplePlugin().register() # 实例化并注册到 Pcbnew

示例输出(节选):

output.txt
<pcbnew.PCB_VIA; proxy of <Swig Object of type 'PCB_VIA *' at 0x7fc49d2a8b70> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8de0> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8c30> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8b40> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8db0> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8c00> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8ba0> >
<pcbnew.PCB_VIA; proxy of <Swig Object of type 'PCB_VIA *' at 0x7fc49d2a8e10> >
<pcbnew.PCB_VIA; proxy of <Swig Object of type 'PCB_VIA *' at 0x7fc49d2a8bd0> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8b70> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8de0> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8c30> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8b40> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8db0> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8c00> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8ba0> >
<pcbnew.PCB_VIA; proxy of <Swig Object of type 'PCB_VIA *' at 0x7fc49d2a8e10> >
<pcbnew.PCB_VIA; proxy of <Swig Object of type 'PCB_VIA *' at 0x7fc49d2a8bd0> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8b70> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8de0> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8c30> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8b40> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8db0> >
<pcbnew.PCB_VIA; proxy of <Swig Object of type 'PCB_VIA *' at 0x7fc49d2a8c00> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8ba0> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8e10> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8bd0> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8b70> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8de0> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8c30> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8b40> >
<pcbnew.PCB_VIA; proxy of <Swig Object of type 'PCB_VIA *' at 0x7fc49d2a8db0> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8c00> >
<pcbnew.PCB_TRACK; proxy of <Swig Object of type 'PCB_TRACK *' at 0x7fc49d2a8ba0> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8e10> >
<pcbnew.FOOTPRINT; proxy of <Swig Object of type 'std::deque< FOOTPRINT * >::value_type' at 0x7fc49d2a8bd0> >
<pcbnew.PCB_VIA; proxy of <Swig Object of type 'PCB_VIA *' at 0x7fc49d2a8b70> >

Check out similar posts by category: Electronics, KiCad