Click here to Skip to main content
Click here to Skip to main content

CadLib for creating DXF(Drawing Interchange Format) files

, 25 Aug 2004 CPOL
Rate this:
Please Sign up or sign in to vote.
A link-library and its interface class to create, load and modify DXF files.


What is DXF?

Drawing Interchange Format (DXF) files enable the interchange of drawings between AutoCAD and other programs. DXF files can be either ASCII or binary formats. Because ASCII DXF files are more common than the binary format, CadLib uses ASCII DXF format.

What is CadLib?

The CadLib is not a Computer Aided Design (CAD) program. It is a tool for creating DXF files that are used in the CAD programs. It consists of two parts. One of them is a Dynamic Link Library to create the DXF file. The other part is the programming interface. It is a class that integrates the cadio.dll functions. It can be used in Microsoft Visual C++ projects. In addition, the cadio.dll can be used in other Win32 programs.

Why use CadLib?

In some programs, it is needed to create a drawing output for use in other programs such as AutoCad. For example, in a "Building Detail Sheet Generator Program", the program needs to create a drawing output. And the most standard format for communicating drawing data is DXF.

DXF file structure

The DXF format is a tagged data representation of all the information contained in a drawing file. Tagged data means that each data element in the file is preceded by an integer number that is called a group code. A group code's value indicates what type of data element follows. This value also indicates the meaning of a data element for a given object (or record) type. Virtually all user-specified information in a drawing file can be represented in DXF format. (from AutoCad's DXF reference)

A DXF file consists of some sections. Each section has some drawing data in itself. The CadLib uses the following sections:


The main reference for DXF file structure that is used for CadLib is the AutoCad's DXF reference. You can find more information about DXF file structure here.


The classes are interfaces between CadIO.dll and the main program. "Test" has come with CadLib to demonstrate how to generate a DXF file with CDxfFileWrite and CDrawing classes.

CDxfFileWrite class

CDxfFileWrite gathers all the commands needed to directly create a DXF file. Usage of CDxfFileWrite is as follows:

  1. Create the DXF file
    CDxfFileWrite dxffile;
    dxffile.Create( "d:\\test.dxf" );
  2. Begin and end the HEADER section. It's here for compatibility with some CAD programs. Others work without having HEADER section.
    // Header Section ------------------------------------------
    // close HEADER section ------------------------------------
  3. Begin the TABLES section and put the LAYER, LTYPE, STYLE, DIMSTYLE table-types as many as you want and then close the section
    // Tables Section ------------------------------------------
    // LTYPE table type -------------------------
    DXFLTYPE ltype;
    double elem[4];
    // Continuous
    ZeroMemory(<ype, sizeof(ltype));
    ltype.Name = "Continuous";
    ltype.DescriptiveText = "Solid line";
    // DASHDOT2
    ZeroMemory(<ype, sizeof(ltype));
    ltype.Name = "DASHDOT2";
    ltype.DescriptiveText = "Dash dot (.5x) _._._._._._._._._._._._._._._.";
    ltype.ElementsNumber = 4;
    ltype.TotalPatternLength = 0.5;
    ltype.Elements = elem;
    elem[0] = 0.25;
    elem[1] = -0.125;
    elem[2] = 0.0;
    elem[3] = -0.125;
    // close LTYPE table type -------------------
    // LAYER table type -------------------------
    result &= dxffile.BeginTableType(TAB_LAYER);
    result &= dxffile.AddLayer("Layer1", 1, "Continuous");
    result &= dxffile.AddLayer("Layer2", 2, "Continuous");
    result &= dxffile.AddLayer("Layer3", 3, "Continuous");
    result &= dxffile.AddLayer("Layer4", 4, "Continuous");
    result &= dxffile.EndTableType();
    // close LAYER table type -------------------
    // STYLE table type -------------------------
    DXFSTYLE tstyle;
    ZeroMemory(&tstyle, sizeof(tstyle));
    tstyle.Name = "Style1";
    tstyle.PrimaryFontFilename = "TIMES.TTF";
    tstyle.Height = 0.3;
    tstyle.WidthFactor = 1;
    // close STYLE table type -------------------
    // DIMSTYLE table type ----------------------
    DXFDIMSTYLE dimstyle;
    // DIM1
    ZeroMemory(&dimstyle, sizeof(dimstyle));
    dimstyle.Name = "DIM1"; // DimStyle Name
    dimstyle.DIMCLRD = 2; // Dimension line & Arrow heads color
    dimstyle.DIMDLE = 0.0000; // Dimension line size after Extensionline
    dimstyle.DIMCLRE = 2; // Extension line color
    dimstyle.DIMEXE = 0.1800; // Extension line size after Dimline
    dimstyle.DIMEXO = 0.0625; // Offset from origin
    dimstyle.DIMBLK1 = "ClosedFilled";// 1st Arrow head
    dimstyle.DIMBLK2 = "ClosedFilled";// 2nd Arrow head
    dimstyle.DIMASZ = 0.1800; // Arrow size
    dimstyle.DIMTXSTY = "Style1"; // Text style
    dimstyle.DIMCLRT = 3; // Text color
    dimstyle.DIMTXT = 0.1800; // Text height
    dimstyle.DIMTAD = 1; // Vertical Text Placement
    dimstyle.DIMGAP = 0.0900; // Offset from dimension line
    // close DIMSTYLE table type ----------------
    // close TABLES section ------------------------------------
  4. Begin ENTITIES section and put entities data (LINE, CIRCLE, SOLID, TEXT, ARC, POINT, DIMLINEAR) and finally close the section
    // Entities Section ------------------------------------------
    // set current layer to Layer2
    // draw a line
    dxffile.Line(1.2, 3.3, 7.5, 7.7);
    // draw a circle
    dxffile.Circle(7.8, 4.3, 1.75);
    // set current layer to Layer4
    // draw a solid
    REALPOINT points[4];
    points[0].x = 10.4; points[0].y = 7.2;
    points[1].x = 13.6; points[1].y = 7.4;
    points[2].x = 13.1; points[2].y = 4.9;
    points[3].x = 10.9; points[3].y = 5.9;
    Solid(4, points);
    // set current textstyle to Style1
    // draw text
    dxffile.Text("Sample Text", 5.9, 6.7, 0.3, 35);
    // draw a dimension line
    dxffile.DimLinear(6.05, 3, 9.55, 3, 9.55, 2, 0, "3.50");
    // close ENTITIES section ----------------------------------
  5. Close the DXF file

