Manual:A gentle introduction/ro: Difference between revisions

From FreeCAD Documentation
No edit summary
No edit summary
Line 28: Line 28:
doc = FreeCAD.newDocument()
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.
Dacă introduceți acest lucru în consola FreeCAD Python, veți observa de îndată ce tastați "FreeCAD." (cuvântul FreeCAD urmat de un punct), se deschide un fereastră, permițând finalizați rapid restul liniei. Chiar mai bine, fiecare intrare din lista de autocompletare are o sugestie care explică ce face. Acest lucru face foarte ușor pentru a explora funcționalitatea disponibilă. Înainte de a alege "newDocument", aruncați o privire asupra celorlalte opțiuni disponibile.


[[Image:Exercise_python_02.jpg]]
[[Image:Exercise_python_02.jpg]]

Revision as of 18:55, 31 August 2018

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 are de asememene un macros system, care vă permite să vă înregistrați acțiunile pentru a fi reluate mai târziu. Acest sistem utilizează, de asemenea, consola Python, care înregistrează pur și simplu tot ceea ce se face în interior.

În acest capitol, vom descoperi într-un mod foarte general limbajul Python. Dacă doriți să aflați mai multe, documentația wiki din FreeCAD are o secțiune extensivă despre programarea Python.Python programming.

Scrierea de cod Python

Sunt două maniere facile de a scrie cod Python în FreeCAD: de la consola Python (menu View -> Panels -> Python Console), sau de la editorul Macro (menu Tools -> Macros -> New). În consolă, scrieți comenzi Python una câte una, ele sunt executate atunci când apăsați retur de car (Enter), în timp ce macrocomenzile pot conține un script mai complex compus din mai multe linii care se execută numai când macroul este lansat din aceeași fereastră Macros (faceți click pe triunghiul verde).

În acest capitol, veți putea utiliza ambele metode, dar este foarte recomandat să utilizați Consola Python, deoarece vă va informa imediat despre eventualele erori pe care le faceți în timp ce tastați.

Dacă este prima dată când utilizați Python, luați în considerare citirea acestei scurte introduceri în programarea Python înainte de a merge mai departe introduction to Python programming. Acest lucru va face conceptele de baza ale Python sa vă fie mai clare.

Manipularea obiectelor FreeCAD

Începem prin a crea un document vid:

doc = FreeCAD.newDocument()

Dacă introduceți acest lucru în consola FreeCAD Python, veți observa că de îndată ce tastați "FreeCAD." (cuvântul FreeCAD urmat de un punct), se deschide un fereastră, permițând să finalizați rapid restul liniei. Chiar mai bine, fiecare intrare din lista de autocompletare are o sugestie care explică ce face. Acest lucru face foarte ușor pentru a explora funcționalitatea disponibilă. Înainte de a alege "newDocument", aruncați o privire asupra celorlalte opțiuni disponibile.

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

Vectorii reprezintă un concept fundamental în orice aplicație 3D. Este o listă cu 3 numere (x, y și z), care descriu un punct sau o poziție în spațiul 3D. O mulțime de lucruri se pot face cu vectori, cum ar fi adăugiri, scăderi, proiecții și multe altele. În FreeCAD vectorii funcționează astfel:

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


O altă caracteristică comună a FreeCAD este plasarea Placement. Proprietate de plasare, care conține poziția (Base) și orientarea (Rotation) obiectului. Aceste proprietăți sunt ușor de manipulat de la Python, de exemplu pentru a muta obiectul nostru:

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

De citit în plus