Click here to Skip to main content
11,477,625 members (74,004 online)
Click here to Skip to main content

Tagged as

Simple DXF Reader/Viewer with Spline Support

, 13 Sep 2013 CPOL 14.7K 2.7K 18
Rate this:
Please Sign up or sign in to vote.
Simple DXF reader and viewer supporting the most common AutoCAD entities

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 strings for layer entry to draw each entity in different colors. If the layer of an entity is not one of these strings, the entity color is green default. Modify SelectLayer function with your own strings. 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.

//fx and fy hold x anf y coordinate values of given spline vertices.
//iCount is the number of generated spline points.
//points are the generated spline points.

int iCount = 0;
float fx[128],fy[128];
POINT points[65535];
 
for(UINT i = 0; i < m_dxf.GetSplineCount(); i++)
{
    //Load fx and fy array from CSpline
    //...

    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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Hasan Gurler
Software Developer
Turkey Turkey
No Biography provided

Comments and Discussions

 
GeneralMy vote of 1 Pin
nMarkj24-Jun-14 3:08
membernMarkj24-Jun-14 3:08 
BugMemory Leak Pin
ravenspoint10-Jun-14 6:26
memberravenspoint10-Jun-14 6:26 
GeneralRe: Memory Leak Pin
Hasan Gurler10-Jun-14 22:16
memberHasan Gurler10-Jun-14 22:16 
GeneralRe: Memory Leak Pin
ravenspoint11-Jun-14 1:29
memberravenspoint11-Jun-14 1:29 
GeneralRe: Memory Leak Pin
Hasan Gurler12-Jun-14 2:37
memberHasan Gurler12-Jun-14 2:37 
QuestionWhitespace problems [modified] Pin
ravenspoint7-Jun-14 9:05
memberravenspoint7-Jun-14 9:05 
AnswerRe: Whitespace problems Pin
Hasan Gurler9-Jun-14 4:40
memberHasan Gurler9-Jun-14 4:40 
GeneralRe: Whitespace problems Pin
ravenspoint9-Jun-14 5:58
memberravenspoint9-Jun-14 5:58 
QuestionTangents Pin
Morteza_Developer14-May-14 3:14
memberMorteza_Developer14-May-14 3:14 
AnswerRe: Tangents Pin
Hasan Gurler14-May-14 22:11
memberHasan Gurler14-May-14 22:11 
GeneralRe: Tangents Pin
Morteza_Developer14-May-14 22:16
memberMorteza_Developer14-May-14 22:16 
GeneralRe: Tangents Pin
Hasan Gurler15-May-14 22:38
memberHasan Gurler15-May-14 22:38 
GeneralMy vote of 5 Pin
Mihai MOGA18-Apr-14 22:30
professionalMihai MOGA18-Apr-14 22:30 
GeneralRe: My vote of 5 Pin
Hasan Gurler21-Apr-14 3:26
memberHasan Gurler21-Apr-14 3:26 
GeneralVery helpful Pin
Member 1052813516-Jan-14 3:40
memberMember 1052813516-Jan-14 3:40 
Questioncode Pin
Member 1037365331-Oct-13 22:22
memberMember 1037365331-Oct-13 22:22 
GeneralMy vote of 5 Pin
DigitalArts14-Oct-13 10:09
memberDigitalArts14-Oct-13 10:09 
GeneralRe: My vote of 5 Pin
Hasan Gurler20-Oct-13 23:44
memberHasan Gurler20-Oct-13 23:44 
GeneralMy vote of 5 Pin
AnOldog17-Sep-13 16:44
memberAnOldog17-Sep-13 16:44 
GeneralRe: My vote of 5 Pin
Hasan Gurler17-Sep-13 21:37
memberHasan Gurler17-Sep-13 21:37 
GeneralRe: My vote of 5 Pin
AnOldog20-Oct-13 5:14
memberAnOldog20-Oct-13 5:14 
GeneralRe: My vote of 5 Pin
Hasan Gurler20-Oct-13 23:43
memberHasan Gurler20-Oct-13 23:43 
GeneralMy vote of 5 Pin
kanalbrummer16-Sep-13 3:54
memberkanalbrummer16-Sep-13 3:54 
GeneralRe: My vote of 5 Pin
Hasan Gurler16-Sep-13 4:30
memberHasan Gurler16-Sep-13 4:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 13 Sep 2013
Article Copyright 2013 by Hasan Gurler
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid