Python Konsole

From FreeCAD Documentation
Revision as of 20:24, 19 December 2019 by Maker (talk | contribs) (Created page with "== Maßnahmen ==")
(Januar 2020) FreeCAD wurde ursprünglich für die Arbeit mit Python 2 entwickelt. Da Python 2 im Jahr 2020 das Ende seiner Lebensdauer erreicht hat, wird die zukünftige Entwicklung von FreeCAD ausschließlich mit Python 3 erfolgen und die Abwärtskompatibilität wird nicht unterstützt.

Einleitung

Die Python-Konsole ist ein Feld, das eine Instanz des Python Interpreters ausführt, mit dem FreeCAD Prozesse gesteuert, Objekte und deren Eigenschaften erstellt und geändert werden können.

Die Python-Konsole in FreeCAD verfügt über eine grundlegende Syntaxhervorhebung, die es ermöglicht, mit verschiedenen Stilen und Farben, Kommentaren, Zeichenketten, numerischen Werten, eingebauten Funktionen, gedruckter Textausgabe und Trennzeichen wie Klammern und Kommata zu unterscheiden. Diese Eigenschaften der Konsole können im Voreinstellungseditor konfiguriert werden.

Die Python-Konsole zeigt Meldungen an, wenn FreeCAD gerade gestartet wurde.

Skriptsprache

Für absolute Anfänger, siehe: Einführung in Python, und Python Skript Tutorial.

Siehe auch: FreeCAD scripting Grundlagen, und Scripted Objekte.

Die Python Konsole kann grundlegende Code Vervollständigung durchführen, wenn ein Punkt nach einem Objekt steht; sie zeigt öffentliche Methoden und Attribute (Variablen) des aktuellen Objekts (Klasse), zum Beispiel obj.

Die Konsole ist auch in der Lage, den Dokumentationsstring einer bestimmten Funktion anzuzeigen, wenn die öffnende Klammer geschrieben wird, z.B. function(

Beispiel Python Code, der Objekte in der 3D Ansicht erzeugt.

Die FreeCAD Initialisierungsskripte laden automatisch einige Module und definieren einige Aliase. In der Python Konsole stehen diese daher zur Verfügung

App = FreeCAD
Gui = FreeCADGui

Daher sind diese gleichwertig

App.newDocument()
FreeCAD.newDocument()

Hinweis: diese vorinstallierten Module und Aliase sind nur über die in das FreeCAD Programm eingebettete Python Konsole verfügbar. Wenn Du FreeCAD als Bibliothek in einem externen Programm verwendest, musst Du daran denken, die Module FreeCAD und FreeCADGui zu laden und die notwendigen Aliase zu definieren, wenn Du möchtest.

Maßnahmen

Right click on the Python console shows some commands:

  • Copy: stores the selected text in the clipboard for later pasting; it is disabled if nothing is selected.
  • Copy command: stores the selected command in the clipboard for later pasting; it is disabled if nothing is selected.
  • Copy history: copy the entire history of Python commands entered in this session.
  • Save history as: save the entire history of Python commands entered in this session to a text file.
  • Paste: paste previously copied text in the clipboard to the Python console.
  • Select all: selects all text in the Python console.
  • Clear console: erases all commands entered into the Python console. This is useful if the Python console is full of messages and previously entered commands that may be distracting when testing a new function. This is merely aesthetic, as this command doesn't delete existing variables nor clears the imported modules in the session.
  • Insert file name: opens a dialog to search for a file in the system, then it inserts the full path of the file. This is useful to test functions that process an input file, without having to write the entire name in the console, which is error prone. This command does not run the file, and does not import it as a Python module, it just returns the full path of that file.
  • Word wrap: wrap very long lines that exceed the horizontal dimension of the Python console.

Template:Interface