CDrawing class

CDrawing class has all the commands to create a drawing in memory and save it as a DXF file. Usage of CDrawing is as follows:

  1. Create the on-memory drawing
    CDrawing drw;
    drw.Create( );
  2. Create new LAYER, LTYPE, STYLE, DIMSTYLE table-types as many as you want.
    // Tables Section ------------------------------------------
    // LTYPE table type -------------------------
    LTYPE ltype;
    OBJHANDLE objhandle1;
    // Continuous
    ZeroMemory(<ype, sizeof(ltype));
    strcpy(ltype.Name, "Continuous");
    strcpy(ltype.DescriptiveText, "Solid line");
    objhandle1 = drw.AddLinetype(<ype);
    // DASHDOT2
    ZeroMemory(<ype, sizeof(ltype));
    strcpy(ltype.Name, "DASHDOT2");
      "Dash dot (.5x) _._._._._._._._._._._._._._._.");
    ltype.ElementsNumber = 4;
    ltype.PatternLength = 0.5;
    ltype.Elements[0] = 0.25;
    ltype.Elements[1] = -0.125;
    ltype.Elements[2] = 0.0;
    ltype.Elements[3] = -0.125;
    // LAYER table type -------------------------
    LAYER layer;
    // Layer1
    ZeroMemory(&layer, sizeof(layer));
    strcpy(layer.Name, "Layer1");
    layer.Color = 1;
    layer.LineTypeObjhandle = objhandle1; // Continuous
    // Layer2
    ZeroMemory(&layer, sizeof(layer));
    strcpy(layer.Name, "Layer2");
    layer.Color = 2;
    layer.LineTypeObjhandle = objhandle1; // Continuous
    // STYLE table type -------------------------
    STYLE style;
    ZeroMemory(&style, sizeof(style));
    strcpy(style.Name, "Style1");
    strcpy(style.PrimaryFontFilename, "TIMES.TTF");
    style.LastHeightUsed = 0.3;
    style.WidthFactor = 1;
    objhandle1 = drw.AddTextStyle(&style);
    // DIMSTYLE table type ----------------------
        DIMSTYLE dimstyle;
    // DIM1
    ZeroMemory(&dimstyle, sizeof(dimstyle));
    strcpy(dimstyle.Name, "DIM1"); // DimStyle Name
    dimstyle.dimclrd = 2; // Dimension line & Arrow heads color
    dimstyle.dimdle = 0.0000; // Dimension line size after Extensionline
    dimstyle.dimclre = 2; // Extension line color
    dimstyle.dimexe = 0.1800; // Extension line size after Dimline
    dimstyle.dimexo = 0.0625; // Offset from origin
    strcpy(dimstyle.dimblk1, "ClosedFilled");// 1st Arrow head
    strcpy(dimstyle.dimblk2, "ClosedFilled");// 2nd Arrow head
    dimstyle.dimasz = 0.1800; // Arrow size
    dimstyle.dimtxstyObjhandle = objhandle1;// Text style
    dimstyle.dimclrt = 3; // Text color
    dimstyle.dimtxt = 0.1800; // Text height
    dimstyle.dimtad = 1; // Vertical Text Placement
    dimstyle.dimgap = 0.0900; // Offset from dimension line
    // Entities Section ------------------------------------------
    // set current layer to Layer2
    // draw a line
    drw.Line(1.2, 3.3, 7.5, 7.7);
    // draw a circle
    drw.Circle(7.8, 4.3, 1.75);
    // set current layer to Layer1
    // draw a solid
    REALPOINT points[4];
    points[0].x = 10.4; points[0].y = 7.2;
    points[1].x = 13.6; points[1].y = 7.4;
    points[2].x = 13.1; points[2].y = 4.9;
    points[3].x = 10.9; points[3].y = 5.9;
    drw.Solid(points[0], points[1], points[2], points[3]);
    // set current textstyle to Style1
    // draw text
    drw.Text("Sample Text", 5.9, 6.7, 0.3, 35);
    // draw a dimension line
    drw.DimLinear(6.05, 3, 9.55, 3, 9.55, 2, 0, "3.50");
  4. Save data to a DXF file.
  5. Destroy CDrawing and free allocated memory.

