明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
123
返回列表 发新帖

一个选择集的增强类,刚写好,大家提提意见

  [复制链接]
 楼主| 发表于 2006-3-18 15:21:00 | 显示全部楼层
本帖最后由 作者 于 2006-3-19 13:28:58 编辑

2006-3-18改进版
1、将过滤器单独作为一个类
2、去掉SetFilterType,SetFilerData两个多余的方法
3、增加AppendData方法
具体看:
http://www1.139.com/xsfhlzh/1025501/Article/325507.html
http://www1.139.com/xsfhlzh/1025501/Article/332841.html
测试例程(选择线型为ACAD_ISO04W100或ACAD_ISO10W100的实体)
  1. Sub test()
  2.     Dim ss As New TlsSel
  3.     Dim i As AcadLayer
  4.     zxxNames = "ACAD_ISO04W100,ACAD_ISO10W100"
  5.     ss.Init
  6.     ss.Filter.SetData -4, "<or", 6, zxxNames
  7.     For Each i In ThisDrawing.Layers
  8.         If InStr(zxxNames, i.LineType) <> 0 Then
  9.             ss.Filter.AppendData -4, "<and", 8, i.Name, 6, "bylayer", -4, "and>"
  10.         End If
  11.     Next
  12.     ss.Filter.AppendData -4, "or>"
  13.     ss.Selectobject acSelectionSetAll
  14.     MsgBox ss.Count
  15. End Sub
发表于 2006-3-19 10:51:00 | 显示全部楼层

好东西

方便多了

 楼主| 发表于 2009-4-3 22:46:00 | 显示全部楼层
本帖最后由 作者 于 2009-4-4 15:18:45 编辑

