File Exchange

image thumbnail


version (693 KB) by Grzegorz Kwiatek
DXFLib is a library written in MATLAB that allows to create simple AutoCAD DXF files.


Updated 16 Dec 2014

View Version History

View License

DXFLib is a library written in MATLAB that allows to create simple AutoCAD DXF files. Autocad DXF is a popular file format that can be interpreted in many CAD-related applications. AutoCAD DXF can be also converted to a PDF file containing 3D geometry using any 3D printer driver.
The DXFLib handles DXF layers and ACI Colors (RGB colors are automatically converted to their ACI counterparts). It is capable of exporting point, polylines, polysurfaces, text, various 3D primitives and 2D markers.


Just unpack zipped m-files to your working directory or create a separate directory and add its location to MATLAB path. All m-files are located in to root directory. The /examples directory contains DXF files which are generated by dxf_test.m script. Some DXF files are converted to 3D PDF files using Bentley View 3D printer driver.


The easiest way to learn how to create DXF files using DXFLib is to simply run the dxf_test.m script and then open it in MATLAB editor and compare the generated DXF files with the code inside the script. Obviously, you'll need any DXF viewer that is capable to handle 3D content. I recommend
to use free Bentley View software ( The
advantage of using Bentley View is that this software is capable to PRINT 3D PDF
files (some DXF files in /exaples subdirectory were converted using this 3D PDF printer driver). More examples and a short tutorial is available at:

DXFLib page:

Cite As

Grzegorz Kwiatek (2021). DXFLib (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (48)

佑彪 何


John Tamkin

Eric Spivey

Great work! Is there a way to generate 2D polylines instead of 3D?

Tomas Klinavicius

@Grzegorz Than you for your response, it worked

Grzegorz Kwiatek

@Tomas The documentation states that "Default drawing units for AutoCAD DesignCenter blocks: 0 = Unitless; 1 = Inches; 2 = Feet; 3 = Miles; 4 = Millimeters;
5 = Centimeters; 6 = Meters; 7 = Kilometers; 8 = Microinches; 9 = Mils; 10 = Yards; 11 = Angstroms; 12 = Nanometers; 13 = Microns; 14 = Decimeters; 15 = Decameters; 16 = Hectometers; 17 = Gigameters; 18 = Astronomical units; 19 = Light years; 20 = Parsecs. That's what's in documentation, I don't maintain this package anymore, apologies.

Grzegorz Kwiatek

@Thomas You can follow the DXF format documentation that is available on the net freely. I guess you may change header of DXF file that my library creates and modify "INSUNITS" variable.

Tomas Klinavicius

Great work. How do I change the units from meters to some different kind of units?

Huabin Zhang

It would be better if this library could do pattern filling

Ale Paladio

Christopher H

The dxf_polyline does a great job at converting 2D images into .dxf files. It can be included in a loop to make a file with multiple polylines

Santosh Konangi

thomas castelain

jahanzaib ahmad

please include 3d convex polygons as well

jahanzaib ahmad

Is there a way of creating a polyline DXF, stopping the polyline and creating a new polyline (in the same DXF)? I am wanting to trace multiple loops together on one file but without line crossing over my completed loops, same question

Tom Hemingway

Is there a way of creating a polyline DXF, stopping the polyline and creating a new polyline (in the same DXF)? I am wanting to trace multiple loops together on one file but without line crossing over my completed loops

Ayan Majumder

This is great. But how do I change the polyline width? Please let me know.

Stephan Bäumler

Hi! Excellent, but I would also need arc entities. Would it be possible to add?

Peyruz Gasimov

or even circles would work

Peyruz Gasimov

Is there a way to generate arcs using the library?

Yonatan Chatzinoff

Great, useful library.
Note that dxf_polyline operates on a columnwise basis, so it will give independent points if given row vectors.

Yonatan Chatzinoff

Felix Fraider

mithu max

Hello, can I use Draft sight to visualize the files?


Nice work! Thank you for your share.
I want to know whether there is a parameter for colors transparency of dxf_polymesh ?

Harsha K

Hello again,

I used KLayout application to view the created DXF files. Unfortunately, there is a line from a single point connecting to all the polygons in the created DXF. I was not expecting this. Looked very awkward, please consider Example 4 and Example 5 for this test, please.


Mads Knude Hovgaard


very excellent~ it would be better if the dxflib can add circle iitem

Carlos Pejendino

Carlos Pejendino

oussama souadka


Hi, I made a Menger's sponge cube on MATLAB and want to export it to Autocad. Could you please help me?

sundus asfour

hello, I am using the dxf_polyline command after drawing a spiral, the result on matlab is goob but the dxf coverter only saves lines so the result on autocad is just lines connected in the shape of a spiral. however I need a curved spiral. please help if you know how to convert to dxf without changing the spiral :)
many thanks.

rajnikant sharma

Hi I am not able to read dxf generated by this dxflib as it uses 'POLYLINE' instead of 'AcdbPolyline' or 'LWPOLYLINE', please help me to read those dxf files generated by dxflib.


rajnikant sharma

Very Excellent library, it helped me a lot.
But still not give same output as i see in imwrite image in matlab.
dxf polyline graph is 90 degree clockwise rotated, pls help.

yjing ning

This website is excellent. It saves me a lot of time. A really convenient way to generate dxf files that could be read by CAD and other softwares.
I am really appreciated.

Amanda Lind

This library is awesome, I have only one complaint. The colors in example 9 don't come through in Rhino. The colors in example 8 do. Any recommendations you can make for colors to make their way across in example 9 to Rhino would be extremely helpful.

Best Regards,

Mauricio B

Great library. I have a simple optimization for large files:
Matlab flushes the buffer for every fprintf statement by default. Since you use a lot of small fprintf statements it can be useful to disable auto-flushing. You can easily do this by using fopen with 'W' instead of 'w'.

For me this speeded it up by a factor 20. But I had quite a large overhead since I was writing to a network folder.

Sam Dehdar

Excellent Library, very good work. Do you by any chance have anything that would be able to create a table in DXF format?

I've tried to do it, but I keep running into a wall! And how do you distinguish between what is optional to have in a DXF and what is not? Maybe you could point me towards some proper literature :)?


Thanks for this library, its very useful. Is there a way to ensure that the polylines are closed? For example (and I don't know if this is the best way to do this) I am writing a series of rotated rectangles based on center points and angles. I use the polyline function but all the resulting rectangles are open. Thanks for the help!

Lucas Solo

Great library, very useful! Thanks

Lars Wadsö

Nice toolbox, but I have a problem. I want to make dxf-files for laser cutting and used dxf_polyline to write X-Y curves/shapes (setting Z to 0) to a dxf-file. When I open the file in 2D with AutoCAD or DraftSight the curves only show up as points (no lines). How can I make 2D lines/curves?

Grzegorz Kwiatek

Hi Lars, thank you for your feedback. The website has been updated to:

Lars Wadsö

The link to the tutorial ( does not work. Is there an alternative link?

Alexei Adalev

Very useful, thanks a lot!

Grzegorz Kwiatek

Varun AV, The circles will be incorporated in the next version of DXFLib.

Varun AV

The library is useful, however, it could include circles too.



MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!