Loading data from a DXF file

  1. Create the on-memory drawing.
    CDrawing drw;
    drw.Create( );
  2. Use LoadDXFFile member function to load DXF file into memory.

That's all!


Since I am a Civil Engineer, I decided to write a program to generate a beam or columns detail sheet without the use of AutoCAD. I have written a program that, with a little data about beam or column, will create the detail sheet automatically. Output of this program is a DXF file and it can be shown in AutoCAD or it can be plotted with it. This program can save the time for drawing the detail sheet with AutoCAD. If you are an AutoCAD operator, you will understand the meaning of words that are used in this article, or if you are a programmer who wants to write a program to create DXF files, first you need a little knowledge about AutoCAD or the drawing programs such as is mentioned above. This code can be useful for programmers who need to create DXF files from their programs. CadLib is not the best one and also there are many commercial software for creating DXF files but they are not open source. Feel free to change the code. Your comments in regards to this article will cause the improvement of CadLib.


  • 20 Dec 2002
    • First release of CadLib
  • 19 Jan 2003
    • Some bug fixes
    • Added Dimension-Line support. It's a combination of other entity commands like "Line" and "Solid"
    • Added BLOCKS section support
    • Added Arc, Point and InsertBlock commands for ENTITIES section
    • Text command has been improved
  • 11 May 2003
    • Added CDrawing class to store drawing data in memory and change the data before saving it as a DXF file.
  • 28 June 2003
    • Added DXF read capability to CDrawing class
    • Some bug fixes of CDrawing class when writing data to a DXF file
  • 22 Nov 2003 (CadLib Version 2.00)
    • Added Drawing View capability
    • Added PolyLine command (by Tran duy Dung)
    • Improved DXF loading speed
    • Some bug fixes of drawing memory management functions
  • 24 Aug 2004 (CadLib Version 2.10)
    • Added ZoomExtents Function
    • Improved Viewing functions to show Dashed Lines
    • Added "ChangeEntity" & "DeleteEntity" commands
    • Added Dimension view capability
    • Fixed a bug occures when viewing a rotated block
    • Improved viewing of texts


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


About the Author

Omid Shahabi
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

