Drawing Interchange File Formats
ASCII DXF File Format
Writing DXF Interface Programs
Writing a program that communicates with AutoCAD by means of the DXF mechanism appears more difficult than it actually is. The DXF format makes it easy to ignore information you don't need, while reading the information you do need.
The following example is a Microsoft BASIC(tm) program that reads a DXF file and extracts all the line entities from the drawing (ignoring lines that appear inside blocks). It displays the endpoints of these lines on the screen. This program is an example of how simple a DXF-reading program can be.1000 REM
2200 CLOSE 1
Writing a program that constructs a DXF file poses different problems. You must maintain consistency within the drawing, although with AutoCAD you can omit many items in a DXF file and still obtain a usable drawing. The entire HEADER section can be omitted if you don't set header variables. Any of the tables in the TABLES section can be omitted if you don't need to make entries, and the entire TABLES section can be dropped if nothing in it is required. If you define any linetypes in the LTYPE table, this table must appear before the LAYER table. If no block definitions are used in the drawing, the BLOCKS section can be omitted. If present, the BLOCKS section must appear before the ENTITIES section. Within the ENTITIES section, you can reference layer names even though you haven't defined them in the LAYER table. Such layers are automatically created with color 7 and the CONTINUOUS linetype. The EOF item must be present at the end of file.
The following Microsoft BASIC program constructs a DXF file representing a polygon with a specified number of sides, leftmost origin point, and side length. This program supplies only the ENTITIES section of the DXF file and places all entities generated on the default layer 0. Because this program doesn't create the drawing header, the drawing limits, extents, and current view will be invalid after performing a DXFIN on the drawing generated by this program. You can do a ZOOM Extents to fill the screen with the drawing generated. Then adjust the limits manually.1000 REM
1420 CLOSE 1
As long as a properly formatted item appears on the line on which the data is expected, DXFIN accepts it. (Of course, string items should not have leading spaces unless these are intended to be part of the string.) This program takes advantage of this flexibility in input format and does not generate a file exactly like one generated by AutoCAD.
In the case of an error in using DXFIN TO load, AutoCAD reports the error with a message indicating the nature of the error and the last line processed in the dxf file before the error was detected. This may not be the line on which the error occurred, especially in the case of errors such as the omission of required groups.