Ein Tool-Plugin schreiben
Dieses Tutorial zeigt, wie Sie ein Python-Tool erstellen, das im Menüsystem von LinkCAD mit einem automatisch erzeugten Optionsdialog erscheint.
Das Tool-Framework
Ein Tool-Plugin besteht aus:
- Einer Klasse, die mit
@tool()dekoriert ist undToolerweitert - Options-Klassenattributen, die den UI-Dialog definieren
- Einer Methode
run(), welche die Logik implementiert
Beispiel: Ebenenstatistik
Erstellen Sie in Ihrem Plugin-Verzeichnis eine Datei layer_stats.py:
from linkcad.plugin import tool, Tool, Option
@tool( name="Layer Statistics", menu="Tools/Analysis", tooltip="Show shape count per layer",)class LayerStats(Tool): include_empty = Option.boolean( "Include empty layers", default=False, tooltip="Show layers with zero shapes", )
def run(self, drawing) -> None: for layer_name, shapes in drawing.shapes_by_layer(): count = sum(1 for _ in shapes) if count > 0 or self.include_empty: print(f"{layer_name}: {count} shapes")So funktioniert es
@tool()registriert die Klasse als LinkCAD-Toolnameerscheint im Menü,menulegt den Menüpfad festOption.boolean(...)erstellt ein Kontrollkästchen im automatisch erzeugten Dialogrun()wird aufgerufen, wenn der Benutzer im Dialog auf OK klickt
Optionstypen
| Factory | UI-Steuerelement | Beispiel |
|---|---|---|
Option.integer() | Spinbox | Option.integer("Count", default=1, min=0, max=100) |
Option.real() | Double-Spinbox | Option.real("Scale", default=1.0, decimals=4) |
Option.boolean() | Kontrollkästchen | Option.boolean("Enable", default=True) |
Option.string() | Textfeld | Option.string("Name", default="output") |
Option.choice() | Dropdown | Option.choice("Mode", choices=["Fast", "Precise"]) |
Option.path() | Dateiauswahl | Option.path("Output", file_filter="*.csv") |
Option.color() | Farbauswahl | Option.color("Fill", default="#FF0000") |
Option.table() | Bearbeitbares Raster | Siehe Panel-Zusammenstellung |
Option.cell_choice() | Zellen-Dropdown | Option.cell_choice("Target Cell") |
Bedingte Optionen
Verwenden Sie enabled_when, um Optionen dynamisch ein-/auszublenden:
class MyTool(Tool): mode = Option.choice("Mode", choices=["Simple", "Advanced"]) threshold = Option.real( "Threshold", default=0.5, enabled_when=lambda self: self.mode == "Advanced", )Das Feld threshold ist nur aktiviert, wenn der Modus „Advanced“ ist.
Tastenkürzel
@tool( name="My Tool", menu="Tools/Custom", shortcut="Ctrl+Shift+M",)class MyTool(Tool): ...Nächste Schritte
- Ein Format-Plugin schreiben — benutzerdefinierte Import-/Export-Formate hinzufügen
- Optionstypen-Referenz — vollständige Options-API