QuestionProblem PinmemberMember 1090518913-Oct-14 0:25 
QuestionHow can I use this dll in C#? Pinmembernooshin29-Sep-14 22:26 
QuestionC# DXF READER PinmemberShobeir20-Sep-14 21:55 
Bugthe resolution of polyline display error and another nibs Pinmemberhaizhongtaojin23-Jun-14 5:51 
QuestionAlmost certainly... Pinmemberalbionrulesok18-Jun-12 0:27 
GeneralMy vote of 5 PinmemberAnOldog27-Feb-12 6:28 
GeneralMy vote of 5 Pinmembermanoj kumar choubey18-Feb-12 1:00 
QuestionHeader.h Pinmemberdirizabal19-Dec-11 4:42 
GeneralMy vote of 5 PinmemberMember 816562112-Oct-11 9:16 
QuestionWhat about 3D drawing? PinmemberDavid M14-Jan-11 12:31 
QuestionAny future improvements ? PinmemberUsama Taha30-Dec-10 9:29 
Questionwhere is dll file? Pinmemberwestanbul28-Nov-10 10:42 
GeneralCalculating area PinmemberWHWASHEN31-Oct-10 0:55 
GeneralDXF Writer in C# PinmemberThe Docterd28-Sep-10 22:14 
GeneralRe: DXF Writer in C# PinmemberThaRudeDude11-Feb-11 0:04 
Generaldxf to g-code Pinmemberfika_fa2-Jun-10 22:25 
GeneralProblem with ELLIPSE Pinmemberigorgrossman3-Feb-10 18:41 
GeneralHi Pinmemberluciyel19-Oct-09 19:57 
GeneralHello PinmemberMurgan111121-Aug-09 12:49 
QuestionHow to reset text color Pinmembersky7321212-Jul-09 22:47 
QuestionGetting coordinates of a line!!!!?? Pinmembervantomas5-Jun-09 4:23 
GeneralLoading a file PinmemberInbarBarkai12-Apr-09 23:28 
Generalbug in writing PinmemberMember 47131459-Apr-09 3:09 
Questionc# dxf reader PinmemberInbarBarkai1-Apr-09 2:08 
AnswerRe: c# dxf reader PinmemberTurboSasa16-Apr-09 22:17 
GeneralRe: c# dxf reader PinmemberThaRudeDude11-Feb-11 0:05 
QuestionHow to Convert CAD coordinate to pixel coordinate Pinmemberjinmengcheng22-Feb-09 16:10 
QuestionHOW TO GET ENTITY INFO? Pinmembervantomas3-Feb-09 12:50 
GeneralUsing library only with dll Pinmembervantomas3-Feb-09 8:00 
GeneralRe: Using library only with dll [modified] PinmemberUsama Taha30-Dec-10 9:54 
QuestionSOS! How to use library? Pinmembervantomas3-Feb-09 7:29 
GeneralThank you! Pinmembervantomas29-Jan-09 12:36 
QuestionIs the CadIO.dll still available for download? PinmemberMember 222115625-Oct-08 2:05 
QuestionHow can i draw only one layer??? PinmemberRafkat23-Oct-08 5:11 
GeneralProblems with Unicode Pinmembermalfaro15-Oct-08 16:53 
GeneralRe: Problems with Unicode PinmemberMember 473089515-Nov-08 7:33 
GeneralRe: Problems with Unicode Pinmembermalfaro15-Nov-08 14:04 
QuestionRe: Problems with Unicode PinmemberMember 473089517-Nov-08 3:26 
AnswerRe: Problems with Unicode Pinmembermalfaro17-Nov-08 7:04 
GeneralRe: Problems with Unicode PinmemberMember 473089517-Nov-08 8:00 
NewsRe: Problems with Unicode PinmemberMember 473089517-Nov-08 11:10 
GeneralRe: Problems with Unicode PinmemberPinballWizard11-Jun-09 4:16 
GeneralRe: Problems with Unicode PinmemberMember 47308952-Oct-09 6:28 
QuestionIt can not be good for the text! PinmemberWan--Vevi10-Oct-08 20:31 
AnswerRe: It can not be good for the text! PinmemberOmid Shahabi15-Oct-08 22:53 
QuestionHow can i draw an elliptical arc PinmemberMember 451675514-May-08 4:35 
GeneralCommercial License PinmemberTheCodeMan11-Mar-08 22:42 
QuestionUse in vb6 -need help PinmemberMember 45046947-Feb-08 0:09 
Generalموفق باشید Pinmembernimar17-Jan-08 16:40 
Generaldrawing a INSERT entity PinmemberKidAlchemy17-Oct-07 9:33 

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
Web01 | 2.8.141223.1 | Last Updated 26 Aug 2004
Article Copyright 2002 by Omid Shahabi
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid