读取 DXF 文件
 
 
 

以下是一个简单的 Visual Basic 6 程序样例,它读取 DXF 文件,并且从给定段的给定对象中提取特定代码和值。

' ReadDXF 从 DXF 文件中提取指定的代码/值对。
' 此函数需要四个字符串参数、一个有效的 DXF
' 文件名、一个 DXF 段名、该段中对象的
' 名称以及由逗号分隔的代码列表。
'
Function ReadDXF( _
        ByVal dxfFile As String, ByVal strSection As String, _
        ByVal strObject As String, ByVal strCodeList As String)
    Dim tmpCode, lastObj As String
    Open dxfFile For Input As #1
    ' 获取第一个代码/值对
    codes = ReadCodes
    ' 遍历整个文件,直到“EOF”行
    While codes(1) <> "EOF"
        ' 如果组码为“0”,并且值为“SECTION”,则
        If codes(0) = "0" And codes(1) = "SECTION" Then
 ' 这必须是一个新的段,以便获取下一个
 ' 代码/值对。
 codes = ReadCodes()
 ' 如果此段是要获取的段,则
 If codes(1) = strSection Then
 ' 获取下一个代码/值对,并
 codes = ReadCodes
 ' 遍历此段,直到“ENDSEC”
 While codes(1) <> "ENDSEC"
 ' 在某段中,所有的“0”代码都表示
 ' 对象。如果找到了“0”代码,则存储
 ' 对象名称,供以后使用。
 If codes(0) = "0" Then lastObj = codes(1)
 ' 如果此对象是用户所需的对象,
 If lastObj = strObject Then
 ' 则在代码前后加上逗号
 tmpCode = "," & codes(0) & ","
 ' 如果此代码位于代码列表中,
 If InStr(strCodeList, tmpCode) Then
 ' 则附加返回值。
 ReadDXF = ReadDXF & _
 codes(0) & "=" & codes(1) & vbCrLf
 End If
 End If
 ' 读取其他代码/值对
 codes = ReadCodes
 Wend
 End If
        否则
 codes = ReadCodes
        End If
    Wend
    Close #1
End Function
' ReadCodes 从打开的文件中读取两行,并返回一个包含两个项目
' 的数组、一个组码及其组码值。只要一次读取 DXF 文件中的两行代码, 
' 所有程序应该都能够顺利运行。但为了使代码 
' 更可靠,应该添加一些进行错误检查和其他
' 检查的代码。
'
Function ReadCodes() As Variant
    Dim codeStr, valStr As String
    Line Input #1, codeStr
    Line Input #1, codeStr
    ' 修剪代码中的前导空格和后续空格
    ReadCodes = Array(Trim(codeStr), valStr)
End Function