Create a FeaturePython object part I/de

Einführung
FunktionsPython Objekte (auch als Geskriptete Objekte bezeichnet) bieten die Möglichkeit, FreeCAD mit Objekten zu erweitern, die sich nahtlos in das FreeCAD Framework integrieren.

Das ermutigt:
 * Schnelle Prototypenerstellung von neuen Objekten und Werkzeugen mit benutzerdefinierten Python Klassen.
 * Speichern und Wiederherstellen von Daten (auch als Serialisierung bekannt) durch Objekte, ohne dass ein Skript in die FreeCAD Dokumentdatei eingebettet wird.
 * Kreative Freiheit, FreeCAD für jede Aufgabe anzupassen.

Auf dieser Seite werden wir ein funktionierendes Beispiel für eine benutzerdefinierte FunktionsPython Klasse konstruieren, wobei wir alle wichtigen Komponenten identifizieren und ein Verständnis dafür gewinnen, wie alles funktioniert, während wir vorankommen.

Wie funktioniert es?
FreeCAD wird mit einer Reihe von Standard Objekttypen zur Verwaltung verschiedener Arten von Geometrie ausgeliefert. Einige von ihnen haben "FeaturePython" Alternativen, die eine Anpassung mit einer benutzerdefinierten Python Klasse ermöglichen.

Diese benutzerdefinierte Python Klasse nimmt einen Verweis auf eines dieser Objekte und modifiziert es. Zum Beispiel kann die Python Klasse dem Objekt Eigenschaften hinzufügen oder es mit anderen Objekten verknüpfen. Außerdem kann die Python Klasse bestimmte Methoden implementieren, um das Objekt in die Lage zu versetzen, auf Dokumentereignisse zu reagieren, wodurch es möglich wird, Änderungen von Objekteigenschaften und Neuberechnungen von Dokumenten abzufangen.

Bei der Arbeit mit benutzerdefinierten Klassen und FunktionsPython Objekten ist es wichtig zu wissen, dass die benutzerdefinierte Klasse und ihr Zustand nicht im Dokument gespeichert werden, da dies die Einbettung eines Skripts in eine FreeCAD Dokumentdatei erfordern würde, was ein erhebliches Sicherheitsrisiko darstellen würde. Nur das FunktionsPython Objekt selbst wird gespeichert (serialisiert). Da aber der Pfad des Skript Moduls im Dokument gespeichert ist, muss ein Benutzer nur den Code der benutzerdefinierten Python Klasse als importierbares Modul installieren und dabei der gleichen Ordnerstruktur folgen, um die verlorene Funktionalität wiederzuerlangen.

Anfang

Einrichten der Dinge
FunktionsPython Objektklassen müssen in FreeCAD als importierbare Module fungieren. Das bedeutet, dass du sie in einem Pfad platzieren musst, der in deiner Python Umgebung existiert (oder füge ihn speziell hinzu). Für die Zwecke dieses Tutoriums werden wir den FreeCAD Benutzerordner Macro verwenden. Aber wenn du eine andere Idee im Kopf hast, kannst du diese stattdessen verwenden.

If you don't know where the FreeCAD Macro folder is type in FreeCAD's Python console:
 * On Linux it is usually  or.
 * On Windows it is, which is usually.
 * On Mac OSX it is usually.

Now we need to create some folders and files:
 * In the folder create a new folder called.
 * In the folder create an empty file:.
 * In the folder, create a  new folder called.
 * In the folder create two files:  and  (leave both empty for now).

Your folder structure should look like this:

Macro/ |--> fpo/ |--> __init__.py        |--> box/ |--> __init__.py            |--> box.py

The folder provides a nice place to play with new FeaturePython objects and the  folder is the module we will be working in. tells Python that there is an importable module in the folder, and will be the class file for our new FeaturePython Object.

