Topological data scripting/es: Difference between revisions
Renatorivo (talk | contribs) (Created page with "=== Explicación detallada ===") |
(Updating to match new version of source page) |
||
(43 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
<languages/> |
|||
Esta página describe diversos métodos para crear y modificar [[Part Module/es|formas de piezas]] desde Python. Antes de leer esta página, si eres nuevo en Python, es una buena idea leer la [[Introduction to python/es|Introducción a Python]] y [[FreeCAD Scripting Basics/es|como funcionan los archivos de guión en FreeCAD]]. |
|||
{{TOCright}} |
|||
<div class="mw-translate-fuzzy"> |
|||
== Introducción == |
== Introducción == |
||
Aquí le explicamos cómo controlar el [[Part Module/es|Módulo de Pieza]] directamente desde el intérprete de Python de FreeCAD, o desde cualquier archivo de guión externo. Asegúrate de navegar por la sección [[ |
Aquí le explicamos cómo controlar el [[Part Module/es|Módulo de Pieza]] directamente desde el intérprete de Python de FreeCAD, o desde cualquier archivo de guión externo. Asegúrate de navegar por la sección [[Power users hub/es|Archivos de guión]] y las páginas [[FreeCAD_Scripting_Basics/es|Conceptos básicos de archivos de guión en FreeCAD]] si necesitas más información acerca de cómo funcionan los archivos de guión de Python en FreeCAD. |
||
</div> |
|||
Here we will explain to you how to control the [[Part_Module|Part Module]] directly from the FreeCAD Python interpreter, or from any external script. Be sure to browse the [[Scripting]] section and the [[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]] pages if you need more information about how Python scripting works in FreeCAD. If you are new to Python, it is a good idea to first read the [[Introduction_to_Python|Introduction to Python]]. |
|||
===See also=== |
|||
* [[Part_scripting|Part scripting]] |
|||
* [[OpenCASCADE|OpenCASCADE]] |
|||
<div class="mw-translate-fuzzy"> |
|||
=== Diagrama de clases === |
=== Diagrama de clases === |
||
Ésta es una descripción [http://es.wikipedia.org/wiki/Lenguaje_Unificado_de_Modelado Lenguaje Unificado de Modelado (UML)] de las clases más importante del módulo de Pieza: |
Ésta es una descripción [http://es.wikipedia.org/wiki/Lenguaje_Unificado_de_Modelado Lenguaje Unificado de Modelado (UML)] de las clases más importante del módulo de Pieza: |
||
[[Image:Part_Classes.jpg|center|Python classes of the Part module]] |
[[Image:Part_Classes.jpg|center|Python classes of the Part module]] |
||
</div> |
|||
This is a [http://en.wikipedia.org/wiki/Unified_Modeling_Language Unified Modeling Language (UML)] overview of the most important classes of the Part module: |
|||
[[Image:Part_Classes.jpg|Python classes of the Part module]] |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
=== Geometría === |
=== Geometría === |
||
Line 18: | Line 37: | ||
* '''circle''' Círculo o segmento de círculo definido por un punto centro y los puntos de inicio y final |
* '''circle''' Círculo o segmento de círculo definido por un punto centro y los puntos de inicio y final |
||
* '''......''' Y en breve más cosas |
* '''......''' Y en breve más cosas |
||
</div> |
|||
The geometric objects are the building blocks of all topological objects: |
|||
* '''Geom''' Base class of the geometric objects. |
|||
* '''Line''' A straight line in 3D, defined by starting point and end point. |
|||
* '''Circle''' Circle or circle segment defined by a center point and start and end point. |
|||
* '''......''' Etc. |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
=== Topología === |
=== Topología === |
||
Los siguientes tipos de datos topológicos están disponibles: |
Los siguientes tipos de datos topológicos están disponibles: |
||
Line 30: | Line 59: | ||
* '''vertex''' Un elemento topológico que se corresponde con un punto. Tiene dimensión cero. |
* '''vertex''' Un elemento topológico que se corresponde con un punto. Tiene dimensión cero. |
||
* '''shape''' Un concepto genérico que abarca todos los anteriores. |
* '''shape''' Un concepto genérico que abarca todos los anteriores. |
||
</div> |
|||
The following topological data types are available: |
|||
* '''Compound''' A group of any type of topological objects. |
|||
* '''Compsolid''' A composite solid is a set of solids connected by their faces. It expands the notions of WIRE and SHELL to solids. |
|||
* '''Solid''' A part of space limited by shells. It is three dimensional. |
|||
* '''Shell''' A set of faces connected by their edges. A shell can be open or closed. |
|||
* '''Face''' In 2D it is part of a plane; in 3D it is part of a surface. Its geometry is constrained (trimmed) by contours. It is two dimensional. |
|||
* '''Wire''' A set of edges connected by their vertices. It can be an open or closed contour depending on whether the edges are linked or not. |
|||
* '''Edge''' A topological element corresponding to a restrained curve. An edge is generally limited by vertices. It has one dimension. |
|||
* '''Vertex''' A topological element corresponding to a point. It has zero dimension. |
|||
* '''Shape''' A generic term covering all of the above. |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
== Ejemplo rápido: Creación de topologías básicas == |
== Ejemplo rápido: Creación de topologías básicas == |
||
</div> |
|||
<div class="mw-translate-fuzzy"> |
|||
[[Image:Wire.png|right|Wire]] |
[[Image:Wire.png|right|Wire]] |
||
</div> |
|||
<div class="mw-translate-fuzzy"> |
|||
Crearemos ahora una topología por construcción de geometría simple. Como un caso de estudio utilizaremos una pieza como se puede ver en la imagen que consiste en cuatro vértices, dos circunferencias y dos líneas. |
Crearemos ahora una topología por construcción de geometría simple. Como un caso de estudio utilizaremos una pieza como se puede ver en la imagen que consiste en cuatro vértices, dos circunferencias y dos líneas. |
||
</div> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de geometría ==== |
==== Creación de geometría ==== |
||
Primero tenemos que crear las distintas partes de la geometría de este contorno. |
Primero tenemos que crear las distintas partes de la geometría de este contorno. |
||
Y tenemos que tener cuidado de que los vértices de las partes de la geometría están en la '''misma''' posición. De otro modo después podríamos no ser capaces de conectar las partes de la geometría en una topología! |
Y tenemos que tener cuidado de que los vértices de las partes de la geometría están en la '''misma''' posición. De otro modo después podríamos no ser capaces de conectar las partes de la geometría en una topología! |
||
</div> |
|||
First we create the distinct geometric parts of this wire. Making sure that parts that have to be connected later share the same vertices. |
|||
<div class="mw-translate-fuzzy"> |
|||
Así que primero creamos los puntos: |
Así que primero creamos los puntos: |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
import Part |
|||
from FreeCAD import Base |
from FreeCAD import Base |
||
V1 = Base.Vector(0,10,0) |
V1 = Base.Vector(0, 10, 0) |
||
V2 = Base.Vector(30,10,0) |
V2 = Base.Vector(30, 10, 0) |
||
V3 = Base.Vector(30,-10,0) |
V3 = Base.Vector(30, -10, 0) |
||
V4 = Base.Vector(0,-10,0) |
V4 = Base.Vector(0, -10, 0) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Arco ==== |
==== Arco ==== |
||
</div> |
|||
[[Image:Circel.png |
[[Image:Circel.png|Circle]] |
||
<div class="mw-translate-fuzzy"> |
|||
Para crear un arco de circunferencia crearemos puntos de ayuda y crearemos el arco a través de tres puntos: |
Para crear un arco de circunferencia crearemos puntos de ayuda y crearemos el arco a través de tres puntos: |
||
</div> |
|||
<syntaxhighlight> |
|||
VC1 = Base.Vector(-10,0,0) |
|||
{{Code|code= |
|||
C1 = Part.Arc(V1,VC1,V4) |
|||
VC1 = Base.Vector(-10, 0, 0) |
|||
# and the second one |
|||
C1 = Part.Arc(V1, VC1, V4) |
|||
VC2 = Base.Vector(40, 0, 0) |
|||
C2 = Part.Arc(V2, VC2, V3) |
|||
</syntaxhighlight> |
|||
}} |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Línea ==== |
==== Línea ==== |
||
</div> |
|||
[[Image:Line.png |
[[Image:Line.png|Line]] |
||
<div class="mw-translate-fuzzy"> |
|||
La línea puede crearse de forma muy simple a partir de los puntos: |
La línea puede crearse de forma muy simple a partir de los puntos: |
||
</div> |
|||
<syntaxhighlight> |
|||
L1 = Part.Line(V1,V2) |
|||
{{Code|code= |
|||
# and the second one |
|||
L1 = Part.LineSegment(V1, V2) |
|||
L2 = Part.LineSegment(V3, V4) |
|||
</syntaxhighlight> |
|||
}} |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Poniendo todo junto ==== |
==== Poniendo todo junto ==== |
||
El último paso es poner los elementos base de la geometría juntos y formar una forma topológica: |
El último paso es poner los elementos base de la geometría juntos y formar una forma topológica: |
||
</div> |
|||
<syntaxhighlight> |
|||
S1 = Part.Shape([C1,C2,L1,L2]) |
|||
The last step is to put the geometric base elements together and bake a topological shape: |
|||
</syntaxhighlight> |
|||
{{Code|code= |
|||
S1 = Part.Shape([C1, L1, C2, L2]) |
|||
}} |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Crear un prisma ==== |
==== Crear un prisma ==== |
||
Ahora extruir el contorno en una dirección y crear una forma 3D real: |
Ahora extruir el contorno en una dirección y crear una forma 3D real: |
||
</div> |
|||
<syntaxhighlight> |
|||
Now extrude the wire in a direction and make an actual 3D shape: |
|||
{{Code|code= |
|||
W = Part.Wire(S1.Edges) |
W = Part.Wire(S1.Edges) |
||
P = W.extrude(Base.Vector(0,0,10)) |
P = W.extrude(Base.Vector(0, 0, 10)) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Mostrar todo ==== |
==== Mostrar todo ==== |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
Part.show(P) |
Part.show(P) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
== Creación de formas básicas == |
== Creación de formas básicas == |
||
Puedes crear fácilmente objetos topológicos simples con los métodos "make...()" del Módulo Parte: |
Puedes crear fácilmente objetos topológicos simples con los métodos "make...()" del Módulo Parte: |
||
</div> |
|||
<syntaxhighlight> |
|||
b = Part.makeBox(100,100,100) |
|||
You can easily create basic topological objects with the {{incode|make...()}} methods from the Part Module: |
|||
{{Code|code= |
|||
b = Part.makeBox(100, 100, 100) |
|||
Part.show(b) |
Part.show(b) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Otros métodos make...() disponibles: |
Otros métodos make...() disponibles: |
||
* makeBox(l,w,h) -- construye una caja ubicada en p y apuntando en la dirección d con las dimensiones (l, w, h). |
* makeBox(l,w,h) -- construye una caja ubicada en p y apuntando en la dirección d con las dimensiones (l, w, h). |
||
Line 108: | Line 215: | ||
Mira la página [[Part API/es|APIde piezas]] para una lista completa de los métodos disponibles del módulo de pieza. |
Mira la página [[Part API/es|APIde piezas]] para una lista completa de los métodos disponibles del módulo de pieza. |
||
</div> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Importing the needed modules ==== |
==== Importing the needed modules ==== |
||
Primero tenemos que importar el módulo de piezas así podremos utilizar su contenido en Python. |
Primero tenemos que importar el módulo de piezas así podremos utilizar su contenido en Python. |
||
También importamos el módulo base desde dentro del módulo de FreeCAD: |
También importamos el módulo base desde dentro del módulo de FreeCAD: |
||
</div> |
|||
<syntaxhighlight> |
|||
First we need to import the Part module so we can use its contents in Python. We'll also import the Base module from inside the FreeCAD module: |
|||
{{Code|code= |
|||
import Part |
import Part |
||
from FreeCAD import Base |
from FreeCAD import Base |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de un Vector ==== |
==== Creación de un Vector ==== |
||
Los [http://en.wikipedia.org/wiki/Euclidean_vector Vectores] son una de las piezas de información más importantes cuando se construyen formas. Contienen 3 números normalmente (pero no necesariamente siempre) las coordenadas cartesianas X, Y y Z. Puedes crear un vector así: |
Los [http://en.wikipedia.org/wiki/Euclidean_vector Vectores] son una de las piezas de información más importantes cuando se construyen formas. Contienen 3 números normalmente (pero no necesariamente siempre) las coordenadas cartesianas X, Y y Z. Puedes crear un vector así: |
||
</div> |
|||
<syntaxhighlight> |
|||
myVector = Base.Vector(3,2,0) |
|||
[http://en.wikipedia.org/wiki/Euclidean_vector Vectors] are one of the most important pieces of information when building shapes. They usually contain three numbers (but not necessarily always): the X, Y and Z cartesian coordinates. You create a vector like this: |
|||
</syntaxhighlight> |
|||
{{Code|code= |
|||
myVector = Base.Vector(3, 2, 0) |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
Simplemente creamos un vector en las coordenadas X=3, Y=2, Z=0. En el módulo de pieza, los vectores se utilizan en todas partes. Las formas de las piezas también utilizan otro tipo de representaciones de punto, llamada Vértice, el cual en realidad no es más que un contenedor para un vector. Puedes acceder al vector de un vértice así: |
Simplemente creamos un vector en las coordenadas X=3, Y=2, Z=0. En el módulo de pieza, los vectores se utilizan en todas partes. Las formas de las piezas también utilizan otro tipo de representaciones de punto, llamada Vértice, el cual en realidad no es más que un contenedor para un vector. Puedes acceder al vector de un vértice así: |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
myVertex = myShape.Vertexes[0] |
myVertex = myShape.Vertexes[0] |
||
print |
print(myVertex.Point) |
||
> Vector (3, 2, 0) |
> Vector (3, 2, 0) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
====Creación de una arista==== |
====Creación de una arista==== |
||
Un borde no es otra cosa mas que una linea entre dos vértices: |
Un borde no es otra cosa mas que una linea entre dos vértices: |
||
</div> |
|||
<syntaxhighlight> |
|||
edge = Part.makeLine((0,0,0), (10,0,0)) |
|||
An edge is nothing but a line with two vertices: |
|||
{{Code|code= |
|||
edge = Part.makeLine((0, 0, 0), (10, 0, 0)) |
|||
edge.Vertexes |
edge.Vertexes |
||
> [<Vertex object at 01877430>, <Vertex object at 014888E0>] |
> [<Vertex object at 01877430>, <Vertex object at 014888E0>] |
||
}} |
|||
</syntaxhighlight> |
|||
Nota: También puedes crear una arista pasándole dos vértices. |
Nota: También puedes crear una arista pasándole dos vértices. |
||
<syntaxhighlight> |
|||
{{Code|code= |
|||
vec1 = Base.Vector(0,0,0) |
|||
vec1 = Base.Vector(0, 0, 0) |
|||
vec2 = Base.Vector(10, 0, 0) |
|||
line = Part.LineSegment(vec1, vec2) |
|||
edge = line.toShape() |
edge = line.toShape() |
||
}} |
|||
</syntaxhighlight> |
|||
Se puede determinar la longitud y el centro de un borde así: |
Se puede determinar la longitud y el centro de un borde así: |
||
<syntaxhighlight> |
|||
{{Code|code= |
|||
edge.Length |
edge.Length |
||
> 10.0 |
> 10.0 |
||
edge.CenterOfMass |
edge.CenterOfMass |
||
> Vector (5, 0, 0) |
> Vector (5, 0, 0) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Poniendo la forma en la pantalla ==== |
==== Poniendo la forma en la pantalla ==== |
||
Hemos creado un objeto arista, pero no aparece en ninguna parte de la pantalla. |
Hemos creado un objeto arista, pero no aparece en ninguna parte de la pantalla. |
||
Esto es porque simplemente manejamos objetos de Python aquí. La escena 3D de FreeCAD sólo muestra lo que le digas que se muestre. Para hacerlo, utilizamos este simple método: |
Esto es porque simplemente manejamos objetos de Python aquí. La escena 3D de FreeCAD sólo muestra lo que le digas que se muestre. Para hacerlo, utilizamos este simple método: |
||
</div> |
|||
<syntaxhighlight> |
|||
So far we created an edge object, but it doesn't appear anywhere on the screen. This is because the FreeCAD 3D scene only displays what you tell it to display. To do that, we use this simple |
|||
method: |
|||
{{Code|code= |
|||
Part.show(edge) |
Part.show(edge) |
||
}} |
|||
</syntaxhighlight> |
|||
Un objeto se creará en nuestro documento de FreeCAD, y nuestra forma "edge" será atribuido a él. Utiliza esto si es momento para mostrar tu creación en la pantalla. |
|||
<div class="mw-translate-fuzzy"> |
|||
Un objeto se creará en nuestro documento de FreeCAD, y nuestra forma "edge" será atribuido a él. Utiliza esto si es momento para mostrar tu creación en la pantalla. |
|||
</div> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de un contorno ==== |
==== Creación de un contorno ==== |
||
Un contorno es una línea de múltiples aristas y se puede crear a partir de una lista de aristas, o incluso de una lista de contornos: |
Un contorno es una línea de múltiples aristas y se puede crear a partir de una lista de aristas, o incluso de una lista de contornos: |
||
</div> |
|||
<syntaxhighlight> |
|||
edge1 = Part.makeLine((0,0,0), (10,0,0)) |
|||
A wire is a multi-edge line and can be created from a list of edges or even a list of wires: |
|||
edge2 = Part.makeLine((10,0,0), (10,10,0)) |
|||
wire1 = Part.Wire([edge1,edge2]) |
|||
{{Code|code= |
|||
edge3 = Part.makeLine((10,10,0), (0,10,0)) |
|||
edge1 = Part.makeLine((0, 0, 0), (10, 0, 0)) |
|||
edge2 = Part.makeLine((10, 0, 0), (10, 10, 0)) |
|||
wire1 = Part.Wire([edge1, edge2]) |
|||
edge3 = Part.makeLine((10, 10, 0), (0, 10, 0)) |
|||
edge4 = Part.makeLine((0, 10, 0), (0, 0, 0)) |
|||
wire2 = Part.Wire([edge3, edge4]) |
|||
wire3 = Part.Wire([wire1, wire2]) |
|||
wire3.Edges |
wire3.Edges |
||
> [<Edge object at 016695F8>, <Edge object at 0197AED8>, <Edge object at 01828B20>, <Edge object at 0190A788>] |
> [<Edge object at 016695F8>, <Edge object at 0197AED8>, <Edge object at 01828B20>, <Edge object at 0190A788>] |
||
Part.show(wire3) |
Part.show(wire3) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Part.show(wire3) mostrará las 4 aristas que componen nuestro contorno. Otra información útil se puede recuperar fácilmente: |
Part.show(wire3) mostrará las 4 aristas que componen nuestro contorno. Otra información útil se puede recuperar fácilmente: |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
wire3.Length |
wire3.Length |
||
> 40.0 |
> 40.0 |
||
Line 185: | Line 347: | ||
wire2.isClosed() |
wire2.isClosed() |
||
> False |
> False |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de una cara ==== |
==== Creación de una cara ==== |
||
Sólo serán válidas las caras creadas a partir de contornos cerrados. En este ejemplo, wire3 es un contorno cerrado pero wire2 no es un contorno cerrado (mira más arriba) |
Sólo serán válidas las caras creadas a partir de contornos cerrados. En este ejemplo, wire3 es un contorno cerrado pero wire2 no es un contorno cerrado (mira más arriba) |
||
</div> |
|||
<syntaxhighlight> |
|||
Only faces created from closed wires will be valid. In this example, wire3 is a closed wire but wire2 is not (see above): |
|||
{{Code|code= |
|||
face = Part.Face(wire3) |
face = Part.Face(wire3) |
||
face.Area |
face.Area |
||
> 99. |
> 99.99999999999999 |
||
face.CenterOfMass |
face.CenterOfMass |
||
> Vector (5, 5, 0) |
> Vector (5, 5, 0) |
||
Line 200: | Line 370: | ||
> True |
> True |
||
sface = Part.Face(wire2) |
sface = Part.Face(wire2) |
||
sface.isValid() |
|||
> False |
> False |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Sólo las caras tendrán un área, ni los contornos ni las aristas. |
Sólo las caras tendrán un área, ni los contornos ni las aristas. |
||
</div> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de una circunferencia ==== |
==== Creación de una circunferencia ==== |
||
Una circunferencia se puede crear de forma tan simple como esta: |
Una circunferencia se puede crear de forma tan simple como esta: |
||
</div> |
|||
<syntaxhighlight> |
|||
A circle can be created like this: |
|||
{{Code|code= |
|||
circle = Part.makeCircle(10) |
circle = Part.makeCircle(10) |
||
circle.Curve |
circle.Curve |
||
> Circle (Radius : 10, Position : (0, 0, 0), Direction : (0, 0, 1)) |
> Circle (Radius : 10, Position : (0, 0, 0), Direction : (0, 0, 1)) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Si deseas crearlo en cierta posición y con cierta dirección: |
Si deseas crearlo en cierta posición y con cierta dirección: |
||
</div> |
|||
<syntaxhighlight> |
|||
ccircle = Part.makeCircle(10, Base.Vector(10,0,0), Base.Vector(1,0,0)) |
|||
{{Code|code= |
|||
ccircle = Part.makeCircle(10, Base.Vector(10, 0, 0), Base.Vector(1, 0, 0)) |
|||
ccircle.Curve |
ccircle.Curve |
||
> Circle (Radius : 10, Position : (10, 0, 0), Direction : (1, 0, 0)) |
> Circle (Radius : 10, Position : (10, 0, 0), Direction : (1, 0, 0)) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
ccircle se creará a una distancia de 10 en el eje x desde el origen, y estará orientado hacia el eje x. Nota: makeCircle sólo acepta Base.Vector() para posición y normal, pero no admite tuplas. Tambien puedes crear una parte de una circunferencia dando su ángulo de inicio y fin, así: |
ccircle se creará a una distancia de 10 en el eje x desde el origen, y estará orientado hacia el eje x. Nota: makeCircle sólo acepta Base.Vector() para posición y normal, pero no admite tuplas. Tambien puedes crear una parte de una circunferencia dando su ángulo de inicio y fin, así: |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
from math import pi |
from math import pi |
||
arc1 = Part.makeCircle(10, Base.Vector(0,0,0), Base.Vector(0,0,1), 0, 180) |
arc1 = Part.makeCircle(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 180) |
||
arc2 = Part.makeCircle(10, Base.Vector(0,0,0), Base.Vector(0,0,1), 180, 360) |
arc2 = Part.makeCircle(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 180, 360) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Juntando arc1 y arc2 obtendremos una circunferencia. Los ángulos deberán indicarse en grados, si los tienes en radianes simplemente conviertelos según la fórmula: |
Juntando arc1 y arc2 obtendremos una circunferencia. Los ángulos deberán indicarse en grados, si los tienes en radianes simplemente conviertelos según la fórmula: |
||
degrees = radians * 180/PI |
degrees = radians * 180/PI |
||
o usando el módulo Python de matemáticas (después de importarlo, obviamente): |
o usando el módulo Python de matemáticas (después de importarlo, obviamente): |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
import math |
|||
degrees = math.degrees(radians) |
degrees = math.degrees(radians) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de un arco por varios puntos ==== |
==== Creación de un arco por varios puntos ==== |
||
Desafortunadamente no hay ninguna función makeArc pero tenemos la función Part.Arc para crear un arco a lo largo de tres puntos. Básicamente se puede suponer como un arco de unión entre el punto de partida y el punto final, pasando por el punto medio. Part.Arc crea un objeto arco en el que .toShape() tiene que ser llamado para obtener el objeto arista, del mismo modo como utilizamos Part.Line en lugar de Part.makeLine. |
Desafortunadamente no hay ninguna función makeArc pero tenemos la función Part.Arc para crear un arco a lo largo de tres puntos. Básicamente se puede suponer como un arco de unión entre el punto de partida y el punto final, pasando por el punto medio. Part.Arc crea un objeto arco en el que .toShape() tiene que ser llamado para obtener el objeto arista, del mismo modo como utilizamos Part.Line en lugar de Part.makeLine. |
||
</div> |
|||
<syntaxhighlight> |
|||
arc = Part.Arc(Base.Vector(0,0,0),Base.Vector(0,5,0),Base.Vector(5,5,0)) |
|||
Unfortunately there is no {{incode|makeArc()}} function, but we have the {{incode|Part.Arc()}} function to create an arc through three points. It creates an arc object joining the start point to the end point through the middle point. The arc object's {{incode|toShape()}} function must be called to get an edge object, the same as when using {{incode|Part.LineSegment}} instead of {{incode|Part.makeLine}}. |
|||
{{Code|code= |
|||
arc = Part.Arc(Base.Vector(0, 0, 0), Base.Vector(0, 5, 0), Base.Vector(5, 5, 0)) |
|||
arc |
arc |
||
> <Arc object> |
> <Arc object> |
||
arc_edge = arc.toShape() |
arc_edge = arc.toShape() |
||
Part.show(arc_edge) |
|||
</syntaxhighlight> |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
Arc solo acepta puntos como Base.Vector() no acepta tuplas. arc_edge es lo que queremos que podemos mostrar utilizando Part.show(arc_edge). También puedes obtener un arco utilizando una porción de una circunferencia: |
Arc solo acepta puntos como Base.Vector() no acepta tuplas. arc_edge es lo que queremos que podemos mostrar utilizando Part.show(arc_edge). También puedes obtener un arco utilizando una porción de una circunferencia: |
||
arc_edge es lo que queríamos conseguir, y podemos visualizar utilizando Part.show (arc_edge). |
arc_edge es lo que queríamos conseguir, y podemos visualizar utilizando Part.show (arc_edge). |
||
Si desea una pequeña parte de un círculo como un arco, también es posible: |
Si desea una pequeña parte de un círculo como un arco, también es posible: |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
from math import pi |
from math import pi |
||
circle = Part.Circle(Base.Vector(0,0,0),Base.Vector(0,0,1),10) |
circle = Part.Circle(Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 10) |
||
arc = Part.Arc( |
arc = Part.Arc(circle,0,pi) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Los arcos son aristas válidas, como las líneas. Así que también pueden utilizarse en los contornos. |
Los arcos son aristas válidas, como las líneas. Así que también pueden utilizarse en los contornos. |
||
</div> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de un polígono ==== |
==== Creación de un polígono ==== |
||
Un polígono es simplemente un contorno con múltiples aristas rectas. La función makePolygon toma una lista de puntos y crea un contorno a través de dichos puntos: |
Un polígono es simplemente un contorno con múltiples aristas rectas. La función makePolygon toma una lista de puntos y crea un contorno a través de dichos puntos: |
||
</div> |
|||
<syntaxhighlight> |
|||
lshape_wire = Part.makePolygon([Base.Vector(0,5,0),Base.Vector(0,0,0),Base.Vector(5,0,0)]) |
|||
A polygon is simply a wire with multiple straight edges. The {{incode|makePolygon()}} |
|||
</syntaxhighlight> |
|||
function takes a list of points and creates a wire through those points: |
|||
==== Creating a Bezier curve ==== |
|||
Bézier curves are used to model smooth curves using a series of poles (points) and optional weights. The function below makes a Part.BezierCurve from a series of FreeCAD.Vector points. (Note: when "getting" and "setting" a single pole or weight indices start at 1, not 0.) |
|||
{{Code|code= |
|||
<syntaxhighlight> |
|||
lshape_wire = Part.makePolygon([Base.Vector(0, 5, 0), Base.Vector(0, 0, 0), Base.Vector(5, 0, 0)]) |
|||
}} |
|||
[[#top|top]] |
|||
===Create a bézier curve=== |
|||
Bézier curves are used to model smooth curves using a series of poles (points) and optional weights. The function below makes a {{incode|Part.BezierCurve()}} from a series of {{incode|FreeCAD.Vector()}} points. Note: when "getting" and "setting" a single pole or weight, indices start at 1, not 0. |
|||
{{Code|code= |
|||
def makeBCurveEdge(Points): |
def makeBCurveEdge(Points): |
||
geomCurve = Part.BezierCurve() |
geomCurve = Part.BezierCurve() |
||
Line 265: | Line 487: | ||
edge = Part.Edge(geomCurve) |
edge = Part.Edge(geomCurve) |
||
return(edge) |
return(edge) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de un plano ==== |
==== Creación de un plano ==== |
||
Un plano es simplemente una superficie rectangular plana. El método utilizado para crear uno es este: '''makePlane(length,width,[start_pnt,dir_normal])'''. Por defecto start_pnt = Vector(0,0,0) y dir_normal = Vector(0,0,1). Utilizando dir_normal = Vector(0,0,1) crearemos el plano orientado hacia el eje Z, mientras que con dir_normal = Vector(1,0,0) crearemos el plano orientado hacia el eje X: |
Un plano es simplemente una superficie rectangular plana. El método utilizado para crear uno es este: '''makePlane(length,width,[start_pnt,dir_normal])'''. Por defecto start_pnt = Vector(0,0,0) y dir_normal = Vector(0,0,1). Utilizando dir_normal = Vector(0,0,1) crearemos el plano orientado hacia el eje Z, mientras que con dir_normal = Vector(1,0,0) crearemos el plano orientado hacia el eje X: |
||
</div> |
|||
<syntaxhighlight> |
|||
plane = Part.makePlane(2,2) |
|||
A Plane is a flat rectangular surface. The method used to create one is {{incode|makePlane(length, width, [start_pnt, dir_normal])}}. By default start_pnt = Vector(0, 0, 0) and dir_normal = Vector(0, 0, 1). Using dir_normal = Vector(0, 0, 1) will create the plane facing in the positive Z axis direction, while dir_normal = Vector(1, 0, 0) will create the plane facing in the positive X axis direction: |
|||
{{Code|code= |
|||
plane = Part.makePlane(2, 2) |
|||
plane |
plane |
||
><Face object at 028AF990> |
> <Face object at 028AF990> |
||
plane = Part.makePlane(2,2, Base.Vector(3,0,0), Base.Vector(0,1,0)) |
plane = Part.makePlane(2, 2, Base.Vector(3, 0, 0), Base.Vector(0, 1, 0)) |
||
plane.BoundBox |
plane.BoundBox |
||
> BoundBox (3, 0, 0, 5, 0, 2) |
> BoundBox (3, 0, 0, 5, 0, 2) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
BoundBox es un prisma encerrando el plano con una diagonal empezando en (3,0,0) y terminando en (5,0,2). Aquí el espesor de BoundBox en el eje Y es cero, ya que nuestra forma es totalmente plana. |
BoundBox es un prisma encerrando el plano con una diagonal empezando en (3,0,0) y terminando en (5,0,2). Aquí el espesor de BoundBox en el eje Y es cero, ya que nuestra forma es totalmente plana. |
||
</div> |
|||
<div class="mw-translate-fuzzy"> |
|||
Nota: makePlane sólo acepta Base.Vector() para start_pnt y dir_normal pero no tuplas |
Nota: makePlane sólo acepta Base.Vector() para start_pnt y dir_normal pero no tuplas |
||
</div> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de una elipse ==== |
==== Creación de una elipse ==== |
||
Para crear una elipse existen varios métodos: |
Para crear una elipse existen varios métodos: |
||
</div> |
|||
<syntaxhighlight> |
|||
There are several ways to create an ellipse: |
|||
{{Code|code= |
|||
Part.Ellipse() |
Part.Ellipse() |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Crea una elipse cuyo radio mayor es 2 y el radio menor 1 con centro en el (0,0,0) |
Crea una elipse cuyo radio mayor es 2 y el radio menor 1 con centro en el (0,0,0) |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
Part.Ellipse(Ellipse) |
Part.Ellipse(Ellipse) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Crea una copia de la elipse dada |
Crea una copia de la elipse dada |
||
</div> |
|||
<syntaxhighlight> |
|||
Part.Ellipse(S1,S2,Center) |
|||
{{Code|code= |
|||
</syntaxhighlight> |
|||
Part.Ellipse(S1, S2, Center) |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
Crea una elipse centrada en el punto Center, donde el plano de la elipse está definido por Center, S1 y S2, su eje mayor está definido por Center y S1, su radio mayor es la distancia entre Center y S1, |
Crea una elipse centrada en el punto Center, donde el plano de la elipse está definido por Center, S1 y S2, su eje mayor está definido por Center y S1, su radio mayor es la distancia entre Center y S1, |
||
y su radio menor es la distancia entre S2 y el eje mayor. |
y su radio menor es la distancia entre S2 y el eje mayor. |
||
</div> |
|||
<syntaxhighlight> |
|||
Part.Ellipse(Center,MajorRadius,MinorRadius) |
|||
{{Code|code= |
|||
</syntaxhighlight> |
|||
Part.Ellipse(Center, MajorRadius, MinorRadius) |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
Crea una elipse con radios mayor y menor MajorRadius y MinorRadius respectivamente, y ubicada en el plano definido por Center y la normal (0,0,1) |
Crea una elipse con radios mayor y menor MajorRadius y MinorRadius respectivamente, y ubicada en el plano definido por Center y la normal (0,0,1) |
||
</div> |
|||
<syntaxhighlight> |
|||
eli = Part.Ellipse(Base.Vector(10,0,0),Base.Vector(0,5,0),Base.Vector(0,0,0)) |
|||
{{Code|code= |
|||
eli = Part.Ellipse(Base.Vector(10, 0, 0), Base.Vector(0, 5, 0), Base.Vector(0, 0, 0)) |
|||
Part.show(eli.toShape()) |
Part.show(eli.toShape()) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
En el código de arriba hemos pasado S1, S2 y center. De forma similar a Arc, Ellipse también crea un objeto elipse pero no una arista, así que tenemos que convertirlo en una arista utilizando toShape() para mostrarlo. |
En el código de arriba hemos pasado S1, S2 y center. De forma similar a Arc, Ellipse también crea un objeto elipse pero no una arista, así que tenemos que convertirlo en una arista utilizando toShape() para mostrarlo. |
||
</div> |
|||
<div class="mw-translate-fuzzy"> |
|||
Nota: Arc sólo acepta Base.Vector() para puntos pero no tuplas |
Nota: Arc sólo acepta Base.Vector() para puntos pero no tuplas |
||
</div> |
|||
<syntaxhighlight> |
|||
eli = Part.Ellipse(Base.Vector(0,0,0),10,5) |
|||
{{Code|code= |
|||
eli = Part.Ellipse(Base.Vector(0, 0, 0), 10, 5) |
|||
Part.show(eli.toShape()) |
Part.show(eli.toShape()) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
para el constructor de la elipse de arriba hemos pasado el centro, MajorRadius y MinorRadius |
para el constructor de la elipse de arriba hemos pasado el centro, MajorRadius y MinorRadius |
||
</div> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de un toro ==== |
==== Creación de un toro ==== |
||
Utilizando el método '''makeTorus(radius1,radius2,[pnt,dir,angle1,angle2,angle])'''. Por defecto pnt=Vector(0,0,0),dir=Vector(0,0,1),angle1=0,angle2=360 y angle=360. |
Utilizando el método '''makeTorus(radius1,radius2,[pnt,dir,angle1,angle2,angle])'''. Por defecto pnt=Vector(0,0,0),dir=Vector(0,0,1),angle1=0,angle2=360 y angle=360. |
||
Considera un toro como un pequeño circulo barrido a lo largo de una circunferencia grande. Radius1 es el radio de la circunferencia grande, radius2 es el radio del círculo pequeño, pnt es el centro del toro y dir es la dirección normal. angle1 y angle2 son ángulos en radianes para el círculo pequeño, el último parámetro angle es para hacer una sección del toro: |
Considera un toro como un pequeño circulo barrido a lo largo de una circunferencia grande. Radius1 es el radio de la circunferencia grande, radius2 es el radio del círculo pequeño, pnt es el centro del toro y dir es la dirección normal. angle1 y angle2 son ángulos en radianes para el círculo pequeño, el último parámetro angle es para hacer una sección del toro: |
||
</div> |
|||
<syntaxhighlight> |
|||
Using {{incode|makeTorus(radius1, radius2, [pnt, dir, angle1, angle2, angle])}}. |
|||
By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = 0, angle2 = 360 and angle = 360. |
|||
Consider a torus as small circle sweeping along a big circle. Radius1 is the radius of the big circle, radius2 is the radius of the small circle, pnt is the center of the torus and dir is the normal direction. angle1 and angle2 are angles in degrees for the small circle; the last angle parameter is to make a section of the torus: |
|||
{{Code|code= |
|||
torus = Part.makeTorus(10, 2) |
torus = Part.makeTorus(10, 2) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
El código de arriba creará un toro con diámetro 20 (radio 10) y espesor 4 (radio del círculo pequeño 2) |
El código de arriba creará un toro con diámetro 20 (radio 10) y espesor 4 (radio del círculo pequeño 2) |
||
</div> |
|||
<syntaxhighlight> |
|||
tor=Part.makeTorus(10,5,Base.Vector(0,0,0),Base.Vector(0,0,1),0,180) |
|||
{{Code|code= |
|||
</syntaxhighlight> |
|||
tor=Part.makeTorus(10, 5, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 180) |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
El código de arriba creará una sección del toro |
El código de arriba creará una sección del toro |
||
</div> |
|||
<syntaxhighlight> |
|||
tor=Part.makeTorus(10,5,Base.Vector(0,0,0),Base.Vector(0,0,1),0,360,180) |
|||
{{Code|code= |
|||
</syntaxhighlight> |
|||
tor=Part.makeTorus(10, 5, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 360, 180) |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
El código de arriba creará un semi toro, sólo el último parámetro se ha cambiado, dando el valor 180 creará el toro desde 0 hasta 180, eso es, medio toro. |
El código de arriba creará un semi toro, sólo el último parámetro se ha cambiado, dando el valor 180 creará el toro desde 0 hasta 180, eso es, medio toro. |
||
</div> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de un cubo o prisma ==== |
==== Creación de un cubo o prisma ==== |
||
Utilizando '''makeBox(length,width,height,[pnt,dir])'''. Por defecto pnt=Vector(0,0,0) y dir=Vector(0,0,1) |
Utilizando '''makeBox(length,width,height,[pnt,dir])'''. Por defecto pnt=Vector(0,0,0) y dir=Vector(0,0,1) |
||
</div> |
|||
<syntaxhighlight> |
|||
box = Part.makeBox(10,10,10) |
|||
Using {{incode|makeBox(length, width, height, [pnt, dir])}}. |
|||
By default pnt = Vector(0, 0, 0) and dir = Vector(0, 0, 1). |
|||
{{Code|code= |
|||
box = Part.makeBox(10, 10, 10) |
|||
len(box.Vertexes) |
len(box.Vertexes) |
||
> 8 |
> 8 |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de una esfera ==== |
==== Creación de una esfera ==== |
||
Utilizando '''makeSphere(radius,[pnt, dir, angle1,angle2,angle3])'''. Por defecto pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=-90, angle2=90 y angle3=360. angle1 y angle2 son el punto vertical mínimo y máximo de la esfera, angle3 es el diámetro de la esfera. |
Utilizando '''makeSphere(radius,[pnt, dir, angle1,angle2,angle3])'''. Por defecto pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=-90, angle2=90 y angle3=360. angle1 y angle2 son el punto vertical mínimo y máximo de la esfera, angle3 es el diámetro de la esfera. |
||
</div> |
|||
<syntaxhighlight> |
|||
Using {{incode|makeSphere(radius, [pnt, dir, angle1, angle2, angle3])}}. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = -90, angle2 = 90 and angle3 = 360. |
|||
Angle1 and angle2 are the vertical minimum and maximum of the sphere, angle3 is the sphere diameter. |
|||
{{Code|code= |
|||
sphere = Part.makeSphere(10) |
sphere = Part.makeSphere(10) |
||
hemisphere = Part.makeSphere(10,Base.Vector(0,0,0),Base.Vector(0,0,1),-90,90,180) |
hemisphere = Part.makeSphere(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), -90, 90, 180) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de un cilindro ==== |
==== Creación de un cilindro ==== |
||
Utilizando '''makeCylinder(radius,height,[pnt,dir,angle])'''. Por defecto pnt=Vector(0,0,0),dir=Vector(0,0,1) y angle=360 |
Utilizando '''makeCylinder(radius,height,[pnt,dir,angle])'''. Por defecto pnt=Vector(0,0,0),dir=Vector(0,0,1) y angle=360 |
||
</div> |
|||
<syntaxhighlight> |
|||
cylinder = Part.makeCylinder(5,20) |
|||
Using {{incode|makeCylinder(radius, height, [pnt, dir, angle])}}. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360. |
|||
{{Code|code= |
|||
</syntaxhighlight> |
|||
cylinder = Part.makeCylinder(5, 20) |
|||
partCylinder = Part.makeCylinder(5, 20, Base.Vector(20, 0, 0), Base.Vector(0, 0, 1), 180) |
|||
}} |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Creación de un cono ==== |
==== Creación de un cono ==== |
||
Utilizando '''makeCone(radius1,radius2,height,[pnt,dir,angle])'''. Por defecto pnt=Vector(0,0,0), dir=Vector(0,0,1) y angle=360 |
Utilizando '''makeCone(radius1,radius2,height,[pnt,dir,angle])'''. Por defecto pnt=Vector(0,0,0), dir=Vector(0,0,1) y angle=360 |
||
</div> |
|||
<syntaxhighlight> |
|||
cone = Part.makeCone(10,0,20) |
|||
Using {{incode|makeCone(radius1, radius2, height, [pnt, dir, angle])}}. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360. |
|||
{{Code|code= |
|||
</syntaxhighlight> |
|||
cone = Part.makeCone(10, 0, 20) |
|||
semicone = Part.makeCone(10, 0, 20, Base.Vector(20, 0, 0), Base.Vector(0, 0, 1), 180) |
|||
}} |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
== Modificando formas == |
== Modificando formas == |
||
Existen diversos métodos para modificar formas. Algunas son simples operaciones de transformación como mover o rotar formas, otras son más complejas, como la unión y diferencia de una forma y otra. are simple transformation operations |
Existen diversos métodos para modificar formas. Algunas son simples operaciones de transformación como mover o rotar formas, otras son más complejas, como la unión y diferencia de una forma y otra. are simple transformation operations |
||
such as moving or rotating shapes, other are more complex, such as unioning and |
such as moving or rotating shapes, other are more complex, such as unioning and |
||
subtracting one shape from another. Tenlo en cuenta |
subtracting one shape from another. Tenlo en cuenta |
||
</div> |
|||
There are several ways to modify shapes. Some are simple transformation operations such as moving or rotating shapes, others are more complex, such as unioning and subtracting one shape from another. |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
=== Operaciones de transformación === |
=== Operaciones de transformación === |
||
</div> |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Traslación de una forma ==== |
==== Traslación de una forma ==== |
||
Traslación es el acto de mover una forma de una situación a otra. |
Traslación es el acto de mover una forma de una situación a otra. |
||
Cualquier forma (aristas, caras, cubos, etc...) se puede trasladar del mismo modo: |
Cualquier forma (aristas, caras, cubos, etc...) se puede trasladar del mismo modo: |
||
</div> |
|||
<syntaxhighlight> |
|||
myShape = Part.makeBox(2,2,2) |
|||
Translating is the act of moving a shape from one place to another. Any shape (edge, face, cube, etc...) can be translated the same way: |
|||
myShape.translate(Base.Vector(2,0,0)) |
|||
{{Code|code= |
|||
</syntaxhighlight> |
|||
myShape = Part.makeBox(2, 2, 2) |
|||
myShape.translate(Base.Vector(2, 0, 0)) |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
Esto moverá nuestra forma "myShape" 2 unidades en la dirección del eje X. |
Esto moverá nuestra forma "myShape" 2 unidades en la dirección del eje X. |
||
</div> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Rotación de una forma ==== |
==== Rotación de una forma ==== |
||
Para rotar una forma, necesitas especificar el centro de rotación, el eje, y el ángulo de rotación: |
Para rotar una forma, necesitas especificar el centro de rotación, el eje, y el ángulo de rotación: |
||
</div> |
|||
<syntaxhighlight> |
|||
myShape.rotate(Vector(0,0,0),Vector(0,0,1),180) |
|||
To rotate a shape, you need to specify the rotation center, the axis, and the rotation angle: |
|||
</syntaxhighlight> |
|||
{{Code|code= |
|||
myShape.rotate(Base.Vector(0, 0, 0),Base.Vector(0, 0, 1), 180) |
|||
}} |
|||
El código de arriba rotará la forma 180 grados alrededor del eje Z. |
El código de arriba rotará la forma 180 grados alrededor del eje Z. |
||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Transformaciones genéricas con matrices ==== |
==== Transformaciones genéricas con matrices ==== |
||
Una matriz es un modo muy conveniente de almacenar transformaciones en el mundo 3D. En una simple matriz, puedes establecer traslaciones, rotaciones y valores de escala a ser aplicados a un objeto. Por ejemplo: |
Una matriz es un modo muy conveniente de almacenar transformaciones en el mundo 3D. En una simple matriz, puedes establecer traslaciones, rotaciones y valores de escala a ser aplicados a un objeto. Por ejemplo: |
||
</div> |
|||
<syntaxhighlight> |
|||
A matrix is a very convenient way to store transformations in the 3D world. In a single matrix, you can set translation, rotation and scaling values to be applied to an object. For example: |
|||
{{Code|code= |
|||
myMat = Base.Matrix() |
myMat = Base.Matrix() |
||
myMat.move(Base.Vector(2,0,0)) |
myMat.move(Base.Vector(2, 0, 0)) |
||
myMat.rotateZ(math.pi/2) |
myMat.rotateZ(math.pi/2) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Nota: Las matrices de FreeCAD funcionan en radianes. También, casi todas las operaciones de matrices que toman un vector pueden tomar 3 números, así estas dos líneas hacen lo mismo: |
Nota: Las matrices de FreeCAD funcionan en radianes. También, casi todas las operaciones de matrices que toman un vector pueden tomar 3 números, así estas dos líneas hacen lo mismo: |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
myMat.move(2,0,0) |
|||
myMat.move |
myMat.move(2, 0, 0) |
||
myMat.move(Base.Vector(2, 0, 0)) |
|||
</syntaxhighlight> |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
Cuando nuestra matriz es establecida, podemos aplicarla a nuestra forma. FreeCAD proporciona 2 métodos para hacerlo: transformShape() y transformGeometry(). La diferencia es que con el primero, estas seguro de que no ocurrirá ninguna deformación (mira "escalando una forma" más abajo). Podemos aplicar nuestra transformación así: |
Cuando nuestra matriz es establecida, podemos aplicarla a nuestra forma. FreeCAD proporciona 2 métodos para hacerlo: transformShape() y transformGeometry(). La diferencia es que con el primero, estas seguro de que no ocurrirá ninguna deformación (mira "escalando una forma" más abajo). Podemos aplicar nuestra transformación así: |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
myShape.trasformShape(myMat) |
|||
myShape.transformShape(myMat) |
|||
</syntaxhighlight> |
|||
}} |
|||
o |
o |
||
{{Code|code= |
|||
<syntaxhighlight> |
|||
myShape.transformGeometry(myMat) |
myShape.transformGeometry(myMat) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Escalando una forma ==== |
==== Escalando una forma ==== |
||
Escalando una forma es una operación más peligrosa porque, a diferencia de la traslación o rotación, un escalado no uniforme (con diferentes valores para los ejes X,Y y Z) puede modificar la estructura de la forma. Por ejemplo, escalando una circunferencia con un valor horizontal superior al vertical la transformará en una elipse, que se comporta matemáticamente de forma muy diferente. Para el escalado, no podemos utilizar transformShape, tenemos que usar transformGeometry(): |
Escalando una forma es una operación más peligrosa porque, a diferencia de la traslación o rotación, un escalado no uniforme (con diferentes valores para los ejes X,Y y Z) puede modificar la estructura de la forma. Por ejemplo, escalando una circunferencia con un valor horizontal superior al vertical la transformará en una elipse, que se comporta matemáticamente de forma muy diferente. Para el escalado, no podemos utilizar transformShape, tenemos que usar transformGeometry(): |
||
</div> |
|||
<syntaxhighlight> |
|||
Scaling a shape is a more dangerous operation because, unlike translation or rotation, scaling non-uniformly (with different values for X, Y and Z) can modify the structure of the shape. For example, scaling a circle with a higher value horizontally than vertically will transform it into an ellipse, which behaves mathematically very differently. For scaling, we cannot use the {{incode|transformShape()}}, we must use {{incode|transformGeometry()}}: |
|||
{{Code|code= |
|||
myMat = Base.Matrix() |
myMat = Base.Matrix() |
||
myMat.scale(2,1,1) |
myMat.scale(2, 1, 1) |
||
myShape=myShape.transformGeometry(myMat) |
myShape=myShape.transformGeometry(myMat) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
=== Operaciones Booleanas === |
=== Operaciones Booleanas === |
||
</div> |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Diferencia ==== |
==== Diferencia ==== |
||
La diferencia de una forma con otra se llama "corte" en el argot de OCC/FreeCAD y se hace así: |
La diferencia de una forma con otra se llama "corte" en el argot de OCC/FreeCAD y se hace así: |
||
</div> |
|||
<syntaxhighlight> |
|||
cylinder = Part.makeCylinder(3,10,Base.Vector(0,0,0),Base.Vector(1,0,0)) |
|||
Subtracting a shape from another one is called "cut" in FreeCAD and is done like this: |
|||
sphere = Part.makeSphere(5,Base.Vector(5,0,0)) |
|||
{{Code|code= |
|||
cylinder = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0)) |
|||
sphere = Part.makeSphere(5, Base.Vector(5, 0, 0)) |
|||
diff = cylinder.cut(sphere) |
diff = cylinder.cut(sphere) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Intersección ==== |
==== Intersección ==== |
||
del mismo modo, la intersección entre dos formas es denominada "común" y se hace de este modo: |
del mismo modo, la intersección entre dos formas es denominada "común" y se hace de este modo: |
||
</div> |
|||
<syntaxhighlight> |
|||
cylinder1 = Part.makeCylinder(3,10,Base.Vector(0,0,0),Base.Vector(1,0,0)) |
|||
The same way, the intersection between two shapes is called "common" and is done this way: |
|||
cylinder2 = Part.makeCylinder(3,10,Base.Vector(5,0,-5),Base.Vector(0,0,1)) |
|||
{{Code|code= |
|||
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0)) |
|||
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1)) |
|||
common = cylinder1.common(cylinder2) |
common = cylinder1.common(cylinder2) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Unión ==== |
==== Unión ==== |
||
La unión se llama "fusión" y funciona del mismo modo: |
La unión se llama "fusión" y funciona del mismo modo: |
||
</div> |
|||
<syntaxhighlight> |
|||
cylinder1 = Part.makeCylinder(3,10,Base.Vector(0,0,0),Base.Vector(1,0,0)) |
|||
Union is called "fuse" and works the same way: |
|||
cylinder2 = Part.makeCylinder(3,10,Base.Vector(5,0,-5),Base.Vector(0,0,1)) |
|||
{{Code|code= |
|||
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0)) |
|||
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1)) |
|||
fuse = cylinder1.fuse(cylinder2) |
fuse = cylinder1.fuse(cylinder2) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Sección ==== |
==== Sección ==== |
||
Una sección es la intersección entre una forma de un sólido y una forma de un plano. |
Una sección es la intersección entre una forma de un sólido y una forma de un plano. |
||
Devolverá una curva de intersección, un componente con aristas |
Devolverá una curva de intersección, un componente con aristas |
||
</div> |
|||
<syntaxhighlight> |
|||
cylinder1 = Part.makeCylinder(3,10,Base.Vector(0,0,0),Base.Vector(1,0,0)) |
|||
A "section" is the intersection between a solid shape and a plane shape. It will return an intersection curve, a compound curve composed of edges. |
|||
cylinder2 = Part.makeCylinder(3,10,Base.Vector(5,0,-5),Base.Vector(0,0,1)) |
|||
{{Code|code= |
|||
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0)) |
|||
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1)) |
|||
section = cylinder1.section(cylinder2) |
section = cylinder1.section(cylinder2) |
||
section.Wires |
section.Wires |
||
Line 455: | Line 836: | ||
<Edge object at 0D86DE18>, <Edge object at 0D9B8E80>, <Edge object at 012A3640>, |
<Edge object at 0D86DE18>, <Edge object at 0D9B8E80>, <Edge object at 012A3640>, |
||
<Edge object at 0D8F4BB0>] |
<Edge object at 0D8F4BB0>] |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
==== Extrusión ==== |
==== Extrusión ==== |
||
Extrusión es el acto de "empujar" una forma plana en determinada dirección resultando en un cuerpo sólido. Piensa en una círculo convirtiéndose en un tubo: |
Extrusión es el acto de "empujar" una forma plana en determinada dirección resultando en un cuerpo sólido. Piensa en una círculo convirtiéndose en un tubo: |
||
</div> |
|||
<syntaxhighlight> |
|||
Extrusion is the act of "pushing" a flat shape in a certain direction, resulting in a solid body. Think of a circle becoming a tube by "pushing it out": |
|||
{{Code|code= |
|||
circle = Part.makeCircle(10) |
circle = Part.makeCircle(10) |
||
tube = circle.extrude(Base.Vector(0,0,2)) |
tube = circle.extrude(Base.Vector(0, 0, 2)) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Si tu círculo está hueco, obtendrás un tubo hueco. Si no es hueco, obtendrás un cilindro sólido: |
Si tu círculo está hueco, obtendrás un tubo hueco. Si no es hueco, obtendrás un cilindro sólido: |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
wire = Part.Wire(circle) |
wire = Part.Wire(circle) |
||
disc = Part. |
disc = Part.Face(wire) |
||
cylinder = disc.extrude(Base.Vector(0,0,2)) |
cylinder = disc.extrude(Base.Vector(0, 0, 2)) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
== Exploración de formas == |
== Exploración de formas == |
||
Puedes explorar fácilmente la estructura de datos topológicos: |
Puedes explorar fácilmente la estructura de datos topológicos: |
||
</div> |
|||
<syntaxhighlight> |
|||
You can easily explore the topological data structure: |
|||
{{Code|code= |
|||
import Part |
import Part |
||
b = Part.makeBox(100,100,100) |
b = Part.makeBox(100, 100, 100) |
||
b.Wires |
b.Wires |
||
w = b.Wires[0] |
w = b.Wires[0] |
||
Line 486: | Line 881: | ||
v = e.Vertexes[0] |
v = e.Vertexes[0] |
||
v.Point |
v.Point |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Escribiendo las líneas de arriba en el interprete de Python, conseguirás una buena comprensión de la estructura de los objetos de piezas. Aquí, nuestro comando makeBox() crea una forma sólida. Este sólido, como todos los sólidos de piezas, contiene caras. Las caras siempre contienen contornos, que son listas de aristas que bordean la cara. Cada cara tiene al menos un contorno cerrado (puede tener más si la cara tiene huecos). En el contorno, podemos mirar en cada arista de forma separada, y dentro de cada arista, podemos ver los vértices. Las aristas rectas tienen sólo dos vértices, obviamente. |
|||
Escribiendo las líneas de arriba en el interprete de Python, conseguirás una buena comprensión de la estructura de los objetos de piezas. Aquí, nuestro comando makeBox() crea una forma sólida. Este sólido, como todos los sólidos de piezas, contiene caras. Las caras siempre contienen contornos, que son listas de aristas que bordean la cara. Cada cara tiene al menos un contorno cerrado (puede tener más si la cara tiene huecos). En el contorno, podemos mirar en cada arista de forma separada, y dentro de cada arista, podemos ver los vértices. Las aristas rectas tienen sólo dos vértices, obviamente. |
|||
</div> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
=== Análisis de aristas === |
=== Análisis de aristas === |
||
En el caso de una arista con una curva arbitraria, es más probable que quieras hacer una discretización. En FreeCAD las aristas son parametrizadas por sus longitudes. Eso significa que puedes recorrer una arista/curva por su longitud: |
En el caso de una arista con una curva arbitraria, es más probable que quieras hacer una discretización. En FreeCAD las aristas son parametrizadas por sus longitudes. Eso significa que puedes recorrer una arista/curva por su longitud: |
||
</div> |
|||
<syntaxhighlight> |
|||
In case of an edge, which is an arbitrary curve, it's most likely you want to do a discretization. In FreeCAD the edges are parametrized by their lengths. That means you can walk an edge/curve by its length: |
|||
{{Code|code= |
|||
import Part |
import Part |
||
box = Part.makeBox(100,100,100) |
box = Part.makeBox(100, 100, 100) |
||
anEdge = box.Edges[0] |
anEdge = box.Edges[0] |
||
print |
print(anEdge.Length) |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Ahora puedes acceder a un montón de propiedades de la arista utilizando la longitud como una posición. Eso significa que si la arista es de 100mm de longitud el punto inicial es y la posición final es 100. |
|||
Ahora puedes acceder a un montón de propiedades de la arista utilizando la longitud como una posición. Eso significa que si la arista es de 100mm de longitud el punto inicial es y la posición final es 100. |
|||
<syntaxhighlight> |
|||
</div> |
|||
anEdge.tangentAt(0.0) # tangent direction at the beginning |
|||
{{Code|code= |
|||
anEdge.valueAt(0.0) # Point at the beginning |
|||
anEdge. |
anEdge.tangentAt(0.0) # tangent direction at the beginning |
||
anEdge. |
anEdge.valueAt(0.0) # Point at the beginning |
||
anEdge. |
anEdge.valueAt(100.0) # Point at the end of the edge |
||
anEdge. |
anEdge.derivative1At(50.0) # first derivative of the curve in the middle |
||
anEdge.derivative2At(50.0) # second derivative of the curve in the middle |
|||
anEdge.derivative3At(50.0) # third derivative of the curve in the middle |
|||
anEdge.centerOfCurvatureAt(50) # center of the curvature for that position |
anEdge.centerOfCurvatureAt(50) # center of the curvature for that position |
||
anEdge.curvatureAt(50.0) # the curvature |
anEdge.curvatureAt(50.0) # the curvature |
||
anEdge.normalAt(50) # normal vector at that position (if defined) |
anEdge.normalAt(50) # normal vector at that position (if defined) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
=== Utilizando la selección === |
=== Utilizando la selección === |
||
Aquí vemos ahora cómo podemos utilizar la selección que el usuario hizo en la vista. |
Aquí vemos ahora cómo podemos utilizar la selección que el usuario hizo en la vista. |
||
Antes de nada creamos un cubo y lo mostramos en la vista |
Antes de nada creamos un cubo y lo mostramos en la vista |
||
</div> |
|||
<syntaxhighlight> |
|||
Here we see now how we can use a selection the user did in the viewer. |
|||
First of all we create a box and show it in the viewer. |
|||
{{Code|code= |
|||
import Part |
import Part |
||
Part.show(Part.makeBox(100,100,100)) |
Part.show(Part.makeBox(100, 100, 100)) |
||
Gui.SendMsgToActiveView("ViewFit") |
Gui.SendMsgToActiveView("ViewFit") |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Selecciona ahora algunas caras o aristas. Con este archivo de guión puedes iterar todos los objetos seleccionados y sus subelementos: |
Selecciona ahora algunas caras o aristas. Con este archivo de guión puedes iterar todos los objetos seleccionados y sus subelementos: |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
for o in Gui.Selection.getSelectionEx(): |
for o in Gui.Selection.getSelectionEx(): |
||
print(o.ObjectName) |
|||
for s in o.SubElementNames: |
|||
print("name: ", s) |
|||
for s in o.SubObjects: |
|||
print("object: ", s) |
|||
}} |
|||
</syntaxhighlight> |
|||
Selecciona algunas aristas y este archivo de guión calculará la longitud: |
Selecciona algunas aristas y este archivo de guión calculará la longitud: |
||
{{Code|code= |
|||
<syntaxhighlight> |
|||
length = 0.0 |
length = 0.0 |
||
for o in Gui.Selection.getSelectionEx(): |
for o in Gui.Selection.getSelectionEx(): |
||
for s in o.SubObjects: |
|||
length += s.Length |
|||
print "Length of the selected edges:" ,length |
|||
print("Length of the selected edges: ", length) |
|||
</syntaxhighlight> |
|||
}} |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
== Examen completo: La botella OCC == |
== Examen completo: La botella OCC == |
||
Un ejemplo típico encontrado en la [http://www.opencascade.org/org/gettingstarted/appli/ página de primeros pasos de OpenCasCade] es cómo construir una botella. Este es un buen ejercicio también para FreeCAD. En realidad, puedes seguir nuestro ejemplo de abajo y la página de OCC simultáneamente, comprenderás bien cómo están implementadas las estructuras de OCC en FreeCAD. El archivo de guión completo de abajo está también incluido en la instalación de FreeCAD (dentro del directorio Mod/Part) y puede llamarse desde el interprete de Python escribiendo: |
Un ejemplo típico encontrado en la [http://www.opencascade.org/org/gettingstarted/appli/ página de primeros pasos de OpenCasCade] es cómo construir una botella. Este es un buen ejercicio también para FreeCAD. En realidad, puedes seguir nuestro ejemplo de abajo y la página de OCC simultáneamente, comprenderás bien cómo están implementadas las estructuras de OCC en FreeCAD. El archivo de guión completo de abajo está también incluido en la instalación de FreeCAD (dentro del directorio Mod/Part) y puede llamarse desde el interprete de Python escribiendo: |
||
</div> |
|||
<syntaxhighlight> |
|||
A typical example found on the [https://www.opencascade.com/doc/occt-6.9.0/overview/html/occt__tutorial.html OpenCasCade Technology website] is how to build a bottle. This is a good exercise for FreeCAD too. In fact, if you follow our example below and the OCC page simultaneously, you will see how well OCC structures are implemented in FreeCAD. The script is included in the FreeCAD installation (inside the {{FileName|Mod/Part}} folder) and can be called from the Python interpreter by typing: |
|||
{{Code|code= |
|||
import Part |
import Part |
||
import MakeBottle |
import MakeBottle |
||
bottle = MakeBottle.makeBottle() |
bottle = MakeBottle.makeBottle() |
||
Part.show(bottle) |
Part.show(bottle) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
<div class="mw-translate-fuzzy"> |
|||
=== El archivo de guión completo === |
=== El archivo de guión completo === |
||
Aquí está el archivo de guión completo MakeBottle: |
Aquí está el archivo de guión completo MakeBottle: |
||
</div> |
|||
<syntaxhighlight> |
|||
import Part, FreeCAD, math |
|||
For the purpose of this tutorial we will consider a reduced version of the script. In this version the bottle will not be hollowed out, and the neck of the bottle will not be threaded. |
|||
{{Code|code= |
|||
import Part, math |
|||
from FreeCAD import Base |
from FreeCAD import Base |
||
def |
def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0): |
||
aPnt1=Base.Vector(-myWidth/2.,0,0) |
aPnt1=Base.Vector(-myWidth / 2., 0, 0) |
||
aPnt2=Base.Vector(-myWidth/2.,-myThickness/4.,0) |
aPnt2=Base.Vector(-myWidth / 2., -myThickness / 4., 0) |
||
aPnt3=Base.Vector(0,-myThickness/2.,0) |
aPnt3=Base.Vector(0, -myThickness / 2., 0) |
||
aPnt4=Base.Vector(myWidth/2.,-myThickness/4.,0) |
aPnt4=Base.Vector(myWidth / 2., -myThickness / 4., 0) |
||
aPnt5=Base.Vector(myWidth/2.,0,0) |
aPnt5=Base.Vector(myWidth / 2., 0, 0) |
||
aArcOfCircle = Part.Arc(aPnt2,aPnt3,aPnt4) |
aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4) |
||
aSegment1=Part. |
aSegment1=Part.LineSegment(aPnt1, aPnt2) |
||
aSegment2=Part. |
aSegment2=Part.LineSegment(aPnt4, aPnt5) |
||
aEdge1=aSegment1.toShape() |
|||
aEdge1=aSegment1.toShape() |
|||
aEdge2=aArcOfCircle.toShape() |
|||
aEdge3=aSegment2.toShape() |
|||
aWire=Part.Wire([aEdge1,aEdge2,aEdge3]) |
|||
aWire=Part.Wire([aEdge1, aEdge2, aEdge3]) |
|||
aTrsf=Base.Matrix() |
|||
aTrsf=Base.Matrix() |
|||
aTrsf.rotateZ(math.pi) # rotate around the z-axis |
|||
aTrsf.rotateZ(math.pi) # rotate around the z-axis |
|||
aMirroredWire=aWire.transformGeometry(aTrsf) |
|||
aMirroredWire=aWire.copy() |
|||
myWireProfile=Part.Wire([aWire,aMirroredWire]) |
|||
aMirroredWire.transformShape(aTrsf) |
|||
myFaceProfile=Part.Face(myWireProfile) |
|||
myWireProfile=Part.Wire([aWire, aMirroredWire]) |
|||
aPrismVec=Base.Vector(0,0,myHeight) |
|||
myBody=myFaceProfile.extrude(aPrismVec) |
|||
myFaceProfile=Part.Face(myWireProfile) |
|||
myBody=myBody.makeFillet(myThickness/12.0,myBody.Edges) |
|||
aPrismVec=Base.Vector(0, 0, myHeight) |
|||
myBody=myFaceProfile.extrude(aPrismVec) |
|||
neckNormal=Base.Vector(0,0,1) |
|||
myNeckRadius = myThickness / 4. |
|||
myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges) |
|||
myNeckHeight = myHeight / 10 |
|||
myNeck = Part.makeCylinder(myNeckRadius,myNeckHeight,neckLocation,neckNormal) |
|||
neckLocation=Base.Vector(0, 0, myHeight) |
|||
myBody = myBody.fuse(myNeck) |
|||
neckNormal=Base.Vector(0, 0, 1) |
|||
faceToRemove = 0 |
|||
myNeckRadius = myThickness / 4. |
|||
zMax = -1.0 |
|||
myNeckHeight = myHeight / 10. |
|||
myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal) |
|||
for xp in myBody.Faces: |
|||
myBody = myBody.fuse(myNeck) |
|||
try: |
|||
surf = xp.Surface |
|||
return myBody |
|||
if type(surf) == Part.Plane: |
|||
z = surf.Position.z |
|||
el = makeBottleTut() |
|||
if z > zMax: |
|||
Part.show(el) |
|||
zMax = z |
|||
}} |
|||
faceToRemove = xp |
|||
[[#top|top]] |
|||
except: |
|||
continue |
|||
<div class="mw-translate-fuzzy"> |
|||
myBody = myBody.makeThickness([faceToRemove],-myThickness/50 , 1.e-3) |
|||
return myBody |
|||
</syntaxhighlight> |
|||
=== Explicación detallada === |
=== Explicación detallada === |
||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
import Part, FreeCAD, math |
|||
import Part, math |
|||
from FreeCAD import Base |
from FreeCAD import Base |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
We will need,of course, the Part module, but also the FreeCAD.Base module, |
|||
Necesitaremos, desde luego, el módulo de piezas, pero también el módulo FreeCAD.Base, que contiene estructuras básicas de FreeCAD como vectores y matrices. |
|||
which contains basic FreeCAD structures like vectors and matrixes. |
|||
</div> |
|||
<syntaxhighlight> |
|||
def makeBottle(myWidth=50.0, myHeight=70.0, myThickness=30.0): |
|||
{{Code|code= |
|||
aPnt1=Base.Vector(-myWidth/2.,0,0) |
|||
def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0): |
|||
aPnt1=Base.Vector(-myWidth / 2., 0, 0) |
|||
aPnt2=Base.Vector(-myWidth / 2., -myThickness / 4., 0) |
|||
aPnt3=Base.Vector(0, -myThickness / 2., 0) |
|||
aPnt4=Base.Vector(myWidth / 2., -myThickness / 4., 0) |
|||
</syntaxhighlight> |
|||
aPnt5=Base.Vector(myWidth / 2., 0, 0) |
|||
Here we define our makeBottle function. This function can be called without |
|||
}} |
|||
arguments, like we did above, in which case default values for width, height, |
|||
and thickness will be used. Then, we define a couple of points that will be used |
|||
<div class="mw-translate-fuzzy"> |
|||
for building our base profile. |
|||
Aquí definimos nuestra función makeBottle. Esta función se puede llamar sin argumentos, como hicimos arriba, en cuyo caso se utilizaran los valores por defecto para ancho, alto, y espesor. Luego, definimos un conjunto de puntos que serán utilizados para construir nuestro perfil base. |
|||
<syntaxhighlight> |
|||
</div> |
|||
aArcOfCircle = Part.Arc(aPnt2,aPnt3,aPnt4) |
|||
aSegment1=Part.Line(aPnt1,aPnt2) |
|||
{{Code|code= |
|||
aSegment2=Part.Line(aPnt4,aPnt5) |
|||
... |
|||
</syntaxhighlight> |
|||
aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4) |
|||
Here we actually define the geometry: an arc, made of 3 points, and two |
|||
aSegment1=Part.LineSegment(aPnt1, aPnt2) |
|||
line segments, made of 2 points. |
|||
aSegment2=Part.LineSegment(aPnt4, aPnt5) |
|||
<syntaxhighlight> |
|||
}} |
|||
aEdge1=aSegment1.toShape() |
|||
aEdge2=aArcOfCircle.toShape() |
|||
<div class="mw-translate-fuzzy"> |
|||
aEdge3=aSegment2.toShape() |
|||
Aquí en realidad definimos la geometría: un arco, creado por 3 puntos, y dos segmentos de línea, creados por 2 puntos. |
|||
aWire=Part.Wire([aEdge1,aEdge2,aEdge3]) |
|||
</div> |
|||
</syntaxhighlight> |
|||
Remember the difference between geometry and shapes? Here we build |
|||
{{Code|code= |
|||
shapes out of our construction geometry. 3 edges (edges can be straight |
|||
... |
|||
or curved), then a wire made of those three edges. |
|||
aEdge1=aSegment1.toShape() |
|||
<syntaxhighlight> |
|||
aEdge2=aArcOfCircle.toShape() |
|||
aEdge3=aSegment2.toShape() |
|||
aTrsf.rotateZ(math.pi) # rotate around the z-axis |
|||
aWire=Part.Wire([aEdge1, aEdge2, aEdge3]) |
|||
aMirroredWire=aWire.transformGeometry(aTrsf) |
|||
}} |
|||
myWireProfile=Part.Wire([aWire,aMirroredWire]) |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Until now we built only a half profile. Easier than building the whole profile |
|||
Recuerdas la diferencia entre geometría y formas? Aquí construimos formas a partir de nuestra geometría de construcción. 3 aristas (las aristas pueden ser rectas o curvas), luego un contorno creado a partir de dichas tres aristas. |
|||
the same way, we can just mirror what we did, and glue both halfs together. |
|||
</div> |
|||
So we first create a matrix. A matrix is a very common way to apply transformations |
|||
to objects in the 3D world, since it can contain in one structure all basic |
|||
{{Code|code= |
|||
transformations that 3D objects can suffer (move, rotate and scale). Here, |
|||
... |
|||
after we create the matrix, we mirror it, and we create a copy of our wire |
|||
aTrsf=Base.Matrix() |
|||
with that transformation matrix applied to it. We now have two wires, and |
|||
aTrsf.rotateZ(math.pi) # rotate around the z-axis |
|||
we can make a third wire out of them, since wires are actually lists of edges. |
|||
<syntaxhighlight> |
|||
aMirroredWire=aWire.copy() |
|||
myFaceProfile=Part.Face(myWireProfile) |
|||
aMirroredWire.transformShape(aTrsf) |
|||
aPrismVec=Base.Vector(0,0,myHeight) |
|||
myWireProfile=Part.Wire([aWire, aMirroredWire]) |
|||
myBody=myFaceProfile.extrude(aPrismVec) |
|||
}} |
|||
myBody=myBody.makeFillet(myThickness/12.0,myBody.Edges) |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
Now that we have a closed wire, it can be turned into a face. Once we have a face, |
|||
Hasta ahora construimos sólo medio perfil. Más sencillo que construir el perfil completo del mismo modo, simplemente podemos crear una simetría de lo que hicimos, y pegar ambas partes. Así primero creamos una matriz. Una matriz es un modo muy común para aplicar transformaciones a objetos en el mundo 3D, ya que puede contener en una estructura todas las transformaciones básicas que los objetos pueden sufrir (mover, rotar y escalar). Aquí, después de crear la matriz, creamos una simétrica, y creamos una copia de nuestro contorno con esa matriz de transformación aplicada. Ahora tenemos 2 contornos, y podemos crear un tercer contorno a partir de ellos, ya que los contornos son en realidad listas de aristas. |
|||
we can extrude it. Doing so, we actually made a solid. Then we apply a nice little |
|||
</div> |
|||
fillet to our object because we care about good design, don't we? |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
neckLocation=Base.Vector(0,0,myHeight) |
|||
... |
|||
neckNormal=Base.Vector(0,0,1) |
|||
myFaceProfile=Part.Face(myWireProfile) |
|||
myNeckRadius = myThickness / 4. |
|||
aPrismVec=Base.Vector(0, 0, myHeight) |
|||
myBody=myFaceProfile.extrude(aPrismVec) |
|||
myNeck = Part.makeCylinder(myNeckRadius,myNeckHeight,neckLocation,neckNormal) |
|||
</syntaxhighlight> |
|||
myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges) |
|||
Then, the body of our bottle is made, we still need to create a neck. So we |
|||
}} |
|||
make a new solid, with a cylinder. |
|||
<syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
myBody = myBody.fuse(myNeck) |
|||
Ahora que tenemos un contorno cerrado, se puede convertir en una cara. Una vez que tengamos una cara, podemos extruirla. Haciendo esto, creamos un sólido. Entonces aplicamos un pequeño redondeo a nuestro objeto porque queremos crear un buen diseño, no es así? |
|||
</syntaxhighlight> |
|||
</div> |
|||
The fuse operation, which in other apps is sometimes called union, is very |
|||
{{Code|code= |
|||
powerful. It will take care of gluing what needs to be glued and remove parts that |
|||
... |
|||
neckLocation=Base.Vector(0, 0, myHeight) |
|||
<syntaxhighlight> |
|||
neckNormal=Base.Vector(0, 0, 1) |
|||
return myBody |
|||
</syntaxhighlight> |
|||
myNeckRadius = myThickness / 4. |
|||
Then, we return our Part solid as the result of our function. That Part solid, |
|||
myNeckHeight = myHeight / 10. |
|||
like any other Part shape, can be attributed to an object in a FreeCAD document, with: |
|||
myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal) |
|||
<syntaxhighlight> |
|||
}} |
|||
myObject = FreeCAD.ActiveDocument.addObject("Part::Feature","myObject") |
|||
<div class="mw-translate-fuzzy"> |
|||
myObject.Shape = bottle |
|||
El cuerpo de nuestra botella está creado, aún tenemos que crear el cuello. Así que creamos un nuevo sólido, con un cilindro. |
|||
</syntaxhighlight> |
|||
</div> |
|||
or, more simple: |
|||
{{Code|code= |
|||
<syntaxhighlight> |
|||
... |
|||
Part.show(bottle) |
|||
myBody = myBody.fuse(myNeck) |
|||
</syntaxhighlight> |
|||
}} |
|||
==Box pierced== |
|||
<div class="mw-translate-fuzzy"> |
|||
Here a complete example of building a box pierced. |
|||
La operación de fusión, que en otras aplicaciones es llamada unión, es muy potente. Tendrá cuidado de pegar lo que necesita ser pegado y eliminar las partes que necesiten ser eliminadas. |
|||
</div> |
|||
{{Code|code= |
|||
... |
|||
return myBody |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
Obtenemos nuestro sólido de pieza como resultado de nuestra función. Ese sólido de pieza, como cualquier otra forma de piezas, se puede atribuir a un objeto en el documento de FreeCAD, con: |
|||
</div> |
|||
{{Code|code= |
|||
el = makeBottleTut() |
|||
Part.show(el) |
|||
}} |
|||
<div class="mw-translate-fuzzy"> |
|||
o, de forma más simple: |
|||
</div> |
|||
[[#top|top]] |
|||
==Example: Pierced box== |
|||
Here is a complete example of building a pierced box. |
|||
The construction is done side |
The construction is done one side at a time. When the cube is finished, it is hollowed out by cutting a cylinder through it. |
||
{{Code|code= |
|||
<syntaxhighlight> |
|||
import |
import Part, math |
||
from math import sqrt, pi, sin, cos, asin |
|||
from FreeCAD import Base |
from FreeCAD import Base |
||
size = 10 |
size = 10 |
||
poly = Part.makePolygon( |
poly = Part.makePolygon([(0, 0, 0), (size, 0, 0), (size, 0, size), (0, 0, size), (0, 0, 0)]) |
||
face1 = Part.Face(poly) |
face1 = Part.Face(poly) |
||
Line 706: | Line 1,151: | ||
face6 = Part.Face(poly) |
face6 = Part.Face(poly) |
||
myMat = |
myMat = Base.Matrix() |
||
myMat.rotateZ(math.pi/2) |
|||
myMat.rotateZ(math.pi / 2) |
|||
face2.transformShape(myMat) |
face2.transformShape(myMat) |
||
face2.translate( |
face2.translate(Base.Vector(size, 0, 0)) |
||
myMat.rotateZ(math.pi/2) |
myMat.rotateZ(math.pi / 2) |
||
face3.transformShape(myMat) |
face3.transformShape(myMat) |
||
face3.translate( |
face3.translate(Base.Vector(size, size, 0)) |
||
myMat.rotateZ(math.pi/2) |
myMat.rotateZ(math.pi / 2) |
||
face4.transformShape(myMat) |
face4.transformShape(myMat) |
||
face4.translate( |
face4.translate(Base.Vector(0, size, 0)) |
||
myMat = Base.Matrix() |
|||
myMat |
myMat.rotateX(-math.pi / 2) |
||
myMat.rotateX(-math.pi/2) |
|||
face5.transformShape(myMat) |
face5.transformShape(myMat) |
||
face6.transformShape(myMat) |
face6.transformShape(myMat) |
||
face6.translate( |
face6.translate(Base.Vector(0, 0, size)) |
||
myShell = Part.makeShell([face1,face2,face3,face4,face5,face6]) |
|||
myShell = Part.makeShell([face1, face2, face3, face4, face5, face6]) |
|||
mySolid = Part.makeSolid(myShell) |
mySolid = Part.makeSolid(myShell) |
||
mySolidRev = mySolid.copy() |
|||
mySolidRev.reverse() |
|||
myCyl = Part.makeCylinder(2,20) |
myCyl = Part.makeCylinder(2, 20) |
||
myCyl.translate( |
myCyl.translate(Base.Vector(size / 2, size / 2, 0)) |
||
cut_part = |
cut_part = mySolid.cut(myCyl) |
||
Part.show(cut_part) |
Part.show(cut_part) |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
== Loading and Saving == |
|||
There are several ways to save your work in the Part module. You can |
|||
of course save your FreeCAD document, but you can also save Part |
|||
objects directly to common CAD formats, such as BREP, IGS, STEP and STL. |
|||
<div class="mw-translate-fuzzy"> |
|||
Saving a shape to a file is easy. There are exportBrep(), exportIges(), |
|||
== Cargando y guardando == |
|||
exportStl() and exportStep() methods availables for all shape objects. |
|||
So, doing: |
|||
Existen diversas formas de guardar tu trabajo en el módulo de piezas. Puedes desde luego guardar el documento de FreeCAD, pero también puedes guardar objetos de pieza directamente en formatos de CAD comunes, como BREP, IGS, STEP y STL. |
|||
<syntaxhighlight> |
|||
</div> |
|||
There are several ways to save your work. You can of course save your FreeCAD document, but you can also save [[Part_workbench|Part]] objects directly to common CAD formats, such as BREP, IGS, STEP and STL. |
|||
<div class="mw-translate-fuzzy"> |
|||
El guardado de una forma en un archivo es sencillo. Existen los métodos exportBrep(), exportIges(), exportStl() y exportStep() disponibles para todos los objetos de forma. Así, haciendo: |
|||
</div> |
|||
{{Code|code= |
|||
import Part |
import Part |
||
s = Part.makeBox( |
s = Part.makeBox(10, 10, 10) |
||
s.exportStep("test.stp") |
s.exportStep("test.stp") |
||
}} |
|||
</syntaxhighlight> |
|||
<div class="mw-translate-fuzzy"> |
|||
this will save our box into a STEP file. To load a BREP, |
|||
se guardará nuestro cubo en un archivo STEP. Para cargar un archivo BREP, IGES o STEP, simplemente haz lo contrario: |
|||
IGES or STEP file, simply do the contrary: |
|||
</div> |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
import Part |
import Part |
||
s = Part.Shape() |
s = Part.Shape() |
||
s.read("test.stp") |
s.read("test.stp") |
||
}} |
|||
</syntaxhighlight> |
|||
To convert |
To convert a STEP file to an IGS file: |
||
{{Code|code= |
|||
<syntaxhighlight> |
|||
import Part |
import Part |
||
s = Part.Shape() |
s = Part.Shape() |
||
s.read("file.stp") # incoming file igs, stp, stl, brep |
s.read("file.stp") # incoming file igs, stp, stl, brep |
||
s.exportIges("file.igs") # outbound file igs |
s.exportIges("file.igs") # outbound file igs |
||
}} |
|||
</syntaxhighlight> |
|||
[[#top|top]] |
|||
Note that importing or opening BREP, IGES or STEP files can also be done |
|||
directly from the File -> Open or File -> Import menu, while exporting |
|||
is with File -> Export |
|||
{{docnav|Mesh Scripting|Mesh to Part}} |
|||
[[Category:Poweruser Documentation]] |
|||
[[Category:Python Code]] |
|||
[[Category:Tutorials]] |
|||
{{Powerdocnavi{{#translation:}}}} |
|||
[[Category:Developer Documentation{{#translation:}}]] |
|||
[[Category:Python Code{{#translation:}}]] |
|||
{{clear}} |
{{clear}} |
||
<languages/> |
Revision as of 17:08, 11 October 2020
Introducción
Aquí le explicamos cómo controlar el Módulo de Pieza directamente desde el intérprete de Python de FreeCAD, o desde cualquier archivo de guión externo. Asegúrate de navegar por la sección Archivos de guión y las páginas Conceptos básicos de archivos de guión en FreeCAD si necesitas más información acerca de cómo funcionan los archivos de guión de Python en FreeCAD.
Here we will explain to you how to control the Part Module directly from the FreeCAD Python interpreter, or from any external script. Be sure to browse the Scripting section and the FreeCAD Scripting Basics pages if you need more information about how Python scripting works in FreeCAD. If you are new to Python, it is a good idea to first read the Introduction to Python.
See also
Diagrama de clases
Ésta es una descripción Lenguaje Unificado de Modelado (UML) de las clases más importante del módulo de Pieza:
This is a Unified Modeling Language (UML) overview of the most important classes of the Part module:
Geometría
Los objetos geométricos son la piedra angular de todos los objetos topológicos:
- geom clase base de los objetos geométricos
- line Una línea recta en 3D, definido por el punto de inicio y el punto final
- circle Círculo o segmento de círculo definido por un punto centro y los puntos de inicio y final
- ...... Y en breve más cosas
The geometric objects are the building blocks of all topological objects:
- Geom Base class of the geometric objects.
- Line A straight line in 3D, defined by starting point and end point.
- Circle Circle or circle segment defined by a center point and start and end point.
- ...... Etc.
Topología
Los siguientes tipos de datos topológicos están disponibles:
- compound Un grupo de cualquier tipo de objetos topológicos.
- compsolid Un sólido compuesto es un grupo de sólidos concetados por sus caras. Es una extensión de las nociones de WIRE y SHELL en el ámbito de los sólido.
- solid Una región del espacio limitada por shells. Es tridimensional.
- shell Un conjunto de caras conectadas por sus bordes. Una shell puede ser abierta o cerrada.
- face En 2D es parte de un plano; en 3D es parte de una superficie. Su geometría está limitada por sus contornos. Es un ente bidimensional.
- wire Un grupo de bordes conectados por sus vértices. Puede tener un contorno abierto o cerrado, dependiendo que que sus bordes estén o no conectados.
- edge Un elemento topológico que corresponde a una curva limitada. Un borde está normalmente limitado por vértices. Es un ente unidimensional.
- vertex Un elemento topológico que se corresponde con un punto. Tiene dimensión cero.
- shape Un concepto genérico que abarca todos los anteriores.
The following topological data types are available:
- Compound A group of any type of topological objects.
- Compsolid A composite solid is a set of solids connected by their faces. It expands the notions of WIRE and SHELL to solids.
- Solid A part of space limited by shells. It is three dimensional.
- Shell A set of faces connected by their edges. A shell can be open or closed.
- Face In 2D it is part of a plane; in 3D it is part of a surface. Its geometry is constrained (trimmed) by contours. It is two dimensional.
- Wire A set of edges connected by their vertices. It can be an open or closed contour depending on whether the edges are linked or not.
- Edge A topological element corresponding to a restrained curve. An edge is generally limited by vertices. It has one dimension.
- Vertex A topological element corresponding to a point. It has zero dimension.
- Shape A generic term covering all of the above.
Ejemplo rápido: Creación de topologías básicas
Crearemos ahora una topología por construcción de geometría simple. Como un caso de estudio utilizaremos una pieza como se puede ver en la imagen que consiste en cuatro vértices, dos circunferencias y dos líneas.
Creación de geometría
Primero tenemos que crear las distintas partes de la geometría de este contorno. Y tenemos que tener cuidado de que los vértices de las partes de la geometría están en la misma posición. De otro modo después podríamos no ser capaces de conectar las partes de la geometría en una topología!
First we create the distinct geometric parts of this wire. Making sure that parts that have to be connected later share the same vertices.
Así que primero creamos los puntos:
import Part
from FreeCAD import Base
V1 = Base.Vector(0, 10, 0)
V2 = Base.Vector(30, 10, 0)
V3 = Base.Vector(30, -10, 0)
V4 = Base.Vector(0, -10, 0)
Arco
Para crear un arco de circunferencia crearemos puntos de ayuda y crearemos el arco a través de tres puntos:
VC1 = Base.Vector(-10, 0, 0)
C1 = Part.Arc(V1, VC1, V4)
VC2 = Base.Vector(40, 0, 0)
C2 = Part.Arc(V2, VC2, V3)
Línea
La línea puede crearse de forma muy simple a partir de los puntos:
L1 = Part.LineSegment(V1, V2)
L2 = Part.LineSegment(V3, V4)
Poniendo todo junto
El último paso es poner los elementos base de la geometría juntos y formar una forma topológica:
The last step is to put the geometric base elements together and bake a topological shape:
S1 = Part.Shape([C1, L1, C2, L2])
Crear un prisma
Ahora extruir el contorno en una dirección y crear una forma 3D real:
Now extrude the wire in a direction and make an actual 3D shape:
W = Part.Wire(S1.Edges)
P = W.extrude(Base.Vector(0, 0, 10))
Mostrar todo
Part.show(P)
Creación de formas básicas
Puedes crear fácilmente objetos topológicos simples con los métodos "make...()" del Módulo Parte:
You can easily create basic topological objects with the make...()
methods from the Part Module:
b = Part.makeBox(100, 100, 100)
Part.show(b)
Otros métodos make...() disponibles:
- makeBox(l,w,h) -- construye una caja ubicada en p y apuntando en la dirección d con las dimensiones (l, w, h).
- makeCircle(radius) -- Hace un círculo con un radio dado.
- makeCone(radius1,radius2,height) -- Hace un cono con un radio y altura dados.
- makeCylinder(radius,height) -- Hace un cilindro con un radio y altura dados.
- makeLine((x1,y1,z1),(x2,y2,z2)) -- Hace una línea entre 2 puntos
- makePlane(length,width) -- Hace un plano con longitud y anchura dados.
- makePolygon(list) -- Hace un polígono con una lista de puntos
- makeSphere(radius) -- Hace una esfera con un radio dado.
- makeTorus(radius1,radius2) -- Hace un toro con sus radios dados.
Mira la página APIde piezas para una lista completa de los métodos disponibles del módulo de pieza.
Importing the needed modules
Primero tenemos que importar el módulo de piezas así podremos utilizar su contenido en Python. También importamos el módulo base desde dentro del módulo de FreeCAD:
First we need to import the Part module so we can use its contents in Python. We'll also import the Base module from inside the FreeCAD module:
import Part
from FreeCAD import Base
Creación de un Vector
Los Vectores son una de las piezas de información más importantes cuando se construyen formas. Contienen 3 números normalmente (pero no necesariamente siempre) las coordenadas cartesianas X, Y y Z. Puedes crear un vector así:
Vectors are one of the most important pieces of information when building shapes. They usually contain three numbers (but not necessarily always): the X, Y and Z cartesian coordinates. You create a vector like this:
myVector = Base.Vector(3, 2, 0)
Simplemente creamos un vector en las coordenadas X=3, Y=2, Z=0. En el módulo de pieza, los vectores se utilizan en todas partes. Las formas de las piezas también utilizan otro tipo de representaciones de punto, llamada Vértice, el cual en realidad no es más que un contenedor para un vector. Puedes acceder al vector de un vértice así:
myVertex = myShape.Vertexes[0]
print(myVertex.Point)
> Vector (3, 2, 0)
Creación de una arista
Un borde no es otra cosa mas que una linea entre dos vértices:
An edge is nothing but a line with two vertices:
edge = Part.makeLine((0, 0, 0), (10, 0, 0))
edge.Vertexes
> [<Vertex object at 01877430>, <Vertex object at 014888E0>]
Nota: También puedes crear una arista pasándole dos vértices.
vec1 = Base.Vector(0, 0, 0)
vec2 = Base.Vector(10, 0, 0)
line = Part.LineSegment(vec1, vec2)
edge = line.toShape()
Se puede determinar la longitud y el centro de un borde así:
edge.Length
> 10.0
edge.CenterOfMass
> Vector (5, 0, 0)
Poniendo la forma en la pantalla
Hemos creado un objeto arista, pero no aparece en ninguna parte de la pantalla. Esto es porque simplemente manejamos objetos de Python aquí. La escena 3D de FreeCAD sólo muestra lo que le digas que se muestre. Para hacerlo, utilizamos este simple método:
So far we created an edge object, but it doesn't appear anywhere on the screen. This is because the FreeCAD 3D scene only displays what you tell it to display. To do that, we use this simple method:
Part.show(edge)
Un objeto se creará en nuestro documento de FreeCAD, y nuestra forma "edge" será atribuido a él. Utiliza esto si es momento para mostrar tu creación en la pantalla.
Creación de un contorno
Un contorno es una línea de múltiples aristas y se puede crear a partir de una lista de aristas, o incluso de una lista de contornos:
A wire is a multi-edge line and can be created from a list of edges or even a list of wires:
edge1 = Part.makeLine((0, 0, 0), (10, 0, 0))
edge2 = Part.makeLine((10, 0, 0), (10, 10, 0))
wire1 = Part.Wire([edge1, edge2])
edge3 = Part.makeLine((10, 10, 0), (0, 10, 0))
edge4 = Part.makeLine((0, 10, 0), (0, 0, 0))
wire2 = Part.Wire([edge3, edge4])
wire3 = Part.Wire([wire1, wire2])
wire3.Edges
> [<Edge object at 016695F8>, <Edge object at 0197AED8>, <Edge object at 01828B20>, <Edge object at 0190A788>]
Part.show(wire3)
Part.show(wire3) mostrará las 4 aristas que componen nuestro contorno. Otra información útil se puede recuperar fácilmente:
wire3.Length
> 40.0
wire3.CenterOfMass
> Vector (5, 5, 0)
wire3.isClosed()
> True
wire2.isClosed()
> False
Creación de una cara
Sólo serán válidas las caras creadas a partir de contornos cerrados. En este ejemplo, wire3 es un contorno cerrado pero wire2 no es un contorno cerrado (mira más arriba)
Only faces created from closed wires will be valid. In this example, wire3 is a closed wire but wire2 is not (see above):
face = Part.Face(wire3)
face.Area
> 99.99999999999999
face.CenterOfMass
> Vector (5, 5, 0)
face.Length
> 40.0
face.isValid()
> True
sface = Part.Face(wire2)
sface.isValid()
> False
Sólo las caras tendrán un área, ni los contornos ni las aristas.
Creación de una circunferencia
Una circunferencia se puede crear de forma tan simple como esta:
A circle can be created like this:
circle = Part.makeCircle(10)
circle.Curve
> Circle (Radius : 10, Position : (0, 0, 0), Direction : (0, 0, 1))
Si deseas crearlo en cierta posición y con cierta dirección:
ccircle = Part.makeCircle(10, Base.Vector(10, 0, 0), Base.Vector(1, 0, 0))
ccircle.Curve
> Circle (Radius : 10, Position : (10, 0, 0), Direction : (1, 0, 0))
ccircle se creará a una distancia de 10 en el eje x desde el origen, y estará orientado hacia el eje x. Nota: makeCircle sólo acepta Base.Vector() para posición y normal, pero no admite tuplas. Tambien puedes crear una parte de una circunferencia dando su ángulo de inicio y fin, así:
from math import pi
arc1 = Part.makeCircle(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 180)
arc2 = Part.makeCircle(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 180, 360)
Juntando arc1 y arc2 obtendremos una circunferencia. Los ángulos deberán indicarse en grados, si los tienes en radianes simplemente conviertelos según la fórmula: degrees = radians * 180/PI o usando el módulo Python de matemáticas (después de importarlo, obviamente):
import math
degrees = math.degrees(radians)
Creación de un arco por varios puntos
Desafortunadamente no hay ninguna función makeArc pero tenemos la función Part.Arc para crear un arco a lo largo de tres puntos. Básicamente se puede suponer como un arco de unión entre el punto de partida y el punto final, pasando por el punto medio. Part.Arc crea un objeto arco en el que .toShape() tiene que ser llamado para obtener el objeto arista, del mismo modo como utilizamos Part.Line en lugar de Part.makeLine.
Unfortunately there is no makeArc()
function, but we have the Part.Arc()
function to create an arc through three points. It creates an arc object joining the start point to the end point through the middle point. The arc object's toShape()
function must be called to get an edge object, the same as when using Part.LineSegment
instead of Part.makeLine
.
arc = Part.Arc(Base.Vector(0, 0, 0), Base.Vector(0, 5, 0), Base.Vector(5, 5, 0))
arc
> <Arc object>
arc_edge = arc.toShape()
Part.show(arc_edge)
Arc solo acepta puntos como Base.Vector() no acepta tuplas. arc_edge es lo que queremos que podemos mostrar utilizando Part.show(arc_edge). También puedes obtener un arco utilizando una porción de una circunferencia:
arc_edge es lo que queríamos conseguir, y podemos visualizar utilizando Part.show (arc_edge). Si desea una pequeña parte de un círculo como un arco, también es posible:
from math import pi
circle = Part.Circle(Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 10)
arc = Part.Arc(circle,0,pi)
Los arcos son aristas válidas, como las líneas. Así que también pueden utilizarse en los contornos.
Creación de un polígono
Un polígono es simplemente un contorno con múltiples aristas rectas. La función makePolygon toma una lista de puntos y crea un contorno a través de dichos puntos:
A polygon is simply a wire with multiple straight edges. The makePolygon()
function takes a list of points and creates a wire through those points:
lshape_wire = Part.makePolygon([Base.Vector(0, 5, 0), Base.Vector(0, 0, 0), Base.Vector(5, 0, 0)])
Create a bézier curve
Bézier curves are used to model smooth curves using a series of poles (points) and optional weights. The function below makes a Part.BezierCurve()
from a series of FreeCAD.Vector()
points. Note: when "getting" and "setting" a single pole or weight, indices start at 1, not 0.
def makeBCurveEdge(Points):
geomCurve = Part.BezierCurve()
geomCurve.setPoles(Points)
edge = Part.Edge(geomCurve)
return(edge)
Creación de un plano
Un plano es simplemente una superficie rectangular plana. El método utilizado para crear uno es este: makePlane(length,width,[start_pnt,dir_normal]). Por defecto start_pnt = Vector(0,0,0) y dir_normal = Vector(0,0,1). Utilizando dir_normal = Vector(0,0,1) crearemos el plano orientado hacia el eje Z, mientras que con dir_normal = Vector(1,0,0) crearemos el plano orientado hacia el eje X:
A Plane is a flat rectangular surface. The method used to create one is makePlane(length, width, [start_pnt, dir_normal])
. By default start_pnt = Vector(0, 0, 0) and dir_normal = Vector(0, 0, 1). Using dir_normal = Vector(0, 0, 1) will create the plane facing in the positive Z axis direction, while dir_normal = Vector(1, 0, 0) will create the plane facing in the positive X axis direction:
plane = Part.makePlane(2, 2)
plane
> <Face object at 028AF990>
plane = Part.makePlane(2, 2, Base.Vector(3, 0, 0), Base.Vector(0, 1, 0))
plane.BoundBox
> BoundBox (3, 0, 0, 5, 0, 2)
BoundBox es un prisma encerrando el plano con una diagonal empezando en (3,0,0) y terminando en (5,0,2). Aquí el espesor de BoundBox en el eje Y es cero, ya que nuestra forma es totalmente plana.
Nota: makePlane sólo acepta Base.Vector() para start_pnt y dir_normal pero no tuplas
Creación de una elipse
Para crear una elipse existen varios métodos:
There are several ways to create an ellipse:
Part.Ellipse()
Crea una elipse cuyo radio mayor es 2 y el radio menor 1 con centro en el (0,0,0)
Part.Ellipse(Ellipse)
Crea una copia de la elipse dada
Part.Ellipse(S1, S2, Center)
Crea una elipse centrada en el punto Center, donde el plano de la elipse está definido por Center, S1 y S2, su eje mayor está definido por Center y S1, su radio mayor es la distancia entre Center y S1, y su radio menor es la distancia entre S2 y el eje mayor.
Part.Ellipse(Center, MajorRadius, MinorRadius)
Crea una elipse con radios mayor y menor MajorRadius y MinorRadius respectivamente, y ubicada en el plano definido por Center y la normal (0,0,1)
eli = Part.Ellipse(Base.Vector(10, 0, 0), Base.Vector(0, 5, 0), Base.Vector(0, 0, 0))
Part.show(eli.toShape())
En el código de arriba hemos pasado S1, S2 y center. De forma similar a Arc, Ellipse también crea un objeto elipse pero no una arista, así que tenemos que convertirlo en una arista utilizando toShape() para mostrarlo.
Nota: Arc sólo acepta Base.Vector() para puntos pero no tuplas
eli = Part.Ellipse(Base.Vector(0, 0, 0), 10, 5)
Part.show(eli.toShape())
para el constructor de la elipse de arriba hemos pasado el centro, MajorRadius y MinorRadius
Creación de un toro
Utilizando el método makeTorus(radius1,radius2,[pnt,dir,angle1,angle2,angle]). Por defecto pnt=Vector(0,0,0),dir=Vector(0,0,1),angle1=0,angle2=360 y angle=360. Considera un toro como un pequeño circulo barrido a lo largo de una circunferencia grande. Radius1 es el radio de la circunferencia grande, radius2 es el radio del círculo pequeño, pnt es el centro del toro y dir es la dirección normal. angle1 y angle2 son ángulos en radianes para el círculo pequeño, el último parámetro angle es para hacer una sección del toro:
Using makeTorus(radius1, radius2, [pnt, dir, angle1, angle2, angle])
.
By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = 0, angle2 = 360 and angle = 360.
Consider a torus as small circle sweeping along a big circle. Radius1 is the radius of the big circle, radius2 is the radius of the small circle, pnt is the center of the torus and dir is the normal direction. angle1 and angle2 are angles in degrees for the small circle; the last angle parameter is to make a section of the torus:
torus = Part.makeTorus(10, 2)
El código de arriba creará un toro con diámetro 20 (radio 10) y espesor 4 (radio del círculo pequeño 2)
tor=Part.makeTorus(10, 5, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 180)
El código de arriba creará una sección del toro
tor=Part.makeTorus(10, 5, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), 0, 360, 180)
El código de arriba creará un semi toro, sólo el último parámetro se ha cambiado, dando el valor 180 creará el toro desde 0 hasta 180, eso es, medio toro.
Creación de un cubo o prisma
Utilizando makeBox(length,width,height,[pnt,dir]). Por defecto pnt=Vector(0,0,0) y dir=Vector(0,0,1)
Using makeBox(length, width, height, [pnt, dir])
.
By default pnt = Vector(0, 0, 0) and dir = Vector(0, 0, 1).
box = Part.makeBox(10, 10, 10)
len(box.Vertexes)
> 8
Creación de una esfera
Utilizando makeSphere(radius,[pnt, dir, angle1,angle2,angle3]). Por defecto pnt=Vector(0,0,0), dir=Vector(0,0,1), angle1=-90, angle2=90 y angle3=360. angle1 y angle2 son el punto vertical mínimo y máximo de la esfera, angle3 es el diámetro de la esfera.
Using makeSphere(radius, [pnt, dir, angle1, angle2, angle3])
. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1), angle1 = -90, angle2 = 90 and angle3 = 360.
Angle1 and angle2 are the vertical minimum and maximum of the sphere, angle3 is the sphere diameter.
sphere = Part.makeSphere(10)
hemisphere = Part.makeSphere(10, Base.Vector(0, 0, 0), Base.Vector(0, 0, 1), -90, 90, 180)
Creación de un cilindro
Utilizando makeCylinder(radius,height,[pnt,dir,angle]). Por defecto pnt=Vector(0,0,0),dir=Vector(0,0,1) y angle=360
Using makeCylinder(radius, height, [pnt, dir, angle])
. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.
cylinder = Part.makeCylinder(5, 20)
partCylinder = Part.makeCylinder(5, 20, Base.Vector(20, 0, 0), Base.Vector(0, 0, 1), 180)
Creación de un cono
Utilizando makeCone(radius1,radius2,height,[pnt,dir,angle]). Por defecto pnt=Vector(0,0,0), dir=Vector(0,0,1) y angle=360
Using makeCone(radius1, radius2, height, [pnt, dir, angle])
. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.
cone = Part.makeCone(10, 0, 20)
semicone = Part.makeCone(10, 0, 20, Base.Vector(20, 0, 0), Base.Vector(0, 0, 1), 180)
Modificando formas
Existen diversos métodos para modificar formas. Algunas son simples operaciones de transformación como mover o rotar formas, otras son más complejas, como la unión y diferencia de una forma y otra. are simple transformation operations such as moving or rotating shapes, other are more complex, such as unioning and subtracting one shape from another. Tenlo en cuenta
There are several ways to modify shapes. Some are simple transformation operations such as moving or rotating shapes, others are more complex, such as unioning and subtracting one shape from another.
Operaciones de transformación
Traslación de una forma
Traslación es el acto de mover una forma de una situación a otra. Cualquier forma (aristas, caras, cubos, etc...) se puede trasladar del mismo modo:
Translating is the act of moving a shape from one place to another. Any shape (edge, face, cube, etc...) can be translated the same way:
myShape = Part.makeBox(2, 2, 2)
myShape.translate(Base.Vector(2, 0, 0))
Esto moverá nuestra forma "myShape" 2 unidades en la dirección del eje X.
Rotación de una forma
Para rotar una forma, necesitas especificar el centro de rotación, el eje, y el ángulo de rotación:
To rotate a shape, you need to specify the rotation center, the axis, and the rotation angle:
myShape.rotate(Base.Vector(0, 0, 0),Base.Vector(0, 0, 1), 180)
El código de arriba rotará la forma 180 grados alrededor del eje Z.
Transformaciones genéricas con matrices
Una matriz es un modo muy conveniente de almacenar transformaciones en el mundo 3D. En una simple matriz, puedes establecer traslaciones, rotaciones y valores de escala a ser aplicados a un objeto. Por ejemplo:
A matrix is a very convenient way to store transformations in the 3D world. In a single matrix, you can set translation, rotation and scaling values to be applied to an object. For example:
myMat = Base.Matrix()
myMat.move(Base.Vector(2, 0, 0))
myMat.rotateZ(math.pi/2)
Nota: Las matrices de FreeCAD funcionan en radianes. También, casi todas las operaciones de matrices que toman un vector pueden tomar 3 números, así estas dos líneas hacen lo mismo:
myMat.move(2, 0, 0)
myMat.move(Base.Vector(2, 0, 0))
Cuando nuestra matriz es establecida, podemos aplicarla a nuestra forma. FreeCAD proporciona 2 métodos para hacerlo: transformShape() y transformGeometry(). La diferencia es que con el primero, estas seguro de que no ocurrirá ninguna deformación (mira "escalando una forma" más abajo). Podemos aplicar nuestra transformación así:
myShape.transformShape(myMat)
o
myShape.transformGeometry(myMat)
Escalando una forma
Escalando una forma es una operación más peligrosa porque, a diferencia de la traslación o rotación, un escalado no uniforme (con diferentes valores para los ejes X,Y y Z) puede modificar la estructura de la forma. Por ejemplo, escalando una circunferencia con un valor horizontal superior al vertical la transformará en una elipse, que se comporta matemáticamente de forma muy diferente. Para el escalado, no podemos utilizar transformShape, tenemos que usar transformGeometry():
Scaling a shape is a more dangerous operation because, unlike translation or rotation, scaling non-uniformly (with different values for X, Y and Z) can modify the structure of the shape. For example, scaling a circle with a higher value horizontally than vertically will transform it into an ellipse, which behaves mathematically very differently. For scaling, we cannot use the transformShape()
, we must use transformGeometry()
:
myMat = Base.Matrix()
myMat.scale(2, 1, 1)
myShape=myShape.transformGeometry(myMat)
Operaciones Booleanas
Diferencia
La diferencia de una forma con otra se llama "corte" en el argot de OCC/FreeCAD y se hace así:
Subtracting a shape from another one is called "cut" in FreeCAD and is done like this:
cylinder = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
sphere = Part.makeSphere(5, Base.Vector(5, 0, 0))
diff = cylinder.cut(sphere)
Intersección
del mismo modo, la intersección entre dos formas es denominada "común" y se hace de este modo:
The same way, the intersection between two shapes is called "common" and is done this way:
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
common = cylinder1.common(cylinder2)
Unión
La unión se llama "fusión" y funciona del mismo modo:
Union is called "fuse" and works the same way:
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
fuse = cylinder1.fuse(cylinder2)
Sección
Una sección es la intersección entre una forma de un sólido y una forma de un plano. Devolverá una curva de intersección, un componente con aristas
A "section" is the intersection between a solid shape and a plane shape. It will return an intersection curve, a compound curve composed of edges.
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder2 = Part.makeCylinder(3, 10, Base.Vector(5, 0, -5), Base.Vector(0, 0, 1))
section = cylinder1.section(cylinder2)
section.Wires
> []
section.Edges
> [<Edge object at 0D87CFE8>, <Edge object at 019564F8>, <Edge object at 0D998458>,
<Edge object at 0D86DE18>, <Edge object at 0D9B8E80>, <Edge object at 012A3640>,
<Edge object at 0D8F4BB0>]
Extrusión
Extrusión es el acto de "empujar" una forma plana en determinada dirección resultando en un cuerpo sólido. Piensa en una círculo convirtiéndose en un tubo:
Extrusion is the act of "pushing" a flat shape in a certain direction, resulting in a solid body. Think of a circle becoming a tube by "pushing it out":
circle = Part.makeCircle(10)
tube = circle.extrude(Base.Vector(0, 0, 2))
Si tu círculo está hueco, obtendrás un tubo hueco. Si no es hueco, obtendrás un cilindro sólido:
wire = Part.Wire(circle)
disc = Part.Face(wire)
cylinder = disc.extrude(Base.Vector(0, 0, 2))
Exploración de formas
Puedes explorar fácilmente la estructura de datos topológicos:
You can easily explore the topological data structure:
import Part
b = Part.makeBox(100, 100, 100)
b.Wires
w = b.Wires[0]
w
w.Wires
w.Vertexes
Part.show(w)
w.Edges
e = w.Edges[0]
e.Vertexes
v = e.Vertexes[0]
v.Point
Escribiendo las líneas de arriba en el interprete de Python, conseguirás una buena comprensión de la estructura de los objetos de piezas. Aquí, nuestro comando makeBox() crea una forma sólida. Este sólido, como todos los sólidos de piezas, contiene caras. Las caras siempre contienen contornos, que son listas de aristas que bordean la cara. Cada cara tiene al menos un contorno cerrado (puede tener más si la cara tiene huecos). En el contorno, podemos mirar en cada arista de forma separada, y dentro de cada arista, podemos ver los vértices. Las aristas rectas tienen sólo dos vértices, obviamente.
Análisis de aristas
En el caso de una arista con una curva arbitraria, es más probable que quieras hacer una discretización. En FreeCAD las aristas son parametrizadas por sus longitudes. Eso significa que puedes recorrer una arista/curva por su longitud:
In case of an edge, which is an arbitrary curve, it's most likely you want to do a discretization. In FreeCAD the edges are parametrized by their lengths. That means you can walk an edge/curve by its length:
import Part
box = Part.makeBox(100, 100, 100)
anEdge = box.Edges[0]
print(anEdge.Length)
Ahora puedes acceder a un montón de propiedades de la arista utilizando la longitud como una posición. Eso significa que si la arista es de 100mm de longitud el punto inicial es y la posición final es 100.
anEdge.tangentAt(0.0) # tangent direction at the beginning
anEdge.valueAt(0.0) # Point at the beginning
anEdge.valueAt(100.0) # Point at the end of the edge
anEdge.derivative1At(50.0) # first derivative of the curve in the middle
anEdge.derivative2At(50.0) # second derivative of the curve in the middle
anEdge.derivative3At(50.0) # third derivative of the curve in the middle
anEdge.centerOfCurvatureAt(50) # center of the curvature for that position
anEdge.curvatureAt(50.0) # the curvature
anEdge.normalAt(50) # normal vector at that position (if defined)
Utilizando la selección
Aquí vemos ahora cómo podemos utilizar la selección que el usuario hizo en la vista. Antes de nada creamos un cubo y lo mostramos en la vista
Here we see now how we can use a selection the user did in the viewer. First of all we create a box and show it in the viewer.
import Part
Part.show(Part.makeBox(100, 100, 100))
Gui.SendMsgToActiveView("ViewFit")
Selecciona ahora algunas caras o aristas. Con este archivo de guión puedes iterar todos los objetos seleccionados y sus subelementos:
for o in Gui.Selection.getSelectionEx():
print(o.ObjectName)
for s in o.SubElementNames:
print("name: ", s)
for s in o.SubObjects:
print("object: ", s)
Selecciona algunas aristas y este archivo de guión calculará la longitud:
length = 0.0
for o in Gui.Selection.getSelectionEx():
for s in o.SubObjects:
length += s.Length
print("Length of the selected edges: ", length)
Examen completo: La botella OCC
Un ejemplo típico encontrado en la página de primeros pasos de OpenCasCade es cómo construir una botella. Este es un buen ejercicio también para FreeCAD. En realidad, puedes seguir nuestro ejemplo de abajo y la página de OCC simultáneamente, comprenderás bien cómo están implementadas las estructuras de OCC en FreeCAD. El archivo de guión completo de abajo está también incluido en la instalación de FreeCAD (dentro del directorio Mod/Part) y puede llamarse desde el interprete de Python escribiendo:
A typical example found on the OpenCasCade Technology website is how to build a bottle. This is a good exercise for FreeCAD too. In fact, if you follow our example below and the OCC page simultaneously, you will see how well OCC structures are implemented in FreeCAD. The script is included in the FreeCAD installation (inside the Mod/Part folder) and can be called from the Python interpreter by typing:
import Part
import MakeBottle
bottle = MakeBottle.makeBottle()
Part.show(bottle)
El archivo de guión completo
Aquí está el archivo de guión completo MakeBottle:
For the purpose of this tutorial we will consider a reduced version of the script. In this version the bottle will not be hollowed out, and the neck of the bottle will not be threaded.
import Part, math
from FreeCAD import Base
def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
aPnt1=Base.Vector(-myWidth / 2., 0, 0)
aPnt2=Base.Vector(-myWidth / 2., -myThickness / 4., 0)
aPnt3=Base.Vector(0, -myThickness / 2., 0)
aPnt4=Base.Vector(myWidth / 2., -myThickness / 4., 0)
aPnt5=Base.Vector(myWidth / 2., 0, 0)
aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4)
aSegment1=Part.LineSegment(aPnt1, aPnt2)
aSegment2=Part.LineSegment(aPnt4, aPnt5)
aEdge1=aSegment1.toShape()
aEdge2=aArcOfCircle.toShape()
aEdge3=aSegment2.toShape()
aWire=Part.Wire([aEdge1, aEdge2, aEdge3])
aTrsf=Base.Matrix()
aTrsf.rotateZ(math.pi) # rotate around the z-axis
aMirroredWire=aWire.copy()
aMirroredWire.transformShape(aTrsf)
myWireProfile=Part.Wire([aWire, aMirroredWire])
myFaceProfile=Part.Face(myWireProfile)
aPrismVec=Base.Vector(0, 0, myHeight)
myBody=myFaceProfile.extrude(aPrismVec)
myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges)
neckLocation=Base.Vector(0, 0, myHeight)
neckNormal=Base.Vector(0, 0, 1)
myNeckRadius = myThickness / 4.
myNeckHeight = myHeight / 10.
myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal)
myBody = myBody.fuse(myNeck)
return myBody
el = makeBottleTut()
Part.show(el)
Explicación detallada
import Part, math
from FreeCAD import Base
Necesitaremos, desde luego, el módulo de piezas, pero también el módulo FreeCAD.Base, que contiene estructuras básicas de FreeCAD como vectores y matrices.
def makeBottleTut(myWidth = 50.0, myHeight = 70.0, myThickness = 30.0):
aPnt1=Base.Vector(-myWidth / 2., 0, 0)
aPnt2=Base.Vector(-myWidth / 2., -myThickness / 4., 0)
aPnt3=Base.Vector(0, -myThickness / 2., 0)
aPnt4=Base.Vector(myWidth / 2., -myThickness / 4., 0)
aPnt5=Base.Vector(myWidth / 2., 0, 0)
Aquí definimos nuestra función makeBottle. Esta función se puede llamar sin argumentos, como hicimos arriba, en cuyo caso se utilizaran los valores por defecto para ancho, alto, y espesor. Luego, definimos un conjunto de puntos que serán utilizados para construir nuestro perfil base.
...
aArcOfCircle = Part.Arc(aPnt2, aPnt3, aPnt4)
aSegment1=Part.LineSegment(aPnt1, aPnt2)
aSegment2=Part.LineSegment(aPnt4, aPnt5)
Aquí en realidad definimos la geometría: un arco, creado por 3 puntos, y dos segmentos de línea, creados por 2 puntos.
...
aEdge1=aSegment1.toShape()
aEdge2=aArcOfCircle.toShape()
aEdge3=aSegment2.toShape()
aWire=Part.Wire([aEdge1, aEdge2, aEdge3])
Recuerdas la diferencia entre geometría y formas? Aquí construimos formas a partir de nuestra geometría de construcción. 3 aristas (las aristas pueden ser rectas o curvas), luego un contorno creado a partir de dichas tres aristas.
...
aTrsf=Base.Matrix()
aTrsf.rotateZ(math.pi) # rotate around the z-axis
aMirroredWire=aWire.copy()
aMirroredWire.transformShape(aTrsf)
myWireProfile=Part.Wire([aWire, aMirroredWire])
Hasta ahora construimos sólo medio perfil. Más sencillo que construir el perfil completo del mismo modo, simplemente podemos crear una simetría de lo que hicimos, y pegar ambas partes. Así primero creamos una matriz. Una matriz es un modo muy común para aplicar transformaciones a objetos en el mundo 3D, ya que puede contener en una estructura todas las transformaciones básicas que los objetos pueden sufrir (mover, rotar y escalar). Aquí, después de crear la matriz, creamos una simétrica, y creamos una copia de nuestro contorno con esa matriz de transformación aplicada. Ahora tenemos 2 contornos, y podemos crear un tercer contorno a partir de ellos, ya que los contornos son en realidad listas de aristas.
...
myFaceProfile=Part.Face(myWireProfile)
aPrismVec=Base.Vector(0, 0, myHeight)
myBody=myFaceProfile.extrude(aPrismVec)
myBody=myBody.makeFillet(myThickness / 12.0, myBody.Edges)
Ahora que tenemos un contorno cerrado, se puede convertir en una cara. Una vez que tengamos una cara, podemos extruirla. Haciendo esto, creamos un sólido. Entonces aplicamos un pequeño redondeo a nuestro objeto porque queremos crear un buen diseño, no es así?
...
neckLocation=Base.Vector(0, 0, myHeight)
neckNormal=Base.Vector(0, 0, 1)
myNeckRadius = myThickness / 4.
myNeckHeight = myHeight / 10.
myNeck = Part.makeCylinder(myNeckRadius, myNeckHeight, neckLocation, neckNormal)
El cuerpo de nuestra botella está creado, aún tenemos que crear el cuello. Así que creamos un nuevo sólido, con un cilindro.
...
myBody = myBody.fuse(myNeck)
La operación de fusión, que en otras aplicaciones es llamada unión, es muy potente. Tendrá cuidado de pegar lo que necesita ser pegado y eliminar las partes que necesiten ser eliminadas.
...
return myBody
Obtenemos nuestro sólido de pieza como resultado de nuestra función. Ese sólido de pieza, como cualquier otra forma de piezas, se puede atribuir a un objeto en el documento de FreeCAD, con:
el = makeBottleTut()
Part.show(el)
o, de forma más simple:
Example: Pierced box
Here is a complete example of building a pierced box.
The construction is done one side at a time. When the cube is finished, it is hollowed out by cutting a cylinder through it.
import Part, math
from FreeCAD import Base
size = 10
poly = Part.makePolygon([(0, 0, 0), (size, 0, 0), (size, 0, size), (0, 0, size), (0, 0, 0)])
face1 = Part.Face(poly)
face2 = Part.Face(poly)
face3 = Part.Face(poly)
face4 = Part.Face(poly)
face5 = Part.Face(poly)
face6 = Part.Face(poly)
myMat = Base.Matrix()
myMat.rotateZ(math.pi / 2)
face2.transformShape(myMat)
face2.translate(Base.Vector(size, 0, 0))
myMat.rotateZ(math.pi / 2)
face3.transformShape(myMat)
face3.translate(Base.Vector(size, size, 0))
myMat.rotateZ(math.pi / 2)
face4.transformShape(myMat)
face4.translate(Base.Vector(0, size, 0))
myMat = Base.Matrix()
myMat.rotateX(-math.pi / 2)
face5.transformShape(myMat)
face6.transformShape(myMat)
face6.translate(Base.Vector(0, 0, size))
myShell = Part.makeShell([face1, face2, face3, face4, face5, face6])
mySolid = Part.makeSolid(myShell)
myCyl = Part.makeCylinder(2, 20)
myCyl.translate(Base.Vector(size / 2, size / 2, 0))
cut_part = mySolid.cut(myCyl)
Part.show(cut_part)
Cargando y guardando
Existen diversas formas de guardar tu trabajo en el módulo de piezas. Puedes desde luego guardar el documento de FreeCAD, pero también puedes guardar objetos de pieza directamente en formatos de CAD comunes, como BREP, IGS, STEP y STL.
There are several ways to save your work. You can of course save your FreeCAD document, but you can also save Part objects directly to common CAD formats, such as BREP, IGS, STEP and STL.
El guardado de una forma en un archivo es sencillo. Existen los métodos exportBrep(), exportIges(), exportStl() y exportStep() disponibles para todos los objetos de forma. Así, haciendo:
import Part
s = Part.makeBox(10, 10, 10)
s.exportStep("test.stp")
se guardará nuestro cubo en un archivo STEP. Para cargar un archivo BREP, IGES o STEP, simplemente haz lo contrario:
import Part
s = Part.Shape()
s.read("test.stp")
To convert a STEP file to an IGS file:
import Part
s = Part.Shape()
s.read("file.stp") # incoming file igs, stp, stl, brep
s.exportIges("file.igs") # outbound file igs
- FreeCAD scripting: Python, Introduction to Python, Python scripting tutorial, FreeCAD Scripting Basics
- Modules: Builtin modules, Units, Quantity
- Workbenches: Workbench creation, Gui Commands, Commands, Installing more workbenches
- Meshes and Parts: Mesh Scripting, Topological data scripting, Mesh to Part, PythonOCC
- Parametric objects: Scripted objects, Viewproviders (Custom icon in tree view)
- Scenegraph: Coin (Inventor) scenegraph, Pivy
- Graphical interface: Interface creation, Interface creation completely in Python (1, 2, 3, 4, 5), PySide, PySide examples beginner, intermediate, advanced
- Macros: Macros, How to install macros
- Embedding: Embedding FreeCAD, Embedding FreeCADGui
- Other: Expressions, Code snippets, Line drawing function, FreeCAD vector math library (deprecated)
- Hubs: User hub, Power users hub, Developer hub