- 积分
- 853
- 明经币
- 个
- 注册时间
- 2012-6-28
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|
发表于 2016-10-25 19:20:36
|
显示全部楼层
给你个解决办法:
Sub 多段线按面积按大小输出()
Dim SSet As AcadSelectionSet
Dim ftype(0 To 1) As Integer
Dim fdata(0 To 1) As Variant
ftype(0) = 0: fdata(0) = "LWPolyline" '定义过滤器筛选类别,筛选 多段线
ftype(1) = 8: fdata(1) = "*" '定义过滤器筛选图层,筛选 parts图层
On Error Resume Next
If Not IsNull(ThisDrawing.SelectionSets.Item("SSetParts")) Then Set SSet = ThisDrawing.SelectionSets.Item("SSetParts"): SSet.Delete
On Error GoTo 0
Set SSet = ThisDrawing.SelectionSets.Add("SSetParts")
SSet.Select acSelectionSetAll, , , ftype, fdata
'选择集转换为对象数组:返回包含于选择集中每一项目的变体数组
Dim i As Long
Dim retVal() As AcadEntity
ReDim retVal(0 To SSet.Count - 1)
For i = 0 To SSet.Count - 1
Set retVal(i) = SSet.Item(i)
Debug.Print SSet.Item(i).area '逐个显示排序前的面积
Next
'冒泡排序:按照面积从小到大的顺序
Dim iOuter As Long
Dim iInner As Long
Dim iLBound As Long
Dim iUBound As Long
Dim iTemp As AcadEntity
iLBound = LBound(retVal): iUBound = UBound(retVal)
'冒泡排序
For iOuter = iLBound To iUBound - 1
For iInner = iLBound To iUBound - iOuter - 1
If retVal(iInner).area > retVal(iInner + 1).area Then '比较相邻项
Set iTemp = retVal(iInner)
Set retVal(iInner) = retVal(iInner + 1) '交换
Set retVal(iInner + 1) = iTemp
End If
Next iInner
Next iOuter
Debug.Print "面积从小到大排序结果: "
Dim ipart2 As AcadEntity
For i = 0 To UBound(retVal)
Set ipart2 = retVal(i)
Debug.Print ipart2.area
Next
End Sub
|
|