Skip to main content

cad2d-ir-native

The native code (Rust) of cad2d-ir: a typed form of the IR, the DXF writer and the entity conversion of the JWW / JWS importer.

cad2d-ir depends on this package and uses it for both. There is no Python implementation of either next to it (there was one up to 0.17.22; the two were compared byte for byte before the Python one was removed in 0.18.0).

Install

pip install cad2d-ir

installs this package with it: cad2d-ir pins the cad2d-ir-native of its own version, and the two are released together. There are wheels for Linux (x86_64 and aarch64, glibc), Windows (x86_64) and macOS (universal2), one for all Python versions from 3.10 (abi3); elsewhere pip builds the source distribution, which needs a Rust toolchain.

The package has no dependencies of its own and is not meant to be installed alone: the DXF writer calls back into cad2d_ir (see "What stays in Python").

Layout

Path What
crates/cad2d-ir-core The typed IR (Document, Entity, Shape, …), the conversion to and from the dynamic form of a dict, and the DXF writer (src/dxf/: R12 and R2010)
crates/cad2d-ir-jww JWW and JWS entities (ezjww-core) to the typed IR
crates/cad2d-ir-py The Python bindings as a library: register(module) adds the functions and classes to an extension module
src/lib.rs The extension module cad2d_ir_native._core: cad2d-ir-py and nothing else
python/cad2d_ir_native The Python package: the names of the extension module
tests/ test_native.py (the typed document, and that a dict and the typed IR, a file and the dict of ezjww give the same result), test_dxf_golden.py with golden/ (the DXF of a set of documents, as files), test_text_cells.py (a table of the native code against the codec of Python)

The crates take ezjww-core from a released tag of the ezjww repository ([workspace.dependencies] in Cargo.toml), so a build needs nothing next to this repository. To build against a checkout of ezjww instead (a change of the reader that is not released yet), name it in native/.cargo/config.toml, which is not committed:

[patch."https://github.com/monozukuri-ai/ezjww"]
ezjww-core = { path = "../../ezjww/crates/ezjww-core" }

--locked fails with such a patch, since Cargo.lock records the tag.

What stays in Python

Each piece of work is done in one place. What needs the codecs and the tables of Python is done there, and the native code asks for it:

  • DXF writer: the font of a text style (cad2d_ir.fonts.style_font), the code page of an R12 file and what a character it cannot store is written as (_select_r12_codepage, _r12_text_plan in cad2d_ir/codecs/dxf.py), the inline codes of MTEXT and its lines for R12 (cad2d_ir.mtext), and the view of a viewport (cad2d_ir.layouts.viewport_to_dxf_view). These are a few calls per document, not per entity.
  • JWW importer: what comes from the header of a file - layers, line types, the pen table, the metadata and the diagnostics about the document - is made in cad2d_ir/importers/jww.py, which reads the header with ezjww. The native code reads the file itself for the entities. A dict of ezjww.read_document (jww_document_to_ir, JWS symbols, a drawing whose strings are UTF-8) is read into the records of ezjww-core first (crates/cad2d-ir-py/src/jww_dict.rs) and converted by the same code.

cad2d-ir-core/src/num.rs and pystr.rs write numbers and compare strings the way Python does (repr of a float, %g, sum, str.casefold): the DXF text is defined by the Python writer that came first.

Build and test

In a checkout, uv sync at the top of the repository builds this directory and installs it ([tool.uv.sources] in ../pyproject.toml), and builds it again when a file here changes. Then:

uv run pytest -q                  # tests/ and native/tests
cd native
cargo fmt --all --check
cargo clippy --workspace --all-targets --locked -- -D warnings
cargo test -p cad2d-ir-core -p cad2d-ir-jww --locked

Without uv: pip install ./native and then pip install -e ".[jww]" (in this order: the version cad2d-ir pins is the one just built).

The tests with real drawings read ../../ezjww/jww_samples (EZJWW_SAMPLES names another directory) and are skipped without it.

Golden files

tests/golden/<name>.json is an IR document; <name>.ac1024.dxf and <name>.ac1009.dxf are what the writer makes of it, and <name>.report.json the export diagnostics and the entity map. They are what notices a change of the writer's output. When the change is meant:

CAD2D_IR_UPDATE_GOLDEN=1 uv run pytest native/tests/test_dxf_golden.py
git diff native/tests/golden      # review what changed

A number in a DXF may differ in its last digits between platforms (it comes from the sine and cosine of the platform), and the test allows for that; everything else is compared to the byte.

Release

cad2d-ir and cad2d-ir-native are one release. The root pyproject.toml defines the release version. From the repository root, update it with:

