CAD开发者社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

DXF 参考手册

读取 DXF 文件 (DXF)

2023-1-6 15:05| 发布者: admin| 查看: 115| 评论: 0|来自: AutoCAD

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

' ReadDXF extracts specified code/value pairs from a DXF file.
' This function requires four string parameters, a valid DXF
' file name, a DXF section name, the name of an object in that
' section, and a comma delimited list of codes.
'
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
    ' Get the first code/value pair
    codes = ReadCodes
    ' Loop through the whole file until the "EOF" line
    While codes(1) <> "EOF"
        ' If the group code is '0' and the value is 'SECTION' ..
        If codes(0) = "0" And codes(1) = "SECTION" Then
            ' This must be a new section, so get the next
            ' code/value pair.
            codes = ReadCodes()
            ' If this section is the right one ..
            If codes(1) = strSection Then
                ' Get the next code/value pair and ..
                codes = ReadCodes
                ' Loop through this section until the 'ENDSEC'
                While codes(1) <> "ENDSEC"
                    ' While in a section, all '0' codes indicate
                    ' an object. If you find a '0' store the
                    ' object name for future use.
                    If codes(0) = "0" Then lastObj = codes(1)
                    ' If this object is one you're interested in
                    If lastObj = strObject Then
                        ' Surround the code with commas
                        tmpCode = "," & codes(0) & ","
                        ' If this code is in the list of codes ..
                        If InStr(strCodeList, tmpCode) Then
                            ' Append the return value.
                            ReadDXF = ReadDXF & _
                                codes(0) & "=" & codes(1) & vbCrLf
                        End If
                    End If
                    ' Read another code/value pair
                    codes = ReadCodes
                Wend
            End If
        Else
            codes = ReadCodes
        End If
    Wend
    Close #1
End Function
' ReadCodes reads two lines from an open file and returns a two item
' array, a group code and its value. As long as a DXF file is read 
' two lines at a time, all should be fine. However, to make your 
' code more reliable, you should add some additional error and
' other checking.
'
Function ReadCodes() As Variant
    Dim codeStr, valStr As String
    Line Input #1, codeStr
    Line Input #1, valStr
    ' Trim the leading and trailing space from the code
    ReadCodes = Array(Trim(codeStr), valStr)
End Function

路过

雷人

握手

鲜花

鸡蛋

最新评论

QQ|Archiver|CAD开发者社区 ( 苏ICP备2022047690号-1 )

GMT+8, 2024-6-10 18:59

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

返回顶部