Stencilmesh is a Python tool that turns DXF outlines into dense square-aperture mesh patterns.
It is designed for workflows where you want to convert custom 2D shapes into fabrication-ready micro-mesh, such as stainless steel ventilation meshes meant to be ordered through SMT stencil services.
- reads DXF files
- detects closed regions from:
- closed
LWPOLYLINE - closed
POLYLINE CIRCLE
- closed
- can also reconstruct regions from loose sketch geometry such as:
LINEARCSPLINEELLIPSE
- fills each region with a square aperture grid (0.3mm squares spaced 0.5mm apart center to center)
- tries multiple grid offsets and keeps the densest valid result
- supports multiprocessing across regions
- can export:
- filled DXF output
- stencil-style Gerber bundle output (very basic implementation, still requires a lot of work)
- custom terrarium ventilation mesh
- stainless vent inserts
- stencil-based perforated sheet experiments
- turning Inventor-exported sketches into dense fabrication geometry
Requires Python 3.10+.
Install dependencies:
pip install ezdxf shapelyOptional:
pip install psutilpsutil is not required, but it improves automatic worker selection when multiprocessing is enabled.
Generate a filled DXF:
py stencilmesh.py --input grate_top_face.dxf --output grate_top_face_filled.dxf --margin 0.2Only write the generated mesh:
py stencilmesh.py --input grate_top_face.dxf --output grate_top_face_filled.dxf --margin 0.2 --replaceGenerate a stencil-style Gerber bundle as well:
py stencilmesh.py --input grate_top_face.dxf --output grate_top_face_filled.dxf --margin 0.2 --gerber-dir gerbers --gerber-name grate_top_face --zip-gerbers-
--input
Input DXF file -
--output
Output DXF file -
--square-size
Square aperture size
Default:0.3 -
--pitch
Grid pitch, center to center
Default:0.5 -
--margin
Minimum inward border to keep clear
Default:0.0 -
--grid-search
Number of grid offsets tried per axis
Default:8 -
--workers
Number of worker processes
Default:0(auto) -
--replace
Write only the generated mesh, without the original DXF entities -
--gerber-dir
Also write a stencil-style Gerber bundle to a directory -
--gerber-name
Base filename for Gerber output
Default:stencilmesh -
--zip-gerbers
Zip the Gerber bundle after writing it -
--quiet
Suppress progress output
If --workers is omitted, or set to 0, Stencilmesh chooses a worker count automatically.
Auto mode is region-aware and conservative:
- it never uses more workers than there are regions
- it prefers physical CPU cores minus 2
- if physical core detection is unavailable, it falls back to logical CPU cores minus 2
- it always uses at least 1 worker
In practice, the behavior is:
if region_count <= 1:
workers = 1
else:
workers = min(region_count, max(1, detected_cores - 2))This keeps the system responsive while still speeding up larger multi-region jobs.
Stencilmesh shrinks each region by the requested --margin, then tries multiple grid offsets and keeps the densest valid fill.
Apertures are only placed if they fit fully inside the usable region.
If you want a larger clear border, increase --margin.
The script prefers real closed DXF entities when they are available.
If the file does not contain closed polylines or circles, it falls back to reconstructing loops from loose sketch-style geometry. This is especially useful for Autodesk Inventor sketch exports, which often come out as separate LINE and ARC entities instead of closed polylines.
Stencilmesh can also write a simple stencil-style Gerber bundle:
GTPfor the aperture patternGKOfor the outlineREADME.txtwith a fabrication note
This export is intentionally minimal and aimed at stencil-style workflows rather than full PCB fabrication output.
For larger jobs the script prints timestamped progress updates to stderr, for example:
- reading DXF
- extracting fill regions
- reporting region count
- fitting regions
- writing DXF
- writing Gerbers
Use --quiet to suppress progress output.
- dense outputs can produce very large DXF files
- some DXF viewers may struggle with very high entity counts
- loop reconstruction from loose sketch geometry works well for many CAD exports, but not every broken DXF can be repaired automatically
- Gerber export is intentionally simple and aimed at stencil-style output, not full PCB fabrication
- very large fills may take time, especially with higher
--grid-searchvalues
This project started as a practical way to generate custom-shaped fine stainless ventilation mesh from DXF geometry without fighting CAD performance on huge repeated patterns.
MIT

