Zum Inhalt springen

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:

  1. Einer Klasse, die mit @tool() dekoriert ist und Tool erweitert
  2. Options-Klassenattributen, die den UI-Dialog definieren
  3. 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

  1. @tool() registriert die Klasse als LinkCAD-Tool
  2. name erscheint im Menü, menu legt den Menüpfad fest
  3. Option.boolean(...) erstellt ein Kontrollkästchen im automatisch erzeugten Dialog
  4. run() wird aufgerufen, wenn der Benutzer im Dialog auf OK klickt

Optionstypen

FactoryUI-SteuerelementBeispiel
Option.integer()SpinboxOption.integer("Count", default=1, min=0, max=100)
Option.real()Double-SpinboxOption.real("Scale", default=1.0, decimals=4)
Option.boolean()KontrollkästchenOption.boolean("Enable", default=True)
Option.string()TextfeldOption.string("Name", default="output")
Option.choice()DropdownOption.choice("Mode", choices=["Fast", "Precise"])
Option.path()DateiauswahlOption.path("Output", file_filter="*.csv")
Option.color()FarbauswahlOption.color("Fill", default="#FF0000")
Option.table()Bearbeitbares RasterSiehe Panel-Zusammenstellung
Option.cell_choice()Zellen-DropdownOption.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