Skip to content

Repository files navigation

DXF2IMG

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.

Features

  • 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

Examples

Simple Rectangle (Single Part Fill)

Simple Rectangle

Single part drawing with fill area 91.6%

Rectangle with Holes (Punch Detection)

Rectangle with Holes

Drawing with punched holes (white regions), fill area 83.9%, hole area 7.3%

Multi-Part Drawing (keep_all_parts)

Multi-Part

Multi-part drawing with keep_all_parts=True, fill area 70.7%

Complex Test (Lines, Circles, Text)

Complex Test

Complex drawing with rectangle, circle, diagonal line and text, fill area 78.3%

Requirements

  • Python 3.10+
  • Dependencies: ezdxf, PyMuPDF, Shapely, OpenCV, Pillow, numpy

Install with uv:

uv sync

Usage

Basic Usage

Convert a single file:

uv run dxf2img input.dxf output.jpg

Convert a DFT file:

uv run dxf2img input.dft output.jpg

Batch Processing

Convert all DXF/DFT files in a directory:

uv run dxf2img files/ --seed 1

Output files are saved in the same directory with .jpg extension.

CLI Options

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)

Examples

Single file with fixed seed:

uv run dxf2img drawing.dxf result.jpg --seed 42

Batch with auto multi-part detection:

uv run dxf2img drawings/ --seed 1 --auto-keep-all

DFT file with circle holes:

uv run dxf2img part.dft output.jpg --circle-mode hole

Output

The 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

Fill Statistics

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 Layer 12 Handling

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:

  1. Excluded from fill analysis (material is retained, not punched out)
  2. 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.

Technical Details

Coordinate Mapping

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

Fill Detection Pipeline

  1. Render: DXF/DFT → PyMuPDF → PIL RGB image
  2. Line extraction: Convert black lines to binary mask
  3. Flood fill: Identify main material region
  4. Hole detection: 2b/2c algorithm for enclosed voids
  5. Circle handling: Optional hole mode for CIRCLE entities
  6. DFT Layer 12: Excluded from analysis, rendered as dashed overlay

Multi-Part Detection

--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.

Project Structure

DXF2img/
├── dxf2img.py          # Main conversion logic
├── pyproject.toml      # Dependencies (uv)
├── files/              # Input/output directory
└── README.md

Verification

Run consistency checks on output:

uv run python verify_all.py

Checks circle counts and fill areas against expected values.

Common Issues

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

License

MIT

Author

Xing Wu

About

Convert DXF/DFT drawings to annotated JPG images with automatic fill analysis, hole detection, and bend line rendering for sheet metal CAD visualization.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages