明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 8231|回复: 19

[转帖]读写DXF文件的示例

  [复制链接]
发表于 2004-4-2 20:23:00 | 显示全部楼层 |阅读模式
<RE class=prog_in_0>一‘读DXF文件</PRE><RE class=prog_in_0>' ReadDXF extracts specified code/value pairs from a DXF file.</PRE><RE class=prog_in_0>' This function requires four string parameters, a valid DXF</PRE><RE class=prog_in_0>' file name, a DXF section name, the name of an object in that</PRE><RE class=prog_in_0>' section, and a comma delimited list of codes.</PRE><RE class=prog_in_0>'</PRE><RE class=prog_in_0>Function ReadDXF( _</PRE><RE class=prog_in_0>        ByVal dxfFile As String, ByVal strSection As String, _</PRE><RE class=prog_in_0>        ByVal strObject As String, ByVal strCodeList As String)</PRE><RE class=prog_in_0>    Dim tmpCode, lastObj As String</PRE><RE class=prog_in_0>    Open dxfFile For Input As #1</PRE><RE class=prog_in_0>    ' Get the first code/value pair</PRE><RE class=prog_in_0>    codes = ReadCodes</PRE><RE class=prog_in_0>    ' Loop through the whole file until the "EOF" line</PRE><RE class=prog_in_0>    While codes(1) &lt;&gt; "EOF"</PRE><RE class=prog_in_0>        ' If the group code is '0' and the value is 'SECTION' ..</PRE><RE class=prog_in_0>        If codes(0) = "0" And codes(1) = "SECTION" Then</PRE><RE class=prog_in_0>            ' This must be a new section, so get the next</PRE><RE class=prog_in_0>            ' code/value pair.</PRE><RE class=prog_in_0>            codes = ReadCodes()</PRE><RE class=prog_in_0>            ' If this section is the right one ..</PRE><RE class=prog_in_0>            If codes(1) = strSection Then</PRE><RE class=prog_in_0>                ' Get the next code/value pair and ..</PRE><RE class=prog_in_0>                codes = ReadCodes</PRE><RE class=prog_in_0>                ' Loop through this section until the 'ENDSEC'</PRE><RE class=prog_in_0>                While codes(1) &lt;&gt; "ENDSEC"</PRE><RE class=prog_in_0>                    ' While in a section, all '0' codes indicate</PRE><RE class=prog_in_0>                    ' an object. If you find a '0' store the</PRE><RE class=prog_in_0>                    ' object name for future use.</PRE><RE class=prog_in_0>                    If codes(0) = "0" Then lastObj = codes(1)</PRE><RE class=prog_in_0>                    ' If this object is one you're interested in</PRE><RE class=prog_in_0>                    If lastObj = strObject Then</PRE><RE class=prog_in_0>                        ' Surround the code with commas</PRE><RE class=prog_in_0>                        tmpCode = "," &amp; codes(0) &amp; ","</PRE><RE class=prog_in_0>                        ' If this code is in the list of codes ..</PRE><RE class=prog_in_0>                        If InStr(strCodeList, tmpCode) Then</PRE><RE class=prog_in_0>                            ' Append the return value.</PRE><RE class=prog_in_0>                            ReadDXF = ReadDXF &amp; _</PRE><RE class=prog_in_0>                                codes(0) &amp; "=" &amp; codes(1) &amp; vbCrLf</PRE><RE class=prog_in_0>                        End If</PRE><RE class=prog_in_0>                    End If</PRE><RE class=prog_in_0>                    ' Read another code/value pair</PRE><RE class=prog_in_0>                    codes = ReadCodes</PRE><RE class=prog_in_0>                Wend</PRE><RE class=prog_in_0>            End If</PRE><RE class=prog_in_0>        Else</PRE><RE class=prog_in_0>            codes = ReadCodes</PRE><RE class=prog_in_0>        End If</PRE><RE class=prog_in_0>    Wend</PRE><RE class=prog_in_0>    Close #1</PRE><RE class=prog_in_0>End Function</PRE><RE class=prog_in_0></PRE><RE class=prog_in_0>' ReadCodes reads two lines from an open file and returns a two item</PRE><RE class=prog_in_0>' array, a group code and its value. As long as a DXF file is read </PRE><RE class=prog_in_0>' two lines at a time, all should be fine. However, to make your </PRE><RE class=prog_in_0>' code more reliable, you should add some additional error and</PRE><RE class=prog_in_0>' other checking.</PRE><RE class=prog_in_0>'</PRE><RE class=prog_in_0>Function ReadCodes() As Variant</PRE><RE class=prog_in_0>    Dim codeStr, valStr As String</PRE><RE class=prog_in_0>    Line Input #1, codeStr</PRE><RE class=prog_in_0>    Line Input #1, valStr</PRE><RE class=prog_in_0>    ' Trim the leading and trailing space from the code</PRE><RE class=prog_in_0>    ReadCodes = Array(Trim(codeStr), valStr)</PRE><RE class=prog_in_end_0>End Function</PRE><RE class=prog_in_end_0>        </PRE><RE class=prog_in_end_0>二、写DXF文件</PRE><RE class=prog_in_end_0><RE class=prog_in_0>' WriteDXFPolygon creates a minimal DXF file that only contains</PRE><RE class=prog_in_0>' the ENTITIES section. This subroutine requires five parameters,</PRE><RE class=prog_in_0>' the DXF file name, the number of sides for the polygon, the X</PRE><RE class=prog_in_0>' and Y coordinates for the bottom end of the right-most side</PRE><RE class=prog_in_0>' (it starts in a vertical direction), and the length for each</PRE><RE class=prog_in_0>' side. Note that because this only requests 2D points, it does</PRE><RE class=prog_in_0>' not include the Z coordinates (codes 30 and 31). The lines are</PRE><RE class=prog_in_0>' placed on the layer "olygon."</PRE><RE class=prog_in_0>'</PRE><RE class=prog_in_0>Sub WriteDXFPolygon( _</PRE><RE class=prog_in_0>        dxfFile As String, iSides As Integer, _</PRE><RE class=prog_in_0>        dblX As Double, dblY As Double, dblLen As Double)</PRE><RE class=prog_in_0>    Dim i As Integer</PRE><RE class=prog_in_0>    Dim dblA1, dblA, dblPI, dblNX, dblNY As Double</PRE><RE class=prog_in_0>    Open dxfFile For Output As #1</PRE><RE class=prog_in_0>    Print #1, 0</PRE><RE class=prog_in_0>    Print #1, "SECTION"</PRE><RE class=prog_in_0>    Print #1, 2</PRE><RE class=prog_in_0>    Print #1, "ENTITIES"</PRE><RE class=prog_in_0>    dblPI = Atn(1) * 4</PRE><RE class=prog_in_0>    dblA1 = (2 * dblPI) / iSides</PRE><RE class=prog_in_0>    dblA = dblPI / 2</PRE><RE class=prog_in_0>    For i = 1 To iSides</PRE><RE class=prog_in_0>        Print #1, 0</PRE><RE class=prog_in_0>        Print #1, "LINE"</PRE><RE class=prog_in_0>        Print #1, 8</PRE><RE class=prog_in_0>        Print #1, "olygon"</PRE><RE class=prog_in_0>        Print #1, 10</PRE><RE class=prog_in_0>        Print #1, dblX</PRE><RE class=prog_in_0>        Print #1, 20</PRE><RE class=prog_in_0>        Print #1, dblY</PRE><RE class=prog_in_0>        dblNX = dblLen * Cos(dblA) + dblX</PRE><RE class=prog_in_0>        dblNY = dblLen * Sin(dblA) + dblY</PRE><RE class=prog_in_0>        Print #1, 11</PRE><RE class=prog_in_0>        Print #1, dblNX</PRE><RE class=prog_in_0>        Print #1, 21</PRE><RE class=prog_in_0>        Print #1, dblNY</PRE><RE class=prog_in_0>        dblX = dblNX</PRE><RE class=prog_in_0>        dblY = dblNY</PRE><RE class=prog_in_0>        dblA = dblA + dblA1</PRE><RE class=prog_in_0>    Next i</PRE><RE class=prog_in_0>    Print #1, 0</PRE><RE class=prog_in_0>    Print #1, "ENDSEC"</PRE><RE class=prog_in_0>    Print #1, 0</PRE><RE class=prog_in_0>    Print #1, "EOF"</PRE><RE class=prog_in_0>    Close #1</PRE><RE class=prog_in_end_0>End Sub</PRE></PRE>
发表于 2004-4-2 21:38:00 | 显示全部楼层
不错!
发表于 2004-4-3 12:12:00 | 显示全部楼层
好帖~~
发表于 2004-4-4 15:56:00 | 显示全部楼层
请问楼主这程序读些什么数据?
发表于 2004-4-10 17:14:00 | 显示全部楼层
不錯,值得學習!
发表于 2004-4-20 09:46:00 | 显示全部楼层
我在vc里写了一个dxf文件,为什么autocad加载了以后没有图形。


