Your First Script
This tutorial walks through running your first Python script in LinkCAD's interactive console.
Open the Console
Go to View → Python Console (Ctrl+Shift+P). A Python prompt appears at the bottom of the window.
Hello World
Type the following and press Enter:
You should see the output in the console.
Exploring the Drawing
With a file loaded, you can inspect its structure:
from linkcad import db
# Get the current drawing
dwg = db.Drawing.current()
# List all cells
for cell in dwg.cells():
print(f"Cell: {cell.name}")
# List all layers
for layer in dwg.layers():
print(f"Layer: {layer.name}")
Counting Shapes
from linkcad import db
dwg = db.Drawing.current()
main = dwg.main_cell()
count = 0
for shape in main.shapes():
count += 1
print(f"Total shapes in main cell: {count}")
Using the Script Editor
For longer scripts, use the built-in editor:
- Open View → Python Script Editor (Ctrl+Shift+E)
- Write your script in the editor
- Press F5 to run the entire script
- Or select lines and press Ctrl+Enter to run just the selection
Next Steps
- Writing a Tool Plugin — create a reusable tool with a GUI dialog
- Writing a Format Plugin — add a custom file format