Topological data scripting/es: Difference between revisions

From FreeCAD Documentation
(Updating to match new version of source page)
(Updating to match new version of source page)
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
<languages/>
<languages/>

{{Docnav
|[[FreeCAD_Scripting_Basics|FreeCAD Scripting Basics]]
|[[Mesh_Scripting|Mesh Scripting]]
}}


{{TOCright}}
{{TOCright}}
Line 14: Line 9:
</div>
</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. The basics about Topological data scripting are described in [[Part_Module#Explaining_the_concepts|Part Module Explaining the concepts]]. 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|Introduction to Python]].
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">
<div class="mw-translate-fuzzy">
Line 95: Line 95:
</div>
</div>


First we create the distinct geometric parts of this wire. Making sure that parts that have to be connected later
First we create the distinct geometric parts of this wire. Making sure that parts that have to be connected later share the same vertices.
share the same vertices.


<div class="mw-translate-fuzzy">
<div class="mw-translate-fuzzy">
Line 157: Line 156:
</div>
</div>


The last step is to put the geometric base elements together
The last step is to put the geometric base elements together and bake a topological shape:
and bake a topological shape:


{{Code|code=
{{Code|code=
Line 197: Line 195:
</div>
</div>


You can easily create basic topological objects with the {{incode|make...()}}
You can easily create basic topological objects with the {{incode|make...()}} methods from the Part Module:
methods from the Part Module:


{{Code|code=
{{Code|code=
Line 229: Line 226:
</div>
</div>


First we need to import the Part module so we can use its contents in Python.
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:
We'll also import the Base module from inside the FreeCAD module:


{{Code|code=
{{Code|code=
Line 245: Line 241:
</div>
</div>


[http://en.wikipedia.org/wiki/Euclidean_vector Vectors] are one of the most
[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:
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:


{{Code|code=
{{Code|code=
Line 260: Line 253:
{{Code|code=
{{Code|code=
myVertex = myShape.Vertexes[0]
myVertex = myShape.Vertexes[0]
print myVertex.Point
print(myVertex.Point)
> Vector (3, 2, 0)
> Vector (3, 2, 0)
}}
}}
Line 307: Line 300:
</div>
</div>


So far we created an edge object, but it doesn't appear anywhere on the screen.
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
This is because the FreeCAD 3D scene
only displays what you tell it to display. To do that, we use this simple
method:
method:


Line 328: Line 319:
</div>
</div>


A wire is a multi-edge line and can be created from a list of edges
A wire is a multi-edge line and can be created from a list of edges or even a list of wires:
or even a list of wires:


{{Code|code=
{{Code|code=
Line 367: Line 357:
</div>
</div>


Only faces created from closed wires will be valid. In this example, wire3
Only faces created from closed wires will be valid. In this example, wire3 is a closed wire but wire2 is not (see above):
is a closed wire but wire2 is not (see above)


{{Code|code=
{{Code|code=
Line 381: Line 370:
> True
> True
sface = Part.Face(wire2)
sface = Part.Face(wire2)
face.isValid()
sface.isValid()
> False
> False
}}
}}
Line 444: Line 433:
</div>
</div>


Unfortunately there is no {{incode|makeArc()}} function, but we have the {{incode|Part.Arc()}} function to
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}}.
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=
{{Code|code=
Line 512: Line 497:
</div>
</div>


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)
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:
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=
{{Code|code=
Line 563: Line 546:
}}
}}


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


{{Code|code=
{{Code|code=
Line 607: Line 592:
Using {{incode|makeTorus(radius1, radius2, [pnt, dir, angle1, angle2, angle])}}.
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.
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
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:
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=
{{Code|code=
Line 662: Line 643:
</div>
</div>


Using {{incode|makeSphere(radius, [pnt, dir, angle1, angle2, angle3])}}. By default
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.
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=
{{Code|code=
Line 680: Line 659:
</div>
</div>


Using {{incode|makeCylinder(radius, height, [pnt, dir, angle])}}. By default
Using {{incode|makeCylinder(radius, height, [pnt, dir, angle])}}. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.
pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.
{{Code|code=
{{Code|code=
cylinder = Part.makeCylinder(5, 20)
cylinder = Part.makeCylinder(5, 20)
Line 694: Line 672:
</div>
</div>


Using {{incode|makeCone(radius1, radius2, height, [pnt, dir, angle])}}. By default
Using {{incode|makeCone(radius1, radius2, height, [pnt, dir, angle])}}. By default pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.
pnt = Vector(0, 0, 0), dir = Vector(0, 0, 1) and angle = 360.
{{Code|code=
{{Code|code=
cone = Part.makeCone(10, 0, 20)
cone = Part.makeCone(10, 0, 20)
Line 710: Line 687:
</div>
</div>


There are several ways to modify shapes. Some are simple transformation operations
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.
such as moving or rotating shapes, others are more complex, such as unioning and
subtracting one shape from another.


[[#top|top]]
[[#top|top]]
Line 727: Line 702:
</div>
</div>


Translating is the act of moving a shape from one place to another.
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:
Any shape (edge, face, cube, etc...) can be translated the same way:
{{Code|code=
{{Code|code=
myShape = Part.makeBox(2, 2, 2)
myShape = Part.makeBox(2, 2, 2)
Line 745: Line 719:
</div>
</div>


To rotate a shape, you need to specify the rotation center, the axis,
To rotate a shape, you need to specify the rotation center, the axis, and the rotation angle:
and the rotation angle:
{{Code|code=
{{Code|code=
myShape.rotate(Base.Vector(0, 0, 0),Base.Vector(0, 0, 1), 180)
myShape.rotate(Base.Vector(0, 0, 0),Base.Vector(0, 0, 1), 180)
Line 760: Line 733:
</div>
</div>


A matrix is a very convenient way to store transformations in the 3D
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:
world. In a single matrix, you can set translation, rotation and scaling
values to be applied to an object. For example:
{{Code|code=
{{Code|code=
myMat = Base.Matrix()
myMat = Base.Matrix()
Line 793: Line 764:
</div>
</div>


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()}}:
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=
{{Code|code=
myMat = Base.Matrix()
myMat = Base.Matrix()
Line 816: Line 782:
</div>
</div>


Subtracting a shape from another one is called "cut" in FreeCAD
Subtracting a shape from another one is called "cut" in FreeCAD and is done like this:
and is done like this:
{{Code|code=
{{Code|code=
cylinder = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
Line 831: Line 796:
</div>
</div>


The same way, the intersection between two shapes is called "common" and is done
The same way, the intersection between two shapes is called "common" and is done this way:
this way:
{{Code|code=
{{Code|code=
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
Line 861: Line 825:
</div>
</div>


A "section" is the intersection between a solid shape and a plane shape.
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.
It will return an intersection curve, a compound curve composed of edges.
{{Code|code=
{{Code|code=
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
cylinder1 = Part.makeCylinder(3, 10, Base.Vector(0, 0, 0), Base.Vector(1, 0, 0))
Line 882: Line 845:
</div>
</div>


Extrusion is the act of "pushing" a flat shape in a certain direction, resulting in
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":
a solid body. Think of a circle becoming a tube by "pushing it out":
{{Code|code=
{{Code|code=
circle = Part.makeCircle(10)
circle = Part.makeCircle(10)
Line 932: Line 894:
</div>
</div>


In case of an edge, which is an arbitrary curve, it's most likely you want to
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:
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=
{{Code|code=
import Part
import Part
Line 941: Line 901:
print(anEdge.Length)
print(anEdge.Length)
}}
}}
<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.
</div>
{{Code|code=
{{Code|code=
anEdge.tangentAt(0.0) # tangent direction at the beginning
anEdge.tangentAt(0.0) # tangent direction at the beginning
Line 1,255: Line 1,217:
}}
}}
[[#top|top]]
[[#top|top]]

<div class="mw-translate-fuzzy">
{{docnav/es|Mesh Scripting/es|Mesh to Part/es}}
</div>


{{Powerdocnavi{{#translation:}}}}
{{Powerdocnavi{{#translation:}}}}

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:

Python classes of the Part module
Python classes of the Part module

This is a Unified Modeling Language (UML) overview of the most important classes of the Part module: Python classes of the Part module

top

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.

top

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.

top

Ejemplo rápido: Creación de topologías básicas

Wire
Wire

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.

top

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)

top

Arco

Circle


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)

top

Línea

Line


La línea puede crearse de forma muy simple a partir de los puntos:

L1 = Part.LineSegment(V1, V2)
L2 = Part.LineSegment(V3, V4)

top

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

top

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

top

Mostrar todo

Part.show(P)

top

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.

top

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

top

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)

top

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)

top

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.

top

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

top

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.

top

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)

top

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.

top

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

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

top

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

top

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

top

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.

top

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

top

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)

top

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)

top

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)

top

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.

top

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.

top

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.

top

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)

top

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)

top

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)

top

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)

top

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)

top

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>]

top

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

top

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.

top

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)

top

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)

top

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)

top

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)

top

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:

top

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)

top

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

top