MATLAB tool for generating meshes from DXF files in 2D. Supports Delaunay triangulation and optimized polygonal meshes D-Polylla with Lloyd relaxation for VEM.
- MATLAB (R2018b or later recommended).
- 2D DXF files with regions only, support regions with holes.
- Clone the repository:
git clone https://github.com/mechaide/matlab_mesh_2D.git
Run the following MATLAB script to generate a mesh from a DXF file and automatically export it as mesh.json.
clear; clc;
% Parameters
dxf_file = 'myfile.dxf'; % Path to DXF file
d = 0.5; % Vertex separation
mesh_type = 'dpolylla'; % Mesh type: 'triangle' or 'dpolylla'
decimales = 2; % Decimal places for coordinate rounding
iterations = 3; % Lloyd relaxation iterations
plot_contour = true; % Plot contour
plot_base_mesh = true; % Plot base mesh
plot_frontier = true; % Plot frontier edges (dpolylla)
plot_contour_vertices = true; % Plot contour vertices
plot_mesh = true; % Plot final mesh
% Generate mesh
mesh_data = mechaide_MESH.generateMeshFromDXF(dxf_file, d, mesh_type, ...
decimales, iterations, plot_contour, plot_base_mesh, plot_frontier, plot_mesh);dxf_file: String. Path to DXF file (e.g.,'examples/test3.dxf'). Must contain valid lines/polylines.d: Double (positive). Max vertex spacing and min point distance for Poisson Disk Sampling. E.g.,0.5. Smaller values increase detail but computational cost.mesh_type: String ('triangle'or'dpolylla').'triangle'for fast Delaunay triangulation;'dpolylla'for optimized polygonal mesh (slower).decimales: Integer (non-negative). Decimal places for coordinate rounding. E.g.,2. Higher values increase precision but may reduce performance.iterations: Integer (non-negative). Number of Lloyd relaxation iterations. E.g.,3. More iterations improve mesh uniformity but increase computation time.plot_contour: Boolean.trueto plot the DXF contour. Useful for geometry verification.plot_base_mesh: Boolean.trueto plot the base mesh before relaxation.plot_frontier: Boolean.trueto plot frontier edges (applies only to'dpolylla').plot_contour_vertices: Boolean.trueto plot contour vertices for debugging.plot_mesh: Boolean.trueto plot the final mesh.
Below are example visualizations:
mesh_data: Struct with:coords: Vertex coordinates (Nx2 matrix).connect: Mesh connectivity (Mx3 matrix for triangles or cell array for polygons).contorno: Contour points (matrix).hoyos: Holes (cell array of point matrices).- Additional fields based on
mesh_type.
Inspiration for this project comes from VEMLab by aaortizb, which provided insights into virtual element methods (VEM). The inspiration for this project comes from VEMLab by aaortizb, a project that contains the code to solve 2D static solids with FEM and VEM. The Direct Polylla (D-Polylla) algorithm mesh type in this project is based on the Polylla-Mesh-DCEL algorithm by ssalinasfe. Their work on polygonal meshing using terminal-edge regions and half-edge data structures was adapted for MATLAB.
- Console logs include execution times for key steps (e.g., point generation, contour extraction).
- Disable plotting (
plot_* = false) to speed up execution if only data is needed. - For large geometries, reduce
doriterationsto optimize performance.
MIT License. See LICENSE file for details.
Nicolás Jesús Muñoz Guamán (nicolasjmunoz@gmail.com / nmunoz@mechaide.com)
Original code in JS: www.tesis.mechaide.com


