Python

From FreeCAD Documentation
Revision as of 08:19, 12 September 2021 by Maker (talk | contribs) (Created page with "Ver Introducción a Python para aprender sobre el lenguaje de programación Python, y luego Python scripting tutorial/es|Tutorial de scripting...")
FreeCAD was originally designed to work with Python 2.x. This series ended with 2.7.18 release dated April, 20th 2020 and is succeeded by Python 3. The further development of FreeCAD will be done exclusively with Python 3, and backwards compatibility will not be supported.

Descripción

Python es un lenguaje de programación de propósito general y de alto nivel que se utiliza muy comúnmente en grandes aplicaciones para automatizar algunas tareas mediante la creación de scripts o macros.

En FreeCAD, el código Python se puede utilizar para crear varios elementos de forma programada, sin necesidad de hacer clic en la interfaz gráfica de usuario. Además, muchas herramientas y ambientes de trabajo de FreeCAD están programados en Python.

Ver Introducción a Python para aprender sobre el lenguaje de programación Python, y luego Tutorial de scripting en Python y Fundamentos de FreeCAD Guion para empezar a hacer scripts en FreeCAD.

Legibilidad

Readability of Python code is one of the most important aspects of this language. Using a clear and consistent style within the Python community facilitates contributions by different developers, as most experienced Python programmers expect the code to be formatted in a certain way and to follow certain rules. When writing Python code, it is advisable to follow PEP8: Style Guide for Python Code and PEP257: Docstring Conventions.

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

Convenciones

En esta documentación deben seguirse algunas convenciones para los ejemplos de Python.

Esta es una signatura de función típica

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

Importaciones

Funciones de Python se almacenan en archivos llamados módulos. Antes de utilizar cualquier función en ese módulo, el módulo debe ser incluido en el documento con la instrucción import.

Esto crea funciones prefijadas, es decir, module.function(). Este sistema evita los choques de nombres con funciones que se llaman igual pero que provienen de módulos diferentes. Por ejemplo, las dos funciones Arch.make_window() y myModule.make_window() pueden coexistir sin problema.

Ejemplos completos deben incluir las importaciones necesarias y las funciones prefijadas.

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")