DXF ファイルを作成するプログラムを書くことは、DXF ファイルを読み込むプログラムを書くことよりももっと難しいかもしれません。なぜなら、受け入れ可能ファイルであることを AutoCAD に分からせるために図面の一貫性を維持する必要があるからです。AutoCAD では、DXF ファイルの多数の項目を省略することができますが、それでも使用可能な図面を得ることができます。
次の Visual Basic 6 サブルーチンは、ポリゴンを表現する DXF ファイルを作成します。
' 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
指定されるデータが、正しい形式の項目としてその行に現れさえすれば、DXFIN[DXF 読み込み]コマンドはそのデータを受け入れます。(もちろん文字列項目は、文字列の一部に含まれる場合を除いて、その前にスペースを入れてはいけません)。上記の Visual Basic プログラムは、入力形式のこのような柔軟性を利用しています。作成されるファイルは、AutoCAD が作成するファイルとは必ずしも正確に同じではありません。
DXFIN コマンドでロードしたときにエラーが発生すると、AutoCAD はエラーの内容と、エラーが検出される前に DXF ファイル内で最後に処理した行を示すメッセージを表示します。報告される行は、エラーが発生した行であるとは限りません。特に、必要なグループが抜け落ちているようなエラーではそうです。