- 积分
- 31855
- 明经币
- 个
- 注册时间
- 2016-9-16
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
本帖最后由 fangmin723 于 2018-10-23 07:51 编辑
最近在接触cad VBA的坐标排序,在明经里面也找到不少,但是效果不是很满意,自己就在前人的基础上重写改良了一下!
网上摘录的 十大经典排序算法(动图演示)在14楼,点击蓝色文字即达!
下面这个是我自己代码演示效果:
这是在本论坛里面找到的,一次性框选没有问题,但是一个一个点选的时候就出现了问题,演示效果如下:
zzyong00版主的帖子:http://bbs.mjtd.com/forum.php?mo ... 364&fromuid=7316343
代码如下:
 - Sub DealText()
- Dim objSset As Object, objtrans As Object, objEntArr As New Collection, i As Integer, j As Integer
- Set zwdoc = AcadApplication.ActiveDocument
- SelectAllText zwdoc, objSset
- For i = 0 To objSset.Count - 1
- objEntArr.Add objSset.Item(i)
- Next i
- If objEntArr.Count > 0 Then
- SortYup objEntArr
- Dim pt As Variant, colObjArr() As New Collection, k As Long, objTmp As Object, ro%, n%
- ro = 0
- ReDim colObjArr(ro)
- Set pt = objEntArr(1)
- basept1 = pt.InsertionPoint
- For i = 1 To objEntArr.Count
- basept2 = objEntArr(i).InsertionPoint
- If Abs(basept1(1) - basept2(1)) < (pt.Height / 2#) Then
- colObjArr(ro).Add objEntArr(i)
- Else
- ro = ro + 1
- ReDim Preserve colObjArr(ro)
- Set pt = objEntArr(i)
- basept1 = pt.InsertionPoint
- colObjArr(ro).Add objEntArr(i)
- End If
- Next i
- n = 1
- For i = 0 To UBound(colObjArr)
- SortXup colObjArr(i)
- For j = 1 To colObjArr(i).Count
- 'MsgBox colObjArr(i).Item(j).TextString
- colObjArr(i).Item(j).TextString = colObjArr(i).Item(j).TextString & CStr(n)
- n = n + 1
- Next j
- Next i
- End If
- End Sub
- Private Sub SelectAllText(ByVal App As Object, objSset As Object, Optional ByVal strSsetname As String = "SELECTION~TEXT~1111")
- On Error GoTo err1
- Dim flag As Boolean
- flag = False
- For Each objSset In App.SelectionSets
- If objSset.Name = strSsetname Then
- flag = True
- Exit For
- End If
- Next
- If flag Then objSset.Delete '创建集合,如集存在,则删除,再新建
- Set objSset = App.SelectionSets.Add(strSsetname)
- Dim gpCode(0) As Integer
- Dim dataValue(0) As Variant
- gpCode(0) = 0
- dataValue(0) = "text,mtext"
- Dim groupCode As Variant, dataCode As Variant
- groupCode = gpCode
- dataCode = dataValue
- objSset.SelectOnScreen groupCode, dataCode
- Exit Sub
- err1:
- Debug.Print Err.Description
- Err.Clear
- End Sub
-------------------------------------------------------------------------------------------------------------------
文字坐标排序方法:(含冒牌排序和归并排序)
经本人测试,冒牌排序和归并排序的速度天壤之别,用2940个文字进行排序:
冒牌排序耗时:2分20多秒,
归并排序耗时:1秒!
按xy轴升降排序---单轴单序
 - '按X排序,从左到右排序
- Private Function SortXup(ByRef objEntArr As Collection)
- Dim objtrans As Object
- For i = objEntArr.Count - 1 To 1 Step -1
- For j = 1 To i
- basept1 = objEntArr(j).InsertionPoint
- basept2 = objEntArr(j + 1).InsertionPoint
- If basept1(0) > basept2(0) Then
- Set objtrans = objEntArr(j)
- objEntArr.Remove j
- objEntArr.Add objtrans, , , j
- End If
- Next j
- Next i
- End Function
- '按X排序,从右到左排序
- Private Function SortXdown(ByRef objEntArr As Collection)
- Dim objtrans As Object
- For i = objEntArr.Count - 1 To 1 Step -1
- For j = 1 To i
- basept1 = objEntArr(j).InsertionPoint
- basept2 = objEntArr(j + 1).InsertionPoint
- If basept1(0) < basept2(0) Then
- Set objtrans = objEntArr(j)
- objEntArr.Remove j
- objEntArr.Add objtrans, , , j
- End If
- Next j
- Next i
- End Function
- '按Y排序,从下到上排序
- Private Function SortYup(ByRef objEntArr As Collection)
- Dim objtrans As Object
- For i = objEntArr.Count - 1 To 1 Step -1
- For j = 1 To i
- basept1 = objEntArr(j).InsertionPoint
- basept2 = objEntArr(j + 1).InsertionPoint
- If basept1(1) > basept2(1) Then
- Set objtrans = objEntArr(j)
- objEntArr.Remove j
- objEntArr.Add objtrans, , , j
- End If
- Next j
- Next i
- End Function
- '按Y排序,从上到下排序
- Private Function SortYdown(ByRef objEntArr As Collection)
- Dim objtrans As Object
- For i = objEntArr.Count - 1 To 1 Step -1
- For j = 1 To i
- basept1 = objEntArr(j).InsertionPoint
- basept2 = objEntArr(j + 1).InsertionPoint
- If basept1(1) < basept2(1) Then
- Set objtrans = objEntArr(j)
- objEntArr.Remove j
- objEntArr.Add objtrans, , , j
- End If
- Next j
- Next i
- End Function
vba 6合一排序方法:xyz升降序排序!
 - '按文字位置坐标排序:
- 'objEntArr-文字集合;
- 'axis-对应轴的索引,默认0->X,1->Y,2->Z;
- 'updown-升序还是降序排列,默认升序True,降序False
- Private Function PointSort(ByRef objEntArr As Collection, Optional ByRef axis As Integer = 0, Optional ByRef updown As Boolean = True)
- Dim objtrans As Object, result As Boolean
- For i = objEntArr.Count - 1 To 1 Step -1
- For j = 1 To i
- basept1 = objEntArr(j).InsertionPoint
- basept2 = objEntArr(j + 1).InsertionPoint
- If updown Then
- result = basept1(axis) > basept2(axis)
- Else
- result = basept1(axis) < basept2(axis)
- End If
- If result Then
- Set objtrans = objEntArr(j)
- objEntArr.Remove j
- objEntArr.Add objtrans, , , j
- End If
- Next j
- Next i
- End Function
归并排序:
 - '按文字位置坐标排序:
- 'Arr-文字集合;
- 'axis-对应轴的索引,默认0->X,1->Y,2->Z;
- 'updown-升序还是降序排列,默认升序True,降序False
- Function PointMergeSort(ByRef arr As Collection, Optional ByRef axis As Integer = 0, Optional ByRef updown As Boolean = True)
- MergeSort arr, 1, arr.COUNT, axis, updown
- End Function
- Function MergeSort(ByRef arr As Collection, ByRef ArrMin As Integer, ByRef ArrMax As Integer, ByRef axis As Integer, ByRef updown As Boolean)
- If ArrMax > ArrMin Then
- Dim ArrMid As Integer
- ArrMid = Int((ArrMin + ArrMax) / 2)
- MergeSort arr, ArrMin, ArrMid, axis, updown
- MergeSort arr, ArrMid + 1, ArrMax, axis, updown
- Merge arr, ArrMin, ArrMid, ArrMax, axis, updown
- End If
- End Function
- Function Merge(ByRef arr As Collection, ByRef ArrMin As Integer, ByRef ArrMid As Integer, ByRef ArrMax As Integer, axis As Integer, updown As Boolean)
- Dim Temp() As Object, result As Boolean
- ArrLen = ArrMax - ArrMin + 1
- i = ArrMin
- j = ArrMid + 1
- k = ArrMin
- ReDim Temp(ArrMin To ArrMax)
- While i <= ArrMid And j <= ArrMax
- basept1 = arr.Item(i).InsertionPoint
- basept2 = arr.Item(j).InsertionPoint
- If updown Then
- result = basept1(axis) < basept2(axis)
- Else
- result = basept1(axis) > basept2(axis)
- End If
- If result Then
- Set Temp(k) = arr.Item(i)
- k = k + 1
- i = i + 1
- Else
- Set Temp(k) = arr.Item(j)
- k = k + 1
- j = j + 1
- End If
- Wend
- While i <= ArrMid
- Set Temp(k) = arr.Item(i)
- k = k + 1
- i = i + 1
- Wend
- While j <= ArrMax
- Set Temp(k) = arr.Item(j)
- k = k + 1
- j = j + 1
- Wend
- For i = ArrMin To ArrMax
- arr.Add Temp(i), , , i
- arr.Remove (i)
- Next
- End Function
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
评分
-
查看全部评分
|