.NET API
Generated P/Invoke over the C ABI, with C# accessors over the FlatBuffers document. One managed call per C entry point; the same lifecycle and status codes.
Quick start
using Dwg;
using var doc = DwgDocument.Open("drawing.dwg"); // any version, R13-R2018
Console.WriteLine(doc.Version); // "AC1032"
// iterate model space in draw order, dispatch on kind
foreach (var e in doc.ModelSpace.Entities)
switch (e.Kind)
{
case EntityKind.Line: var l = e.AsLine(); Console.WriteLine($"{l.Start} → {l.End}"); break;
case EntityKind.MText: Console.WriteLine(e.AsMText().Text); break;
case EntityKind.Insert: Console.WriteLine(e.AsInsert().BlockName); break;
}
// or by type across the whole drawing: doc.Lines, doc.Circles, doc.Inserts, doc.Texts
foreach (var layer in doc.Layers) Console.WriteLine(layer.Name);
doc.Header.InsUnits = 4; // edit — set units to mm
doc.Save("out.dwg"); // serializeDwgDocument is the API you use — open, read records, edit, and save, with no FlatBuffers or pointers. It wraps a generated P/Invoke layer over the C ABI (documented below for reference and for advanced use); the same 12 C entry points are there if you need them.
Entry points
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
dwg_set_license
dwg_status dwg_set_license(const char *key)Install a licence key (the hex text issued to the customer).
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.
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.
dwg_doc_close
void dwg_doc_close(dwg_doc *doc)Close a document and free everything it owns.
dwg_doc_version
const char *dwg_doc_version(const dwg_doc *doc)The document's version string, e.g. `"AC1032"`.
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.
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.
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.
dwg_doc_write
dwg_status dwg_doc_write(const dwg_doc *doc, dwg_buffer **out)Serialize the document back to DWG bytes.
dwg_buffer_data
const uint8_t *dwg_buffer_data(const dwg_buffer *buf)Pointer to a buffer's bytes. `NULL` if `buf` is `NULL`.
dwg_buffer_len
size_t dwg_buffer_len(const dwg_buffer *buf)A buffer's length in bytes. 0 if `buf` is `NULL`.
dwg_buffer_free
void dwg_buffer_free(dwg_buffer *buf)Free a buffer. Passing `NULL` is a no-op.
The document API
DwgDocument hides the plumbing entirely.
using Dwg;
using var doc = DwgDocument.Open("drawing.dwg");
// ── read ──────────────────────────────────────────────
foreach (var layer in doc.Layers) Console.WriteLine(layer.Name);
foreach (var m in doc.MTexts) Console.WriteLine(m.Text);
// ── header variables ──────────────────────────────────
int units = doc.Header.InsUnits; // read
doc.Header.InsUnits = 4; // set — millimetres
doc.Header.LineTypeScale = 2.0;
doc.Header.DimensionScale = 1.5;
// ── save ──────────────────────────────────────────────
doc.Save("out.dwg"); // or: byte[] bytes = doc.Save();
Assigning a header property applies the change immediately and safely — an unsupported edit throws DwgException rather than corrupting the file. Records (Layers, MTexts, …) are read views over the drawing.
Under the hood
Advanced callers can reach the generated layers directly: P/Invoke (NativeMethods) over the C ABI, and FlatBuffers accessors over the document and edit schemas (dwg_db.fbs, dwg_edit.fbs, generate with flatc --csharp). DwgDocument is built on exactly these; you rarely need them.