如何使用 KiCAD pcbnew 插件 Python API 获取所有选中的封装
在我们的之前文章中,我们展示了如何使用 KiCAD python API 获取所有选中的对象
get_current_selection_call.py
pcbnew.GetCurrentSelection()你可以简单地过滤这些条目以仅获取选中的封装列表,使用带内联过滤的 for 循环:
filter_selected_footprints.py
for selected_object in pcbnew.GetCurrentSelection():
if type(selected_object).__name__ == 'FOOTPRINT':
print(selected_object.GetReference())或使用列表推导:
selected_footprints_comp.py
selected_footprints: list[pcbnew.FOOTPRINT] = [
footprint for footprint in pcbnew.GetCurrentSelection() if type(footprint).__name__ == 'FOOTPRINT'
]完整插件示例:
kicad_simple_plugin_get_selected_footprints.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()
# TODO 用 [board] 做些有用的事
for selected_object in pcbnew.GetCurrentSelection():
print(selected_object)
SimplePlugin().register() # 实例化并注册到 Pcbnew示例输出(节选):
output.txt
D39
D32
D23
D37
D18
D34
D11
D15Check out similar posts by category:
Electronics, KiCad
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow