FreeCAD Scripting Basics/pl

Tworzenie skryptów Python w środowisku FreeCAD
FreeCAD jest zbudowany od podstaw tak, aby być całkowicie kontrolowanym przez skrypty Python. Prawie wszystkie komponenty programu FreeCAD, takie jak interfejs, zawartość sceny, a nawet reprezentacja tej zawartości w widokach 3D, są dostępne z wbudowanego interpretera Pythona lub z własnych skryptów. W rezultacie, FreeCAD jest prawdopodobnie jedną z najgłębiej konfigurowalnych aplikacji inżynierskich dostępnych obecnie.

Jeśli nie znasz Pythona, zalecamy poszukać poradników w internecie i szybko zapoznać się z jego strukturą. Python jest bardzo łatwym językiem do nauki, zwłaszcza, że może być uruchamiany wewnątrz interpretera, gdzie proste polecenia, aż do kompletnych programów, mogą być wykonywane w locie, bez konieczności kompilowania czegokolwiek. FreeCAD posiada wbudowany interpreter Pythona. Jeśli nie widzisz okna oznaczonego jako konsola Pythona, jak pokazano poniżej, możesz go aktywować pod menu.

Interpreter
Z poziomu interpretera masz dostęp do wszystkich zainstalowanych w systemie modułów Pythona, jak również do wbudowanych modułów FreeCAD oraz wszystkich dodatkowych modułów FreeCAD, które zainstalowałeś później. Poniższy zrzut ekranu przedstawia interpreter Pythona:



Z poziomu interpretera można wykonać kod Python i przeglądać dostępne klasy i funkcje. FreeCAD zapewnia bardzo poręczną przeglądarkę klas do eksploracji: Po wpisaniu nazwy znanej klasy, po której następuje kropka (co oznacza, że chcesz dodać coś z tej klasy), otwiera się okno przeglądarki klas, gdzie możesz poruszać się pomiędzy dostępnymi podklasami i metodami. Kiedy coś wybierzesz, wyświetli się powiązany tekst pomocniczy (jeśli istnieje):



So, start here by typing or  and see what happens. Another more generic Python way of exploring the content of modules and classes is to use the command. For example, typing will list all modules currently loaded in FreeCAD. will show you everything inside the App module, etc.

Kolejną przydatną funkcją interpretera jest możliwość cofnięcia się przez całą historię poleceń i odtworzenia linii kodu, którą już wcześniej wpisałeś. Aby poruszać się po historii poleceń, wystarczy użyć klawisza lub.

By right-clicking in the interpreter window, you also have several other options, such as copy the entire history (useful when you want to experiment with things before making a full script of them), or insert a filename with complete path.

na początek strony

Python Help
In the FreeCAD menu, you'll find an entry labeled, which will open a browser window containing a complete, realtime-generated documentation of all Python modules available to the FreeCAD interpreter, including Python and FreeCAD built-in modules, system-installed modules, and FreeCAD additional modules. The documentation available there depends on how much effort each module developer put into documenting his code, but Python modules have a reputation for being fairly well documented. Your FreeCAD window must stay open for this documentation system to work. The entry will give you a quick link to the Power users hub wiki section.

na początek strony

Built-in modules
Since FreeCAD is designed so that it can also be run without a Graphical User Interface (GUI), almost all its functionality is separated into two groups: Core functionality, named, and GUI functionality, named. These two modules can also be accessed from scripts outside of the interpreter, by the names and  respectively.


 * In the module you'll find everything related to the application itself, like methods for opening or closing files, and to the documents, like  setting the active document or listing their contents.


 * In the module, you'll find tools for accessing and managing Gui elements, like the workbenches and their toolbars, and, more interestingly, the graphical representation of all FreeCAD content.

Listing the content of these modules is not very useful because they grow quite fast as FreeCAD develops. But the two browsing tools provided (the class browser and the Python help) should give you complete and up-to-date documentation at any moment.

na początek strony

The App and Gui objects
As already mentioned, in FreeCAD everything is separated into core and representation. This includes the 3D objects. You can access defining properties of objects (called features in FreeCAD) via the module, and change the way they are represented on screen via the  module. For example, a cube has properties that define it (like width, length, height) that are stored in an object, and representation properties (like faces color, drawing mode) that are stored in a corresponding  object.

This way of doing things allows a very wide range of uses, like having algorithms work only on the definition part of features, without the need to care about any visual part, or even redirect the content of the document to non-graphical application, such as lists, spreadsheets, or element analysis.

For every object in your document, there exists a corresponding  object. In fact the document itself has both an and a  object. This, of course, only applies when you run FreeCAD with its full interface. In the command-line version no GUI exists, so only objects are available. Note that the part of objects is re-generated every time an  object is marked as 'to be recomputed' (for example when one of its parameters changes), so any changes made directly to the  object may be lost.

To access the part of something, you type:

where is the name of your object. You can also type:

To access the part of the same object, you type:

where is the name of your object. You can also type:

If you are in command-line mode and have no GUI, the last line will return.

na początek strony

The Document objects
In FreeCAD all your work resides inside documents. A document contains your geometry and can be saved to a file. Several documents can be opened at the same time. The document, like the geometry contained inside, has and  objects. The object contains your actual geometry definitions, while the  object contains the different views of your document. You can open several windows, each one viewing your work with a different zoom factor or from a different direction. These views are all part of your document's object.

To access the part of the currently open (active) document, you type:

To create a new document, type:

To access the part of the currently open (active) document, you type:

To access the current view, you type:

na początek strony

Using additional modules
The and  modules are only responsible for creating and managing objects in the FreeCAD document. They don't actually do anything more such as creating or modifying geometry. This is because that geometry can be of several types, and therefore requires additional modules, each responsible for managing a certain geometry type. For example, the Part Module, using the OpenCascade kernel, is able to create and manipulate BRep type geometry. Whereas the Mesh Module is able to build and modify mesh objects. In this manner FreeCAD is able to handle a wide variety of object types, that can all coexist in the same document, and new types can easily be added in the future.

na początek strony

Creating objects
Each module has its own way of dealing with geometry, but one thing they usually all can do is create objects in the document. But the FreeCAD document is also aware of the available object types provided by the modules:

will list all possible objects you can create. For example, let's create a mesh (handled by the module) and a part (handled by the  module):

The first argument is the object type, the second the name of the object. Our two objects look almost the same: They don't contain any geometry yet, and most of their properties are the same when you inspect them with and. Except for one thing, has a  property and  has a  property. That is where the Mesh and Part data are stored. For example, let's create a cube and store it in our  object:

You could try storing the cube inside the property of the  object, but it will return an error. That is because each properties is made to store only a certain type. In a property, you can only save stuff created with the  module. Note that most modules also have a shortcut to add their geometry to the document:

na początek strony

Modifying objects
Modifying an object is done in the same way:

Now let's change the shape by a bigger one:

na początek strony

Querying objects
You can always look at the type of an object like this:

or check if an object is derived from one of the basic ones (Part Feature, Mesh Feature, etc):

Now you can really start playing with FreeCAD! For a complete list of available modules and their tools, visit the Category:API section.

na początek strony