明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1190|回复: 1

[原创]Line的startpoint(0)和(1)的不重复排序

[复制链接]
发表于 2008-5-18 20:30:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2008-5-21 23:43:28 编辑

在做材料表时将其表格线按不重复排序进行处理。
  1. Function RemoveOverlap(ByRef Ary)
  2.                
  3.             On Error Resume Next
  4.                
  5.             Dim i     As Long
  6.                
  7.             Dim colTmp     As New Collection
  8.             For i = 0 To UBound(Ary) - 1
  9.                     colTmp.Add Ary(i), "K" & Ary(i)
  10.             Next
  11.                
  12.             Dim aryTmp()     As String
  13.             ReDim aryTmp(colTmp.Count - 1) As String
  14.             For i = 0 To colTmp.Count - 1
  15.                     aryTmp(i) = colTmp.Item(i + 1)
  16.             Next
  17.                
  18.             Set colTmp = Nothing
  19.             RemoveOverlap = aryTmp
  20.                
  21.     End Function
  22. '主程序
  23. Sub ll()
  24.      Dim xm(1000) As Double, xm1(1000) As Double, TextArray(10000) As String, TextInsertPoint(10000, 2)
  25.      Dim tt As AcadText, ll As AcadLine, Ent As AcadEntity
  26.      xm_i = 0: xm1_i = 0: tt_i = 0
  27.      ''
  28.      Dim x1 As Double, y1 As Double
  29.      'ReDim xm(1000) As Double, xm1(1000) As Long
  30.      For Each Ent In ThisDrawing.ModelSpace
  31.        Select Case Ent.ObjectName
  32.          Case "AcDbLine"
  33.            Set ll = Ent
  34.            Select Case ll.Layer
  35.              Case "零件表格竖线"
  36.                'ReDim xm(xm_i) As Double
  37.                xm(xm_i) = Round(ll.EndPoint(0), 0)
  38.                'Debug.Print xm_i, xm(xm_i), Round(ll.EndPoint(0), 3)
  39.                xm_i = xm_i + 1
  40.              Case "零件表格横线"
  41.                'ReDim xm1(xm1_i) As Double
  42.                xm1(xm1_i) = Round(ll.EndPoint(1), 0)
  43.                xm1_i = xm1_i + 1
  44.            End Select
  45.          Case "AcDbText"
  46.            Set tt = Ent
  47.              If tt.Layer = "零件表格文本" Then
  48.                TextInsertPoint(tt_i, 0) = tt.insertionPoint(0)
  49.                TextInsertPoint(tt_i, 1) = tt.insertionPoint(1)
  50.                TextArray(tt_i) = tt.textString
  51.                tt_i = tt_i + 1
  52.              End If
  53.         End Select
  54.      Next Ent
  55.      
  56.      MM = RemoveOverlap(xm1)
  57.      xx = Bubble_Sort(MM)
  58.      
  59.      MM = RemoveOverlap(xm)
  60.      yy = Bubble_Sort(MM)
  61.      Dim gg
  62.      ReDim gg(UBound(xx) - 2, UBound(yy) - 2)
  63.      
  64. For kk = 0 To tt_i - 1
  65.     x1 = TextInsertPoint(kk, 1)
  66.     For ii = 1 To UBound(xx) - 1
  67.       If x1 > xx(ii) And x1 < xx(ii + 1) Then
  68.        Exit For
  69.       End If
  70.     Next ii
  71. y1 = Val(TextInsertPoint(kk, 0))
  72. For jj = 1 To UBound(yy) - 1
  73.       If y1 > yy(jj) And y1 < yy(jj + 1) Then
  74.        Exit For
  75.       End If
  76.     Next jj
  77.     gg(ii - 1, jj - 1) = TextArray(kk)
  78. Next kk
  79. Dim insertionPoint(0 To 2) As Double, alignmentPoint(0 To 2) As Double
  80.     'alignmentPoint(0) = 5: alignmentPoint(1) = 3: alignmentPoint(2) = 0
  81. 'Dim tt As AcadText
  82.     For ii = 1 To UBound(xx) - 1
  83.       insertionPoint(1) = xx(ii) + 15
  84.       insertionPoint(2) = 0
  85.       For jj = 1 To UBound(yy) - 1
  86.          insertionPoint(0) = yy(jj) + (yy(jj + 1) - yy(jj)) / 2
  87.          
  88.          alignmentPoint(0) = insertionPoint(0): alignmentPoint(1) = insertionPoint(1): alignmentPoint(2) = 0
  89. 'Set pointObj = ThisDrawing.ModelSpace.AddPoint(alignmentPoint)
  90.          'Debug.Print yy(jj), pp(0)
  91.          'Debug.Print gg(ii-1, jj-1)
  92.          Set tt = ThisDrawing.ModelSpace.AddText(Trim(gg(ii - 1, jj - 1)), insertionPoint, 18)
  93.          With tt
  94.            .StyleName = "WMF-宋体0"
  95.            .HorizontalAlignment = acHorizontalAlignmentCenter
  96.            .TextAlignmentPoint = alignmentPoint
  97.            '.Alignment = acAlignmentCenter
  98.            
  99.          End With
  100.       Next jj
  101.     Next ii
  102. End Sub
  103. '冒泡程序
  104. Function Bubble_Sort(Ary)
  105.      Dim aryUBound, i, j
  106.      aryUBound = UBound(Ary)
  107.      For ii = 0 To aryUBound
  108.        Ary(ii) = Val(Round(Ary(ii), 2))
  109.      Next ii
  110.      For i = 0 To aryUBound
  111.        For j = i + 1 To aryUBound
  112.          If Ary(i) > Ary(j) Then
  113.            Swap Ary(i), Ary(j)
  114.          End If
  115.        Next
  116.      Next
  117.      Bubble_Sort = Ary
  118. End Function
  119. Function Swap(a, b)
  120.      Dim tmp
  121.      tmp = a
  122.      a = b
  123.      b = tmp
  124. End Function
 楼主| 发表于 2008-5-18 22:55:00 | 显示全部楼层
