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 ↵:
print("Hello from LinkCAD!")You should see the output in the console.
Exploring the Drawing
The console and script editor define a global named drawing, which always refers to the drawing currently open in LinkCAD. It is None when nothing is loaded.
With a file loaded, you can inspect its structure:
if drawing is None: print("Open a file first")else: for cell in drawing.cells: print(f"Cell: {cell.name}")
for layer in drawing.layers: print(f"Layer: {layer.name}")cells and layers are properties, not methods — a noun costs no parentheses. That convention runs through the whole API: shape.area, cell.bounds, polygon.vertices. Only verbs keep their parentheses: shape.destroy(), cell.clone(name).
Counting Shapes
main = drawing.main_cellprint(f"Total shapes in main cell: {len(main.shapes)}")Or, a little more usefully, per layer:
from collections import Counter
counts = Counter( shape.layer.name for cell in drawing.cells for shape in cell.shapes if shape.layer is not None)
for name, count in counts.most_common(): print(f"{name}: {count} shapes")Creating a Drawing from Scratch
A drawing holds the entire database in memory and must be released. Use it as a context manager and that happens automatically:
from linkcad.v1.db import Drawingfrom linkcad.v1.geom import Point
with Drawing("first-script") as dwg: top = dwg.add_cell("TOP") dwg.main_cell = top
metal = dwg.add_layer("METAL1")
top.add_polygon(metal, [(0, 0), (10_000, 0), (10_000, 10_000), (0, 10_000)]) top.add_circle(metal, Point(20_000, 5_000), diameter=6_000) top.add_text(metal, "TOP", Point(0, 12_000), height=2_000)
print(f"{len(top.shapes)} shapes, bounds {top.bounds}")Geometry is always created by the object that will own it: the drawing makes cells and layers, and the cell makes shapes.
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+↵ 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