你的第一个脚本
本教程演示如何在 LinkCAD 的交互式控制台中运行你的第一个 Python 脚本。
打开控制台
转到 View → Python Console (Ctrl+Shift+P)。窗口底部会出现 Python 提示符。
Hello World
输入以下内容并按 ↵:
print("Hello from LinkCAD!")你应该能在控制台中看到输出。
探索图纸
加载文件后,可以检查其结构:
from linkcad import db
# Get the current drawingdwg = db.Drawing.current()
# List all cellsfor cell in dwg.cells(): print(f"Cell: {cell.name}")
# List all layersfor layer in dwg.layers(): print(f"Layer: {layer.name}")统计形状
from linkcad import db
dwg = db.Drawing.current()main = dwg.main_cell()
count = 0for shape in main.shapes(): count += 1print(f"Total shapes in main cell: {count}")使用脚本编辑器
对于较长的脚本,请使用内置编辑器:
- 打开 View → Python Script Editor (Ctrl+Shift+E)
- 在编辑器中编写脚本
- 按 F5 运行整个脚本
- 或选择若干行并按 Ctrl+↵ 仅运行所选内容