← DWG SDK
API Reference

C API

The C ABI — one small, stable boundary. Lifecycle, read (compile to a FlatBuffers document), and write (apply an edit script, then serialize). UTF-8 in and out, opaque handles, status codes.

Quick start

#include "dwg.h"

dwg_doc *doc = NULL;
if (dwg_doc_open_path("drawing.dwg", &doc) != DWG_OK) {
    fprintf(stderr, "%s\n", dwg_last_error());
    return 1;
}

dwg_buffer *document = NULL;
dwg_doc_compile(doc, &document);          /* FlatBuffers document (schema/dwg_db.fbs) */
const uint8_t *data = dwg_buffer_data(document);
size_t len = dwg_buffer_len(document);
/* read `data`/`len` with the flatc-generated C++ accessors */

dwg_buffer_free(document);
dwg_doc_close(doc);

Status codes

Every entry point returns dwg_status; DWG_OK is 0.

Code#Meaning
DWG_OK0
DWG_ERR_IO1A file could not be read from disk.
DWG_ERR_PARSE2The bytes are not a DWG this reader can parse.
DWG_ERR_UNSUPPORTED_VERSION3A recognisable DWG whose version this build does not read.
DWG_ERR_UNSUPPORTED_FAMILY4The operation is not supported for this object's family — notably an edit to a family with no byte-exact writer.
DWG_ERR_INVALID_ARG5A null pointer, or a string that was not valid UTF-8.
DWG_ERR_INTERNAL6A panic crossed into the boundary and was caught. A bug; please report.
DWG_ERR_UNLICENSED7A paid operation (write, apply) was attempted without a valid licence. Reading and compiling never return this.

Functions

dwg_last_error

const char *dwg_last_error(void)

Result of every entry point. `DWG_OK` is 0; everything else is a failure and leaves any out-parameter untouched. / enum dwg_status #if defined(__cplusplus) || __STDC_VERSION__ >= 202311L : int32_t #endif // defined(__cplusplus) || __STDC_VERSION__ >= 202311L { DWG_OK = 0, /** A file could not be read from disk. / DWG_ERR_IO = 1, /** The bytes are not a DWG this reader can parse. / DWG_ERR_PARSE = 2, /** A recognisable DWG whose version this build does not read. / DWG_ERR_UNSUPPORTED_VERSION = 3, /** The operation is not supported for this object's family — notably an edit to a family with no byte-exact writer. / DWG_ERR_UNSUPPORTED_FAMILY = 4, /** A null pointer, or a string that was not valid UTF-8. / DWG_ERR_INVALID_ARG = 5, /** A panic crossed into the boundary and was caught. A bug; please report. / DWG_ERR_INTERNAL = 6, /** A paid operation (write, apply) was attempted without a valid licence. Reading and compiling never return this. / DWG_ERR_UNLICENSED = 7, }; #ifndef __cplusplus #if __STDC_VERSION__ >= 202311L typedef enum dwg_status dwg_status; #else typedef int32_t dwg_status; #endif // __STDC_VERSION__ >= 202311L #endif // __cplusplus

