4 Writing DXF Files
To write a DXF file, you need to wrap the entities, layers, blocks, .. you have into the wrapper classes of dxflib. Since dxflib does not store any entities, you need to iterate through your entities and call the write functions for each of them. Please note that you have to stick to the exact order in which you call the write functions of dxflib. Otherwise your DXF file will not be standard conform.
Creating the Writer Object
To create a DXF writer object you need to specify the file name as well as the DXF version you want to produce. At the time of writing only two DXF versions were supported: R12 and DXF 2000/2002. The dxflib codes for DXF version R12 is DL_Codes::AC1009 and for DXF 2000/2002 DL_Codes::AC1015.
There are two APIs you will need to write a DXF file. The API in DL_WriterA offers low level functions to write basic key/value tuples on which a DXF file is based. Creating a valid DXF file using only these functions would be very difficult and inconvenient. Therefore, there is a higher level API in the DL_Dxf class which allows you to write for example a whole line without knowing the key/value tuples that are needed for it.
The following code creates and opens a file for a DXF 2000/2002 drawing:
Writing the DXF Header
Opening the DXF Header
The DXF header contains information about the DXF version. It has to be written before anything else with
The following list shows how a DXF header typically looks like:
As you can see, the writeHeader() function does not close the header. This is because you might want to store a set of variables into it. If you have to store variables, you have to do it now. If not, proceed with "Closing the Header".
Storing Additional Variables
Variables in the DXF header are used to store meta data for the drawing contained in the file. For a description of all supported variables, please refer to the DXF documentation [DXF].
The following code snippet shows examples for storing variables of different types. You can store as many variables as you need but you have to stick to the supported variable names and types in order to create a valid DXF file.
// int variable:
Closing the Header
Use the following code to close the DXF header (end the current section):
Writing the Tables Section
Opening the Tables Section
The tables section of a DXF file contains some tables defining viewports, linestyles, layers, etc.
Open the tables section with the function:
Writing the Viewports
Viewports are not directly supported by dxflib. However, they still need to be there in a valid DXF file. You can write the standard viewports using the function:
Writing the Linetypes
Only linetypes that are actually used need to be defined in the DXF file. For simplification, you might want to store all linetypes supported by dxflib as shown below.
Writing the Layers
Layers are a substantial part of most DXF drawings. All layers that are used in the drawing need to be defined in this table section. The following example code writes three layers with names "0", "mainlayer" and "anotherlayer" to the DXF file. Note that before writing the layers, you need to specify how many layers there are in total. Layer "0" is the default layer. It cannot be omitted.
int numberOfLayers = 3;
The default line width is given in 1/100mm. The color enum in namespace DL_Codes defines the most common colors.
Writing Various Other Tables
These tables are also needed. For more information, please refer to the DXF documentation [DXF].
Writing Dimension Styles
Dimension Styles define the look of dimensions.
Writing Block Records
Block records define the names of available blocks in the DXF file. The following example declares the existence of two blocks with names "myblock1" and "myblock2". Note that the first call is also needed. It opens the blocks table and writes some standard blocks that might be required by the DXF version.
Ending the Tables Section
Writing the Blocks Section
The blocks section defines the entities of each block.
Writing the Entities Section
The entities section defines the entities of the drawing. The two entities in the following example use the attributes of their layer (256 = color by layer, -1 = line width by layer, "BYLAYER" = line style by layer).
Writing the Objects Section
Ending and Closing the File