面板拼版教程
这个高级教程演示如何构建一个真实场景工具,使用 Option.table() 类型创建交互式网格来指定面板版图。
目标
创建一个 “Panelizer” 工具,将多个单元排列到面板网格中,并支持配置间距和重复次数。
完整插件
from linkcad.plugin import tool, Tool, Option, TableColumnfrom linkcad.db import Drawing, Cell, Transactionfrom linkcad.geom import Point
@tool( name="Panelize", menu="Tools/Drawing", tooltip="Arrange cells into a panel layout", requires_drawing=True,)class Panelizer(Tool): panel_name = Option.string( "Panel cell name", default="PANEL", tooltip="Name for the generated panel cell", )
entries = Option.table( "Panel Layout", columns=[ TableColumn( key="cell_name", label="Cell", col_type="cell_choice", ), TableColumn( key="offset_x", label="Offset X (µm)", col_type="real", default=0.0, decimals=3, ), TableColumn( key="offset_y", label="Offset Y (µm)", col_type="real", default=0.0, decimals=3, ), TableColumn( key="repeat_x", label="Repeat X", col_type="integer", default=1, min_value=1, max_value=1000, ), TableColumn( key="repeat_y", label="Repeat Y", col_type="integer", default=1, min_value=1, max_value=1000, ), TableColumn( key="pitch_x", label="Pitch X (µm)", col_type="real", default=1000.0, decimals=3, ), TableColumn( key="pitch_y", label="Pitch Y (µm)", col_type="real", default=1000.0, decimals=3, ), ], )
flatten_result = Option.boolean( "Flatten result", default=False, tooltip="Flatten the panel cell after assembly", )
def run(self, drawing) -> None: dwg = Drawing.current()
with Transaction(dwg) as txn: panel = Cell.create(dwg, self.panel_name)
for row in self.entries: cell_name = row["cell_name"] if not cell_name: continue
cell = dwg.find_cell(cell_name) if cell is None: print(f"Warning: cell '{cell_name}' not found, skipping") continue
ox = int(row["offset_x"] * 1000) # µm to nm oy = int(row["offset_y"] * 1000) rx = int(row.get("repeat_x", 1)) ry = int(row.get("repeat_y", 1)) px = int(row["pitch_x"] * 1000) py = int(row["pitch_y"] * 1000)
for ix in range(rx): for iy in range(ry): x = ox + ix * px y = oy + iy * py panel.add_ref(cell, Point(x, y))
# auto-commits on successful exit
print(f"Created panel '{self.panel_name}' with " f"{len(self.entries)} cell group(s)")表格选项的工作方式
Option.table() 会在工具对话框中创建可编辑网格:
- 列由
TableColumn对象定义 - 行由用户通过按钮添加/删除
- 值是一个
list[dict],其中每个字典将列键映射到值 - 列类型包括
string、integer、real、choice和cell_choice
TableColumn 属性
| 属性 | 说明 |
|---|---|
key | 此列值的字典键 |
label | 列标题文本 |
col_type | string、integer、real、choice、cell_choice |
default | 新行的默认值 |
choices | choice 列的选项 |
decimals | real 列的小数位数 |
min_value | 数值列的最小值 |
max_value | 数值列的最大值 |
cell_choice 列类型
cell_choice 列会渲染为一个下拉框,并用当前图纸中的所有单元名称填充。这是让用户选择单元的推荐方式。
关键模式
访问表格行
for row in self.entries: cell_name = row["cell_name"] offset_x = row["offset_x"]使用数据库事务
始终将图纸修改包装在事务中:
with Transaction(dwg) as txn: # ... modify drawing ... # auto-commits on successful exit; auto-rolls-back on exception单位转换
图纸数据库内部使用纳米。请从面向用户的单位进行转换:
x_nm = int(x_um * 1000) # µm → nm