Manual:A gentle introduction/ro

Python este un limbaj de programare popular, open source, foarte adesea încorporat în aplicații ca limbaj de scripting, ca în cazul FreeCAD. Are o serie de caracteristici care îl fac potrivite pentru utilizatorii noștri de pe FreeCAD: este foarte ușor de învățat, mai ales pentru cei care nu au programat niciodată înainte și este încorporat în multe alte aplicații. Acest lucru face ca acesta să fie un instrument valoros pentru a învăța, așa cum îl veți putea folosi în alte programe, cum ar fi Blender, Inkscape ori GRASS.

FreeCAD folosește extensiv Python. Cu aceasta, puteți accesa și controla aproape orice caracteristică a FreeCAD. De exemplu, puteți să creați obiecte noi, să modificați geometria, să analizați conținutul acestora sau chiar să creați noi controale de interfață, instrumente și panouri. Unele ateliere de lucru FreeCAD și majoritatea atelierelor addon sunt programate total în Python. FreeCAD are o consolă avansată Python, disponibilă din meniuView->Panels->Python console. Este adesea util să efectuați operații pentru care nu există încă buton pentru bara de unelte sau să verificați formele pentru probleme sau să efectuați sarcini repetitive:



Dar consola Python are și o altă utilizare foarte importantă: de fiecare dată când apăsați un buton al barei de instrumente sau efectuați alte operații în FreeCAD, fragmente de cod Python sunt tipărite în consola și executate. Lăsând deschisă consola Python, puteți vedea în mod literal codul Python care se desfășoară în timp ce lucrați și, în cel mai scurt timp, aproape fără a ști asta, veți învăța limbajul Python.

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.

Scrierea de cod Python
There are two easy ways to write Python code in FreeCAD: From the Python console (menu View -> Panels -> Python Console), or from the Macro editor (menu Tools -> Macros -> New). In the console, you write Python commands one by one, which are executed when you press return, while the macros can contain a more complex script made of several lines, which is executed only when the macro is launched from the same Macros window.

In this chapter, you will be able to use both methods, but it is highly recommended to use the Python Console, since it will immediately inform you of any errors you make while typing.

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.

Manipularea obiectelor FreeCAD
Let's start by creating a new empty document:

doc = FreeCAD.newDocument

If you type this in the FreeCAD Python console, you will notice that as soon as you type "FreeCAD." (the word FreeCAD followed by a dot), a windows pops up, allowing to quickly autocomplete the rest of your line. Even better, each entry in the autocomplete list has a tooltip explaining what it does. This makes it very easy to explore the functionality available. Before choosing "newDocument", have a look at the other options available.



As soon as you press Enter our new document will be created. This is similar to pressing the "new document" button on the toolbar. In Python, the dot is used to indicate something that is contained inside something else (newDocument is a function that is inside the FreeCAD module). The window that pops up therefore shows you everything that is contained inside "FreeCAD". If you would add a dot after newDocument, instead of the parentheses, it would show you everything that is contained inside the newDocument function. The parentheses are mandatory when you are calling a Python function, such as this one. We will illustrate that better below.

Now let's get back to our document. Let's see what we can do with it. Type the following and explore the available options:

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

Vectori și Plasament
Vectors are a fundamental concept in any 3D application. It is a list of 3 numbers (x, y and z), describing a point or position in the 3D space. A lot of things can be done with vectors, such as additions, subtractions, projections and much more. In FreeCAD vectors work like this:

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

Another common feature of FreeCAD objects is their Placement. As we saw in earlier chapters, each object has a Placement property, which contains the position (Base) and orientation (Rotation) of the object. These properties are easy to manipulate from Python, for example to move our object:

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

Read more


 * Python
 * Working with Macros
 * Introduction to Python scripting
 * Using Python in FreeCAD
 * The Python scripting wiki hub