Handbuch:Eine behutsame Einführung

From FreeCAD Documentation
Revision as of 20:44, 1 September 2020 by Maker (talk | contribs) (Created page with "In diesem Kapitel wirst du beide Methoden verwenden können, aber es wird dringend empfohlen, die Python Konsole zu verwenden, da sie dich sofort über alle Fehler informiert,...")

Python ist eine populäre, quelloffene Programmiersprache, die sehr oft als Skriptsprache in Anwendungen eingebettet ist, wie es bei FreeCAD der Fall ist. Sie hat eine Reihe von Eigenschaften, die sie für uns FreeCAD Anwender geeignet machen: Es ist sehr leicht zu erlernen, besonders für Leute, die noch nie programmiert haben, und es ist in viele andere Anwendungen eingebettet. Das macht es zu einem wertvollen Lernwerkzeug, da Sie es in anderen Programmen wie Blender, Inkscape oder GRASS verwenden können.

FreeCAD macht umfangreichen Gebrauch von Python. Damit kannst du auf fast alle Funktionen von FreeCAD zugreifen und diese steuern. So kannst du beispielsweise neue Objekte erstellen, ihre Geometrie ändern, ihren Inhalt analysieren oder sogar neue Bedienelemente, Werkzeuge und Bedienfelder erstellen. Einige FreeCAD Arbeitsbereiche und die meisten Addon Arbeitsbereiche sind vollständig in Python programmiert. FreeCAD verfügt über eine erweiterte Python Konsole, die über das Menü 'Ansicht->Paneele->Python Konsole verfügbar ist. Sie ist oft nützlich, um Operationen durchzuführen, für die es noch keine Schaltfläche in der Werkzeugleiste gibt, oder um Formen auf Probleme zu überprüfen oder um sich wiederholende Aufgaben durchzuführen:

But the Python console has another very important use: Every time you press a toolbar button, or perform other operations in FreeCAD, some Python code is printed in the console and executed. By leaving the Python console open, you can literally see the Python code unfold as you work, and in no time, almost without knowing it, you will learning some of the Python language.

FreeCAD also has a macros system, which allows you to record actions to be replayed later. This system also uses the Python console, by simply recording everything that is done in it.

In this chapter, we will discover very generally the Python language. If you are interested in learning more, the FreeCAD documentation wiki has an extensive section related to Python programming.

Schreiben von Python Code

Es gibt zwei einfache Möglichkeiten, Python Code in FreeCAD zu schreiben: Von der Python Konsole aus (Menü Ansicht -> Tafeln -> Python Konsole), oder vom Makro Editor aus (Menü Werkzeuge -> Makros -> Neu). In der Konsole schreibst du nacheinander Python Befehle, die ausgeführt werden, wenn du die Eingabetaste drückst, während die Makros ein komplexeres Skript aus mehreren Zeilen enthalten können, das nur dann ausgeführt wird, wenn das Makro vom gleichen Makro Fenster aus gestartet wird.

In diesem Kapitel wirst du beide Methoden verwenden können, aber es wird dringend empfohlen, die Python Konsole zu verwenden, da sie dich sofort über alle Fehler informiert, die du beim Tippen machst.

If this is your first time using Python, consider reading this short introduction to Python programming before going any further, it will make the basic concepts of Python clearer.

Handhabung von FreeCAD Objekten

Beginnen wir damit, ein neues leeres Dokument zu erstellen:

doc = FreeCAD.newDocument()

Wenn du dies in der FreeCAD Python Konsole eingibst, wirst du dies bemerken, sobald du "FreeCAD" eingibst. (das Wort FreeCAD gefolgt von einem Punkt), ein Fenster öffnet sich, das ermöglicht den Rest deiner Zeile schnell automatisch vervollständigen zu können. Besser noch, jeder Eintrag in der Autovervollständigungsliste hat eine QuickInfo, die erklärt, was er tut. Dies macht es sehr einfach, die verfügbaren Funktionen zu untersuchen. Bevor du "newDocument" wählst, wirf einen Blick auf die anderen verfügbaren Optionen.

