如何使用 KiCAD pcbnew 插件 Python API 获取所有选中的区域
在我们之前的文章中,我们展示了如何使用 KiCAD python API 获取所有选中的对象
get_current_selection.py
pcbnew.GetCurrentSelection()你可以简单地过滤这些条目,使用带内联过滤的 for 循环获取仅选中区域的列表:
filter_selected_zones.py
for selected_object in pcbnew.GetCurrentSelection():
if type(selected_object).__name__ == 'ZONE':
print(selected_object.GetReference())或使用列表推导:
selected_zones_listcomp.py
selected_footprints: list[pcbnew.FOOTPRINT] = [
zone for zone in pcbnew.GetCurrentSelection() if type(zone).__name__ == 'ZONE'
]完整插件示例:
simple_kicad_plugin.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示例输出(节选):
selected_zones_example_output.txt
D39
D32
D23
D37
D18
D34
D11
D15If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow