DXF를 사용하면 고품질의 CAD 파일을 출력할 수 있다. 즉 VBA를 통하여 자동으로 도면을 그릴 수 있다. DXF는 AutoCAD와 호환이 되도록 설계된 파일형식이다.
작성의도 : 도면 작성 자동화
아래의 코드는 여기에 있는 DXF파일을 출력하는 코드이다.
Sub WriteDXFFileTest()
Dim fileNum As Integer
Dim fileName As String
'
fileNum = FreeFile
fileName = "C:\DXFTest.dxf"
'
Open fileName For Output As #fileNum
'
Print #fileNum, Format(999, "@@@")
Print #fileNum, "Created by SolarView"
'
Print #fileNum, Format(0, "@@@")
Print #fileNum, "SECTION"
'
Print #fileNum, Format(2, "@@@")
Print #fileNum, "ENTITIES"
'
Call WriteDXFLine(fileNum, 0, 4, 12.5, 13.5, 0, 100.7, 101.7, 0)
'
Print #fileNum, Format(0, "@@@")
Print #fileNum, "ENDSEC"
'
Print #fileNum, Format(0, "@@@")
Print #fileNum, "EOF"
'
Close #fileNum
'
End Sub
LINE명령어를 처리하는 함수는 다음과 같이 작성할 수 있다.
Sub WriteDXFLine(FileNumber As Integer, Layer As String, Color As Integer, _
X1 As Double, Y1 As Double, Z1 As Double, _
X2 As Double, Y2 As Double, Z2 As Double)
'
'"Line" entity
Print #FileNumber, Format(0, "@@@")
Print #FileNumber, "LINE"
'
'Layer Name
Print #FileNumber, Format(8, "@@@")
Print #FileNumber, Layer
'
'Line Color
Print #FileNumber, Format(62, "@@@")
Print #FileNumber, Color
'
'x coordinate of start point
Print #FileNumber, Format(10, "@@@")
Print #FileNumber, X1
'
'y coordinate of start point
Print #FileNumber, Format(20, "@@@")
Print #FileNumber, Y1
'
'z coordinate of start point
Print #FileNumber, Format(30, "@@@")
Print #FileNumber, Z1
'
'x coordinate of end point
Print #FileNumber, Format(11, "@@@")
Print #FileNumber, X2
'
'y coordinate of end point
Print #FileNumber, Format(21, "@@@")
Print #FileNumber, Y2
'
'z coordinate of end point
Print #FileNumber, Format(31, "@@@")
Print #FileNumber, Z2
'
End Sub
이와 같은 요령으로 AutoCAD의 line뿐만 아니라 circle, arc, pline 등 모든 도면 요소(drawing entities)를 그릴 수 있다.