text文件数据读取问题

zyzy577048814 2014-05-12 10:43:32
下面一组text文件,是显示了圆和方形的参数(方形是以四条直线的形式分别写出来的)。
第 1 个实体: 圆
图层: 0 (0层)
颜色: BYLAYER
线型: BYLAYER
视图: 未知视图
圆心: X=249.287 Y=144.405
半径: R=21.047
周长: L=132.241

第 2 个实体: 圆
图层: 0 (0层)
颜色: BYLAYER
线型: BYLAYER
视图: 未知视图
圆心: X=196.221 Y=46.844
半径: R=30.660
周长: L=192.642

第 3 个实体: 直线
图层: 0 (0层)
颜色: BYLAYER
线型: BYLAYER
视图: 未知视图
起点: X=315.349 Y=172.589
终点: X=315.349 Y=135.733
增量: dX=0.000dY=-36.856
长度: L=36.856

第 4 个实体: 直线
图层: 0 (0层)
颜色: BYLAYER
线型: BYLAYER
视图: 未知视图
起点: X=315.349 Y=135.733
终点: X=398.739 Y=135.733
增量: dX=83.390dY=0.000
长度: L=83.390

第 5 个实体: 直线
图层: 0 (0层)
颜色: BYLAYER
线型: BYLAYER
视图: 未知视图
起点: X=398.739 Y=135.733
终点: X=398.739 Y=172.589
增量: dX=0.000 dY=36.856
长度: L=36.856

第 6 个实体: 直线
图层: 0 (0层)
颜色: BYLAYER
线型: BYLAYER
视图: 未知视图
起点: X=398.739 Y=172.589
终点: X=315.349 Y=172.589
增量: dX=-83.390 dY=0.000
长度: L=83.390

第 7 个实体: 圆
图层: 0 (0层)
颜色: BYLAYER
线型: BYLAYER
视图: 未知视图
圆心: X=374.913 Y=280.990
半径: R=40.783
周长: L=256.244
想通过VB实现对这个文件的转换达到输出另一个文本,文本格式是如下,并且将按照X^2+y^2(xy为实体中心坐标)计算值由小到大的顺序(意思就是离原点越近的实体先存入)分别将所有实体坐标x、y存入a(i)、q(i)数组中,将方形宽长存入b()、L()数组中。
第 1 个实体: 圆
圆心: X=249.287 Y=144.405
半径: R=21.047

第 2 个实体: 圆
圆心: X=196.221 Y=46.844
半径: R=30.660


第 3 个实体: 方形
中心点X:=249.287 Y=144.405
宽度L=36.856
长度: L=83.390

第 7 个实体: 圆
圆心: X=374.913 Y=280.990
半径: R=40.783
知道一点原理但是还是不太会写,不知道有没有大神会,可以写简单一点,写几个数据。能全写出来更好了。
...全文
112 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
of123 2014-05-13
  • 打赏
  • 举报
回复
大致是这个样子:
Option Explicit

Private Sub Command1_Click()
Dim i As Long, point_index As Long, rectangle_index As Long, num_line As Integer, shape_num As Integer

Dim strLine As String, strType As String
Dim x As Double, y As Double, X1 As Double, X2 As Double, Y1 As Double, Y2 As Double
Dim a() As Double, q() As Double, b() As Double, L() As Double
Dim x_tmp() As Double, y_tmp() As Double

Open "c:\test\1.txt" For Input As #1
Open "c:\test\2.txt" For Output As #2

point_index = -1
rectangle_index = -1

