>_ DevTrendsja

言語

ホーム

言語

セクション

フロントエンド バックエンド モバイル DevOps AI / ML ゲーム開発 ブロックチェーン 組み込み セキュリティ
C

How to Read an AutoCAD Drawing Without AutoCAD Itself

Imagine this: you need to extract data from an old engineering drawing or automate the generation of diagrams, but your budget doesn't allow for purchasing AutoCAD licenses, and proprietary SDKs cost as much as an airplane wing. The DWG format is a true "black box" of the engineering world. It's closed, binary, and changes every few years. Parsing it manually is a dubious pleasure that can stretch on for months.

That's where LibreDWG comes onto the scene. This is a free C library that is part of the GNU project and sets itself an ambitious goal: to give developers a full-featured tool for reading and writing DWG files without vendor lock-in.

What This Project Is About

LibreDWG grew out of a fork of LibDWG. Ironically, the reason for the split was the language: the original project used Esperanto in its code and documentation. The community decided that English was better for attracting contributors, and so LibreDWG was born.

Today, it's arguably the most serious Open Source solution for working with the DWG format. The library can decode virtually all versions of files. Writing is a bit more complicated (especially for versions newer than R2004), but the project is actively developing. If you just need to read data, extract text, or convert a drawing to SVG — this is exactly what you've been looking for.

What the Library Can Do

The developers didn't just release the library code, but also equipped it with a bunch of useful utilities. These are great examples of how to use the API in practice.

Converting to Vector and JSON

Using dwgread, you can turn a binary drawing into something human-readable. JSON, DXF, and even GeoJSON are supported. This opens the door to web visualization: run the file through the converter, get JSON, render it on Canvas or SVG.

Searching Through Drawings

The dwggrep utility lets you search for text strings inside DWG files using regular expressions. If you have an archive of a thousand drawings and need to find a specific node or specification, this tool will save you hours of life.

Layer Manipulation

Through dwglayers, you can quickly pull out a list of all layers. This is useful for automated compliance checking systems or just for understanding the structure of a complex project.

Generating Drawings from Scratch

The dwgadd tool is the easiest way to programmatically create a DWG file. It accepts simple commands for creating entities and their properties. No need to dive into the intricacies of the binary format — just describe what you want to see on the drawing.

Technical Underpinnings

The library is written in C99, which makes it maximally portable. It feels right at home on Linux and Windows (via MSYS2 or Visual Studio 2019+).

An interesting point relates to encodings. Old versions of DWG used about 30 different code pages. LibreDWG handles all the dirty work of converting this zoo to UTF-8. Internally, it uses UTF-8 (more precisely, WTF8, since Windows UCS-2 has its own quirks with surrogate pairs), which eliminates problems with "garbled text" in layer names or texts.

For those who don't want to write in pure C, bindings for Python and Perl are available via SWIG. This allows you to quickly whip up a script for processing drawings, using the full power of high-level languages.

Who This Is For

I see several scenarios where LibreDWG will be indispensable:

  1. Document workflow automation. When you need to automatically extract data from drawing stamps and enter it into a database.
  2. Web services for engineers. Creating online viewers or converters without using heavy server-side software.
  3. Report generation. If you need to attach an equipment layout diagram to a report generated in PDF or PostScript.
  4. GIS integration. Thanks to GeoJSON support, data from drawings can be directly piped into geographic information systems.

Is It Worth Trying

You need to understand that working with DWG is always walking on thin ice. The format is proprietary, and reverse engineering doesn't always keep up with AutoCAD updates. For example, writing files of versions R2010-R2018 may still result in checksum (CRC) errors.

However, for reading and conversion tasks, LibreDWG is a powerful and, most importantly, free tool. If you've encountered the need to programmatically process drawings, start by exploring the examples in the examples folder. There's a load_dwg.c file there — that's an excellent skeleton for your future application.

The project is alive, commits are coming in, and documentation in Texinfo and Doxygen formats will help you navigate the depths of the API. If you're ready to tolerate some limitations when writing new formats, LibreDWG will become a great addition to your toolkit.

関連プロジェクト