LibreDWG
Open Source C Library to Manage AutoCAD DWG Files
Generate, Edit, Read, Manipulate & Convert AutoCAD DWG Models to SVG Files via Open Source C++ CAD API.
What is LibreDWG?
LibreDWG is a handy C library that comes for free and it’s super useful when dealing with AutoCAD DWG drawings. DWG is a popular file format created by AutoDesk back in the 1970s for CAD applications that were starting to emerge. Even though DWG is a proprietary format, it’s still the go-to choice for many contractors. This library is user-friendly and is designed to provide an API that can assist software developers in building applications that work with DWG files.
The library has many useful features to work with AutoCAD DWG files. You can open and view DWG drawings, make new DWG files, convert them to various formats, change them to DXF and JSON, turn DWG into SVG and Postscript, use dwggrep to find text, dwglayer to show layers, and much more.
In the library, software developers can turn on or off the DWG writing support. Initially, this feature is turned off, but when you activate it, you can use it to write in the DWG format. Converting most DWGs to the r2000 format usually goes smoothly. The DWG reader can handle a variety of file formats including JSON, DXF, DXFB, GeoJSON, YAML, XML/OGR, GPX, SVG, and PS.
Getting Started with LibreDWG
You need to install basic development tools (a C99 compiler like gcc/clang, make, autoconf, automake and libtool). The create clone of the library using the following command.
Clone Library using the Following Command
git clone git://git.sv.gnu.org/libredwg.git
Now change dir to libredwg folder created by git ( cd libredwg ) and issue following commands in terminal
Change dir to libredwg folder
$ sh autogen.sh
$ ./configure --enable-trace # (this will enable debugging messages)
$ make
$ sudo make install
$ make check
Create AutoCAD DWG via C Library
The Free LibreDWG C library enables software developers to create new AutoCAD DWG Files inside their own C applications. Once the file is created you can insert various types of entities inside the DWG drawings such as tables, shape, hatch, camera, light, line, mesh, mtext, polyline 2D, polyline 3D, text, table, trace, and many more. The library also supports set alignment for entities, access, and updates the properties of the entities with ease. The DWG writer has included support for various input formats, such as DXF, DXFB, and JSON.
Read AutoCAD DWG via C Library
The open source LibreDWG library has included complete support for programmatically accessing and reading AutoCAD DWG Files inside .NET applications with just a couple of lines of C code. The DWG reader supports various output formats, such as JSON, DXF, DXFB, GeoJSON. Later also YAML, XML/OGR, GPX, SVG, and PS file formats.
How to Read AutoCAD DWG via C Library?
int error;
Dwg_Data dwg;
error = dwg_read_file(filename, &dwg);
if (!error)
{
model_xmin = dwg_model_x_min(&dwg);
model_ymin = dwg_model_y_min(&dwg);
double dx = (dwg_model_x_max(&dwg) - dwg_model_x_min(&dwg));
double dy = (dwg_model_y_max(&dwg) - dwg_model_y_min(&dwg));
double scale_x = dx / (dwg_page_x_max(&dwg) - dwg_page_x_min(&dwg));
double scale_y = dy / (dwg_page_y_max(&dwg) - dwg_page_y_min(&dwg));
//...
}
dwg_free(&dwg);
Convert DWG File to SVG via C Library
SVG is a W3C standard for 2d vector graphics and is very popular. The open source LibreDWG library enables software developers to convert AutoCAD DWG drawing to SVG file formats using C commands. To convert SVG (Scalable Vector Graphics) file you need to open a DWG file and output an SVG file. Please remember that the library will handle the 2D data and will handle the 3d content from DWG since SVG only supports 2-dimensional images.