Do Until EOF(1)
    Line Input #1, strLine
    If InStr(strLine, "实体: 圆") Then
        Print #2, strLine
        strType = "Circle"
        point_index = point_index + 1
    ElseIf InStr(strLine, "实体: 直线") Then
        strType = "Line"
        shape_num = Val(Mid(strLine, 2))
    End If
    
    If strType = "Circle" And InStr(strLine, "圆心") Then
        Print #2, strLine
        Call Get_Coordinate(strLine, x, y)
        ReDim Preserve x_tmp(point_index)
        x_tmp(point_index) = x
        ReDim Preserve y_tmp(point_index)
        y_tmp(point_index) = y
        
        List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
        List1.ItemData(List1.NewIndex) = point_index
    End If
    
    If strType = "Circle" And InStr(strLine, "半径") Then
        Print #2, strLine & vbCrLf
    End If
    
    If strType = "Line" And InStr(strLine, "起点") Then
        num_line = num_line + 1
        Call Get_Coordinate(strLine, x, y)
        If num_line = 1 Then
            point_index = point_index + 1
            ReDim Preserve x_tmp(point_index)
            x_tmp(point_index) = x
            ReDim Preserve y_tmp(point_index)
            y_tmp(point_index) = y
            List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
            List1.ItemData(List1.NewIndex) = point_index

            X1 = x
            Y1 = y
        ElseIf num_line = 2 Then
            point_index = point_index + 1
            ReDim Preserve x_tmp(point_index)
            x_tmp(point_index) = x
            ReDim Preserve y_tmp(point_index)
            y_tmp(point_index) = y
            List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
            List1.ItemData(List1.NewIndex) = point_index
        
            Y2 = y
        ElseIf num_line = 3 Then
            point_index = point_index + 1
            ReDim Preserve x_tmp(point_index)
            x_tmp(point_index) = x
            ReDim Preserve y_tmp(point_index)
            y_tmp(point_index) = y
            List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
            List1.ItemData(List1.NewIndex) = point_index
        
            X2 = x
        ElseIf num_line = 4 Then
            point_index = point_index + 1
            ReDim Preserve x_tmp(point_index)
            x_tmp(point_index) = x
            ReDim Preserve y_tmp(point_index)
            y_tmp(point_index) = y
            List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
            List1.ItemData(List1.NewIndex) = point_index
            
            rectangle_index = rectangle_index + 1
            ReDim Preserve b(rectangle_index)
            b(rectangle_index) = Abs(Y1 - Y2)
            ReDim Preserve L(rectangle_index)
            L(rectangle_index) = Abs(X1 - X2)
            
            Print #2, "第 " & shape_num - 3 & " 个实体:方形"
            Print #2, Space(4) & "宽度=" & Format(b(rectangle_index), "0.000")
            Print #2, Space(4) & "长度=" & Format(L(rectangle_index), "0.000") & vbCrLf
            
            num_line = 0
        End If
    End If
Loop

Close #2
Close #1

ReDim a(List1.ListCount - 1)
ReDim q(List1.ListCount - 1)
For i = 0 To List1.ListCount - 1
    a(i) = x_tmp(List1.ItemData(i))
    q(i) = y_tmp(List1.ItemData(i))
Next i

End Sub

Private Sub Get_Coordinate(ByVal strLine As String, ByRef x As Double, ByRef y As Double)
Dim p As Integer

    p = InStr(strLine, "X=")
    If p Then
        x = Val(Mid(strLine, p + 2))
        p = InStr(strLine, "Y=")
        If p Then y = Val(Mid(strLine, p + 2))
    Else
        Exit Sub
    End If
End Sub
方型的四条直线必须连续,不得与其他方型的直线交错。 四条直线的画线顺序,必须按例子中的顺序。否则要有更多的判断语句,你可以自己加。 此例中,方形的宽度专指 Y 方向高度,长度指 X 方向长度。 输出文件如下:
第 1 个实体: 圆
    圆心: X=249.287 Y=144.405
    半径: R=21.047

第 2 个实体: 圆
    圆心: X=196.221 Y=46.844
    半径: R=30.660

第 3 个实体:方型
    宽度=36.856
    长度=83.390

第 7 个实体: 圆
    圆心: X=374.913 Y=280.990
    半径: R=40.783
坐标点的排序:
 196.221       46.844 
 249.287       144.405 
 315.349       135.733 
 315.349       172.589 
 398.739       135.733 
 398.739       172.589 
 374.913       280.990
