File Exchange

image thumbnail


version (43.8 KB) by Mikkel Pedersen
Tool for reading and plotting DXF files


Updated 26 Mar 2018

View License

Plots 2D DXF file in current axes/figure (or creates a new figure/axes if none exist).

USAGE: dxf = DXFtool('filename.dxf')
INPUT: filename of dxf file as a string (may also include path).
OUTPUT: dxf object (optional).

- colored entities (line and hatch color)
- respects ordering of objects (back to front)
- supports bulges, open/closed polygons
- line dashing

- more entities: DIMENSION, 3D stuff.
- improve handling of splines (currently piecewise linear)
- implement line weight handling

1) Showing the possibilities
2) Using DXFplot to generate graphical legend
3) Importing and plotting CAD model from SolidWorks

Cite As

Mikkel Pedersen (2021). DXFtool (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)

Mikkel Pedersen

Dear Zhao. You can get the vertices of most entities in a dxf file as follows: Try running example1.m. It will create an object called 'dxf', which contains all the information. The vertices of say the first polygon can be retrieved by typing 'dxf.entities(1).poly'. It will return an array with x,y coordinates. For other entities, just replace the '1' with the entity number. Good luck.
BR Mikkel

zhao chuan

Thanks, could you provide an example to show how to use the code?I don't know how to use the code to read polygon and get vertex of the polygon by DXFtool

zhao chuan

Mikkel Pedersen

Thanks Himanshu. Please see the answer for Aurea94 below.

BR Mikkel

Himanshu Pandey

This is amazing and works so good!!! Could you please tell me how I could extract all the circles in the dxf file, thanks!

mohammad ibrahim

hello Mikkel

first of all, thank you for this awesome work.
I want to import a CAD file into a GUI axis. this works fine; however, I can no longer select the points on the figure (in a normal "figure" I can do that but not in a GUI axis)
could you please help me solve this issue

many thanks


Hi Mikkel!

Thanks! My geometry contains lines, splines and arcs. Should I then use the methodology you propose and extract the X Y points of each of the entities manually?

Mikkel Pedersen

Hi Aurea.
Yes, it is in the optional output, try e.g. dxf = DXFtool('some_dxf_file'). Then the output (dxf) will be a struct containing all relevant information about all the graphical entities in the file. Depending on which entities are contained in the file, it will have fields matching that. For example, if there is only a circle in there, you will get dxf.entities(1).circle, containing Xc,Yc and R = center and radius of the circle, such that you can easily reconstruct it manually!
BR Mikkel


Is there a way to obtain XY points of the DXF for post-tratment during my program?


Breanna Belz

Very handy, but when working with polylines that contain arcs, it seems that the coordinates for the arc ends repeat (for example, XData = [1 2 3 3 4 5 . . . 51 52 53 53 54 55] and YData = [3 4 5 5 6 7 . . . 53 54 55 55 56 57], where (3,5) and (53, 55) are the ends of an arc). This makes it a little trickier to work with the XData and YData, so I've taken to exploding my polylines before exporting my file as a .dxf file to separate polylines into lines and arcs. Other than that, this is a very useful function.

huihui Zhou

Hanlin Zhu


Mikkel Pedersen

Dear Gabriel

I think the problem is due to your old version. Thanks for posting a solution.

BR Mikkel

Gabriel Mühlebach

I solved issue by adding:

if isempty(dxf.entities(i).linetype)
dxf.entities(i).linetype = 'CASE_HATCH';

before switch upper(dxf.entities(i).linetype)

Gabriel Mühlebach

I have an issue with "ex1.dxf": in function plot(dxf), HATCH case don't seems works...
if switch dxf.entities(i).name case is 'HATCH' ther is a bug because switch upper(dxf.entities(i).linetype) is []... I have R2013B version.

Thank you for your work!

Tue Truong


works pretty well

wei pang

yh tao

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

Inspired by: Read DXF File Data

Community Treasure Hunt

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

Start Hunting!