Writing a DXF File

AutoCAD DXF Format

 
Writing a DXF File
 
 
 

Writing a program that creates a DXF file can be more difficult than one that reads a DXF file, because you must maintain consistency within the drawing in order for AutoCAD to find the file acceptable. AutoCAD lets you 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 Visual Basic 6 subroutine constructs a DXF file representing a polygon.

' WriteDXFPolygon creates a minimal DXF file that only contains
' the ENTITIES section. This subroutine requires five parameters,
' the DXF file name, the number of sides for the polygon, the X
' and Y coordinates for the bottom end of the right-most side
' (it starts in a vertical direction), and the length for each
' side. Note that because this only requests 2D points, it does
' not include the Z coordinates (codes 30 and 31). The lines are
' placed on the layer "Polygon."
'
Sub WriteDXFPolygon( _
        dxfFile As String, iSides As Integer, _
        dblX As Double, dblY As Double, dblLen As Double)
    Dim i As Integer
    Dim dblA1, dblA, dblPI, dblNX, dblNY As Double
    Open dxfFile For Output As #1
    Print #1, 0
    Print #1, "SECTION"
    Print #1, 2
    Print #1, "ENTITIES"
    dblPI = Atn(1) * 4
    dblA1 = (2 * dblPI) / iSides
    dblA = dblPI / 2
    For i = 1 To iSides
        Print #1, 0
        Print #1, "LINE"
        Print #1, 8
        Print #1, "Polygon"
        Print #1, 10
        Print #1, dblX
        Print #1, 20
        Print #1, dblY
        dblNX = dblLen * Cos(dblA) + dblX
        dblNY = dblLen * Sin(dblA) + dblY
        Print #1, 11
        Print #1, dblNX
        Print #1, 21
        Print #1, dblNY
        dblX = dblNX
        dblY = dblNY
        dblA = dblA + dblA1
    Next i
    Print #1, 0
    Print #1, "ENDSEC"
    Print #1, 0
    Print #1, "EOF"
    Close #1
End Sub

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 BASIC 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.