📄 writing_dxf_interface_programs_al_u05_b.htm
字号:
<html>
<head>
<meta name=ID content="Writing_DXF_Interface_Programs_al_u05_b">
<title>Writing DXF Interface Programs [ACG]</title>
<meta name=KEYWORDS content="DXF files, interface programs;DXF files, writing interface programs">
<meta name=BROWSE content="BROWSE1:000">
<meta name=WINDOW content="main">
</head>
<body bgcolor=#ffffff link=#003399 vlink=#003399>
<table width=100% cellpadding=5 cellspacing=0 border=0><tr><td>
<font face=Arial color=#003399 size=3><b> <a href="drawing_interchange_file_formats.htm">Drawing Interchange File Formats</a>
</b></font>
<hr size=1>
<font face=Arial color=#003399 size=3><b><a href="ascii_dxf_file_format_al_u05_b.htm"><img src=images/book2.gif border=0> ASCII DXF File Format</a>
</b></font><br>
</td></tr><tr><td>
<font face=Arial color=#003399 size=3><b><img src=images/bk_space.gif border=0> <img src=images/book2.gif border=0> Writing DXF Interface Programs
</b></font>
<hr size=1>
</td></tr></table>
<p><font face="MS Sans Serif,Arial" size=2>Writing a program that communicates with AutoCAD by means of the DXF mechanism appears more difficult than it actually is. The DXF format makes it easy to ignore information you don't need, while reading the information you do need.
</font></p>
<p><font face="MS Sans Serif,Arial" size=2>The following example is a Microsoft BASIC(tm) program that reads a DXF file and extracts all the line entities from the drawing (ignoring lines that appear inside blocks). It displays the endpoints of these lines on the screen. This program is an example of how simple a DXF-reading program can be.
</font></p>
<font face="Courier New,Courier" size=2>1000 REM
</font><br>
<font face="Courier New,Courier" size=2>1010 REM Extract lines from DXF file
</font><br>
<font face="Courier New,Courier" size=2>1020 REM
</font><br>
<font face="Courier New,Courier" size=2>1030 G1% = 0
</font><br>
<font face="Courier New,Courier" size=2>1040 LINE INPUT "DXF file name: "; A$
</font><br>
<font face="Courier New,Courier" size=2>1050 OPEN "i", 1, A$ + ".dxf"
</font><br>
<font face="Courier New,Courier" size=2>1060 REM
</font><br>
<font face="Courier New,Courier" size=2>1070 REM Ignore until section start encountered
</font><br>
<font face="Courier New,Courier" size=2>1080 REM
</font><br>
<font face="Courier New,Courier" size=2>1090 GOSUB 2000
</font><br>
<font face="Courier New,Courier" size=2>1100 IF G% <> 0 THEN 1090
</font><br>
<font face="Courier New,Courier" size=2>1110 IF S$ <> "SECTION" THEN 1090
</font><br>
<font face="Courier New,Courier" size=2>1120 GOSUB 2000
</font><br>
<font face="Courier New,Courier" size=2>1130 REM
</font><br>
<font face="Courier New,Courier" size=2>1140 REM Skip unless ENTITIES section
</font><br>
<font face="Courier New,Courier" size=2>1150 REM
</font><br>
<font face="Courier New,Courier" size=2>1160 IF S$ <> "ENTITIES" THEN 1090
</font><br>
<font face="Courier New,Courier" size=2>1170 REM
</font><br>
<font face="Courier New,Courier" size=2>1180 REM Scan until end of section, processing LINEs
</font><br>
<font face="Courier New,Courier" size=2>1190 REM
</font><br>
<font face="Courier New,Courier" size=2>1200 GOSUB 2000
</font><br>
<font face="Courier New,Courier" size=2>1210 IF G% = 0 AND S$ = "ENDSEC" THEN 2200
</font><br>
<font face="Courier New,Courier" size=2>1220 IF G% = 0 AND S$ = "LINE" THEN GOSUB 1400 : GOTO 1210
</font><br>
<font face="Courier New,Courier" size=2>1230 GOTO 1200
</font><br>
<font face="Courier New,Courier" size=2>1400 REM
</font><br>
<font face="Courier New,Courier" size=2>1410 REM Accumulate LINE entity groups
</font><br>
<font face="Courier New,Courier" size=2>1420 REM
</font><br>
<font face="Courier New,Courier" size=2>1430 GOSUB 2000
</font><br>
<font face="Courier New,Courier" size=2>1440 IF G% = 10 THEN X1 = X : Y1 = Y : Z1 = Z
</font><br>
<font face="Courier New,Courier" size=2>1450 IF G% = 11 THEN X2 = X : Y2 = Y : Z2 = Z
</font><br>
<font face="Courier New,Courier" size=2>1460 IF G% = 0 THEN PRINT "Line from
</font><br>
<font face="Courier New,Courier" size=2> (";X1;",";Y1;",";Z1;") to (";X2;",";Y2;",";Z2;")":RETURN
</font><br>
<font face="Courier New,Courier" size=2>1470 GOTO 1430
</font><br>
<font face="Courier New,Courier" size=2>2000 REM
</font><br>
<font face="Courier New,Courier" size=2>2010 REM Read group code and following value
</font><br>
<font face="Courier New,Courier" size=2>2020 REM For X coordinates, read Y and possibly Z also
</font><br>
<font face="Courier New,Courier" size=2>2030 REM
</font><br>
<font face="Courier New,Courier" size=2>2040 IF G1% < 0 THEN G% = -G1% : G1% = 0 ELSE INPUT #1, G%
</font><br>
<font face="Courier New,Courier" size=2>2050 IF G% < 10 OR G% = 999 THEN LINE INPUT #1, S$ : RETURN
</font><br>
<font face="Courier New,Courier" size=2>2060 IF G% >= 38 AND G% <= 49 THEN INPUT #1, V : RETURN
</font><br>
<font face="Courier New,Courier" size=2>2080 IF G% >= 50 AND G% <= 59 THEN INPUT #1, A : RETURN
</font><br>
<font face="Courier New,Courier" size=2>2090 IF G% >= 60 AND G% <= 69 THEN INPUT #1, P% : RETURN
</font><br>
<font face="Courier New,Courier" size=2>2100 IF G% >= 70 AND G% <= 79 THEN INPUT #1, F% : RETURN
</font><br>
<font face="Courier New,Courier" size=2>2110 IF G% >= 210 AND G% <= 219 THEN 2130
</font><br>
<font face="Courier New,Courier" size=2>2115 IF G% >= 1000 THEN LINE INPUT #1, T$ : RETURN
</font><br>
<font face="Courier New,Courier" size=2>2120 IF G% >= 20 THEN PRINT "Invalid group code";G% : STOP
</font><br>
<font face="Courier New,Courier" size=2>2130 INPUT #1, X
</font><br>
<font face="Courier New,Courier" size=2>2140 INPUT #1, G1%
</font><br>
<font face="Courier New,Courier" size=2>2150 IF G1% <> (G%+10) THEN PRINT "Invalid Y coord code";
</font><br>
<font face="Courier New,Courier" size=2> G1% : STOP
</font><br>
<font face="Courier New,Courier" size=2>2160 INPUT #1, Y
</font><br>
<font face="Courier New,Courier" size=2>2170 INPUT #1, G1%
</font><br>
<font face="Courier New,Courier" size=2>2180 IF G1% <> (G%+20) THEN G1% = -G1% ELSE INPUT #1, Z
</font><br>
<font face="Courier New,Courier" size=2>2190 RETURN
</font><br>
<p><font face="Courier New,Courier" size=2>2200 CLOSE 1
</font></p>
<p><font face="MS Sans Serif,Arial" size=2>Writing a program that constructs a DXF file poses different problems. You must maintain consistency within the drawing, although with AutoCAD you can omit many items in a DXF file and still obtain a usable drawing. The entire HEADER section can be omitted if you don't set header variables. Any of the tables in the TABLES section can be omitted if you don't need to make entries, and the entire TABLES section can be dropped if nothing in it is required. If you define any linetypes in the LTYPE table, this table must appear before the LAYER table. If no block definitions are used in the drawing, the BLOCKS section can be omitted. If present, the BLOCKS section must appear before the ENTITIES section. Within the ENTITIES section, you can reference layer names even though you haven't defined them in the LAYER table. Such layers are automatically created with color 7 and the CONTINUOUS linetype. The EOF item must be present at the end of file.
</font></p>
<p><font face="MS Sans Serif,Arial" size=2>The following Microsoft BASIC program constructs a DXF file representing a polygon with a specified number of sides, leftmost origin point, and side length. This program supplies only the ENTITIES section of the DXF file and places all entities generated on the default layer 0. Because this program doesn't create the drawing header, the drawing limits, extents, and current view will be invalid after performing a DXFIN on the drawing generated by this program. You can do a ZOOM Extents to fill the screen with the drawing generated. Then adjust the limits manually.
</font></p>
<font face="Courier New,Courier" size=2>1000 REM
</font><br>
<font face="Courier New,Courier" size=2>1010 REM Polygon generator
</font><br>
<font face="Courier New,Courier" size=2>1020 REM
</font><br>
<font face="Courier New,Courier" size=2>1030 LINE INPUT "Drawing (DXF) file name: "; A$
</font><br>
<font face="Courier New,Courier" size=2>1040 OPEN "o", 1, A$ + ".dxf"
</font><br>
<font face="Courier New,Courier" size=2>1050 PRINT #1, 0
</font><br>
<font face="Courier New,Courier" size=2>1060 PRINT #1, "SECTION"
</font><br>
<font face="Courier New,Courier" size=2>1070 PRINT #1, 2
</font><br>
<font face="Courier New,Courier" size=2>1080 PRINT #1, "ENTITIES"
</font><br>
<font face="Courier New,Courier" size=2>1090 PI = ATN(1) * 4
</font><br>
<font face="Courier New,Courier" size=2>1100 INPUT "Number of sides for polygon: "; S%
</font><br>
<font face="Courier New,Courier" size=2>1110 INPUT "Starting point (X,Y): "; X, Y
</font><br>
<font face="Courier New,Courier" size=2>1120 INPUT "Polygon side: "; D
</font><br>
<font face="Courier New,Courier" size=2>1130 A1 = (2 * PI) / S%
</font><br>
<font face="Courier New,Courier" size=2>1140 A = PI / 2
</font><br>
<font face="Courier New,Courier" size=2>1150 FOR I% = 1 TO S%
</font><br>
<font face="Courier New,Courier" size=2>1160 PRINT #1, 0
</font><br>
<font face="Courier New,Courier" size=2>1170 PRINT #1, "LINE"
</font><br>
<font face="Courier New,Courier" size=2>1180 PRINT #1, 8
</font><br>
<font face="Courier New,Courier" size=2>1190 PRINT #1, "0"
</font><br>
<font face="Courier New,Courier" size=2>1200 PRINT #1, 10
</font><br>
<font face="Courier New,Courier" size=2>1210 PRINT #1, X
</font><br>
<font face="Courier New,Courier" size=2>1220 PRINT #1, 20
</font><br>
<font face="Courier New,Courier" size=2>1230 PRINT #1, Y
</font><br>
<font face="Courier New,Courier" size=2>1240 PRINT #1, 30
</font><br>
<font face="Courier New,Courier" size=2>1250 PRINT #1, 0.0
</font><br>
<font face="Courier New,Courier" size=2>1260 NX = D * COS(A) + X
</font><br>
<font face="Courier New,Courier" size=2>1270 NY = D * SIN(A) + Y
</font><br>
<font face="Courier New,Courier" size=2>1280 PRINT #1, 11
</font><br>
<font face="Courier New,Courier" size=2>1290 PRINT #1, NX
</font><br>
<font face="Courier New,Courier" size=2>1300 PRINT #1, 21
</font><br>
<font face="Courier New,Courier" size=2>1310 PRINT #1, NY
</font><br>
<font face="Courier New,Courier" size=2>1320 PRINT #1, 31
</font><br>
<font face="Courier New,Courier" size=2>1330 PRINT #1, 0.0
</font><br>
<font face="Courier New,Courier" size=2>1340 X = NX
</font><br>
<font face="Courier New,Courier" size=2>1350 Y = NY
</font><br>
<font face="Courier New,Courier" size=2>1360 A = A + A1
</font><br>
<font face="Courier New,Courier" size=2>1370 NEXT I%
</font><br>
<font face="Courier New,Courier" size=2>1380 PRINT #1, 0
</font><br>
<font face="Courier New,Courier" size=2>1390 PRINT #1, "ENDSEC"
</font><br>
<font face="Courier New,Courier" size=2>1400 PRINT #1, 0
</font><br>
<font face="Courier New,Courier" size=2>1410 PRINT #1, "EOF"
</font><br>
<p><font face="Courier New,Courier" size=2>1420 CLOSE 1
</font></p>
<p><font face="MS Sans Serif,Arial" size=2>As long as a properly formatted item appears on the line on which the data is expected, DXFIN accepts it. (Of course, string items should not have leading spaces unless these are intended to be part of the string.) This program takes advantage of this flexibility in input format and does not generate a file exactly like one generated by AutoCAD.
</font></p>
<p><font face="MS Sans Serif,Arial" size=2>In the case of an error in using DXFIN TO load, AutoCAD reports the error with a message indicating the nature of the error and the last line processed in the <font face=Arial><i>dxf</i></font> file before the error was detected. This may not be the line on which the error occurred, especially in the case of errors such as the omission of required groups.
</font></p>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -