vbcad 发表于 2016-4-27 19:49:49


Dim L As Integer
Dim H(10000) As Double, X(10000) As Double, Y(10000) As Double, Z(10000) As Double
Dim Xmax As Double, Xmin As Double, Ymax As Double, Ymin As Double
Sub txt_read()
Dim txtname As String
Dim L As Integer
    Xmax = 0 '初始值
    Ymax=0
    Xmin=0
    Ymin=0       
    Open "E:\demdata.txt" For Input As #1'打开文件
    Do While Not EOF(1) '文件读取循环
    L = L + 1 '个数加1
    Input #1, H(L), X(L), Y(L), Z(L) '读取文件数据, H贮存点序号,XYZ为坐标
    if Xmax<X(L) then Xmax=X(L)'x找最大
    if Ymax<Y(L) then Ymax=Y(L)'y找最大
    if Xmin>X(L) then Xmin=X(L)'x找最小
    if Ymin>Y(L) then Ymin=Y(L)'y找最小
    Loop '文件读取循环
    Close #1 '关闭文件
debug.print "x最大:";Xmax
debug.print "y最大:";Ymax
End Sub

馨馨 发表于 2016-4-27 20:04:40

vbcad 发表于 2016-4-27 19:49 static/image/common/back.gif
Dim L As Integer
Dim H(10000) As Double, X(10000) As Double, Y(10000) As Double, Z(10000) As Doub ...

谢谢,我想用求出的最大最小值画一个格网,写的代码指画出一个框,不知道大神会不会,非常感谢Sub txt_read()
Dim txtname As String
Dim L As Integer

'读取文件
    L = 0 '初始值
    Open "E:\demdata.txt" For Input As #1'打开文件
    Xmax = X(0): Xmin = X(0)
    Ymax = Y(0): Ymin = Y(0)
    Do While Not EOF(1) '文件读取循环
    If X(L) >= Xmax Then
    Xmax = X(L)
    End If
    If Y(L) >= Ymax Then
    Ymax = Y(L)
    End If
    If X(L) <= Xmin Then
    Xmin = X(L)
    End If
    If Y(L) <= Ymin Then
    Ymin = Y(L)
    End If
    L = L + 1 '个数加1
    Input #1, H(L), X(L), Y(L), Z(L) '读取文件数据, H贮存点序号,XYZ为坐标
    Loop '文件读取循环
    Close #1 '关闭文件
   
'绘制格网
Dim plineObj As AcadLWPolyline
Dim points(0 To 11) As Double
Dim M As Integer'行数
Dim N As Integer'列数
Dim Dx As Double'每个网格x值
Dim Dy As Double'每个网格y值
' 定义多段线的点
    Dx = 14.5
    Dy = 15.6
    M = (Xmax - Xmin) / Dx '求出行数
    N = (Ymax - Ymin) / Dy '求出列数
    points(0) = Xmin: points(1) = Ymin: points(2) = 0
    points(3) = Xmin: points(4) = Ymax: points(5) = 0
    points(6) = Xmax: points(7) = Ymax: points(8) = 0
    points(9) = Xmax: points(10) = Ymin: points(11) = 0
    '在模型空间中创建一个多段线对象
    Set plineObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)
    ZoomAll
End Sub
页: 1 [2]
查看完整版本: VBA开发,刚学请多指教