Mesh Scripting/de: Difference between revisions
Renatorivo (talk | contribs) (Created page with "Danach haben Sie Zugriff auf das Mesh-Modul und die Mesh-Klasse, die die Funktionen des FreeCAD C++ Mesh-Kernel erleichtern.") |
(Updating to match new version of source page) |
||
(75 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
<languages/> |
|||
=== Einführung === |
|||
{{TOCright}} |
|||
==Einführung== |
|||
Um Zugriff auf das Modul {{incode|Mesh}} zu erhalten, musst du es zuerst importieren: |
|||
{{Code|code= |
|||
Zunächst einmal müssen Sie das Mesh-Modul importieren: |
|||
<syntaxhighlight> |
|||
import Mesh |
import Mesh |
||
}} |
|||
</syntaxhighlight> |
|||
Danach haben Sie Zugriff auf das Mesh-Modul und die Mesh-Klasse, die die Funktionen des FreeCAD C++ Mesh-Kernel erleichtern. |
|||
==Erstellung== |
|||
=== Creation and Loading === |
|||
To create an empty mesh object just use the standard constructor: |
|||
Um ein leeres Netz Objekt zu erstellen, benutze einfach den Standard Konstruktor: |
|||
<syntaxhighlight> |
|||
{{Code|code= |
|||
mesh = Mesh.Mesh() |
mesh = Mesh.Mesh() |
||
}} |
|||
</syntaxhighlight> |
|||
Du kannst auch ein Objekt aus einer Datei erstellen: |
|||
You can also create an object from a file |
|||
{{Code|code= |
|||
<syntaxhighlight> |
|||
mesh = Mesh.Mesh( |
mesh = Mesh.Mesh("D:/temp/Something.stl") |
||
}} |
|||
</syntaxhighlight> |
|||
Oder erstelle es aus einer Reihe von durch ihre Eckpunkte beschriebenen Dreiecken: |
|||
(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], |
||
] |
] |
||
meshObject = Mesh.Mesh(triangles) |
|||
Mesh.show( |
Mesh.show(meshObject) |
||
}} |
|||
</syntaxhighlight> |
|||
Der Netz-Kernel kümmert sich um die Schaffung einer topologisch richtigen Datenstruktur, durch Sortieren deckungsgleicher Punkte und Kanten. |
|||
The Mesh-Kernel takes care about creating a topological correct data structure by sorting |
|||
{{Top}} |
|||
coincident points and edges together. |
|||
==Modellieren== |
|||
Um regelmäßige Geometrien zu erstellen, kannst Du eine der {{incode|create*()}} Methoden verwenden. Ein Torus kann zum Beispiel wie folgt erstellt werden: |
|||
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) |
|||
}} |
|||
Die ersten beiden Parameter definieren die Radien des Torus und der dritte Parameter ist ein Teilabtastfaktor dafür, wie viele Dreiecke erzeugt werden. Je höher dieser Wert, desto glatter das Netz. |
|||
<syntaxhighlight> |
|||
import BuildRegularGeoms |
|||
</syntaxhighlight> |
|||
Das {{incode|Netz}} Modul bietet auch drei boolesche Methoden: {{incode|vereinigen()}}, {{incode|kreuzung()}} und {{incode|differenz()}}: |
|||
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 68: | 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> |
|||
Hier ist ein Beispiel, das eine Pipe mit der Methode {{incode|difference()}} erstellt: |
|||
Finally, a full example that computes the intersection between a sphere and a cylinder that intersects the sphere. |
|||
{{Code|code= |
|||
<syntaxhighlight> |
|||
import |
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 |
|||
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}} |
|||
==Hinweise== |
|||
Eine umfangreiche, wenn auch schwer zu bedienende Quelle für auf Mesh bezogenes Skripten sind die Einheitstestkripte des {{incode|Mesh}} Moduls. |
|||
=== Examining and Testing === |
|||
In diesen Komponententests werden buchstäblich alle Methoden aufgerufen und alle Eigenschaften/Attribute optimiert. |
|||
Wenn Du also kühn genug bist, schau Dir das [http://free-cad.svn.sourceforge.net/viewvc/free-cad/trunk/src/Mod/Mesh/App/MeshTestsApp.py?view=markup Komponententestmodul] an. |
|||
=== 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]] |
|||
Siehe auch: [[Mesh_API/de|Mesh API]] |
|||
{{Top}} |
|||
{{Powerdocnavi{{#translation:}}}} |
|||
[[Category:Developer Documentation{{#translation:}}]] |
|||
[[Category:Python Code{{#translation:}}]] |
|||
{{Mesh Tools navi{{#translation:}}}} |
|||
{{clear}} |
{{clear}} |
||
<languages/> |
Latest revision as of 08:28, 5 September 2021
Einführung
Um Zugriff auf das Modul Mesh
zu erhalten, musst du es zuerst importieren:
import Mesh
Erstellung
Um ein leeres Netz Objekt zu erstellen, benutze einfach den Standard Konstruktor:
mesh = Mesh.Mesh()
Du kannst auch ein Objekt aus einer Datei erstellen:
mesh = Mesh.Mesh("D:/temp/Something.stl")
Oder erstelle es aus einer Reihe von durch ihre Eckpunkte beschriebenen Dreiecken:
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)
Der Netz-Kernel kümmert sich um die Schaffung einer topologisch richtigen Datenstruktur, durch Sortieren deckungsgleicher Punkte und Kanten.
Modellieren
Um regelmäßige Geometrien zu erstellen, kannst Du eine der create*()
Methoden verwenden. Ein Torus kann zum Beispiel wie folgt erstellt werden:
m = Mesh.createTorus(8.0, 2.0, 50)
Mesh.show(m)
Die ersten beiden Parameter definieren die Radien des Torus und der dritte Parameter ist ein Teilabtastfaktor dafür, wie viele Dreiecke erzeugt werden. Je höher dieser Wert, desto glatter das Netz.
Das Netz
Modul bietet auch drei boolesche Methoden: vereinigen()
, kreuzung()
und differenz()
:
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
Hier ist ein Beispiel, das eine Pipe mit der Methode difference()
erstellt:
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()
Hinweise
Eine umfangreiche, wenn auch schwer zu bedienende Quelle für auf Mesh bezogenes Skripten sind die Einheitstestkripte des Mesh
Moduls.
In diesen Komponententests werden buchstäblich alle Methoden aufgerufen und alle Eigenschaften/Attribute optimiert.
Wenn Du also kühn genug bist, schau Dir das Komponententestmodul an.
Siehe auch: Mesh API
- 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
- Miscellaneous: Import mesh, Export mesh, Create mesh from shape, Regular solid, Unwrap Mesh, Unwrap Face
- Modifying: Harmonize normals, Flip normals, Fill holes, Close hole, Add triangle, Remove components, Remove components by hand, Smooth, Refinement, Decimation, Scale
- Boolean: Union, Intersection, Difference
- Cutting: Cut mesh, Trim mesh, Trim mesh with a plane, Create section from mesh and plane, Cross-sections
- Components and segments: Merge, Split by components, Create mesh segments, Create mesh segments from best-fit surfaces