Skip to content

Python Scripting

LinkCAD includes an embedded Python interpreter for custom automation, tool development, and format plugin creation. Python scripts can access the full drawing database and geometry API.

Features

  • Custom Tools — create menu items that process drawing geometry
  • Format Plugins — add support for new import/export file formats
  • Interactive Console — explore and manipulate drawings live
  • Script Editor — write and run scripts from within LinkCAD

Getting Started

  1. Setup & Requirements — Python environment and prerequisites
  2. Your First Script — "Hello World" in the Python console
  3. Writing a Tool Plugin — create a menu-integrated tool
  4. Writing a Format Plugin — add a new file format
  5. Panel Assembly Tutorial — complete real-world example

API Reference

  • Option Types — integer, real, boolean, string, choice, path, color, table, cell_choice
  • Tool Decorator@tool() decorator and Tool base class
  • Format Decorators@format_reader(), @format_writer(), and base classes
  • API Moduleslinkcad.plugin, linkcad.db, linkcad.geom

Quick Example

A simple tool that counts shapes per layer:

from linkcad.plugin import tool, Tool

@tool(
    name="Layer Statistics",
    menu="Tools/Analysis",
    tooltip="Count shapes per layer",
)
class LayerStats(Tool):
    def run(self, drawing) -> None:
        for layer_name, shapes in drawing.shapes_by_layer():
            count = sum(1 for _ in shapes)
            print(f"{layer_name}: {count} shapes")