本帖最后由 作者 于 2008-5-21 22:36:01 编辑

另一种方法
  1. Function RemoveOverlap(ByRef Ary)
  2.               
  3.            On Error Resume Next
  4.               
  5.            Dim i     As Long
  6.               
  7.            Dim colTmp     As New Collection
  8.            For i = 0 To UBound(Ary) - 1
  9.                    colTmp.Add Ary(i), "K" & Ary(i)
  10.            Next
  11.               
  12.            Dim aryTmp()     As String
  13.            ReDim aryTmp(colTmp.Count - 1) As String
  14.            For i = 0 To colTmp.Count - 1
  15.                    aryTmp(i) = colTmp.Item(i + 1)
  16.            Next
  17.               
  18.            Set colTmp = Nothing
  19.            RemoveOverlap = aryTmp
  20.               
  21.    End Function
  22. '主程序
  23. Sub ll()
  24.     Dim xm(1000) As Double, xm1(1000) As Double, TextArray(10000) As String, TextInsertPoint(10000, 2)
  25.     Dim tt As AcadText, ll As AcadLine, Ent As AcadEntity
  26.     xm_i = 0: xm1_i = 0: tt_i = 0
  27.     ''
  28.     Dim x1 As Double, y1 As Double
  29.     'ReDim xm(1000) As Double, xm1(1000) As Long
  30.     For Each Ent In ThisDrawing.ModelSpace
  31.       Select Case Ent.ObjectName
  32.         Case "AcDbLine"
  33.           Set ll = Ent
  34.           Select Case ll.Layer
  35.             Case "零件表格竖线"
  36.               'ReDim xm(xm_i) As Double
  37.               xm(xm_i) = Round(ll.EndPoint(0), 0)
  38.               'Debug.Print xm_i, xm(xm_i), Round(ll.EndPoint(0), 3)
  39.               xm_i = xm_i + 1
  40.             Case "零件表格横线"
  41.               'ReDim xm1(xm1_i) As Double
  42.               xm1(xm1_i) = Round(ll.EndPoint(1), 0)
  43.               xm1_i = xm1_i + 1
  44.           End Select
  45.         Case "AcDbText"
  46.           Set tt = Ent
  47.             If tt.Layer = "零件表格文本" Then
  48.               TextInsertPoint(tt_i, 0) = tt.InsertionPoint(0)
  49.               TextInsertPoint(tt_i, 1) = tt.InsertionPoint(1)
  50.               TextArray(tt_i) = tt.TextString
  51.               tt_i = tt_i + 1
  52.             End If
  53.        End Select
  54.     Next Ent
  55.    
  56.     MM = RemoveOverlap(xm1)
  57.     xx = Bubble_Sort(MM)
  58.    
  59.     MM = RemoveOverlap(xm)
  60.     yy = Bubble_Sort(MM)
  61.     Dim gg
  62.     ReDim gg(UBound(xx) - 2, UBound(yy) - 2)
  63.    
  64. For kk = 0 To tt_i - 1
  65.    x1 = TextInsertPoint(kk, 1)
  66.    For ii = 1 To UBound(xx) - 1
  67.      If x1 > xx(ii) And x1 < xx(ii + 1) Then
  68.       Exit For
  69.      End If
  70.    Next ii
  71. y1 = Val(TextInsertPoint(kk, 0))
  72. For jj = 1 To UBound(yy) - 1
  73.      If y1 > yy(jj) And y1 < yy(jj + 1) Then
  74.       Exit For
  75.      End If
  76.    Next jj
  77.    gg(ii - 1, jj - 1) = TextArray(kk)
  78. Next kk
  79. Dim pp(0 To 2) As Double
  80. 'Dim tt As AcadText
  81.    For ii = 1 To UBound(xx) - 1
  82.      pp(1) = xx(ii) + 10
  83.      For jj = 1 To UBound(yy) - 1
  84.         pp(0) = yy(jj) + (yy(jj + 1) - yy(jj)) / 2
  85.         Debug.Print yy(jj), pp(0)
  86.         'Debug.Print gg(ii-1, jj-1)
  87.         Set tt = ThisDrawing.ModelSpace.AddText(Trim(gg(ii - 1, jj - 1)), pp, 18)
  88.      Next jj
  89.    Next ii
  90. End Sub
  91. '冒泡程序
  92. Function Bubble_Sort(Ary)
  93.     Dim aryUBound, i, j
  94.     aryUBound = UBound(Ary)
  95.     For ii = 0 To aryUBound
  96.       Ary(ii) = Val(Round(Ary(ii), 2))
  97.     Next ii
  98.     For i = 0 To aryUBound
  99.       For j = i + 1 To aryUBound
  100.         If Ary(i) > Ary(j) Then
  101.           Swap Ary(i), Ary(j)
  102.         End If
  103.       Next
  104.     Next
  105.     Bubble_Sort = Ary
  106. End Function
  107. Function Swap(a, b)
  108.     Dim tmp
  109.     tmp = a
  110.     a = b
  111.     b = tmp
  112. End Function

您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-26 09:52 , Processed in 0.166842 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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