Python/ro: Difference between revisions

From FreeCAD Documentation
(Created page with "Python")
 
(Updating to match new version of source page)
 
(28 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<languages/>
<languages/>
<div class="mw-translate-fuzzy">
{{VeryImportantMessage|(November 2018) FreeCAD was originally designed to work with Python 2. Many features work with Python 3, but some do not. Making FreeCAD fully support Python 3 is a work in process.}}
{{VeryImportantMessage|(November 2018)
FreeCAD a fost proiectat inițial pentru a lucra cu Python 2. Multe funcții lucrează cu Python 3, dar unele nu funcționează. Efectuarea suportului deplin al FreeCAD Python 3 este o lucrare în desfășurare.}}
</div>


== Description ==
==Descriere==


{{TOCright}}
[https://www.python.org Python] is a general purpose, high level programming language that is very commonly used in large applications to automate some tasks by creating scripts or [[macros|macros]].


[https://www.python.org Python] este un limbaj de programare cu scop general, care este foarte frecvent utilizat în aplicații mari pentru a automatiza anumite sarcini prin crearea de scripturi sau [[macros/ro|macros]].
In FreeCAD, Python code can be used to create various elements programmatically, without needing to click on the graphical user interface. Additionally, many tools and workbenches of FreeCAD are programmed in Python.


În programul FreeCAD, codul Python poate fi folosit pentru a crea diverse elemente programabil, fără a fi nevoie să faceți clic pe interfața grafică a utilizatorului. În plus, multe instrumente și ateliere de lucru ale programului FreeCAD sunt programate în Python.
See [[Introduction to Python|Introduction to Python]] to learn about the Python programming language, and then [[Python scripting tutorial|Python scripting tutorial]] and [[FreeCAD Scripting Basics|FreeCAD Scripting Basics]] to start scripting in FreeCAD.


A se vedea [[Introduction to Python/ro|Introduction to Python]] pentru a afla despre limbajul de programare Python și apoi [[Python scripting tutorial/ro|Python scripting tutorial]] și [[FreeCAD Scripting Basics/ro|FreeCAD Scripting Basics]] pentru a porni scripting-ul în FreeCAD.
When writing Python code, it's advisable to follow [https://www.python.org/dev/peps/pep-0008/ PEP8: Style Guide for Python Code].


== Conventions ==
== Readability ==
In this documentation, some conventions for Python examples should be followed.


<div class="mw-translate-fuzzy">
This is a typical function signature
Atunci când se scrie codul în Python, este de preferat se urmeze [https://www.python.org/dev/peps/pep-0008/ PEP8: Style Guide for Python Code].
</div>

These documents present explanations in a more user-friendly way:
* [https://realpython.com/python-pep8/ How to Write Beautiful Python Code With PEP 8]
* [https://realpython.com/documenting-python-code/ Documenting Python Code: A Complete Guide].

== Convenții ==
În această documentație, ar trebui urmate câteva convenții pentru exemplele Python.

Aceasta este o semnătură tipică a funcției


{{Code|code=
{{Code|code=
Wire = makeWire(pointslist, closed=False, placement=None, face=None, support=None)
Wire = make_wire(pointslist, closed=False, placement=None, face=None, support=None)
}}
}}


* Argumentele cu perechi cheie-valoare sunt opționale, cu valoarea implicită indicată în semnătură. Aceasta înseamnă că următoarele apeluri sunt echivalente:
* Arguments with key-value pairs are optional, with the default value indicated in the signature. This means that the following calls are equivalent:


{{Code|code=
{{Code|code=
Wire = makeWire(pointslist, False, None, None, None)
Wire = make_wire(pointslist, False, None, None, None)
Wire = makeWire(pointslist, False, None, None)
Wire = make_wire(pointslist, False, None, None)
Wire = makeWire(pointslist, False, None)
Wire = make_wire(pointslist, False, None)
Wire = makeWire(pointslist, False)
Wire = make_wire(pointslist, False)
Wire = makeWire(pointslist)
Wire = make_wire(pointslist)
}}
}}


: În acest exemplu, primul argument nu are o valoare implicită, așa că trebuie întotdeauna inclus.
: In this example the first argument doesn't have a default value so it should always be included.


* Atunci când argumentele sunt date cu cheia explicită, argumentele opționale pot fi date în orice ordine. Aceasta înseamnă că următoarele apeluri sunt echivalente:
* When the arguments are given with the explicit key, the optional arguments can be given in any order. This means that the following calls are equivalent:
{{Code|code=
{{Code|code=
Wire = makeWire(pointslist, closed=False, placement=None, face=None)
Wire = make_wire(pointslist, closed=False, placement=None, face=None)
Wire = makeWire(pointslist, closed=False, face=None, placement=None)
Wire = make_wire(pointslist, closed=False, face=None, placement=None)
Wire = makeWire(pointslist, placement=None, closed=False, face=None)
Wire = make_wire(pointslist, placement=None, closed=False, face=None)
Wire = makeWire(pointslist, support=None, closed=False, placement=None, face=None)
Wire = make_wire(pointslist, support=None, closed=False, placement=None, face=None)
}}
}}


* Normele lui Python subliniază lizibilitatea codului; în special, parantezele trebuie să urmeze imediat numele funcției, iar un spațiu trebuie să urmeze după o virgulă.
* Python's guidelines stress readability of code; in particular, parentheses should immediately follow the function name, and a space should follow a comma.


{{Code|code=
{{Code|code=
Line 47: Line 60:
p2 = Vector(1, 1, 0)
p2 = Vector(1, 1, 0)
p3 = Vector(2, 0, 0)
p3 = Vector(2, 0, 0)
Wire = makeWire([p1, p2, p3], closed=True)
Wire = make_wire([p1, p2, p3], closed=True)
}}
}}


* Dacă codul trebuie să fie rupt în mai multe rânduri, acest lucru trebuie făcut cu o virgulă între paranteze pătrate sau paranteze; a doua linie trebuie aliniată cu cea anterioară.
* If code needs to be broken over several lines, this should be done at a comma inside brackets or parentheses; the second line should be aligned with the previous one.


{{Code|code=
{{Code|code=
Line 56: Line 69:
2, 4, 5]
2, 4, 5]


Wire = makeWire(pointslist,
Wire = make_wire(pointslist,
False, None,
False, None,
None, None)
None, None)
}}
}}


* Funcțiile pot returna un obiect care poate fi folosit ca bază a unei alte funcții de desen.
* Functions may return an object that can be used as the base of another drawing function.
{{Code|code=
{{Code|code=
Wire = makeWire(pointslist, closed=True, face=True)
Wire = make_wire(pointslist, closed=True, face=True)
Window = makeWindow(Wire, name="Big window")
Window = make_window(Wire, name="Big window")
}}
}}


== Imports ==
== Importuri ==


Python functions are stored in files called modules. Before using any function in that module, the module must be included in the document with the {{incode|import}} instruction.
Funcțiile Python sunt stocate în fișiere numite module. Înainte de a utiliza orice funcție din modulul respectiv, modulul trebuie inclus în document cu instrucțiunea {{incode|import}}.


<div class="mw-translate-fuzzy">
This creates prefixed functions, that is, {{incode|module.function()}}. This system prevents name clashes with functions that are named the same but that come from different modules.
Aceasta creează funcții prefixate, {{incode|module.function()}}.
For example, the two functions {{incode|Arch.makeWindow()}} and {{incode|myModule.makeWindow()}} may coexist without problem.
Acest sistem previne conflictele de nume cu funcții numite aceleași, dar care provin de la diferite module.
De exemplu, două funcții {{incode|Arch.makeWindow()}} și {{incode|myModule.makeWindow()}} pot coexista fără probleme.
</div>


Exemplele complete ar trebui să includă importurile necesare și funcțiile prefixate.
Full examples should include the necessary imports and the prefixed functions.


{{Code|code=
{{Code|code=
import FreeCAD, Draft
import FreeCAD as App
import Draft


p1 = FreeCAD.Vector(0, 0, 0)
p1 = App.Vector(0, 0, 0)
p2 = FreeCAD.Vector(1, 1, 0)
p2 = App.Vector(1, 1, 0)
p3 = FreeCAD.Vector(2, 0, 0)
p3 = App.Vector(2, 0, 0)
Wire = Draft.makeWire([p1, p2, p3], closed=True)
Wire = Draft.make_wire([p1, p2, p3], closed=True)
}}
}}


{{Code|code=
{{Code|code=
import FreeCAD, Draft, Arch
import FreeCAD as App
import Draft
import Arch


p1 = FreeCAD.Vector(0, 0, 0)
p1 = App.Vector(0, 0, 0)
p2 = FreeCAD.Vector(1, 0, 0)
p2 = App.Vector(1, 0, 0)
p3 = FreeCAD.Vector(1, 1, 0)
p3 = App.Vector(1, 1, 0)
p4 = FreeCAD.Vector(0, 2, 0)
p4 = App.Vector(0, 2, 0)
pointslist = [p1, p2, p3, p4]
pointslist = [p1, p2, p3, p4]


Wire = Draft.makeWire(pointslist, closed=True, face=True)
Wire = Draft.make_wire(pointslist, closed=True, face=True)
Structure = Arch.makeStructure(Wire, name="Big pillar")
Structure = Arch.make_structure(Wire, name="Big pillar")
}}
}}


{{Powerdocnavi{{#translation:}}}}
[[Category:API Documentation]]
[[Category:Developer Documentation]]
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Poweruser Documentation]]
[[Category:API{{#translation:}}]]
[[Category:User Documentation]]
[[Category:Python Code{{#translation:}}]]
[[Category:Glossary{{#translation:}}]]

Latest revision as of 09:22, 17 October 2021

(November 2018) FreeCAD a fost proiectat inițial pentru a lucra cu Python 2. Multe funcții lucrează cu Python 3, dar unele nu funcționează. Efectuarea suportului deplin al FreeCAD Python 3 este o lucrare în desfășurare.

Descriere

Python este un limbaj de programare cu scop general, care este foarte frecvent utilizat în aplicații mari pentru a automatiza anumite sarcini prin crearea de scripturi sau macros.

În programul FreeCAD, codul Python poate fi folosit pentru a crea diverse elemente programabil, fără a fi nevoie să faceți clic pe interfața grafică a utilizatorului. În plus, multe instrumente și ateliere de lucru ale programului FreeCAD sunt programate în Python.

A se vedea Introduction to Python pentru a afla despre limbajul de programare Python și apoi Python scripting tutorial și FreeCAD Scripting Basics pentru a porni scripting-ul în FreeCAD.

Readability

Atunci când se scrie codul în Python, este de preferat să se urmeze PEP8: Style Guide for Python Code.

These documents present explanations in a more user-friendly way:

Convenții

În această documentație, ar trebui urmate câteva convenții pentru exemplele Python.

Aceasta este o semnătură tipică a funcției

Wire = make_wire(pointslist, closed=False, placement=None, face=None, support=None)
  • Argumentele cu perechi cheie-valoare sunt opționale, cu valoarea implicită indicată în semnătură. Aceasta înseamnă că următoarele apeluri sunt echivalente:
Wire = make_wire(pointslist, False, None, None, None)
Wire = make_wire(pointslist, False, None, None)
Wire = make_wire(pointslist, False, None)
Wire = make_wire(pointslist, False)
Wire = make_wire(pointslist)
În acest exemplu, primul argument nu are o valoare implicită, așa că trebuie întotdeauna inclus.
  • Atunci când argumentele sunt date cu cheia explicită, argumentele opționale pot fi date în orice ordine. Aceasta înseamnă că următoarele apeluri sunt echivalente:
Wire = make_wire(pointslist, closed=False, placement=None, face=None)
Wire = make_wire(pointslist, closed=False, face=None, placement=None)
Wire = make_wire(pointslist, placement=None, closed=False, face=None)
Wire = make_wire(pointslist, support=None, closed=False, placement=None, face=None)
  • Normele lui Python subliniază lizibilitatea codului; în special, parantezele trebuie să urmeze imediat numele funcției, iar un spațiu trebuie să urmeze după o virgulă.
p1 = Vector(0, 0, 0)
p2 = Vector(1, 1, 0)
p3 = Vector(2, 0, 0)
Wire = make_wire([p1, p2, p3], closed=True)
  • Dacă codul trebuie să fie rupt în mai multe rânduri, acest lucru trebuie făcut cu o virgulă între paranteze pătrate sau paranteze; a doua linie trebuie aliniată cu cea anterioară.
a_list = [1, 2, 3,
          2, 4, 5]

Wire = make_wire(pointslist,
                False, None,
                None, None)
  • Funcțiile pot returna un obiect care poate fi folosit ca bază a unei alte funcții de desen.
Wire = make_wire(pointslist, closed=True, face=True)
Window = make_window(Wire, name="Big window")

Importuri

Funcțiile Python sunt stocate în fișiere numite module. Înainte de a utiliza orice funcție din modulul respectiv, modulul trebuie inclus în document cu instrucțiunea import.

Aceasta creează funcții prefixate, module.function(). Acest sistem previne conflictele de nume cu funcții numite aceleași, dar care provin de la diferite module. De exemplu, două funcții Arch.makeWindow() și myModule.makeWindow() pot coexista fără probleme.

Exemplele complete ar trebui să includă importurile necesare și funcțiile prefixate.

import FreeCAD as App
import Draft

p1 = App.Vector(0, 0, 0)
p2 = App.Vector(1, 1, 0)
p3 = App.Vector(2, 0, 0)
Wire = Draft.make_wire([p1, p2, p3], closed=True)
import FreeCAD as App
import Draft
import Arch

p1 = App.Vector(0, 0, 0)
p2 = App.Vector(1, 0, 0)
p3 = App.Vector(1, 1, 0)
p4 = App.Vector(0, 2, 0)
pointslist = [p1, p2, p3, p4]

Wire = Draft.make_wire(pointslist, closed=True, face=True)
Structure = Arch.make_structure(Wire, name="Big pillar")