/** A block of bytes produced by the library. Freed with [`dwg_buffer_free`]. / typedef struct dwg_buffer dwg_buffer;

/** An open DWG document. Owns its decompressed sections and object inventory. / typedef struct dwg_doc dwg_doc;

#ifdef __cplusplus extern "C" { #endif // __cplusplus

/** Detail for the last failure on this thread, or `NULL` if the last call succeeded.

The pointer is owned by the library and stays valid until the next `dwg_*` call on this thread. Copy it if you need to keep it.

Safety. The returned pointer must not be freed or used after the next call.

dwg_set_license

dwg_status dwg_set_license(const char *key)

Install a licence key (the hex text issued to the customer).

This is the authenticate call the whole SDK requires: in a release build `dwg_doc_open`, `dwg_doc_compile`, `dwg_doc_write` and `dwg_doc_apply` all return `DWG_ERR_UNLICENSED` until a valid key is installed. Call it once at startup, before opening any document. Returns `DWG_ERR_UNLICENSED` if the key is malformed or its signature does not verify. In a debug build the gate is compiled out, so this is accepted but not consulted.

Safety. `key` must be a valid NUL-terminated UTF-8 string.

dwg_doc_open

dwg_status dwg_doc_open(const uint8_t *bytes, size_t len, dwg_doc **out)

Parse a DWG from memory. Every version from R13 to R2018.

Safety. `bytes` must point to at least `len` readable bytes, and `out` must be a valid, writable `dwg_doc*`. The buffer is not retained — the document owns a copy of everything it needs.

dwg_doc_open_path

dwg_status dwg_doc_open_path(const char *utf8_path, dwg_doc **out)

Parse a DWG from a UTF-8 path.

Safety. `utf8_path` must be a valid NUL-terminated UTF-8 string, `out` a valid writable `dwg_doc*`.

dwg_doc_close

void dwg_doc_close(dwg_doc *doc)

Close a document and free everything it owns.

Passing `NULL` is a no-op, so this is safe to call unconditionally in a cleanup path.

Safety. `doc` must have come from `dwg_doc_open*` and must not be used afterwards. Buffers already handed out stay valid — they do not borrow the document.

dwg_doc_version

const char *dwg_doc_version(const dwg_doc *doc)

The document's version string, e.g. `"AC1032"`.

Borrowed from the document; valid until [`dwg_doc_close`]. `NULL` if `doc` is `NULL`.

Safety. `doc` must be a live document from `dwg_doc_open*`.

dwg_doc_object_count

size_t dwg_doc_object_count(const dwg_doc *doc)

How many objects the document holds — every entity and non-graphical object in `AcDb:AcDbObjects`, as the reader recovered them.

A cheap liveness check that needs no schema on the caller's side. Returns 0 for a `NULL` document.

Safety. `doc` must be a live document from `dwg_doc_open*`.

dwg_doc_compile

dwg_status dwg_doc_compile(const dwg_doc *doc, dwg_buffer **out)

Compile the document into the FlatBuffers form `the read schema (dwg_db.fbs)` describes.

The result is the read API: symbol tables, blocks with their entities in draw order, layouts, and one sorted index every cross-reference resolves through. Read it with the `flatc` accessors generated for your language.

The buffer is independent of the document — it stays valid after [`dwg_doc_close`], and it is what a caller would cache or mmap.

Safety. `doc` must be live, `out` a valid writable `dwg_buffer*`.

dwg_doc_apply

dwg_status dwg_doc_apply(dwg_doc *doc, const uint8_t *script, size_t len, size_t *out_objects_changed)

Apply an edit script — a `the edit schema (dwg_edit.fbs)` buffer — to the document.

The authoring direction. A caller builds the script with the `flatc` accessors generated for their language, exactly as they read the compiled document with the read schema's. Nothing per-family crosses this boundary.

Objects the script does not name keep their original bytes, so a later [`dwg_doc_write`] preserves everything it was not asked to change. An operation on a family with no byte-exact writer is refused with `DWG_ERR_UNSUPPORTED_FAMILY` rather than re-encoded lossily.

The script is applied in order and is **not** transactional: if operation N fails, operations before it have already been applied. Check the status, and re-open the document if you need all-or-nothing.

`out_objects_changed` may be `NULL`. When given, it receives the number of objects whose bytes actually changed — lower than the operation count when a field was set to the value it already had.

Safety. `doc` must be live, `script` must point to at least `len` readable bytes, and `out_objects_changed` must be null or a valid writable `size_t*`.

dwg_doc_write

dwg_status dwg_doc_write(const dwg_doc *doc, dwg_buffer **out)

Serialize the document back to DWG bytes.

Sections the caller has not changed are written back as they were read. That passthrough is the contract the corpus round-trip gate enforces: writing a document nobody edited reproduces the input byte for byte.

Safety. `doc` must be live, `out` a valid writable `dwg_buffer*`.

dwg_buffer_data

const uint8_t *dwg_buffer_data(const dwg_buffer *buf)

Pointer to a buffer's bytes. `NULL` if `buf` is `NULL`.

Valid until [`dwg_buffer_free`]. Never `free()` it directly.

Safety. `buf` must be a live buffer from a `dwg_*` call.

dwg_buffer_len

size_t dwg_buffer_len(const dwg_buffer *buf)

A buffer's length in bytes. 0 if `buf` is `NULL`.

Safety. `buf` must be a live buffer from a `dwg_*` call.

dwg_buffer_free

void dwg_buffer_free(dwg_buffer *buf)

Free a buffer. Passing `NULL` is a no-op.

Safety. `buf` must have come from a `dwg_*` call and must not be used afterwards.

Schemas

The read model and the edit vocabulary are FlatBuffers schemas. Generate accessors for your language with flatc, then read the document / build edit scripts with them.

flatc --c   dwg_edit.fbs      # C builders for the edit script
flatc --cpp dwg_db.fbs        # C++ accessors for the document

Building an edit script

An edit script is a FlatBuffers EditScript: a list of operations. Two exist today — set a field on an object, and set a header/system variable (units, scales). Build it, then hand its bytes to dwg_doc_apply. C++ flatbuffers shown for readability:

#include "dwg_edit_generated.h"
using namespace dwg::edit;

flatbuffers::FlatBufferBuilder fbb;
// set INSUNITS = 4 (millimetres)
auto shv = CreateSetHeaderVar(fbb, fbb.CreateString("INSUNITS"),
                              Value_IntValue, CreateIntValue(fbb, 4).Union());
auto op  = CreateOp(fbb, OpKind_SetHeaderVar, shv.Union());
fbb.Finish(CreateEditScript(fbb, fbb.CreateVector(
    std::vector<flatbuffers::Offset<Op>>{op})));

size_t changed = 0;
dwg_doc_apply(doc, fbb.GetBufferPointer(), fbb.GetSize(), &changed);
dwg_doc_write(doc, &out);

For an object field, use CreateSetField with the object's handle, a field name (the names the document exposes, e.g. "Radius"), and a value. A family without a byte-exact writer is refused with DWG_ERR_UNSUPPORTED_FAMILY.