Introduction
The aim of this tip is to deal with DXF files with the most common AutoCAD entities.
Background
I searched a DXF reader source code in the CodeProject archive and found two articles. One is A DXF Reader Solution and Simple DXF Viewer by Evren Daglioglu, another is DXF Import .NET: Read and View AutoCAD Format Files by Chuzhakin. Both are C# code and have no spline solution. In the comments of these articles, I have seen that other developers need it. So, I decided to implement a basic DXF reader supporting spline entity. I used C++ classes to provide you easy integration of reader to your project. Hope to help you.
General
Line, lwpolyline, arc, circle and spline entities are supported. All the entities in the file are loaded into proper classes when the file is opened. Afterwards, they are drawn on the screen. Highlight and selection of each entity, indication of points, sliding, zoom in, zoom out and pan zoom functionalities are available.
Using the Code
The source code is organized by C++ classes to provide you easy integration into your own code. CLine
, CPolyline
, CArc
, CCircle
and CSpline
classes are used to hold the entities. These classes are in very basic structure. You may improve them for your own purposes. CDxf
class is used to hold the file. Use LoadFile
member function of CDxf
class to hold the file. In order to load sample.dxf file, use the following code:
CDxf m_dxf;
m_dxf.Init();
m_dxf.LoadFile("sample.dxf");
All the entities are loaded to proper classes by LoadFile
function. CDxf
class has member variables of CLine
, CPolyline
, CArc
, CCircle
and CSpline
. The number of each entity can be obtained. For example; to get how many circle entity is available in the file, use GetCircleCount
member function. The number of line, lwpolyline, arc and spline entity can be got in a similar way.
UINT iCircleCount;
iCircleCount = m_dxf.GetCircleCount();
After loading all the entity in CDxf
class, they are drawn on the screen. I used several string
s for layer entry to draw each entity in different colors. If the layer of an entity is not one of these string
s, the entity color is green default. Modify SelectLayer
function with your own string
s. Drawing of spline entity is a little bit different. I don't use the poly bezier drawing functions of MFC. I generated the intermediate points between given spline vertices. By the way, you can use these points for different purposes other than drawing.
int iCount = 0;
float fx[128],fy[128];
POINT points[65535];
for(UINT i = 0; i < m_dxf.GetSplineCount(); i++)
{
Spline spline(fx,fy,m_dxf.m_Spline[i].m_FitPointCount);
spline.Generate();
spline.GetCurve(points,iCount);
}
The source code, binary, and a sample dxf file are in a single zip file. Download and explore the source code. Have fun...
DXF Standard
DXF standard is an open ASCII format supplied by Autodesk. Plenty of information is available on the net. If these five entities solve your problem, you don't need to deal with the standard. Just use the classes.