zyzy577048814 2014-05-13
  • 打赏
  • 举报
回复
有没有人做过这种数据提取的呢
zyzy577048814 2014-05-13
  • 打赏
  • 举报
回复
引用 6 楼 of123 的回复:
大致是这个样子:
Option Explicit

Private Sub Command1_Click()
Dim i As Long, point_index As Long, rectangle_index As Long, num_line As Integer, shape_num As Integer

Dim strLine As String, strType As String
Dim x As Double, y As Double, X1 As Double, X2 As Double, Y1 As Double, Y2 As Double
Dim a() As Double, q() As Double, b() As Double, L() As Double
Dim x_tmp() As Double, y_tmp() As Double

Open "c:\test\1.txt" For Input As #1
Open "c:\test\2.txt" For Output As #2

point_index = -1
rectangle_index = -1

Do Until EOF(1)
    Line Input #1, strLine
    If InStr(strLine, "实体: 圆") Then
        Print #2, strLine
        strType = "Circle"
        point_index = point_index + 1
    ElseIf InStr(strLine, "实体: 直线") Then
        strType = "Line"
        shape_num = Val(Mid(strLine, 2))
    End If
    
    If strType = "Circle" And InStr(strLine, "圆心") Then
        Print #2, strLine
        Call Get_Coordinate(strLine, x, y)
        ReDim Preserve x_tmp(point_index)
        x_tmp(point_index) = x
        ReDim Preserve y_tmp(point_index)
        y_tmp(point_index) = y
        
        List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
        List1.ItemData(List1.NewIndex) = point_index
    End If
    
    If strType = "Circle" And InStr(strLine, "半径") Then
        Print #2, strLine & vbCrLf
    End If
    
    If strType = "Line" And InStr(strLine, "起点") Then
        num_line = num_line + 1
        Call Get_Coordinate(strLine, x, y)
        If num_line = 1 Then
            point_index = point_index + 1
            ReDim Preserve x_tmp(point_index)
            x_tmp(point_index) = x
            ReDim Preserve y_tmp(point_index)
            y_tmp(point_index) = y
            List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
            List1.ItemData(List1.NewIndex) = point_index

            X1 = x
            Y1 = y
        ElseIf num_line = 2 Then
            point_index = point_index + 1
            ReDim Preserve x_tmp(point_index)
            x_tmp(point_index) = x
            ReDim Preserve y_tmp(point_index)
            y_tmp(point_index) = y
            List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
            List1.ItemData(List1.NewIndex) = point_index
        
            Y2 = y
        ElseIf num_line = 3 Then
            point_index = point_index + 1
            ReDim Preserve x_tmp(point_index)
            x_tmp(point_index) = x
            ReDim Preserve y_tmp(point_index)
            y_tmp(point_index) = y
            List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
            List1.ItemData(List1.NewIndex) = point_index
        
            X2 = x
        ElseIf num_line = 4 Then
            point_index = point_index + 1
            ReDim Preserve x_tmp(point_index)
            x_tmp(point_index) = x
            ReDim Preserve y_tmp(point_index)
            y_tmp(point_index) = y
            List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14)
            List1.ItemData(List1.NewIndex) = point_index
            
            rectangle_index = rectangle_index + 1
            ReDim Preserve b(rectangle_index)
            b(rectangle_index) = Abs(Y1 - Y2)
            ReDim Preserve L(rectangle_index)
            L(rectangle_index) = Abs(X1 - X2)
            
            Print #2, "第 " & shape_num - 3 & " 个实体:方形"
            Print #2, Space(4) & "宽度=" & Format(b(rectangle_index), "0.000")
            Print #2, Space(4) & "长度=" & Format(L(rectangle_index), "0.000") & vbCrLf
            
            num_line = 0
        End If
    End If
Loop

Close #2
Close #1

ReDim a(List1.ListCount - 1)
ReDim q(List1.ListCount - 1)
For i = 0 To List1.ListCount - 1
    a(i) = x_tmp(List1.ItemData(i))
    q(i) = y_tmp(List1.ItemData(i))
