A free, open-source C++ library for reading and writing DXF files in both ASCII and binary formats, with support for reading DWG files from R14 to V2015.
This project is a fork of the original libdxfrw project on SourceForge. This fork includes additional improvements, updated build configurations, comprehensive test suites, and enhanced documentation to make the library more accessible and easier to use.
For the original project, please visit: http://sourceforge.net/projects/libdxfrw
- DXF Support: Read and write DXF files in both ASCII and binary formats
- DWG Support: Read DWG files from AutoCAD R14 through 2015
- Cross-Platform: Works on Linux, macOS, Windows, and other platforms
- Multiple Build Systems: CMake, Autotools, Visual Studio, and MinGW support
- Comprehensive API: Full access to DXF entities, objects, headers, and tables
- Open Source: Licensed under GNU GPL v2 or later
- About this Fork
- Features
- Requirements
- Building the Library
- Installation
- Usage
- Examples
- Testing
- Documentation
- Contributing
- License
- C++ Compiler: C++11 compatible compiler (GCC, Clang, MSVC)
- CMake: Version 3.10 or later (for CMake builds)
- libiconv: Character encoding conversion library
Ubuntu/Debian:
sudo apt-get install build-essential cmake libiconv-hook-devFedora/RHEL/CentOS:
sudo yum install gcc-c++ cmakemacOS (using Homebrew):
brew install cmake libiconvWindows:
- Visual Studio 2013 or later, or
- MinGW with CMake
The recommended build method using CMake:
# Create a build directory
mkdir build
cd build
# Configure the build
cmake .. -DCMAKE_BUILD_TYPE=Release
# Build the library
cmake --build . --config Release
# Install (optional, requires sudo on Linux/macOS)
sudo cmake --build . --config Release --target installThe library will be installed to:
- Headers:
/usr/local/include/ - Library:
/usr/local/lib/libdxfrw.a
Alternative build method using GNU Autotools:
# Generate configure script (optional, only if building from git)
autoreconf -vfi
# Configure the build
./configure
# Build the library
make
# Install (requires root/sudo)
sudo make install- Open
vs2013/libdxfrw.slnwith Visual Studio 2013 or later - Select your desired configuration (Debug/Release)
- Build → Build Solution (or press F7)
- The library will be built in
vs2013/Debug/orvs2013/Release/
- Open
dwg2dxf/vs2013/dwg2dxf.slnwith Visual Studio - Build Solution
- The executable will be in the respective Debug/Release folder
- Open Visual Studio
- File → Open → CMake → Select
CMakeLists.txt - Build → Build All
mkdir build
cd build
cmake .. -G "Visual Studio 16 2019" -A x64
cmake --build . --config Release
cmake --build . --config Release --target installBuild portable binaries for multiple OS distributions using Docker.
Note: Dockerfiles are in
docker/directory. Build process updated from CentOS 7 (EOL) to AlmaLinux 9.
Available OS Distributions:
| OS | Tag | Use Case |
|---|---|---|
| AlmaLinux 9 | almalinux |
RHEL/CentOS compatible (CentOS successor) |
| Ubuntu 22.04 LTS | ubuntu |
Debian/Ubuntu compatible |
| Amazon Linux 2023 | amazonlinux |
AWS optimized |
| Alpine Linux | alpine |
Lightweight musl-based |
Quick Start:
# Build for single OS (creates dxfrw-{os}.tar.gz)
./docker/build-docker.sh run almalinux
./docker/build-docker.sh run ubuntu
# Build for all OS distributions
./docker/build-docker.sh run all-os
# Extract and install
sudo tar xzf dxfrw-almalinux.tar.gz -C /optManual Docker Commands (if needed):
# Build image and library
docker build -t codelibs/libdxfrw:ubuntu -f docker/Dockerfile.ubuntu .
docker run -t --rm -v `pwd`:/work codelibs/libdxfrw:ubuntu /work/build.shPush to Docker Hub:
./docker/build-docker.sh push almalinuxAfter building, you can install the library system-wide:
sudo make install
# or with CMake:
sudo cmake --build build --target installCopy the headers and library files manually:
# Copy headers
sudo cp src/*.h /usr/local/include/
# Copy library
sudo cp build/libdxfrw.a /usr/local/lib/
# Update library cache (Linux only)
sudo ldconfig#include "libdxfrw.h"
#include "drw_interface.h"
class MyInterface : public DRW_Interface {
public:
virtual void addLine(const DRW_Line& data) {
// Process line entity
printf("Line from (%.2f, %.2f) to (%.2f, %.2f)\n",
data.basePoint.x, data.basePoint.y,
data.secPoint.x, data.secPoint.y);
}
virtual void addCircle(const DRW_Circle& data) {
// Process circle entity
printf("Circle at (%.2f, %.2f) radius %.2f\n",
data.basePoint.x, data.basePoint.y, data.radious);
}
// Implement other virtual methods as needed...
};
int main() {
MyInterface iface;
dxfRW dxf("input.dxf");
// Read the DXF file
if (!dxf.read(&iface, false)) {
printf("Error reading DXF file\n");
return 1;
}
return 0;
}#include "libdxfrw.h"
#include "drw_interface.h"
class MyWriter : public DRW_Interface {
public:
void exportToFile(const char* filename) {
dxfRW dxf(filename);
// Write DXF file in AutoCAD 2000 format, ASCII mode
dxf.write(this, DRW::AC1015, false);
}
virtual void writeEntities() {
// Create a line
DRW_Line line;
line.basePoint.x = 0;
line.basePoint.y = 0;
line.secPoint.x = 100;
line.secPoint.y = 100;
dxf->writeLine(&line);
// Create a circle
DRW_Circle circle;
circle.basePoint.x = 50;
circle.basePoint.y = 50;
circle.radious = 25;
dxf->writeCircle(&circle);
}
};CMake:
find_library(DXFRW_LIBRARY NAMES dxfrw)
find_path(DXFRW_INCLUDE_DIR libdxfrw.h)
target_include_directories(your_target PRIVATE ${DXFRW_INCLUDE_DIR})
target_link_libraries(your_target ${DXFRW_LIBRARY})g++ command line:
g++ -o myapp myapp.cpp -ldxfrw -liconvThe library includes example programs that demonstrate usage:
Converts DWG files to DXF format:
cd dwg2dxf
# Build using the same method as the main library
./dwg2dxf input.dwg output.dxfThe source code in dwg2dxf/ serves as a comprehensive reference implementation showing how to:
- Read DWG files
- Process entities and objects
- Write DXF output
Extracts text information from DWG files.
The library includes utility scripts in the bin/ directory for extracting text content from DXF/DWG files.
Extracts text entities from DWG files:
# Usage
./bin/dwg2txt <input.dwg> <output.txt>
# Example
./bin/dwg2txt drawing.dwg output.txtRequirements:
dwg2textexecutable (built with the library)
Extracts text entities from DXF files:
# Usage
./bin/dxf2txt <input.dxf> <output.txt>
# Example
./bin/dxf2txt drawing.dxf output.txtRequirements:
- Python 3.9 or later (minimum version across all Docker build environments)
ezdxfPython package:pip install ezdxf
Features:
- Supports both TEXT and MTEXT entities
- Handles multiple DXF versions (R12 through 2018+)
- Automatic encoding detection (UTF-8, CP932/Shift-JIS)
- Preserves multi-line text formatting
You can also use the Python script directly:
# Install ezdxf if not already installed
pip install ezdxf
# Run the script
python3 bin/dxf2txt.py input.dxf output.txtThe library includes comprehensive tests covering various aspects:
cd build
cmake ..
make
ctestOr run tests individually:
./test_basic # Basic functionality tests
./test_entities # Entity reading/writing tests
./test_polylines # Polyline tests
./test_text # Text entity tests
./test_tables # Table tests
./test_blocks # Block tests
./test_versions # DXF version compatibility tests
./test_errors # Error handling testsTest files are available in the fess-testdata repository.
- Original Project Website: http://sourceforge.net/projects/libdxfrw
- API Documentation: Generate with Doxygen using
libdxfrw.dox - Specifications: See SPECIFICATIONS.md for DXF format details
- Examples: Check the
dwg2dxf/directory for reference implementation
doxygen libdxfrw.doxThis creates HTML documentation in the doc/ directory.
libdxfrw/
├── src/ # Library source code
│ ├── intern/ # Internal implementation
│ └── *.h/cpp # Public headers and implementation
├── include/ # Public header files
├── dwg2dxf/ # DWG to DXF converter example
├── dwg2text/ # DWG to text converter example
├── tests/ # Unit tests
├── vs2013/ # Visual Studio project files
└── CMakeLists.txt # CMake build configuration
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
See the COPYING file for the full license text.
See AUTHORS file for the list of contributors.
See ChangeLog for version history and changes.
- Issues: Report bugs and request features on GitHub Issues
- Repository: https://github.com/codelibs/libdxfrw