With our module paths and files created, let's make sure FreeCAD is set up properly:
 * Start FreeCAD (if you haven't done so already).
 * Enable the Report view.
 * Enable the Python console see FreeCAD Scripting Basics.

Finally, navigate to the folder and open  in your favorite code editor. We will only edit that file.

Anfang

A FeaturePython object
Let's get started by writing our class and its constructor:

The method breakdown:

Add the following code at the top of the file:

The method breakdown:

The method is not required, but it provides a nice way to encapsulate the object creation code.

Anfang

Testen des Codes
Now we can test our new object. Save your code and return to FreeCAD. Make sure you have opened a new document, you can do this by pressing + or selecting.

In the Python console type the following:

Now we need to create our object:

You should see a new object appear in the Tree view labelled "my_box".

Note that the icon is gray. FreeCAD is telling us that the object is not able to display anything in the 3D view. Click on the object and look at its properties in the Property editor. There is not much there, just the name of the object.

Also note that there is a small blue check mark next to the FeaturePython object in the Tree view. That is because when an object is created or changed it is "touched" and needs to be recomputed. Pressing the button will accomplish this. We will add some code to automate this later.

Let's look at our object's attributes:

This will return:

There are a lot of attributes because we're accessing the native FreeCAD FeaturePyton object created in the first line of our method. The property we added in our  method is there too.

Let's inspect it with the method:

This will return:

We can see our property. Let's check it:

This will return:

This is indeed the assigned value, so we know we're accessing the custom class through the FeaturePython object.

Now let's see if we can make our class a little more interesting, and maybe more useful as well.

top

Hinzufügen von Eigenschaften
Properties are the lifeblood of a FeaturePython class. Fortunately, FreeCAD supports a number of property types for FeaturePython classes. These properties are attached directly to the FeaturePython object and are fully serialized when the file is saved. To avoid having to serialize data yourself, it is advisable to only use these property types.

Adding properties is done using the method. The syntax for the method is:

add_property(type, name, section, description)

You can view the list of supported properties by typing:

Let's try adding a property to our box class. Switch to your code editor, move to the method, and at the end of the method add:

Note how we're using the reference to the (serializable) FeaturePython object, and not the (non-serializable) Python class instance.

Once you're done, save the changes and switch back to FreeCAD. Before we can observe the changes made to our code, we need to reload the module. This can be accomplished by restarting FreeCAD, but restarting FreeCAD every time we edit the code would be inconvenient. To make things easier type the following in the Python console:

With the module reloaded, let's see what we get when we create an object:

You should see the new box object appear in the Tree view:
 * Select it and look at the Property editor. There, you should see the Description property.
 * Hover over the property name on the left and the tooltip should appear with the description you provided.
 * Select the field and type whatever you like. You'll notice that Python update commands are executed and displayed in the console as you type letters and the property changes.

Anfang

Let's add some more properties. Return to your source code and add the following properties to the method:

And let's also add some code to recompute the document automatically. Add the following line above the statement in the  method :

'''Be careful where you recompute a FeaturePython object. Recomputing should be handled by a method external to its class.'''



Now, test your changes as follows:
 * Save your changes and reload your module.
 * Delete all objects in the Tree view.
 * Create a new box object from the Python console by calling.

Once the box is created and you've checked to make sure it has been recomputed, select the object and look at its properties. You should note two things:
 * A new property group: Dimensions.
 * Three new properties: Height, Length and Width.

Note also how the properties have units. More specifically, they have taken on the linear units set in the user preferences.

No doubt you noticed that three different values were entered for the dimensions: a floating-point value and two different strings ( and ). The type assumes floating-point values are in millimeters, string values are parsed according to the units specified, and in the GUI all values are converted to the units specified in the user preferences ( in the image). This built-in behavior makes the type ideal for dimensions.

Anfang

Trapping events
The last element required for a basic FeaturePython object is event trapping. A FeaturePython object can react to events with callback functions. In our case we want the object to react whenever it is recomputed. In other words we want to trap recomputes. To accomplish this we need to add a function with a specific name,, to the object class. There are several other events that can be trapped, both in the FeaturePython object itself and in the ViewProvider, which we'll cover in Create a FeaturePython object part II.

For a complete reference of methods available to implement on FeautrePython classes, see FeaturePython methods.

Add the following after the function:

Test the code by again following these steps:
 * Save and reload the module.
 * Delete all objects.
 * Create a new box object.

You should see the printed output in the Python Console, thanks to the call we added to the  method. Of course, the method doesn't do anything here, except tell us that it was called, but it is the key to the magic of FeaturePython objects.

That's it, you now know how to build a basic, functional FeaturePython object!

Anfang

Vollständiger Code
Anfang