Next i

End Sub

Private Sub Get_Coordinate(ByVal strLine As String, ByRef x As Double, ByRef y As Double)
Dim p As Integer

    p = InStr(strLine, "X=")
    If p Then
        x = Val(Mid(strLine, p + 2))
        p = InStr(strLine, "Y=")
        If p Then y = Val(Mid(strLine, p + 2))
    Else
        Exit Sub
    End If
End Sub
方型的四条直线必须连续,不得与其他方型的直线交错。 四条直线的画线顺序,必须按例子中的顺序。否则要有更多的判断语句,你可以自己加。 此例中,方形的宽度专指 Y 方向高度,长度指 X 方向长度。 输出文件如下:
第 1 个实体: 圆
    圆心: X=249.287 Y=144.405
    半径: R=21.047

第 2 个实体: 圆
    圆心: X=196.221 Y=46.844
    半径: R=30.660

第 3 个实体:方型
    宽度=36.856
    长度=83.390

第 7 个实体: 圆
    圆心: X=374.913 Y=280.990
    半径: R=40.783
坐标点的排序:
 196.221       46.844 
 249.287       144.405 
 315.349       135.733 
 315.349       172.589 
 398.739       135.733 
 398.739       172.589 
 374.913       280.990
只能说你太厉害了,不愧是大神。佩服佩服。我就找你这种方法在改善改善。分全给你了。系统最高只能给100本来想给200的。不好意思。 提取的坐标如果方形只要第一个起点,那后面几个 List1.AddItem Right("0000000000" & Format((x ^ 2 + y ^ 2), "0.000"), 14) List1.ItemData(List1.NewIndex) = point_index删了就可以了吧。
of123 2014-05-12
  • 打赏
  • 举报
回复
方形的判断麻烦一些,不能在读取数据时及时处理,需要缓存后处理。
zyzy577048814 2014-05-12
  • 打赏
  • 举报
回复
引用 3 楼 of123 的回复:
这就是设计上的缺陷。如果输出文本文件的时候,就已经知道是方型,大可以输出: 第 3 个实体: 方型 图层: 0 (0层) 颜色: BYLAYER 线型: BYLAYER 视图: 未知视图 顶点1: X=315.349 Y=172.589 顶点2: X=315.349 Y=135.733 顶点3: X=398.739 Y=135.733 顶点4: X=398.739 Y=172.589 一点不耽误画直线。 现在用零散的组件,要判断出组装的总成,很麻烦。当然,你事先知道这就是方形,没有别的可能,想起来很简单。作为一般性的处理,你知道要排除多少种可能吗?要是菱形呢?要梯形呢?要是五边形呢?要是各自分离呢?……
我做的事专用机床,只加工圆和方形,请问能写出来吗?希望帮助我一下啊。
of123 2014-05-12
  • 打赏
  • 举报
回复
这就是设计上的缺陷。如果输出文本文件的时候,就已经知道是方型,大可以输出: 第 3 个实体: 方型 图层: 0 (0层) 颜色: BYLAYER 线型: BYLAYER 视图: 未知视图 顶点1: X=315.349 Y=172.589 顶点2: X=315.349 Y=135.733 顶点3: X=398.739 Y=135.733 顶点4: X=398.739 Y=172.589 一点不耽误画直线。 现在用零散的组件,要判断出组装的总成,很麻烦。当然,你事先知道这就是方形,没有别的可能,想起来很简单。作为一般性的处理,你知道要排除多少种可能吗?要是菱形呢?要梯形呢?要是五边形呢?要是各自分离呢?……
zyzy577048814 2014-05-12
  • 打赏
  • 举报
回复
引用 1 楼 of123 的回复:
方形的判断麻烦一些,不能在读取数据时及时处理,需要缓存后处理。
判断不难吧,只要判断第一行有没有“直线”字符串,有的话就知道下面四个实体是方形四条边。能不能帮忙写一点,VB学的不好啊,就差这一环了。

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