A Python converter for binary CGM (Computer Graphics Metafile) files to SVG, with specific support for the military and aerospace profiles used in defense technical publications programs.
CGM is an ISO 8632 vector graphics format from the late 1980s that has survived in defense and aerospace technical documentation almost entirely through contractual inertia. Programs started under MIL-D-28003 and MIL-STD-28003 requirements are still delivering technical manuals in CGM format today because nobody has gone back to update the contract data requirements list.
SVG is strictly better in every dimension — human readable, browser native, scales perfectly, universally supported. But until every program migrates away from CGM requirements, someone has to convert the files. Existing open source CGM converters handle generic CGM reasonably well but tend to fail on the military-specific profiles in ways that are difficult to debug: polygons covering illustrations, black outlines on filled shapes, misidentified colors, and truncated drawings.
This converter was built to handle real Army and aviation CGM files correctly. It came out of the development of Ironmark Authoring Suite, a structured XML authoring platform for MIL-STD-40051, S1000D, and allied technical publications standards.
The code is released under the MIT license because better tools make better manuals, and better manuals help the people who actually use them in the field. If this accelerates the obsolescence of CGM as an authoring format, that is a feature not a bug.
cgm2svg auto-detects the CGM version and application profile from the metafile header and applies profile-specific defaults:
| Profile | Standard | Detection |
|---|---|---|
| ATA GREXCHANGE | ATA iSpec 2200 / S1000D | ProfileId:ATA GRAPHICS.GREXCHANGE in description |
| WebCGM | WebCGM 1.0 / 2.0 / 2.1 | ProfileId:WebCGM in description |
| CALS V1 | MIL-D-28003 | MIL-D-28003 in description, or CGM version 1 |
| CALS V3 | MIL-STD-28003 | MIL-STD-28003 / CALS in description |
| PIP | Process Plant Industry | PIP / PROCESS in description |
| Generic | Unknown | Fallback — inferred from version and element types |
Profile-specific defaults applied automatically:
- CALS V1 — Integer VDC (0–32767), indexed colour only, Hershey stroke fonts
- PIP — Real VDC (engineering coordinates), direct colour
- ATA / WebCGM / Generic — Standard ISO 8632 defaults, auto-adapted from metafile descriptor elements
- Polyline, disjoint polyline
- Polygon, polygon set
- Circle
- Circular arc (3-point)
- Circular arc (3-point, closed)
- Circular arc (centre, radius, angles)
- Circular arc (centre close — pie and chord)
- Ellipse
- Elliptical arc
- Rectangle
- Closed figure / arrowhead
- Polybezier (cubic spline)
- Cell array (raster image block — rendered as placeholder)
- Text, restricted text, append text
- Line colour, line width, line type (solid, dashed, dotted)
- Fill colour, fill style (solid, hollow, hatch, empty)
- Edge visibility, edge colour
- Text colour, character height, text alignment
- Colour selection mode (indexed and direct)
- ATA Mode 3 extended colour encoding
- VDC integer and real precision
- Colour precision and value extent
APS hotspot coordinates — ATA GREXCHANGE and WebCGM CGM files often embed Application Structure (APS) hotspot region coordinates as trailing data in polygon set elements. These coordinates are in the same VDC space as drawing coordinates and cannot be distinguished from legitimate polygon points by value alone. cgm2svg uses a proximity filter: points beyond 3× the triangle's own span from its centroid are treated as APS data and dropped. This correctly handles the Army TM files tested but may need tuning for other datasets.
Cell array (raster images) — CGM cell arrays (embedded raster image blocks) are rendered as a semi-transparent gray placeholder rectangle. Full pixel-level raster decode is not implemented. This affects CGM files that embed photographs or raster artwork directly in the vector file.
Hershey fonts — CALS V1 files using Hershey stroke fonts render with system fallback fonts. Hershey stroke font rendering is not implemented.
Elliptical arcs — Rendered as full ellipses (the arc extent is approximated). Partial elliptical arcs will appear as complete ellipses. Full arc extent computation is on the roadmap.
No dependencies required for SVG output:
python cgm2svg.py input.CGMFor PNG output, install one of:
pip install cairosvg # preferred — high quality
pip install Pillow # fallback via rsvg-convert or ImageMagick# Single file — produces input.svg
python cgm2svg.py input.CGM
# Specify output path
python cgm2svg.py input.CGM -o output.svg
# Also produce PNG
python cgm2svg.py input.CGM --png
# PNG at specific DPI
python cgm2svg.py input.CGM --png --dpi 150
# Batch convert a directory
python cgm2svg.py *.CGM --out-dir ./svg_out
# Batch convert with PNG
python cgm2svg.py *.CGM --png --out-dir ./outThe converter logs the detected profile for each file:
SVG → output.svg [v4/ata_grexchange]
SVG → output.svg [v4/webcgm]
SVG → output.svg [v1/cals_v1]
Military CGM illustrations are typically designed for a white background — line art with black or colored lines on white. When rendered on a dark background (as in the Ironmark IETP viewer), SVG files need a background color applied by the host application. The converter does not embed a background rectangle in the SVG output. The calling application is responsible for providing an appropriate background.
Pull requests welcome. The most useful contributions:
- Improved cell array / raster image decode
- Hershey stroke font rendering
- Full elliptical arc extent computation
- Additional CGM profiles or edge cases from real-world files
- Test fixtures — real CGM files that currently convert incorrectly
If you have CGM files from a specific program or standard that are not converting correctly, opening an issue with the file (or a sanitized version) and a description of what the output should look like is the fastest way to get it fixed.
MIT. Use it, modify it, incorporate it into your tools. If it helps CGM become a legacy input format that nobody needs to author natively anymore, that is the intended outcome.
- ralcgm — Reference CGM converter (C)
- Ironmark Authoring Suite — Structured XML authoring for MIL-STD-40051, S1000D, and allied technical publications standards
- ISO 8632 — CGM standard (paid document)
- MIL-D-28003 — CALS CGM profile (superseded)
- MIL-STD-28003 — Updated CALS CGM profile
- ATA iSpec 2200 — Aviation technical publications standard
- S1000D — International specification for technical publications