我把autocad自己的dxf文件只保留上述格式以后,同样没有反映。


所以我觉得只写dxf的实体段可能不行。


希望大家对我的观点发表一点观点!
发表于 2004-6-26 12:01:00 | 显示全部楼层
如果能读取而没有图形,可能是显示区域的问题,试一试z回车,a回车,可能就显示出来的,我也遇到过类似的问题
发表于 2004-9-17 00:35:00 | 显示全部楼层
请教楼上各位老师,我上传DXF是同一个图,后二个文件是CAD画的图,我在任一软件里都没有打开,怎样转换才可以在AOTOCAD里打开这图形。
发表于 2004-9-17 02:06:00 | 显示全部楼层
dxf: 0
SECTION
2
BLOCKS
0
BLOCK
8
1
2
1
70
64
10
0.0
20
0.0
0
TEXT
8
1
10
0.000000
20
0.000000
40
0.4999999924907534
1
PIECE NAME: 003P53508
7
STANDARD
0
TEXT
8
1
10
0
20
0
40
0
1
SIZE: 42
7
STANDARD
0
POLYLINE
8
1
66
1
70
0
0
VERTEX
8
1
10
242.700012
20
16.400001
0
VERTEX
8
1
10
244.200012
20
25.600001
0
VERTEX
8
1
10
244.500012
20
26.700001
42
0.00
0
VERTEX
8
1
10
245.100012
20
27.700001
0
VERTEX
8
1
10
270.000013
20
30.900001
42
0.00
0
VERTEX
8
1
10
295.500014
20
31.900002
0
VERTEX
8
1
10
342.300016
20
30.600001
42
0.00
0
VERTEX
8
1
10
389.000018
20
27.000001
0
VERTEX
8
1
10
397.700019
20
26.100001
0
VERTEX
8
1
10
400.500019
20
34.400002
0
VERTEX
8
1
10
403.300019
20
39.900002
42
0.00
0
VERTEX
8
1
10
407.600019
20
44.000002
0
VERTEX
8
1
10
415.200020
20
47.700002
42
0.00
0
VERTEX
8
1
10
423.200020
20
49.100002
0
VERTEX
8
1
10
522.200025
20
43.200002
42
0.00
0
VERTEX
8
1
10
651.300031
20
29.400001
0
VERTEX
8
1
10
659.300031
20
28.400001
0
VERTEX
8
1
10
662.700031
20
35.800002
0
VERTEX
8
1
10
672.100032
20
56.500003
0
VERTEX
8
1
10
679.100032
20
71.900003
0
VERTEX
8
1
10
662.200031
20
72.100003
0
VERTEX
8
1
10
632.800030
20
77.500004
42
0.00
0
VERTEX
8
1
10
604.900029
20
94.000004
0
VERTEX
8
1
10
596.700028
20
102.800005
42
0.00
0
VERTEX
8
1
10
589.600028
20
112.600005
0
VERTEX
8
1
10
581.600028
20
128.400006
42
0.00
0
VERTEX
8
1
10
577.800027
20
139.400007
0
VERTEX
8
1
10
575.800027
20
148.000007
0
VERTEX
8
1
10
567.000027
20
148.000007
0
VERTEX
8
1
10
386.000018
20
148.000007
0
VERTEX
8
1
10
41.000002
20
148.000007
0
VERTEX
8
1
10
35.400002
20
147.900007
0
VERTEX
8
1
10
10.100000
20
135.500006
42
0.00
0
VERTEX
8
1
10
0.100000
20
109.900005
0
VERTEX
8
1
10
0.000000
20
96.900005
0
VERTEX
8
1
10
0.000000
20
96.600005
0
VERTEX
8
1
10
2.700000
20
46.500002
42
0.00
0
VERTEX
8
1
10
6.900000
20
10.200000
0
VERTEX
8
1
10
8.400000
20
0.000000
0
VERTEX
8
1
10
18.800001
20
0.900000
0
VERTEX
8
1
10
83.800004
20
6.600000
42
0.00
0
VERTEX
8
1
10
162.300008
20
13.200001
0
VERTEX
8
1
10
189.100009
20
15.100001
0
VERTEX
8
1
10
212.700010
20
16.100001
42
0.00
0
VERTEX
8
1
10
233.400011
20
16.400001
0
VERTEX
8
1
10
242.700012
20
16.400001
0
SEQEND
8
1
0
POLYLINE
8
8
66
1
70
0
0
VERTEX
8
8
10
339.500016
20
74.000004
0
VERTEX
8
8
10
359.500017
20
74.000004
0
SEQEND
8
8
0
POLYLINE
8
8
66
1
70
0
0
VERTEX
8
8
10
339.500016
20
74.000004
0
VERTEX
8
8
10
339.500016
20
54.000003
0
SEQEND
8
8
0
POLYLINE
8
8
66
1
70
0
0
VERTEX
8
8
10
339.500016
20
74.000004
0
VERTEX
8
8
10
359.500017
20
74.000004
0
SEQEND
8
8
0
POLYLINE
8
8
66
1
70
0
0
VERTEX
8
8
10
339.500016
20
74.000004
0
VERTEX
8
8
10
339.500016
20
54.000003
0
SEQEND
8
8
0
POLYLINE
8
8
66
1
70
0
0
VERTEX
8
8
10
623.400030
20
55.300003
0
VERTEX
8
8
10
628.400030
20
55.300003
0
SEQEND
8
8
0
POLYLINE
8
8
66
1
70
0
0
VERTEX
8
8
10
625.900030
20
57.800003
0
VERTEX
8
8
10
625.900030
20
52.800003
0
SEQEND
8
8
0
POLYLINE
8
8
66
1
70
0
0
VERTEX
8
8
10
420.300020
20
49.100002
0
VERTEX
8
8
10
425.300020
20
49.100002
0
SEQEND
8
8
0
POLYLINE
8
8
66
1
70
0
0
VERTEX
8
8
10
422.800020
20
51.600002
0
VERTEX
8
8
10
422.800020
20
46.600002
0
SEQEND
8
8
0
POLYLINE
8
8
66
1
70
0
0
VERTEX
8
8
10
242.300012
20
27.300001
0
VERTEX
8
8
10
247.300012
20
27.300001
0
SEQEND
8
8
0
POLYLINE
8
8
66
1
70
0
0
VERTEX
8
8
10
244.800012
20
29.800001
0
VERTEX
8
8
10
244.800012
20
24.800001
0
SEQEND
8
8
0
LINE
8
1
10
339.500016
20
74.000004
11
359.500017
21
74.000004
0
ENDBLK
0
ENDSEC
0
SECTION
2
ENTITIES
0
TEXT
8
1
10
0.0000
20
0.0000
40
10.0000
1
STYLE NAME: 女装
7
STANDARD
0
TEXT
8
1
10
0.0000
20
0.0000
40
10.0000
1
CREATION DATE: 2003-5-3
7
STANDARD
0
TEXT
8
1
10
0.0000
20
0.0000
40
10.0000
1
CREATION TIME: 5-3-20
7
STANDARD
0
TEXT
8
1
10
0.0000
20
0.0000
40
10.0000
1
AUTHOR: ECHO SOFT CO. LTD
7
STANDARD
0
TEXT
8
1
10
0.0000
20
0.0000
40
10.0000
1
SAMPLE SIZE: 42
7
STANDARD
0
TEXT
8
1
10
0.0000
20
0.0000
40
10.0000
1
UNITS: METRIC
7
STANDARD
0
ENDSEC
0
EOF
发表于 2004-10-7 16:57:00 | 显示全部楼层
不能处理所有类型的对象!
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 13:35 , Processed in 0.199449 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表