![]() |
![]() |
![]() |
![]() |
|||||
|
||||||||
4 Writing DXF FilesTo 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 ObjectTo 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:
DL_Dxf dxf; Writing the DXF HeaderOpening the DXF HeaderThe DXF header contains information about the DXF version. It has to be written before anything else with dxf.writeHeader(*dw); The following list shows how a DXF header typically looks like:
999 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 VariablesVariables 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 HeaderUse the following code to close the DXF header (end the current section): dw->sectionEnd(); Writing the Tables SectionOpening the Tables SectionThe tables section of a DXF file contains some tables defining viewports, linestyles, layers, etc. Open the tables section with the function: dw->sectionTables(); Writing the ViewportsViewports 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: dxf.writeVPort(*dw); Writing the LinetypesOnly 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.
dw->tableLineTypes(25); Writing the LayersLayers 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 TablesThese tables are also needed. For more information, please refer to the DXF documentation [DXF].
dxf.writeStyle(*dw); Writing Dimension StylesDimension Styles define the look of dimensions.
dxf.writeDimStyle(*dw, Writing Block RecordsBlock 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.
dxf.writeBlockRecord(*dw); Ending the Tables Sectiondw->sectionEnd(); Writing the Blocks SectionThe blocks section defines the entities of each block.
dw->sectionBlocks(); Writing the Entities SectionThe 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).
dw->sectionEntities(); Writing the Objects Section
dxf.writeObjects(*dw); Ending and Closing the File
dw->dxfEOF(); |
||||||||
|