Convert DXF/DFT drawings to annotated JPG images with automatic fill analysis.
A Python tool for visualizing sheet metal CAD drawings with intelligent fill detection, hole identification, and bend line/forming mark rendering.
- Dual Format Support: DXF (via ezdxf) and DFT (Metalix cncKad) files
- Automatic Fill Analysis: Flood fill with 2b/2c hole detection
- DFT Layer 12 Handling: Bend lines and forming marks rendered as dashed lines (excluded from fill analysis)
- Circle Mode: Configurable hole detection for punched circles
- Multi-Part Support: Auto-detection of segmented/nested drawings
- Reproducible Colors: Seed-based random fill colors
- Batch Processing: CLI for directory-wide conversion
Single part drawing with fill area 91.6%
Drawing with punched holes (white regions), fill area 83.9%, hole area 7.3%
Multi-part drawing with keep_all_parts=True, fill area 70.7%
Complex drawing with rectangle, circle, diagonal line and text, fill area 78.3%
- Python 3.10+
- Dependencies:
ezdxf,PyMuPDF,Shapely,OpenCV,Pillow,numpy
Install with uv:
uv syncConvert a single file:
uv run dxf2img input.dxf output.jpgConvert a DFT file:
uv run dxf2img input.dft output.jpgConvert all DXF/DFT files in a directory:
uv run dxf2img files/ --seed 1Output files are saved in the same directory with .jpg extension.
dxf2img <input> <output> [OPTIONS]
Arguments:
input Input file or directory (DXF/DFT)
output Output file (JPG) or directory
Options:
--seed SEED Random seed for fill colors (default: random)
--dpi DPI Render DPI (default: 300)
--circle-mode MODE Circle handling: 'hole' (punch white) or 'keep' (default: hole)
--keep-all-parts Keep all detected parts (for multi-part drawings)
--auto-keep-all Auto-detect segmented drawings (area heuristic)
--margins MM Edge margins in mm (default: 2.0)
--snap MM Coordinate snap tolerance (default: 0.05)
Single file with fixed seed:
uv run dxf2img drawing.dxf result.jpg --seed 42Batch with auto multi-part detection:
uv run dxf2img drawings/ --seed 1 --auto-keep-allDFT file with circle holes:
uv run dxf2img part.dft output.jpg --circle-mode holeThe tool generates JPG images with:
- Filled regions: Colored areas representing material (random color per seed)
- Holes: White regions for punched circles and detected voids
- Dashed lines: DFT Layer 12 entities (bend lines, forming marks) rendered as black dashed lines
- Solid lines: Cutting contours and part outlines
Console output includes:
[OK] Conversion complete! Output: files\part.jpg
Image size: 17916x1946 pixels
Fill area: 32974298 pixels (94.6%)
Hole area: 194187 pixels (0.6%)
- Fill %: Material coverage (higher = more solid material)
- Hole %: Void coverage (punched holes, slots, cutouts)
DFT files from Metalix cncKad use Layer 12 for dashed mark entities:
- Bend lines: Parallel lines indicating bend zones
- Forming marks: Rounded rectangles or other forming indicators
These entities are:
- Excluded from fill analysis (material is retained, not punched out)
- Rendered as dashed lines in output (matching original drawing style)
Layer 12 entities include both LINE and ARC primitives. Arcs are sampled into 32-segment polylines for consistent dashed rendering.
DXF coordinates are mapped to pixel space using a 6-tuple transform:
img_x = origin_x + (dxf_x - content_min_x) * scale_x
img_y = origin_y + (content_max_y - dxf_y) * scale_y # Y-axis flipped
- Render: DXF/DFT → PyMuPDF → PIL RGB image
- Line extraction: Convert black lines to binary mask
- Flood fill: Identify main material region
- Hole detection: 2b/2c algorithm for enclosed voids
- Circle handling: Optional hole mode for CIRCLE entities
- DFT Layer 12: Excluded from analysis, rendered as dashed overlay
--auto-keep-all triggers when:
- Largest region < 70% of total
- Second-largest > 50% of largest, OR
- Removed area > 50% of largest
Useful for segmented drawings where flood fill splits the material.
DXF2img/
├── dxf2img.py # Main conversion logic
├── pyproject.toml # Dependencies (uv)
├── files/ # Input/output directory
└── README.md
Run consistency checks on output:
uv run python verify_all.pyChecks circle counts and fill areas against expected values.
Low fill percentage (< 70%)
- May indicate real large cutouts (e.g., K26B5640 with 25% hole area)
- Or segmented drawing needing
--auto-keep-all
Missing holes
- Ensure
--circle-mode hole(default) - Check circle layer assignments in source file
DFT bend lines not dashed
- Verify Layer 12 entities are present in DFT [300] section
- Layer number is in LINE second-row penultimate field, ARC first-row 4th field
MIT
Xing Wu