<html><head><title>Mesh Scripting/id</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><link type='text/css' href='wiki.css' rel='stylesheet'></head><body><h1>Mesh Scripting/id</h1></div> <div id="mw-content-text" lang="id" dir="ltr" class="mw-content-ltr"><hr/><div class="mw-parser-output"><div id="toc" class="toc"><div class="toctitle"><h2>Contents</h2></div> <ul> <li class="toclevel-1 tocsection-1"><a href="#Introduction"><span class="tocnumber">1</span> <span class="toctext">Introduction</span></a></li> <li class="toclevel-1 tocsection-2"><a href="#Creation_and_Loading"><span class="tocnumber">2</span> <span class="toctext">Creation and Loading</span></a></li> <li class="toclevel-1 tocsection-3"><a href="#Modeling"><span class="tocnumber">3</span> <span class="toctext">Modeling</span></a></li> <li class="toclevel-1 tocsection-4"><a href="#Examining_and_Testing"><span class="tocnumber">4</span> <span class="toctext">Examining and Testing</span></a></li> <li class="toclevel-1 tocsection-5"><a href="#Write_your_own_Algorithms"><span class="tocnumber">5</span> <span class="toctext">Write your own Algorithms</span></a></li> <li class="toclevel-1 tocsection-6"><a href="#Exporting"><span class="tocnumber">6</span> <span class="toctext">Exporting</span></a></li> <li class="toclevel-1 tocsection-7"><a href="#Gui_related_stuff"><span class="tocnumber">7</span> <span class="toctext">Gui related stuff</span></a></li> <li class="toclevel-1 tocsection-8"><a href="#Odds_and_Ends"><span class="tocnumber">8</span> <span class="toctext">Odds and Ends</span></a></li> </ul> </div> <h3><span class="mw-headline" id="Introduction">Introduction</span></h3> <p>First of all you have to import the Mesh module: </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>import Mesh</pre></div> <p>After that you have access to the Mesh module and the Mesh class which facilitate the functions of the FreeCAD C++ Mesh-Kernel. </p> <h3><span class="mw-headline" id="Creation_and_Loading">Creation and Loading</span></h3> <p>To create an empty mesh object just use the standard constructor: </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>mesh = Mesh.Mesh()</pre></div> <p>You can also create an object from a file </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>mesh = Mesh.Mesh('D:/temp/Something.stl')</pre></div> <p>(A list of compatible filetypes can be found under 'Meshes' <a href="/wiki/Feature_list#IO" title="Feature list">here</a>.) </p><p>Or create it out of a set of triangles described by their corner points: </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>planarMesh = [ # 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], ] planarMeshObject = Mesh.Mesh(planarMesh) Mesh.show(planarMeshObject)</pre></div> <p>The Mesh-Kernel takes care about creating a topological correct data structure by sorting coincident points and edges together. </p><p>Later on you will see how you can test and examine mesh data. </p> <h3><span class="mw-headline" id="Modeling">Modeling</span></h3> <p>To create regular geometries you can use the Python script BuildRegularGeoms.py. </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>import BuildRegularGeoms</pre></div> <p>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: </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>t = BuildRegularGeoms.Toroid(8.0, 2.0, 50) # list with several thousands triangles m = Mesh.Mesh(t)</pre></div> <p>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. </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>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</pre></div> <p>Finally, a full example that computes the intersection between a sphere and a cylinder that intersects the sphere. </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>import Mesh, BuildRegularGeoms sphere = Mesh.Mesh( BuildRegularGeoms.Sphere(5.0, 50) ) cylinder = Mesh.Mesh( BuildRegularGeoms.Cylinder(2.0, 10.0, True, 1.0, 50) ) diff = sphere diff = diff.difference(cylinder) d = FreeCAD.newDocument() d.addObject("Mesh::Feature","Diff_Sphere_Cylinder").Mesh=diff d.recompute()</pre></div> <h3><span class="mw-headline" id="Examining_and_Testing">Examining and Testing</span></h3> <h3><span class="mw-headline" id="Write_your_own_Algorithms">Write your own Algorithms</span></h3> <h3><span class="mw-headline" id="Exporting">Exporting</span></h3> <p>You can even write the mesh to a python module: </p> <div class="mw-highlight mw-content-ltr" dir="ltr"><pre>m.write("D:/Develop/Projekte/FreeCAD/FreeCAD_0.7/Mod/Mesh/SavedMesh.py") import SavedMesh m2 = Mesh.Mesh(SavedMesh.faces)</pre></div> <h3><span class="mw-headline" id="Gui_related_stuff">Gui related stuff</span></h3> <h3><span class="mw-headline" id="Odds_and_Ends">Odds and Ends</span></h3> <p>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 <a rel="nofollow" class="external text" href="http://free-cad.svn.sourceforge.net/viewvc/free-cad/trunk/src/Mod/Mesh/App/MeshTestsApp.py?view=markup">Unit Test module</a>. </p><p>See also <a href="Mesh_API.html" title="Mesh API">Mesh API</a> </p> <div style="clear:both"></div> </div> </div> </div><div class="printfooter"> Online version: "<a dir="ltr" href="https://www.freecadweb.org/wiki/index.php?title=Mesh_Scripting/id&oldid=175300">http://www.freecadweb.org/wiki/index.php?title=Mesh_Scripting/id&oldid=175300</a>"</div> <div id="catlinks" class="catlinks" data-mw="interface"></div><div class="visualClear"></div> </div> </div> <div id="mw-navigation"> <h2>Navigation menu</h2> </body></html>