Sobald du Enter drückst, wird unser neues Dokument erstellt. Dies ist ähnlich wie das Drücken der Schaltfläche "Neues Dokument" in der Werkzeugleiste. In Python wird der Punkt verwendet, um etwas anzuzeigen, das in etwas anderem enthalten ist (newDocument ist eine Funktion, die sich innerhalb des FreeCAD Moduls befindet). Das sich öffnende Fenster zeigt dir daher alles, was in "FreeCAD" enthalten ist. Wenn du nach "newDocument" anstelle der Klammern einen Punkt hinzufügen würdest, würde es dir alles anzeigen, was in der Funktion "newDocument" enthalten ist. Die Klammern sind obligatorisch, wenn du eine Python Funktion, wie diese hier, aufrufst. Wir werden das weiter unten besser veranschaulichen.

Kommen wir nun zurück zu unserem Dokument. Lasse uns sehen, was wir damit machen können. Gib Folgendes ein und untersuche die verfügbaren Optionen:

doc.

Usually names that begin with an upper-case letter are attributes: they contain a value. Names that begin with a lower-case letter are functions (also called methods): they "do something". Names that begin with an underscore are usually there for the internal use of the module, and you should ignore them. Let's use one of the methods to add a new object to our document:

box = doc.addObject("Part::Box","myBox")

Our box is added in the tree view, but nothing happens in the 3D view yet, because when working from Python, the document is never recomputed automatically. We must do that manually, whenever required:

doc.recompute()

Now our box has appeared in the 3D view. Many of the toolbar buttons that add objects in FreeCAD actually do two things: add the object, and recompute. If you turned on the "show script commands in Python console" option above, try now adding a sphere with the appropriate button in the Part Workbench, and you will see the two lines of Python code being executed one after the other.

You can get a list of all possible object types like Part::Box:

doc.supportedTypes()

Now let's explore the contents of our box:

box.

You'll immediately see a couple of very interesting things such as:

box.Height 

This will print the current height of our box. Now let's try to change that:

box.Height = 5 

If you select your box with the mouse, you will see that in the properties panel, under the Data tab, our Height property appears with the new value. All properties of a FreeCAD object that appear in the Data and View tabs are directly accessible by Python too, by their names, like we did with the Height property. Data properties are accessed directly from the object itself, for example:

box.Length 

View properties are stored inside a ViewObject. Each FreeCAD object possesses a ViewObject, which stores the visual properties of the object. When running FreeCAD without its Graphical Interface (for example when launching it from a terminal with the -c command line option, or using it from another Python script), the ViewObject is not available, since there is no visual at all.

Try the following example to access the line color of our box:

box.ViewObject.LineColor 

Vektoren und Platzierungen

Vektoren sind ein grundlegendes Konzept in jeder 3D Anwendung. Es ist eine Liste von 3 Zahlen (x, y und z), die einen Punkt oder eine Position im 3D Raum beschreiben. Mit Vektoren können viele Dinge gemacht werden, wie z.B. Additionen, Subtraktionen, Projektionen und vieles mehr. In FreeCAD funktionieren Vektoren wie folgt:

myvec = FreeCAD.Vector(2,0,0)
print(myvec)
print(myvec.x)
print(myvec.y)
othervec = FreeCAD.Vector(0,3,0)
sumvec = myvec.add(othervec)


Ein weiteres gemeinsames Merkmal von FreeCAD Objekten ist ihre Platzierung. Wie wir in früheren Kapiteln gesehen haben, hat jedes Objekt eine Placement Eigenschaft, die die Position (Base) und die Orientierung (Rotation) des Objekts enthält. Diese Eigenschaften lassen sich von Python aus leicht manipulieren, z.B. um unser Objekt zu verschieben:

print(box.Placement)
print(box.Placement.Base)
box.Placement.Base = sumvec
otherpla = FreeCAD.Placement()
otherpla.Base = FreeCAD.Vector(5,5,0)
box.Placement = otherpla

Mehr lesen