2 <opt name="PageTitle" value="Writing DXF Files" />
3 <section>Writing DXF Files</section>
4 <idx>Writing,Output,DL_Codes,DXF 2000,DXF 2002,R12</idx>
7 To write a DXF file, you need to wrap the entities, layers, blocks, ..
8 you have into the wrapper classes of dxflib. Since dxflib does not
9 store any entities, you need to iterate through your entities and
10 call the write functions for each of them. Please note that you
11 have to stick to the exact order in which you call the write
12 functions of dxflib. Otherwise your DXF file will not be standard
16 <subsection>Creating the Writer Object</subsection>
19 To create a DXF writer object you need to specify the file name
20 as well as the DXF version you want to produce. At the time of
21 writing only two DXF versions were supported: R12 and
22 DXF 2000/2002. The dxflib codes for DXF version R12 is
23 DL_Codes::AC1009 and for DXF 2000/2002 DL_Codes::AC1015.
27 There are two APIs you will need to write a DXF file. The
28 API in DL_WriterA offers low level functions to write basic
29 key/value tuples on which a DXF file is based.
30 Creating a valid DXF file using only these functions would
31 be very difficult and inconvenient. Therefore, there is
32 a higher level API in the DL_Dxf class which allows you
33 to write for example a whole line without knowing the
34 key/value tuples that are needed for it.
38 The following code creates and opens a file for a DXF 2000/2002
42 <includecode src="code/write01.cpp" />
47 Writing the DXF Header
52 Opening the DXF Header
56 The DXF header contains information about the DXF version. It has
57 to be written before anything else with
60 <includecode src="code/write02.cpp" />
63 The following list shows how a DXF header typically looks like:
66 <includecode src="code/header.dxf" />
69 As you can see, the writeHeader() function does not close
70 the header. This is because you might want to store a set of
71 variables into it. If you have to store variables, you have to
72 do it now. If not, proceed with "Closing the Header".
76 Storing Additional Variables
81 Variables in the DXF header are used to store meta data for
82 the drawing contained in the file. For a description of
83 all supported variables, please refer to the DXF documentation
84 <a href="appendix_bib.html#dxf">[DXF]</a>.
88 The following code snippet shows examples for storing variables of
89 different types. You can store as many variables as you need but
90 you have to stick to the supported variable names and types in order
91 to create a valid DXF file.
94 <includecode src="code/write03.cpp" />
101 Use the following code to close the DXF header (end the current section):
104 <includecode src="code/write04.cpp" />
109 Writing the Tables Section
114 Opening the Tables Section
118 The tables section of a DXF file contains some tables defining
119 viewports, linestyles, layers, etc.
123 Open the tables section with the function:
126 <includecode src="code/write10.cpp" />
129 Writing the Viewports
134 Viewports are not directly supported by dxflib. However, they
135 still need to be there in a valid DXF file. You can write the
136 standard viewports using the function:
139 <includecode src="code/write11.cpp" />
142 Writing the Linetypes
147 Only linetypes that are actually used need to be defined in the
148 DXF file. For simplification, you might want to store all
149 linetypes supported by dxflib as shown below.
152 <includecode src="code/write12.cpp" />
160 Layers are a substantial part of most DXF drawings. All layers
161 that are used in the drawing need to be defined in this
162 table section. The following example code writes three layers
163 with names "0", "mainlayer" and "anotherlayer" to the DXF file.
164 Note that before writing the layers, you need to specify
165 how many layers there are in total. Layer "0" is the default
166 layer. It cannot be omitted.
169 <includecode src="code/write13.cpp" />
172 The default line width is given in 1/100mm. The color enum
173 in namespace DL_Codes defines the most common colors.
177 Writing Various Other Tables
181 These tables are also needed. For more information, please refer
182 to the DXF documentation <a href="appendix_bib.html#dxf">[DXF]</a>.
185 <includecode src="code/write14.cpp" />
188 Writing Dimension Styles
190 <idx>Dimension Styles</idx>
193 Dimension Styles define the look of dimensions.
196 <includecode src="code/write15.cpp" />
199 Writing Block Records
201 <idx>Block Records</idx>
204 Block records define the names of available blocks in the DXF
205 file. The following example declares the existence of two blocks
206 with names "myblock1" and "myblock2". Note that the first
207 call is also needed. It opens the blocks table and writes
208 some standard blocks that might be required by the DXF
212 <includecode src="code/write16.cpp" />
215 Ending the Tables Section
218 <includecode src="code/write17.cpp" />
221 Writing the Blocks Section
226 The blocks section defines the entities of each block.
229 <includecode src="code/write20.cpp" />
232 Writing the Entities Section
237 The entities section defines the entities of the drawing. The
238 two entities in the following example use the attributes
239 of their layer (256 = color by layer, -1 = line width by layer,
240 "BYLAYER" = line style by layer).
243 <includecode src="code/write30.cpp" />
246 Writing the Objects Section
249 <includecode src="code/write40.cpp" />
252 Ending and Closing the File
255 <includecode src="code/write50.cpp" />