Introduction
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:
- HEADER
- TABLES
- BLOCKS
- ENTITIES
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.
Classes
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:
- Create the DXF file
CDxfFileWrite dxffile;
dxffile.Create( "d:\\test.dxf" );
- Begin and end the HEADER section. It's here for compatibility with some CAD
programs. Others work without having HEADER section.
dxffile.BeginSection(SEC_HEADER);
dxffile.EndSection();
- Begin the TABLES section and put the LAYER, LTYPE, STYLE, DIMSTYLE
table-types as many as you want and then close the section
dxffile.BeginSection(SEC_TABLES);
dxffile.BeginTableType(TAB_LTYPE);
DXFLTYPE ltype;
double elem[4];
ZeroMemory(<ype, sizeof(ltype));
ltype.Name = "Continuous";
ltype.DescriptiveText = "Solid line";
dxffile.AddLinetype(<ype);
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;
dxffile.AddLinetype(<ype);
dxffile.EndTableType();
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();
dxffile.BeginTableType(TAB_STYLE);
DXFSTYLE tstyle;
ZeroMemory(&tstyle, sizeof(tstyle));
tstyle.Name = "Style1";
tstyle.PrimaryFontFilename = "TIMES.TTF";
tstyle.Height = 0.3;
tstyle.WidthFactor = 1;
dxffile.AddTextStyle(&tstyle);
dxffile.EndTableType();
dxffile.BeginTableType(TAB_DIMSTYLE);
DXFDIMSTYLE dimstyle;
ZeroMemory(&dimstyle, sizeof(dimstyle));
dimstyle.Name = "DIM1";
dimstyle.DIMCLRD = 2;
dimstyle.DIMDLE = 0.0000;
dimstyle.DIMCLRE = 2;
dimstyle.DIMEXE = 0.1800;
dimstyle.DIMEXO = 0.0625;
dimstyle.DIMBLK1 = "ClosedFilled";
dimstyle.DIMBLK2 = "ClosedFilled";
dimstyle.DIMASZ = 0.1800;
dimstyle.DIMTXSTY = "Style1";
dimstyle.DIMCLRT = 3;
dimstyle.DIMTXT = 0.1800;
dimstyle.DIMTAD = 1;
dimstyle.DIMGAP = 0.0900;
dxffile.AddDimStyle(&dimstyle);
dxffile.EndTableType();
dxffile.EndSection();
- Begin ENTITIES section and put entities data (LINE, CIRCLE, SOLID, TEXT,
ARC, POINT, DIMLINEAR) and finally close the section
dxffile.BeginSection(SEC_ENTITIES);
dxffile.SetCurrentLayer("Layer2");
dxffile.Line(1.2, 3.3, 7.5, 7.7);
dxffile.Circle(7.8, 4.3, 1.75);
dxffile.SetCurrentLayer("Layer4");
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);
dxffile.SetCurrentTextStyle("Style1");
dxffile.Text("Sample Text", 5.9, 6.7, 0.3, 35);
dxffile.SetCurrentDimStyle("DIM1");
dxffile.DimLinear(6.05, 3, 9.55, 3, 9.55, 2, 0, "3.50");
dxffile.EndSection();
- Close the DXF file
dxffile.Close();
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:
- Create the on-memory drawing
CDrawing drw;
drw.Create( );
- Create new LAYER, LTYPE, STYLE, DIMSTYLE table-types as many as you want.
LTYPE ltype;
OBJHANDLE objhandle1;
ZeroMemory(<ype, sizeof(ltype));
strcpy(ltype.Name, "Continuous");
strcpy(ltype.DescriptiveText, "Solid line");
objhandle1 = drw.AddLinetype(<ype);
ZeroMemory(<ype, sizeof(ltype));
strcpy(ltype.Name, "DASHDOT2");
strcpy(ltype.DescriptiveText,
"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;
drw.AddLinetype(<ype);
LAYER layer;
ZeroMemory(&layer, sizeof(layer));
strcpy(layer.Name, "Layer1");
layer.Color = 1;
layer.LineTypeObjhandle = objhandle1;
drw.AddLayer(&layer);
ZeroMemory(&layer, sizeof(layer));
strcpy(layer.Name, "Layer2");
layer.Color = 2;
layer.LineTypeObjhandle = objhandle1;
drw.AddLayer(&layer);
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 dimstyle;
ZeroMemory(&dimstyle, sizeof(dimstyle));
strcpy(dimstyle.Name, "DIM1");
dimstyle.dimclrd = 2;
dimstyle.dimdle = 0.0000;
dimstyle.dimclre = 2;
dimstyle.dimexe = 0.1800;
dimstyle.dimexo = 0.0625;
strcpy(dimstyle.dimblk1, "ClosedFilled");
strcpy(dimstyle.dimblk2, "ClosedFilled");
dimstyle.dimasz = 0.1800;
dimstyle.dimtxstyObjhandle = objhandle1;
dimstyle.dimclrt = 3;
dimstyle.dimtxt = 0.1800;
dimstyle.dimtad = 1;
dimstyle.dimgap = 0.0900;
drw.AddDimStyle(&dimstyle);
- Make entities data (LINE, CIRCLE, SOLID, TEXT, ARC, POINT, DIMLINEAR,
POLYLINE).
drw.SetLayer("Layer2");
drw.Line(1.2, 3.3, 7.5, 7.7);
drw.Circle(7.8, 4.3, 1.75);
drw.SetLayer("Layer1");
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]);
drw.SetTextStyle("Style1");
drw.Text("Sample Text", 5.9, 6.7, 0.3, 35);
drw.SetDimStyle("DIM1");
drw.DimLinear(6.05, 3, 9.55, 3, 9.55, 2, 0, "3.50");
- Save data to a DXF file.
drw.SaveDXFFile(DxfFileName);
- Destroy
CDrawing
and free allocated memory. drw.Destroy();
Loading data from a DXF file
- Create the on-memory drawing.
CDrawing drw;
drw.Create( );
- Use
LoadDXFFile
member function to load DXF file into memory. drw.LoadDXFFile("Sample.dxf");
That's all!
Conclusion
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.
History
- 20 Dec 2002
- 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