uv run --no-sync python scripts/set_version.py 0.18.2

This sets the version, generates the exact cad2d-ir-native dependency pin and the Cargo workspace version, refreshes both lockfiles without requesting dependency upgrades, and runs the release consistency check. It requires uv and Cargo; if a command fails, it restores the previous metadata and lockfiles. Omit the version argument to synchronize after editing the root version. Versions use the stable X.Y.Z form. The command does not commit, tag or publish.

The native Python package gets its version from Cargo through Maturin's dynamic metadata, and cad2d_ir.__version__ reads the installed distribution metadata. Neither needs a separate version edit. CI checks the generated metadata and lockfiles with python scripts/check_release.py and tests/test_package_metadata.py.

When the tag of ezjww-core in native/Cargo.toml changes, the floor of ezjww in the jww and all extras of pyproject.toml follows it.

Pushing the tag v<version> runs .github/workflows/release.yml: it checks the versions and the tag, runs the tests, builds cad2d-ir (wheel and source distribution) and cad2d-ir-native (a wheel on each platform and a source distribution), installs each native wheel with the cad2d-ir of the release and runs native/tests on its platform, and publishes all files to PyPI, cad2d-ir-native first. Run by hand (workflow_dispatch) it does everything but publish.

Publishing uses PyPI trusted publishing: the repository stores no token, and PyPI accepts the upload because it comes from this workflow. Each of the two projects on PyPI names the same publisher (for a project that does not exist yet, as a pending publisher of the account):

Field Value
Owner monozukuri-ai
Repository name cad2d-ir
Workflow name release.yml
Environment name pypi

Use

The writer and the importer are used through cad2d_ir (ir_to_dxf, convert_jww_file_to_ir, …). What this package adds is a document that stays in native code:

from cad2d_ir.importers.base import ImportOptions
from cad2d_ir.importers.jww import import_jww_file

result = import_jww_file("plan.jww", options=ImportOptions(strict=False))
dxf_bytes, export_diagnostics = result.document.to_dxf_bytes()
document = result.document.to_dict()  # the dict convert_jww_file_to_ir returns

result.document is a NativeDocument. Its entities do not become Python objects unless to_dict() is asked for: the entities of a large drawing take a fraction of the memory of their dicts. NativeDocument.from_dict holds any IR dict that way, and to_dxf(target_version=..., curve_segments=..., generic_dimensions=..., entity_map=...) writes it with all the options of ir_to_dxf. to_dxf_bytes() gives the bytes of the DXF R2010 file (UTF-8, lines ending with CR and LF), the same as dxf_text_to_bytes() makes of the text.

The few entities a caller has to look at in Python can be read and changed without the whole document becoming a dict:

for block, index, entity in document.entities_of_kind("IMAGE", without=["data"]):
    pixels = document.entity_bytes(block, index, "data")  # base64 decoded, or None
    document.set_entity_value(block, index, "name", "plan_" + entity["name"])

without leaves keys out of the dicts, and entity_bytes gives the bytes of a base64 value without the text becoming a Python str: the data of an embedded image is as large as its file, which can be hundreds of megabytes.

Native code of an application

An application with Rust code of its own that works on the typed IR (a renderer, an exporter) links cad2d-ir-py into its extension module, calls cad2d_ir_py::register(module) there and takes a NativeDocument as an argument of its own functions (NativeDocument::document() is the typed Document). The classes of two extension modules are different types to Python, so the application uses the bindings of its own module throughout:

result = import_jww_file("plan.jww", options=..., core=my_app._core)
my_app._core.render(result.document)  # a function of the application

core is the extension module whose bindings do the work; it defaults to the one of this package. Unsupported is an exception the bindings define and never raise: it is for the native code of such an application, when it declines a document its Python side can handle in another way.

The typed IR

LINE, CIRCLE, ARC, POINT, ELLIPSE, TEXT, INSERT, HATCH and LWPOLYLINE are structs. Every other entity, and every value that is not of the type the schema gives it, stays dynamic and converts back unchanged, so a document is the same dict after NativeDocument.from_dict(...).to_dict(). Two things are not kept as they came: a tuple is a list afterwards, and the halves of a surrogate pair held as two code points are the character they stand for (a half alone is U+FFFD) - Rust has no such string.

NativeDocument.from_dict raises TypeError for a value JSON does not have (bytes, a set, a key that is no str), which it could not give back. The writer, which only reads, passes over such a value where it is not what is written.

A dict that ir_to_dxf hands over is converted for the writer alone: one entity at a time (the dynamic form of an entity, every key and value an object of its own, is several kilobytes and exists for one entity only), and without what the writer never reads - the source of an entity, and the metadata of the kinds that are written from their fields (dxf::reads_entity_key). An imported drawing carries both on every entity, and without them an entity is about a hundred bytes. NativeDocument keeps everything; the two are written the same (tests/test_native.py).

Measurements

Taken with 0.17.22, when both implementations existed: JWW drawings of lines only (synthetic), one process per run, JWW import and DXF R2010 export, the same DXF for the first two columns. ezjww is ezjww.write_dxf_with_report, whose output is a different DXF.

JWW Python (removed in 0.18.0) native ezjww
0.7 MB 0.58 s, 64 MB 0.16 s, 46 MB 0.05 s, 31 MB
3.6 MB 2.69 s, 195 MB 0.33 s, 96 MB 0.15 s, 70 MB
9.4 MB 7.08 s, 467 MB 0.72 s, 199 MB 0.47 s, 155 MB
15 MB 11.08 s, 709 MB 1.04 s, 282 MB 0.66 s, 218 MB

Metadata

Release files for cad2d-ir-native 0.18.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cad2d-ir-native 0.18.6
File Size Uploaded Provenance Actions
cad2d_ir_native-0.18.6.tar.gz 167.5 kB Without provenance Details for cad2d_ir_native-0.18.6.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for cad2d-ir-native 0.18.6
File Actions
cad2d_ir_native-0.18.6-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details for cad2d_ir_native-0.18.6-cp310-abi3-win_amd64.whl
cad2d_ir_native-0.18.6-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details for cad2d_ir_native-0.18.6-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
cad2d_ir_native-0.18.6-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details for cad2d_ir_native-0.18.6-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
cad2d_ir_native-0.18.6-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 10.12+ universal2 (ARM64, x86-64), macOS 11.0+ ARM64, macOS 10.12+ x86-64 Details for cad2d_ir_native-0.18.6-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl

Total release size: 5.2 MB

Release files / cad2d_ir_native-0.18.6.tar.gz

Download URL cad2d_ir_native-0.18.6.tar.gz
Size 167.5 kB
Tags Source
SHA-256 checksum
How to use checksums
1be29693f154272c0dcd62d7b5ebf80d55e030362246ef420d316fa570a89e51
BLAKE2b-256 checksum
How to use checksums
c30d99b44d7cbe41c721dcc596a52201c25af7dab9c0294d2f95a7ba6cada5bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / cad2d_ir_native-0.18.6-cp310-abi3-win_amd64.whl

Download URL cad2d_ir_native-0.18.6-cp310-abi3-win_amd64.whl
Size 936.2 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
7b29b256655a1d379791f0d3caed756d2770361edac206fdfb44ada070fcb625
BLAKE2b-256 checksum
How to use checksums
4ec39bdeb6cbda7ef5e08df7573d44b08d7ab3318fe825022104fbff4bb375ca
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / cad2d_ir_native-0.18.6-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl

Download URL cad2d_ir_native-0.18.6-cp310-abi3-manylinux2014_x86_64.manylinux_2_17_x86_64.whl
Size 1.1 MB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
3886ded94bfb2eb7e50b58ed990486cce5f1c006dc9be62845bb58fa2d8475b2
BLAKE2b-256 checksum
How to use checksums
52aeb55e8ccd1dcc1de53949677e871a7261a657efe458b4dcb615377150cb6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / cad2d_ir_native-0.18.6-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl

Download URL cad2d_ir_native-0.18.6-cp310-abi3-manylinux2014_aarch64.manylinux_2_17_aarch64.whl
Size 1.0 MB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
9f25b69f3b7aead6a64e09a28d5e94b7c949b968c48c53635f59a9508ae58b80
BLAKE2b-256 checksum
How to use checksums
d4c54915461525f5f15b58c5a8a044dc76e720e3a1924e4bb21128668cdb2307
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / cad2d_ir_native-0.18.6-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl

Download URL cad2d_ir_native-0.18.6-cp310-abi3-macosx_10_12_universal2.macosx_10_12_x86_64.macosx_11_0_arm64.whl
Size 2.0 MB
Tags CPython 3.10 abi3 macOS 10.12+ universal2 (ARM64, x86-64) macOS 10.12+ x86-64 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
4997fecc20217e1a7eb384962d468a9a3c8582d7f26c114f005f632743313441
BLAKE2b-256 checksum
How to use checksums
e09182f5f119a8e8b49815be6f0a850735eb526241e8e44b1efa0dd422677dd0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for cad2d-ir-native 0.18.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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