又把老帖子翻出来了,很早以前写过的,陆陆续续改过n回,哈
把最新版本还是贴上来吧
  1. 'ClassName:TlsResultBuffer
  2. 'Writer:xsfhlzh
  3. '用于选择集过滤器、扩展数据及字典
  4. Private m_TypeCodes, m_Datas
  5. Private m_Count As Integer
  6. Public Sub SetData(ParamArray Values())
  7. On Error Resume Next
  8.    
  9.      Dim i
  10.      Dim n As Integer
  11.      Dim nCount As Integer
  12.      nCount = (UBound(Values) + 1) / 2 - 1
  13.    
  14.      If nCount = 0 And IsArray(Values(0)) And IsArray(Values(1)) Then
  15.    
  16.         m_TypeCodes = Values(0)
  17.         m_Datas = Values(1)
  18.    
  19.      Else
  20.    
  21.         Dim t() As Integer, d()
  22.         ReDim t(nCount), d(nCount)
  23.         For i = 0 To nCount
  24.             n = i * 2
  25.             t(i) = Values(n)
  26.             d(i) = Values(n + 1)
  27.         Next i
  28.    
  29.         m_TypeCodes = t
  30.         m_Datas = d
  31.    
  32.     End If
  33.     m_Count = UBound(m_TypeCodes) + 1
  34. End Sub
  35. Public Sub AppendData(ParamArray Values())
  36. On Error Resume Next
  37.     Dim m As Integer, n As Integer, nCount As Integer
  38.     nCount = (UBound(Values) + 1) / 2
  39.     n = m_Count + nCount - 1
  40.     ReDim Preserve m_TypeCodes(n), m_Datas(n)
  41.     For i = 0 To nCount - 1
  42.         m = m_Count + i
  43.         n = i * 2
  44.         m_TypeCodes(m) = Values(n)
  45.         m_Datas(m) = Values(n + 1)
  46.     Next i
  47.     m_Count = m_Count + nCount
  48.    
  49. End Sub
  50. Public Sub GetData(ByRef TypeCodes, ByRef Datas)
  51.     TypeCodes = m_TypeCodes
  52.     Datas = m_Datas
  53. End Sub
  54. Public Sub Clear()
  55.     m_TypeCodes = Null
  56.     m_Datas = Null
  57.    
  58. End Sub
  59. Public Property Get TypeCodes() As Variant
  60.     TypeCodes = m_TypeCodes
  61. End Property
  62. Public Property Let TypeCodes(ByVal vNewValue As Variant)
  63.     m_TypeCodes = vNewValue
  64. End Property
  65. Public Property Get Datas() As Variant
  66.     Datas = m_Datas
  67.    
  68. End Property
  69. Public Property Let Datas(ByVal vNewValue As Variant)
  70.     m_Datas = vNewValue
  71.    
  72. End Property
  73. Public Property Get Count() As Integer
  74. On Error Resume Next
  75.     Count = m_Count
  76.    
  77. End Property
  78. Public Property Get IsNull() As Boolean
  79.     IsNull = (Count = 0)
  80.    
  81. End Property
  1. 'ClassName:TlsSelectionSet
  2. 'Writer:xsfhlzh
  3. '增强选择集
  4. '暂不支持当前选择集
  5. Private oSel As AcadSelectionSet
  6. Private mFilter As New TlsResultBuffer
  7. Private sName As String
  8. Private bDeleted As Boolean
  9. Private Function IsNull() As Boolean
  10.     If oSel Is Nothing Then
  11.         IsNull = True
  12.     ElseIf oSel.Count = 0 Then
  13.         IsNull = True
  14.     Else
  15.         IsNull = False
  16.     End If
  17.    
  18. End Function
  19. Public Sub Init(Optional ByVal Name As String = "TlsSelectionSet", Optional ClearFilter As Boolean = True, Optional Deleted As Boolean = True)
  20. '创建选择集
  21. On Error Resume Next
  22.    
  23.     If ClearFilter Then mFilter.Clear
  24.     bDeleted = Deleted
  25.     If Not oSel Is Nothing Then oSel.Delete
  26.     sName = Name
  27.     ThisDrawing.SelectionSets(sName).Delete
  28.     Set oSel = ThisDrawing.SelectionSets.Add(sName)
  29.    
  30. End Sub
  31. Private Sub Class_Terminate()
  32. '类析构时清除选择集
  33. On Error Resume Next
  34.    
  35.     If bDeleted Then oSel.Delete
  36.    
  37. End Sub
  38. Public Function ToArray()
  39. '转化选择集为对象数组输出
  40. On Error Resume Next
  41.   
  42.     Dim i
  43.     Dim objs() As AcadEntity
  44.     Dim nCount As Integer
  45.    
  46.     nCount = oSel.Count - 1
  47.     ReDim objs(nCount)
  48.    
  49.     For i = 0 To nCount
  50.         Set objs(i) = oSel(i)
  51.     Next i
  52.    
  53.     ToArray = objs
  54.    
  55. End Function
  56. Public Property Get Count() As Integer
  57. '获取选择集实体个数
  58. On Error Resume Next
  59.     Count = oSel.Count
  60.    
  61. End Property
  62. Public Property Get Name() As String
  63. '获取选择集名称
  64. On Error Resume Next
  65.     Name = sName
  66.    
  67. End Property
  68. Public Property Get Item(ByVal Index) As AcadEntity
  69. '获取选择集实体
  70. On Error Resume Next
  71.     Set Item = oSel(Index)
  72.    
  73. End Property
  74. Public Property Get Deleted() As Boolean
  75.     Deleted = bDeleted
  76. End Property
  77. Public Property Let Deleted(ByVal Value As Boolean)
  78.     bDeleted = Value
  79. End Property
  80. Public Property Get AcSet() As Variant
  81. '获取选择集
  82. On Error Resume Next
  83.    
  84.     Set AcSet = oSel
  85.    
  86. End Property
  87. Public Property Set AcSet(Value As Variant)
  88. '获取选择集
  89. On Error Resume Next
  90.    
  91.     Set oSel = Value
  92.     sName = oSel.Name
  93.    
  94. End Property
  95. Public Sub AddItems(ByVal objs)
  96. '向选择集加入实体
  97. On Error Resume Next
  98.    
  99.     If IsArray(objs) Then
  100.         oSel.AddItems objs
  101.     ElseIf IsObject(objs) Then
  102.         If TypeOf objs Is AcadSelectionSet Then
  103.             Dim temp As New TlsSelectionSet
  104.             temp.Deleted = False
  105.             temp.AcSet = objs
  106.             oSel.AddItems temp.ToArray
  107.         ElseIf TypeOf objs Is TlsSelectionSet Then
  108.             oSel.AddItems objs.ToArray
  109.         Else
  110.             Dim ents(0) As AcadEntity
  111.             Set ents(0) = objs
  112.             oSel.AddItems ents
  113.         End If
  114.     End If
  115.    
  116. End Sub
  117. Public Sub RemoveItems(ByVal objs)
  118. '在选择集中移除实体
  119. On Error Resume Next
  120.    
  121.     If IsArray(objs) Then
  122.         oSel.RemoveItems objs
  123.     ElseIf IsObject(objs) Then
  124.         If TypeOf objs Is AcadSelectionSet Then
  125.             Dim temp As New TlsSelectionSet
  126.             temp.Deleted = False
  127.             temp.AcSet = objs
  128.             oSel.RemoveItems temp.ToArray
  129.         ElseIf TypeOf objs Is TlsSelectionSet Then
  130.             oSel.RemoveItems objs.ToArray
  131.         Else
  132.             Dim ents(0) As AcadEntity
  133.             Set ents(0) = objs
  134.             oSel.RemoveItems ents
  135.         End If
  136.     End If
  137.    
  138. End Sub
  139. Public Sub Clear()
  140. '清空选择集
  141. On Error Resume Next
  142.    
  143.     Init sName
  144.     oSel.Clear
  145.    
  146. End Sub
  147. Public Sub Update()
  148. On Error Resume Next
  149.    
  150.     oSel.Update
  151. End Sub
  152. Public Property Get PickfirstSelectionSet() As AcadSelectionSet
  153. '获取Pickfirst选择集
  154. On Error Resume Next
  155.     ThisDrawing.SelectionSets("PICKFIRST").Delete
  156.     Set PickfirstSelectionSet = ThisDrawing.PickfirstSelectionSet
  157.    
  158. End Property
  159. Public Property Get ActiveSelectionSet() As AcadSelectionSet
  160. '获取Active选择集
  161. On Error Resume Next
  162.     ThisDrawing.SelectionSets("CURRENT").Delete
  163.     Set ActiveSelectionSet = ThisDrawing.ActiveSelectionSet
  164. End Property
  165. Public Sub SelectOnScreen()
  166. On Error Resume Next
  167.         
  168.     If mFilter.IsNull Then
  169.         oSel.SelectOnScreen
  170.     Else
  171.         oSel.SelectOnScreen mFilter.TypeCodes, mFilter.Datas
  172.     End If
  173.    
  174. End Sub
  175. Public Sub SelectSingleObject(Optional Prompt As String = "")
  176. On Error GoTo ErrHandle
  177.     Dim obj As AcadEntity, pnt
  178.     If mFilter.IsNull Then
  179.         If Prompt = "" Then
  180.             ThisDrawing.Utility.GetEntity obj, pnt
  181.         Else
  182.             ThisDrawing.Utility.GetEntity obj, pnt, Prompt
  183.         End If
  184.         AddItems obj
  185.     Else
  186.         SelectObject acSelectionSetAll
  187.         Do
  188.             If Prompt = "" Then
  189.                 ThisDrawing.Utility.GetEntity obj, pnt
  190.             Else
  191.                 ThisDrawing.Utility.GetEntity obj, pnt, Prompt
  192.             End If
  193.             oCount = Count
  194.             RemoveItems obj
  195.             If oCount <> Count Then
  196.                 Clear
  197.                 AddItems obj
  198.                 Exit Do
  199.             End If
  200.         Loop
  201.     End If
  202.     Exit Sub
  203. ErrHandle:
  204.     Clear
  205. End Sub
  206. Public Sub SelectObject(ByVal Mode As AcSelect, Optional ByVal Point1, Optional ByVal Point2)
  207. On Error Resume Next
  208.         
  209.     If mFilter.IsNull Then
  210.         If IsMissing(Point1) Then
  211.             oSel.Select Mode
  212.         Else
  213.             oSel.Select Mode, Point1, Point2
  214.         End If
  215.     Else
  216.         If IsMissing(Point1) Then
  217.             oSel.Select Mode, , , mFilter.TypeCodes, mFilter.Datas
  218.         Else
  219.             oSel.Select Mode, Point1, Point2, mFilter.TypeCodes, mFilter.Datas
  220.         End If
  221.     End If
  222.    
  223. End Sub
  224. Public Sub SelectAtPoint(ByVal Point)
  225. On Error Resume Next
  226.         
  227.     If mFilter.IsNull Then
  228.         oSel.SelectAtPoint Point
  229.     Else
  230.         oSel.SelectAtPoint Point, mFilter.TypeCodes, mFilter.Datas
  231.     End If
  232.    
  233. End Sub
  234. Public Sub SelectByPolygon(ByVal Mode As AcSelect, Optional ByVal Points)
  235. On Error Resume Next
  236.         
  237.     If mFilter.IsNull Then
  238.         oSel.SelectByPolygon Mode, Points
  239.     Else
  240.         oSel.SelectByPolygon Mode, Points, mFilter.TypeCodes, mFilter.Datas
  241.     End If
  242.    
  243. End Sub
  244. Public Property Let Visible(ByVal Value As Boolean)
  245. On Error Resume Next
  246.     If IsNull() Then Exit Property
  247.    
  248.     Dim i As AcadEntity
  249.     For Each i In oSel
  250.         i.Visible = Value
  251.     Next i
  252.    
  253. End Property
  254. Public Property Let Layer(ByVal Value As String)
  255. On Error Resume Next
  256.     If IsNull() Then Exit Property
  257.    
  258.     Dim i As AcadEntity
  259.     For Each i In oSel
  260.         i.Layer = Value
  261.     Next i
  262.    
  263. End Property
  264. Public Property Let LineType(ByVal Value As String)
  265. On Error Resume Next
  266.     If IsNull() Then Exit Property
  267.    
  268.     Dim i As AcadEntity
  269.     For Each i In oSel
  270.         i.LineType = Value
  271.     Next i
  272.    
  273. End Property
  274. Public Property Let Color(ByVal Value As ACAD_COLOR)
  275. On Error Resume Next
  276.     If IsNull() Then Exit Property
  277.    
  278.     Dim i As AcadEntity
  279.     For Each i In oSel
  280.         i.Color = Value
  281.     Next i
  282.    
  283. End Property
  284. Public Sub Move(Optional ByVal Point1, Optional ByVal Point2)
  285. On Error Resume Next
  286.     If IsNull() Then Exit Sub
  287.    
  288.     If IsMissing(Point1) Then Point1 = CreatePoint()
  289.     If IsMissing(Point2) Then Point2 = CreatePoint()
  290.    
  291.     Dim i As AcadEntity
  292.     For Each i In oSel
  293.         i.Move Point1, Point2
  294.     Next i
  295.    
  296. End Sub
  297. Public Function Copy(Optional ByVal Point1, Optional ByVal Point2)
  298. On Error Resume Next
  299.     If IsNull() Then Exit Function
  300.    
  301.     If IsMissing(Point1) Then Point1 = CreatePoint()
  302.     If IsMissing(Point2) Then Point2 = CreatePoint()
  303.    
  304.     Dim objs() As AcadEntity
  305.     Dim i
  306.     ReDim objs(Count - 1)
  307.    
  308.     For i = 0 To Count
  309.         Set objs(i) = oSel(i).Copy
  310.         objs(i).Move Point1, Point2
  311.     Next i
  312.    
  313.     Copy = objs
  314.    
  315. End Function
  316. Public Sub Rotate(Optional ByVal BasePoint, Optional ByVal RotationAngle As Double = 1#)
  317. On Error Resume Next
  318.     If IsNull() Then Exit Sub
  319.    
  320.     If IsMissing(BasePoint) Then BasePoint = CreatePoint()
  321.         
  322.     Dim i As AcadEntity
  323.     For Each i In oSel
  324.         i.Rotate BasePoint, RotationAngle
  325.     Next i
  326. End Sub
  327. Public Sub Rotate3D(Optional ByVal Point1, Optional ByVal Point2, Optional ByVal RotationAngle As Double = 1#)
  328. On Error Resume Next
  329.     If IsNull() Then Exit Sub
  330.    
  331.     If IsMissing(Point1) Then Point1 = CreatePoint()
  332.     If IsMissing(Point2) Then Point2 = CreatePoint()
  333.    
  334.     Dim i As AcadEntity
  335.     For Each i In oSel
  336.         i.Rotate3D Point1, Point2, RotationAngle
  337.     Next i
  338. End Sub
  339. Public Sub ScaleAll(Optional ByVal BasePoint, Optional ByVal ScaleFactor As Double = 1)
  340. On Error Resume Next
  341.     If IsNull() Then Exit Sub
  342.    
  343.     If IsMissing(BasePoint) Then BasePoint = CreatePoint()
  344.    
  345.     Dim i As AcadEntity
  346.     For Each i In oSel
  347.         i.ScaleEntity BasePoint, ScaleFactor
  348.     Next i
  349. End Sub
  350. Public Sub Mirror(Optional ByVal Point1, Optional ByVal Point2)
  351. On Error Resume Next
  352.     If IsNull() Then Exit Sub
  353.    
  354.     If IsMissing(Point1) Then Point1 = CreatePoint()
  355.     If IsMissing(Point2) Then Point2 = CreatePoint()
  356.    
  357.     Dim i As AcadEntity
  358.     For Each i In oSel
  359.         i.Mirror Point1, Point2
  360.     Next i
  361. End Sub
  362. Public Sub Mirror3D(Optional ByVal Point1, Optional ByVal Point2, Optional ByVal Point3)
  363. On Error Resume Next
  364.     If IsNull() Then Exit Sub
  365.     If IsMissing(Point1) Then Point1 = CreatePoint()
  366.     If IsMissing(Point2) Then Point2 = CreatePoint()
  367.     If IsMissing(Point3) Then Point3 = CreatePoint()
  368.    
  369.     Dim i As AcadEntity
  370.     For Each i In oSel
  371.         i.Mirror3D Point1, Point2, Point3
  372.     Next i
  373. End Sub
  374. Public Sub Highlight(Optional ByVal HighlightFlag As Boolean = True)
  375. On Error Resume Next
  376.    
  377.     Dim i As AcadEntity
  378.     For Each i In oSel
  379.         i.Highlight HighlightFlag
  380.     Next i
  381. End Sub
  382. Public Sub Delete()
  383. On Error Resume Next
  384.    
  385.     oSel.Erase
  386. End Sub
  387. Public Sub CopyObjects(Optional ByVal Owner, Optional ByVal IdPairs)
  388. On Error Resume Next
  389.     If IsNull() Then Exit Sub
  390.     If IsMissing(Owner) Then
  391.         If IsMissing(IdPairs) Then
  392.             ThisDrawing.CopyObjects ToArray
  393.         Else
  394.             ThisDrawing.CopyObjects ToArray, , IdPairs
  395.         End If
  396.     Else
  397.         If IsMissing(IdPairs) Then
  398.             ThisDrawing.CopyObjects ToArray, Owner
  399.         Else
  400.             ThisDrawing.CopyObjects ToArray, Owner, IdPairs
  401.         End If
  402.     End If
  403. End Sub
  404. Public Sub GetBoundingBox(ByRef MinPoint, ByRef MaxPoint)
  405. On Error Resume Next
  406.     Dim i
  407.     Dim d1, d2, p1, p2
  408.    
  409.     If IsNull() Then Exit Sub
  410.    
  411.     oSel(0).GetBoundingBox d1, d2
  412.    
  413.     For i = 1 To Count - 1
  414.    
  415.         oSel(i).GetBoundingBox p1, p2
  416.         
  417.         If p1(0) < d1(0) Then d1(0) = p1(0)
  418.         If p1(1) < d1(1) Then d1(1) = p1(1)
  419.         If p2(0) > d2(0) Then d2(0) = p2(0)
  420.         If p2(1) > d2(1) Then d2(1) = p2(1)
  421.         
  422.     Next i
  423.    
  424.     MinPoint = d1
  425.     MaxPoint = d2
  426.    
  427. End Sub
  428. Public Function ToBlock(Optional ByVal InsertionPoint, Optional ByVal Name As String = "*U") As String
  429. On Error GoTo ErrHandle
  430.     If IsMissing(InsertionPoint) Then InsertionPoint = CreatePoint()
  431.    
  432.     If IsNull() Then Exit Function
  433.    
  434.     Dim oBlock As AcadBlock
  435.     Set oBlock = ThisDrawing.Blocks.Add(InsertionPoint, Name)
  436.     CopyObjects oBlock
  437.    
  438.     ToBlock = oBlock.Name
  439.    
  440. ErrHandle:
  441. End Function
  442. Public Property Get Filter() As TlsResultBuffer
  443. On Error Resume Next
  444.     Set Filter = mFilter
  445.    
  446.     If Err Then
  447.         Set mFilter = New TlsResultBuffer
  448.         Set Filter = mFilter
  449.         Err.Clear
  450.     End If
  451.    
  452. End Property
  453. Private Function CreatePoint(Optional ByVal x As Double = 0#, Optional ByVal y As Double = 0#, Optional ByVal Z As Double = 0#)
  454.    
  455.     Dim pnt(2) As Double
  456.     pnt(0) = x: pnt(1) = y: pnt(2) = Z
  457.     CreatePoint = pnt
  458.    
  459. End Function


发表于 2010-1-22 00:33:00 | 显示全部楼层
本帖最后由 作者 于 2010-1-23 11:45:16 编辑

看到这个帖子,真是太惊喜了。有了这个类,就可以集中精力研究那些更有用的东西了。
发表于 2010-1-22 08:44:00 | 显示全部楼层
进来学习的~
发表于 2015-3-3 21:01:16 | 显示全部楼层
谢谢了,进来学习学习
发表于 2015-3-10 17:25:56 | 显示全部楼层
雪山飞狐_lzh版主牛!学习了
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-23 02:59 , Processed in 0.160548 second(s), 16 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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