Skip to main content

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, 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[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]
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/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.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

  • TABLES contains LAYER, LTYPE, and STYLE records from IR tables.
  • GENERIC dimensions expand to their preserved visual primitives by default.
  • R12 declares $DWGCODEPAGE=ANSI_932 and 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

  • constraints are IR-only and are diagnosed when omitted from DXF.
  • A complete affine INSERT.transform cannot be represented by plain DXF INSERT and is diagnosed when omitted.
  • R12 has no true-color, lineweight, or $INSUNITS equivalent 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. ezdgn rejects 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.

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

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cad2d_ir-0.8.0.tar.gz (80.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cad2d_ir-0.8.0-py3-none-any.whl (93.3 kB view details)

Uploaded Python 3

File details

Details for the file cad2d_ir-0.8.0.tar.gz.

File metadata

  • Download URL: cad2d_ir-0.8.0.tar.gz
  • Upload date:
  • Size: 80.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.15

File hashes

Hashes for cad2d_ir-0.8.0.tar.gz
Algorithm Hash digest
SHA256 8e13891189e70ce8d7d7afd09f281ea4f1596199687930ec6b1be6d21e798277
MD5 14c3457bfbd4693e15495f4baf374b49
BLAKE2b-256 41195d043de0957e1ae5b34441f5f4a6250d4927e2efbbae7dac8190eba29752

See more details on using hashes here.

File details

Details for the file cad2d_ir-0.8.0-py3-none-any.whl.

File metadata

  • Download URL: cad2d_ir-0.8.0-py3-none-any.whl
  • Upload date:
  • Size: 93.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.15

File hashes

Hashes for cad2d_ir-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aee6e2b90a2ce2f04a717fac3883385340530c1f46e3371496e2e826ed0f1210
MD5 cdaefa3ebe7fc280cad6828a27d2b39a
BLAKE2b-256 c0b81109f96a0d5e4e933547d07102307ad0fb02e88d5bf4b49df6e193b2a024

See more details on using hashes here.

Release history Release notifications | RSS feed

0.9.8

2 files

0.9.7

2 files

0.9.6

2 files

0.9.5

2 files

0.9.4

2 files

0.9.3

2 files

0.9.2

2 files

0.9.1

2 files

0.9.0

2 files

This release

0.8.0 This release

2 files

0.7.2

2 files

0.7.1

2 files

0.7.0

2 files

0.6.1

2 files

0.6.0

2 files

0.5.0

2 files

0.4.0

2 files

0.3.0

2 files

0.2.0

2 files

0.1.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page