Scripts/pl

Wprowadzenie
Przez tworzenie skryptów rozumiemy tworzenie obiektów topologicznych za pomocą interpretera Python dla FreeCAD. Program FreeCAD może być używany jako "bardzo dobry" zamiennik OpenSCAD, głównie dlatego, że ma prawdziwy interpreter środowiska Python, co oznacza, że ma na pokładzie prawdziwy język programowania, prawie wszystko, co można zrobić z GUI, można zrobić za pomocą skryptu Python.

Niestety informacje na temat skryptów w dokumentacji, a nawet na tej Wiki, są rozproszone i brakuje im jednolitości "pisania”, a większość z nich jest wyjaśniona w zbyt techniczny sposób.



Zaostrzenie apetytu
Pierwszą przeszkodą w łatwej drodze do tworzenia skryptów jest to, że nie ma bezpośredniego sposobu na dostęp do wewnętrznego edytora Python FreeCAD poprzez pozycję menu lub ikonę na pasku narzędzi, ale wiedząc, że FreeCAD otwiera plik z rozszerzeniem w wewnętrznym edytorze Python, najprostszą sztuczką jest utworzenie go w ulubionym edytorze tekstu, a następnie otwarcie go za pomocą zwykłego polecenia.

Aby zrobić to w uprzejmy sposób, plik musi być napisany w pewnym porządku, edytor FreeCAD Python ma dobre podświetlanie składni, którego brakuje w wielu prostych edytorach, takich jak Notatnik Windows lub niektóre podstawowe edytory Linuksa, więc wystarczy napisać te kilka linijek:

Zapisz je ze znaczącą nazwą z rozszerzeniem i załaduj wynikowy plik w FreeCAD, za pomocą wspomnianego polecenia.

Minimalny przykład tego, co jest niezbędne w skrypcie, jest pokazany w tym fragmencie kodu, który można wykorzystać jako szablon dla prawie każdego przyszłego skryptu:

Powyższy kod zawiera kilka sztuczek:


 * Ta linia importuje FreeCAD w interpreterze FreeCAD Python, może wydawać się zbędna, ale tak nie jest.
 * Base i Vector są szeroko stosowane w skryptach FreeCAD, importowanie ich w ten sposób pozwoli zaoszczędzić na wywoływaniu ich za pomocą lub  zamiast  lub, zaoszczędzi to wiele naciśnięć klawiszy i sprawi, że linie kodu będą znacznie krótsze.

Zacznijmy od małego skryptu, który wykonuje bardzo małą pracę, ale pokazuje moc tej metody.

Put these lines after the "template" code and press the green arrow in the Macro toolbar

You will see some magic things, a new document is open named "Pippo" (Italian name of Goofy) and you will see in the 3d view a Cube, like the one in the image below.



Something more...
Not too amazing? Yes, but we have to start somewhere, we can do the same thing with a Cylinder, add these lines of code after the method and before the line:.

Even here nothing too exciting. But please note some peculiarities:


 * The absence of the usual reference to the, present in many Documentation code snippets, is deliberate, this code could be used even invoking FreeCAD as a module in an external Python interpreter, the thing is not easily doable with an AppImage, but with some care it could be done. Plus in the standard Python motto that "better explicit than implicit"  is explaining in a very "poor" way where the things are from.
 * Note the use of the "constant" name assigned to the active Document in = ; activeDocument is not a "constant" in a strict sense, but in a "semantical"  way is our "active Document", that for our use is a proper "constant" so the Python convention to use the  "ALL CAPS" name for "constants", not to mention that  is much shorten than.
 * Every method returns a geometry, this will be clear in the continuation of the page.
 * Geometry didn't have the property, when using the simple geometries to make more complex geometry, managing  is a awkward thing.

Now what to do with this geometries?

Let's introduce boolean operations. As a starter example put these lines after, this create a method for a Fusion also know as Union operation:

Nothing exceptional also here, note however the uniformity in method coding; This approach is more linear that those seen around other tutorial on scripting, this "linearity" help greatly in readability and also with cut-copy-paste operations.

Let's use the geometries, delete lines below the code section starting with, and insert the following lines:

Launch the script with the green arrow and we will see in the 3D view something like:



Placement
Placement Concept is relatively complex, see Aeroplane Tutorial for a more deep explanation.

We usually are in need of placing geometries respect each other, when building complex object this is a recurring task, the most common way is to use the geometry property.

FreeCAD offer a wide choice of ways to set this property, one is more tailored to another depending the knowledge and the background of the user, but the more plain writing is explained in the cited Tutorial, it use a peculiar definition of the portion of, quite easy to learn.

But over other consideration, one thing is crucial, geometry reference point, in other word the point from which the object is modeled by FreeCAD, as described in this table, copied from Placement:

This information has to be kept in mind especially when we have to apply a rotation.

Some examples may help, delete all the line after method and insert the portion of code below:

Let's explain something in the code:


 * We have used a method to define a sphere, using the most easy definition, using only the radius.
 * We have introduced a second writing for the Union or Fusion, using multiple objects, not more distant from the usual Part::Fuse it uses Part:Multifuse. We only use one property . We have passed a tuple as arguments, but it accepts also a list.
 * We have defined a complex object aeroplano (italian word for aeroplane), but we have done it in a "parametric" way, defining some parameters and deriving other parameters, through some calculation, based on the main parameters.
 * We have used some Placement poperties around in the method and before returning the final geometries we have used a  property with the Yaw-Pitch-Roll writing. Note the last, that define a center of rotation of the whole geometry.

It can be easily noted that aeroplano geometry rotate around his "barycenter" or "center of gravity", that I've fixed at wing center, a place that is relatively "natural", but could be placed wherever you want.

The first  is the Translation vector, not used here, but if you substitute  with these lines:

You will see in the Report window this text:

What has happened?

FreeCAD has translated the in other word our  definition that specifies three components, Translation, Rotation and center of rotation in the "internal" values of only two components, Translation and Rotation'.

you can easily visualize the value of using a print statement in the  method and see that it is:

in other word the rotation center of the geometry is at, but this rotation center is not shown in the GUI, it could be entered as a value, it could not be easily retrieved.

This is the meaning of the word "awkward" that I've used to define property.