Python

From FreeCAD Documentation
Revision as of 10:44, 16 March 2019 by Luc (talk | contribs) (Created page with "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 {{...")
(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.

See Introduction to Python to learn about the Python programming language, and then Python scripting tutorial and FreeCAD Scripting Basics to start scripting in FreeCAD.

When writing Python code, it's advisable to follow PEP8: Style Guide for Python Code.

Conventions

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

This is a typical function signature

Wire = makeWire(pointslist, closed=False, placement=None, face=None, support=None)
  • Arguments with key-value pairs are optional, with the default value indicated in the signature. This means that the following calls are equivalent:
Wire = makeWire(pointslist, False, None, None, None)
Wire = makeWire(pointslist, False, None, None)
Wire = makeWire(pointslist, False, None)
Wire = makeWire(pointslist, False)
Wire = makeWire(pointslist)
In this example the first argument doesn't have a default value so it should always be included.
  • 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:
Wire = makeWire(pointslist, closed=False, placement=None, face=None)
Wire = makeWire(pointslist, closed=False, face=None, placement=None)
Wire = makeWire(pointslist, placement=None, closed=False, face=None)
Wire = makeWire(pointslist, support=None, closed=False, placement=None, face=None)
  • Python's guidelines stress readability of code; in particular, parentheses should immediately follow the function name, and a space should follow a comma.
p1 = Vector(0, 0, 0)
p2 = Vector(1, 1, 0)
p3 = Vector(2, 0, 0)
Wire = makeWire([p1, p2, p3], closed=True)
  • 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.
a_list = [1, 2, 3,
          2, 4, 5]

Wire = makeWire(pointslist,
                False, None,
                None, None)
  • Functions may return an object that can be used as the base of another drawing function.
Wire = makeWire(pointslist, closed=True, face=True)
Window = makeWindow(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.

This creates prefixed functions, that is, module.function(). This system prevents name clashes with functions that are named the same but that come from different modules. For example, the two functions Arch.makeWindow() and myModule.makeWindow() may coexist without problem.

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

import FreeCAD, Draft

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

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

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