DEV Community

Cover image for CAD File Import and Mesh Generation with Gmsh
Precise Simulation
Precise Simulation

Posted on • Originally published at featool.com

CAD File Import and Mesh Generation with Gmsh

Gmsh is a popular cross platform and open source mesh generation software which supports many CAD file formats such as STEP, IGES, and STL. In addition to its own mesh generation algorithms, Gmsh also includes support for a variety of integrated mesh generators, such as Netgen, Tetgen, and Triangle. As such is it one of the most flexible mesh generators, and can be used with FEATool Multiphysics to allow meshing complex geometries and models than the default built-in grid generator.

Although FEATool includes built-in support for import and automatic meshing of CAD geometries, controlling the mesh generation steps manually offer more control and can result in higher quality meshes, and more accurate simulation results. Manual meshing can for instance be useful for more difficult 3D geometries, which may require repair or defeaturing in order to allow for creation of good meshes for simulation.

The following section explains how to use Gmsh to manually load and import a CAD geometry from a STEP file, and then generate a mesh which can be imported and used with FEATool Multiphysics.

Gmsh CAD File Import and Mesh Generation Tutorial

This is a tutorial showing how to import a STEP model into Gmsh, and
generate a 3D volume mesh for finite element analysis (FEA)
simulations. The STEP file and CAD model used in the example can be
downloaded from the link below (but any STEP model should technically
work)

Fixed Spanner CAD STEP Model

1. Start Gmsh (using version 2 Mesh File Format)

If the resulting mesh is to be used with the FEATool Multiphysics toolbox (or the Gmsh MATLAB API), it is important that the Gmsh mesh file is saved in Gmsh ASCII mesh file format version 2. This format is default in Gmsh versions 2-3, but not in the current Gmsh versions 4.x.

To make sure Gmsh saves and exports meshes in version 2 format, one can either start Gmsh with the command line argument -format msh2, for example

gmsh.exe -format msh2
Enter fullscreen mode Exit fullscreen mode

or set the following option in the general or mesh specific Gmsh .opt options file

Mesh.MshFileVersion = 2;
Enter fullscreen mode Exit fullscreen mode

The option files can be generated by selecting Save Model Options or Save Options As Default in the File menu of the Gmsh GUI.

If there are issues importing the mesh into FEATool one can also optionally try to add the -save_all command line argument which corresponds to the Mesh.SaveAll = 1; .opt file option.

2. CAD Model Import with the Merge Operation

The second step is to use the Merge… option, from the File menu in the Gmsh menubar. Choosing this menu option opens a file selection dialog box where the CAD file to import can be chosen. In this example, select the spanner.step file and press the Open button to automatically let Gmsh load, import, and construct edges and faces for the geometry. If the model has been imported successfully it should be displayed and shown in the main GUI window of Gmsh (you can use mouse controls to rotate and zoom in/out).

Gmsh CAD Step File Import And Mesh Generation Tutorial - Merge

3. Setting Mesh Options and Specifying Grid Size

Gmsh supports several options to specify mesh and grid sizes. One can for example prescribe specific sizes at various points, or define functions to determine variable mesh sizing. Here the Min/Max element size option is used to set a global maximum mesh size of 6 mm (as the units in this CAD model is defined in millimeters). This option can be found on the General tab, in the Mesh section of the Options dialog box (which is accessed by selecting Options from the Tools menu).

Gmsh CAD Step File Import And Mesh Generation Tutorial - Options

A description of the different mesh options is available in the Mesh options section of the Gmsh reference guide.

4. 3D Mesh and Grid Generation

To generate a 3D volume mesh, first click on the [+] Mesh node in the left hand side tree list to expand it, then click to the 3D node to start and run the selected mesh generation algorithm. Once the mesh generation algorithm has finished the resulting mesh should be displayed in the Gmsh GUI window.

Gmsh CAD Step File Import And Mesh Generation Tutorial - 3D

5. Mesh Info, Saving, and Mesh Export

Progress information and messages (and errors) are displayed in the bottom line of the Gmsh UI. Clicking on this line also expands and opens the message and log window, where one can see information and statistics from the mesh generation process.

Once a mesh has been generated it can be saved and exported by using the Save Mesh option from the File menu (the file will be saved in the same directory and named as the imported CAD file but with the .msh file extension).

Gmsh CAD Step File Import And Mesh Generation Tutorial - Info and Save

Gmsh Import and Export in FEATool Multiphysics

To import and use a Gmsh .msh file with FEATool Multiphysics use the Import Grid > Gmsh Format… option found under the Grid mode menu of the toolbox GUI (note that the mesh file has to have the same 2D or 3D space dimension as the model, and be in Gmsh version 2 ASCII format to be compatible with the toolbox).

FEATool and MATLAB Gmsh Mesh and Grid Import

An illustrative example on how to import the spanner CAD model, define, set up, and run simulations in FEATool Multiphysics is explained in the linked tutorial for a structural mechanics simulation with a parametric displacement study.

MATLAB Gmsh Import and Export Functionality

Alternatively, the impexp_gmsh function, from the MATLAB programming and scripting API, can be used directly on the command line to import a Gmsh mesh directly into MATLAB. The following commands imports, exports, and visualizes the mesh in MATLAB

input_file = 'C:\temp\spanner.msh';   % Full path to input mesh file.
mesh = impexp_gmsh( input_file, 'import' );

plotgrid( mesh )   % Plot and visualize mesh

plotsubd( mesh )   % Plot and visualize subdomains

plotbdr( mesh )    % Plot and visualize boundaries

output_file = 'C:\temp\output.msh';   % Full path to output mesh file.
impexp_gmsh( output_file, 'export', mesh );
Enter fullscreen mode Exit fullscreen mode

Toolbox Availability and Download

The FEATool Multiphysics™ toolbox is available as stand-alone
Desktop App, and also as MATLAB toolbox Add-Ons, with fully interactive GUI and cross-platform support for the Microsoft Windows, Linux, and MacOS operating systems. The toolboxes can be downloaded directly from the homepage https://www.featool.com (or installed with one-click from the MATLAB Add-Ons Toolbar).

Top comments (0)