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
1010  REM Extract lines from DXF file
1020  REM
1030  G1% = 0
1040  LINE INPUT "DXF file name: "; A$
1050  OPEN "i", 1, A$ + ".dxf"
1060  REM
1070  REM Ignore until section start encountered
1080  REM
1090  GOSUB 2000
1100  IF G% <> 0 THEN 1090
1110  IF S$ <> "SECTION" THEN 1090
1120  GOSUB 2000
1130  REM
1140  REM Skip unless ENTITIES section
1150  REM
1160  IF S$ <> "ENTITIES" THEN 1090
1170  REM
1180  REM Scan until end of section, processing LINEs
1190  REM
1200  GOSUB 2000
1210  IF G% = 0 AND S$ = "ENDSEC" THEN 2200
1220  IF G% = 0 AND S$ = "LINE" THEN GOSUB 1400 : GOTO 1210
1230  GOTO 1200
1400  REM
1410  REM Accumulate LINE entity groups
1420  REM
1430  GOSUB 2000
1440  IF G% = 10 THEN X1 = X : Y1 = Y : Z1 = Z
1450  IF G% = 11 THEN X2 = X : Y2 = Y : Z2 = Z
1460  IF G% = 0 THEN PRINT "Line from
(";X1;",";Y1;",";Z1;") to (";X2;",";Y2;",";Z2;")":RETURN
1470  GOTO 1430
2000  REM
2010  REM Read group code and following value
2020  REM For X coordinates, read Y and possibly Z also
2030  REM
2040  IF G1% < 0 THEN G% = -G1% : G1% = 0 ELSE INPUT #1, G%
2050  IF G% < 10 OR G% = 999 THEN LINE INPUT #1, S$ : RETURN
2060  IF G% >= 38 AND G% <= 49 THEN INPUT #1, V : RETURN
2080  IF G% >= 50 AND G% <= 59 THEN INPUT #1, A : RETURN
2090  IF G% >= 60 AND G% <= 69 THEN INPUT #1, P% : RETURN
2100  IF G% >= 70 AND G% <= 79 THEN INPUT #1, F% : RETURN
2110  IF G% >= 210 AND G% <= 219 THEN 2130
2115  IF G% >= 1000 THEN LINE INPUT #1, T$ : RETURN
2120  IF G% >= 20 THEN PRINT "Invalid group code";G% : STOP
2130  INPUT #1, X
2140  INPUT #1, G1%
2150  IF G1% <> (G%+10) THEN PRINT "Invalid Y coord code";
G1% : STOP
2160  INPUT #1, Y
2170  INPUT #1, G1%
2180  IF G1% <> (G%+20) THEN G1% = -G1% ELSE INPUT #1, Z
2190  RETURN

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
1010  REM Polygon generator
1020  REM
1030  LINE INPUT "Drawing (DXF) file name: "; A$
1040  OPEN "o", 1, A$ + ".dxf"
1050  PRINT #1, 0
1060  PRINT #1, "SECTION"
1070  PRINT #1, 2
1080  PRINT #1, "ENTITIES"
1090  PI = ATN(1) * 4
1100  INPUT "Number of sides for polygon: "; S%
1110  INPUT "Starting point (X,Y): "; X, Y
1120  INPUT "Polygon side: "; D
1130  A1 = (2 * PI) / S%
1140  A = PI / 2
1150  FOR I% = 1 TO S%
1160  PRINT #1, 0
1170  PRINT #1, "LINE"
1180  PRINT #1, 8
1190  PRINT #1, "0"
1200  PRINT #1, 10
1210  PRINT #1, X
1220  PRINT #1, 20
1230  PRINT #1, Y
1240  PRINT #1, 30
1250  PRINT #1, 0.0
1260  NX = D * COS(A) + X
1270  NY = D * SIN(A) + Y
1280  PRINT #1, 11
1290  PRINT #1, NX
1300  PRINT #1, 21
1310  PRINT #1, NY
1320  PRINT #1, 31
1330  PRINT #1, 0.0
1340  X = NX
1350  Y = NY
1360  A = A + A1
1370  NEXT I%
1380  PRINT #1, 0
1390  PRINT #1, "ENDSEC"
1400  PRINT #1, 0
1410  PRINT #1, "EOF"

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.