Mesh Scripting/es: Difference between revisions

From FreeCAD Documentation
(Created page with "También puedes crear un objeto desde un archivo")
(Created page with "Ver también: Interfaz de programación de aplicaciones Malla.")
 
(43 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<languages/>
=== Introducción ===

{{TOCright}}

==Introducción==

Para obtener acceso al módulo {{incode|Malla}} hay que importarlo primero:


{{Code|code=
En primer lugar tienes que importar el módulo Malla:
<syntaxhighlight>
import Mesh
import Mesh
}}
</syntaxhighlight>
Después de eso, ya tendrás acceso al módulo Malla y la clase Malla, que ofrecen las funciones del nucleo FreeCAD C++ Mesh-Kernel.


=== Creación y Carga ===
==Creación==


Para crear un objeto malla vacío sólo tienes que utilizar el constructor estándar:
Para crear un objeto de malla vacío basta con utilizar el constructor estándar:


{{Code|code=
<syntaxhighlight>
mesh = Mesh.Mesh()
mesh = Mesh.Mesh()
}}
</syntaxhighlight>


También puedes crear un objeto desde un archivo
También puede crear un objeto a partir de un archivo:


{{Code|code=
<syntaxhighlight>
mesh = Mesh.Mesh('D:/temp/Something.stl')
mesh = Mesh.Mesh("D:/temp/Something.stl")
}}
</syntaxhighlight>


O crearla a partir de un conjunto de triángulos descritos por sus puntos de esquina:
(A list of compatible filetypes can be found under 'Meshes' [[Feature_list#IO|here]].)


{{Code|code=
Or create it out of a set of triangles described by their corner points:
triangles = [

<syntaxhighlight>
planarMesh = [
# triangle 1
# triangle 1
[-0.5000,-0.5000,0.0000],[0.5000,0.5000,0.0000],[-0.5000,0.5000,0.0000],
[-0.5000, -0.5000, 0.0000], [0.5000, 0.5000, 0.0000], [-0.5000, 0.5000, 0.0000],
#triangle 2
#triangle 2
[-0.5000,-0.5000,0.0000],[0.5000,-0.5000,0.0000],[0.5000,0.5000,0.0000],
[-0.5000, -0.5000, 0.0000], [0.5000, -0.5000, 0.0000], [0.5000, 0.5000, 0.0000],
]
]
planarMeshObject = Mesh.Mesh(planarMesh)
meshObject = Mesh.Mesh(triangles)
Mesh.show(planarMeshObject)
Mesh.show(meshObject)
}}
</syntaxhighlight>


El Núcleo-Malla se encarga de crear una estructura de datos topológicamente correcta ordenando los puntos y aristas coincidentes.
The Mesh-Kernel takes care about creating a topological correct data structure by sorting
{{Top}}
coincident points and edges together.
==Modelización==


Para crear geometrías regulares se puede utilizar uno de los métodos {{incode|create*()}}. Un toroide, por ejemplo, se puede crear de la siguiente manera:
Later on you will see how you can test and examine mesh data.


{{Code|code=
=== Modeling ===
m = Mesh.createTorus(8.0, 2.0, 50)
To create regular geometries you can use the Python script BuildRegularGeoms.py.
Mesh.show(m)
}}


Los dos primeros parámetros definen los radios del toroide y el tercer parámetro es un factor de submuestreo para saber cuántos triángulos se crean. Cuanto mayor sea este valor, más suave será la malla.
<syntaxhighlight>
import BuildRegularGeoms
</syntaxhighlight>


El módulo {{incode|Malla}} también proporciona tres métodos booleanos: {{incode|union()}}, {{incode|intersection()}} y {{incode|difference()}}:
This script provides methods to define simple rotation bodies like spheres, ellipsoids, cylinders, toroids and cones. And it also has a method to create a simple cube.
To create a toroid, for instance, can be done as follows:


{{Code|code=
<syntaxhighlight>
t = BuildRegularGeoms.Toroid(8.0, 2.0, 50) # list with several thousands triangles
m = Mesh.Mesh(t)
</syntaxhighlight>

The first two parameters define the radiuses of the toroid and the third parameter is a sub-sampling factor for how many triangles are created. The higher this value the smoother and the lower the coarser the body is.
The Mesh class provides a set of boolean functions that can be used for modeling purposes. It provides union, intersection and difference of two mesh objects.

<syntaxhighlight>
m1, m2 # are the input mesh objects
m1, m2 # are the input mesh objects
m3 = Mesh.Mesh(m1) # create a copy of m1
m3 = Mesh.Mesh(m1) # create a copy of m1
Line 69: Line 63:
m6 = Mesh.Mesh(m2)
m6 = Mesh.Mesh(m2)
m6.difference(m1) # the difference of m2 and m1, usually the result is different to m5
m6.difference(m1) # the difference of m2 and m1, usually the result is different to m5
}}
</syntaxhighlight>


Este es un ejemplo que crea una tubería utilizando el método {{incode|difference()}}:
Finally, a full example that computes the intersection between a sphere and a cylinder that intersects the sphere.


{{Code|code=
<syntaxhighlight>
import Mesh, BuildRegularGeoms
import FreeCAD, Mesh
sphere = Mesh.Mesh( BuildRegularGeoms.Sphere(5.0, 50) )
cylA = Mesh.createCylinder(2.0, 10.0, True, 1.0, 36)
cylinder = Mesh.Mesh( BuildRegularGeoms.Cylinder(2.0, 10.0, True, 1.0, 50) )
cylB = Mesh.createCylinder(1.0, 12.0, True, 1.0, 36)
cylB.Placement.Base = (FreeCAD.Vector(-1, 0, 0)) # move cylB to avoid co-planar faces
diff = sphere
pipe = cylA
diff = diff.difference(cylinder)
pipe = pipe.difference(cylB)
d = FreeCAD.newDocument()
pipe.flipNormals() # somehow required
d.addObject("Mesh::Feature","Diff_Sphere_Cylinder").Mesh=diff
doc = FreeCAD.ActiveDocument
d.recompute()
obj = d.addObject("Mesh::Feature", "Pipe")
</syntaxhighlight>
obj.Mesh = pipe
doc.recompute()
}}
{{Top}}
==Notas==


Una fuente extensa, aunque difícil de usar, de scripts relacionados con la malla son los scripts de pruebas unitarias del módulo {{incode|Mesh}}.
=== Examining and Testing ===
En estas pruebas unitarias se llaman literalmente todos los métodos y se ajustan todas las propiedades/atributos.

Así que si eres lo suficientemente audaz, echa un vistazo al módulo [https://github.com/FreeCAD/FreeCAD/blob/master/src/Mod/Mesh/App/MeshTestsApp.py Unit Test].
=== Write your own Algorithms ===

=== Exporting ===
You can even write the mesh to a python module:

<syntaxhighlight>
m.write("D:/Develop/Projekte/FreeCAD/FreeCAD_0.7/Mod/Mesh/SavedMesh.py")
import SavedMesh
m2 = Mesh.Mesh(SavedMesh.faces)
</syntaxhighlight>

=== Gui related stuff ===

=== Odds and Ends ===
An extensive (though hard to use) source of Mesh related scripting are the unit test scripts of the Mesh-Module.
In this unit tests literally all methods are called and all properties/attributes are tweaked.
So if you are bold enough, take a look at the [http://free-cad.svn.sourceforge.net/viewvc/free-cad/trunk/src/Mod/Mesh/App/MeshTestsApp.py?view=markup Unit Test module].

{{docnav|FreeCAD Scripting Basics|Topological data scripting}}

[[Category:Poweruser Documentation]]
[[Category:Python Code]]


Ver también: [[Mesh_API|Interfaz de programación de aplicaciones Malla]].
{{Top}}
{{Powerdocnavi{{#translation:}}}}
[[Category:Developer Documentation{{#translation:}}]]
[[Category:Python Code{{#translation:}}]]
{{Mesh Tools navi{{#translation:}}}}
{{clear}}
{{clear}}
<languages/>

Latest revision as of 13:32, 19 September 2021

Introducción

Para obtener acceso al módulo Malla hay que importarlo primero:

import Mesh

Creación

Para crear un objeto de malla vacío basta con utilizar el constructor estándar:

mesh = Mesh.Mesh()

También puede crear un objeto a partir de un archivo:

mesh = Mesh.Mesh("D:/temp/Something.stl")

O crearla a partir de un conjunto de triángulos descritos por sus puntos de esquina:

triangles = [
# triangle 1
[-0.5000, -0.5000, 0.0000], [0.5000, 0.5000, 0.0000], [-0.5000, 0.5000, 0.0000],
#triangle 2
[-0.5000, -0.5000, 0.0000], [0.5000, -0.5000, 0.0000], [0.5000, 0.5000, 0.0000],
]
meshObject = Mesh.Mesh(triangles)
Mesh.show(meshObject)

El Núcleo-Malla se encarga de crear una estructura de datos topológicamente correcta ordenando los puntos y aristas coincidentes.

Inicio

Modelización

Para crear geometrías regulares se puede utilizar uno de los métodos create*(). Un toroide, por ejemplo, se puede crear de la siguiente manera:

m = Mesh.createTorus(8.0, 2.0, 50)
Mesh.show(m)

Los dos primeros parámetros definen los radios del toroide y el tercer parámetro es un factor de submuestreo para saber cuántos triángulos se crean. Cuanto mayor sea este valor, más suave será la malla.

El módulo Malla también proporciona tres métodos booleanos: union(), intersection() y difference():

m1, m2              # are the input mesh objects
m3 = Mesh.Mesh(m1)  # create a copy of m1
m3.unite(m2)        # union of m1 and m2, the result is stored in m3
m4 = Mesh.Mesh(m1)
m4.intersect(m2)    # intersection of m1 and m2
m5 = Mesh.Mesh(m1)
m5.difference(m2)   # the difference of m1 and m2
m6 = Mesh.Mesh(m2)
m6.difference(m1)   # the difference of m2 and m1, usually the result is different to m5

Este es un ejemplo que crea una tubería utilizando el método difference():

import FreeCAD, Mesh
cylA = Mesh.createCylinder(2.0, 10.0, True, 1.0, 36)
cylB = Mesh.createCylinder(1.0, 12.0, True, 1.0, 36)
cylB.Placement.Base = (FreeCAD.Vector(-1, 0, 0)) # move cylB to avoid co-planar faces
pipe = cylA
pipe = pipe.difference(cylB)
pipe.flipNormals() # somehow required
doc = FreeCAD.ActiveDocument
obj = d.addObject("Mesh::Feature", "Pipe")
obj.Mesh = pipe
doc.recompute()

Inicio

Notas

Una fuente extensa, aunque difícil de usar, de scripts relacionados con la malla son los scripts de pruebas unitarias del módulo Mesh. En estas pruebas unitarias se llaman literalmente todos los métodos y se ajustan todas las propiedades/atributos. Así que si eres lo suficientemente audaz, echa un vistazo al módulo Unit Test.

Ver también: Interfaz de programación de aplicaciones Malla.

Inicio