cad2d-ir
cad2d-ir is a typed intermediate representation and conversion toolkit
for 2D CAD data.
It provides:
- a canonical JSON Schema for 2D CAD geometry and tables;
- native DXF, DWG, DGN, DWF/DWFx, JWW, MI/BI, and SXF import paths;
- DXF R12 (
AC1009) and R2010 (AC1024) export; - structured import/export diagnostics;
- deterministic IR-entity to DXF-handle correspondence.
Installation
pip install cad2d-ir
pip install "cad2d-ir[dwg]"
pip install "cad2d-ir[dgn]"
pip install "cad2d-ir[dwf]"
pip install "cad2d-ir[jww]"
pip install "cad2d-ir[mi]"
pip install "cad2d-ir[sxf]"
pip install "cad2d-ir[all]"
Python 3.10 or newer is required. The core wheel is pure Python; native format adapters are optional extras.
For development:
uv sync
uv sync --all-extras
uv run python -m pytest
Supported data
IR entities include LINE, CIRCLE, ARC, POINT,
ELLIPSE, LWPOLYLINE, SPLINE, TEXT,
MTEXT, INSERT, HATCH, and DIMENSION.
Layer, linetype, text-style, dimension-style, and block tables are represented
by the schema.
Import adapters:
| Format | Path | Dependency |
|---|---|---|
| DXF | built-in parser | core |
| DWG | native ezdwg model |
cad2d-ir[dwg] |
| DGN V7 2D / V8 | native ezdgn models |
cad2d-ir[dgn] |
| DWF/DWFx 2D | normalized ezdwf sheet model |
cad2d-ir[dwf] |
| JWW | native ezjww model |
cad2d-ir[jww] |
| MI / gzip-wrapped BI | native ezmi2d document model |
cad2d-ir[mi] |
| SXF SFC/P21 | backend-neutral ezsxf drawing |
cad2d-ir[sxf] |
Native adapters preserve source semantics and provenance rather than flattening through an intermediate DXF.
CLI
# Validate IR
cad2d-ir validate examples/ir/minimal.json
# DXF -> IR with BOM/codepage/UTF-8/CP932 detection
cad2d-ir dxf2ir drawing.dxf -o drawing.json --pretty
cad2d-ir dxf2ir drawing.dxf --encoding cp932 -o drawing.json
# Auto-detected DXF/DWG/DGN/DWF/DWFx/JWW/MI/BI/SFC/P21 -> IR
cad2d-ir import drawing.JWW -o drawing.json --pretty
cad2d-ir import drawing.dwg -o drawing.json --pretty
cad2d-ir import drawing.dgn -o drawing.json --pretty
cad2d-ir import drawing.dwfx -o drawing.json --pretty
cad2d-ir import drawing.mi -o drawing.json --pretty
cad2d-ir import drawing.p21 -o drawing.json --pretty
# IR -> R2010 or R12 DXF
cad2d-ir ir2dxf drawing.json --target-version AC1024 -o drawing-r2010.dxf
cad2d-ir ir2dxf drawing.json --target-version AC1009 \
--encoding cp932 --curve-segments 128 -o drawing-r12.dxf
Python API
from cad2d_ir import convert_file_to_ir, convert_ir_to_dxf_text
imported = convert_file_to_ir("drawing.dxf", strict=False)
print(imported.statistics["encoding"])
for diagnostic in imported.diagnostics:
print(diagnostic.code, diagnostic.as_dict())
exported = convert_ir_to_dxf_text(
imported.document,
target_version="AC1024",
generic_dimensions="explode",
)
for entry in exported.entity_map:
print(entry["ir_id"], entry["handle"], entry["dxf_type"])
R2010 output assigns deterministic handles and emits $HANDSEED.
Re-importing the DXF records each handle in entity.source.id. R12 omits
handles and retains deterministic index values in the entity map.
Export behavior
TABLEScontains LAYER, LTYPE, and STYLE records from IR tables.- GENERIC dimensions expand to their preserved visual primitives by default.
- R12 declares
$DWGCODEPAGE=ANSI_932and file output uses CP932 by default; LWPOLYLINE becomes POLYLINE/VERTEX, MTEXT becomes TEXT, ELLIPSE/SPLINE become sampled polylines, and HATCH becomes boundary polylines. - Every skip, approximation, explosion, and normalization is represented by an
ExportDiagnostic. - The same document and options produce byte-identical DXF and entity-map output within a package minor version.
Compatibility boundaries
constraintsare IR-only and are diagnosed when omitted from DXF.- A complete affine
INSERT.transformcannot be represented by plain DXF INSERT and is diagnosed when omitted. - R12 has no true-color, lineweight, or
$INSUNITSequivalent with the same semantics; downgrade diagnostics disclose those losses. - HATCH support focuses on polyline-style loops.
- Ellipse start/end parameters are radians independently of
header.angle_unit. - DGN import covers V7 2D and native V8 documents. V7 text encoding
auto-detection probes ASCII, CP932, then Latin-1 at file scope; V8 text
arrives already decoded per element.
ezdgnrejects V7 3D; V8 3D models and multi-model files are reduced to one projected model with explicit loss diagnostics, and V8 shared-cell definitions, fills, color tables, and spline knots are not decoded yet. - DWF/DWFx import preserves sheet identity in metadata while placing supported 2D entities and markups in one IR modelspace. Raster payloads and complex brush semantics remain source metadata/diagnostics rather than invented IR geometry.
- MI import consumes
ezmi2d>=0.2,<0.3directly. Part definitions and shared occurrences become IR blocks and INSERTs; source radians are normalized to IR degrees..bidispatch covers the gzip-wrapped MI container verified byezmi2d, not every historical Drafting/ME10 BI compression variant.
See:
Repository layout
src/cad2d_ir/
api.py
cli.py
diagnostics.py
schema.py
codecs/dxf.py
importers/
data/ir_schema.json
ir_schema.json
tests/
examples/
docs/
License
MIT (LICENSE).
Release files for cad2d-ir 0.9.10
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| cad2d_ir-0.9.10.tar.gz | 110.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| cad2d_ir-0.9.10-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 233.2 kB
Release files / cad2d_ir-0.9.10.tar.gz
| Download URL | cad2d_ir-0.9.10.tar.gz |
|---|---|
| Size | 110.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a8de387627b6ce9acf2e9ab4c6c89ee09e96a4f64562654092d677d03b6136bf
|
|
BLAKE2b-256 checksum How to use checksums |
a66c1eb7f75201d5b6258be1f3dcb80c03adf5f6ecffa2175820be3c1a9af08b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / cad2d_ir-0.9.10-py3-none-any.whl
| Download URL | cad2d_ir-0.9.10-py3-none-any.whl |
|---|---|
| Size | 123.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
20e2acc71c42dd907e32f786748940c8749a601eb9ef04a6b07e4f9ab7b5193e
|
|
BLAKE2b-256 checksum How to use checksums |
423cf28c8ebcf78bd08d0aebbfc3255c40fa31225503183be336a14bc876fe0b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|