明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 9199|回复: 26

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

  [复制链接]
发表于 2004-11-12 15:25:00 | 显示全部楼层 |阅读模式
本帖最后由 作者 于 2006-3-16 14:27:42 编辑
  1. Private oSel As AcadSelectionSet
  2. Private TlsFt, TlsFd
  3. Private sName As String
  4. Public Sub NullFilter()
  5. '清空过滤器
  6.     TlsFt = Null
  7.     TlsFd = Null
  8. End Sub
  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 = "TlsSel")
  20. '创建选择集
  21. On Error Resume Next
  22.    
  23.     NullFilter
  24.     If Not oSel Is Nothing Then oSel.Delete
  25.     sName = Name
  26.     ThisDrawing.SelectionSets(sName).Delete
  27.     Set oSel = ThisDrawing.SelectionSets.Add(sName)
  28.    
  29. End Sub
  30. Private Sub Class_Terminate()
  31. '类析构时清除选择集
  32. On Error Resume Next
  33.    
  34.     If Not oSel Is Nothing Then oSel.Delete
  35.    
  36. End Sub
  37. Public Function ToArray()
  38. '转化选择集为对象数组输出
  39. On Error Resume Next
  40.   
  41.     Dim i
  42.     Dim objs() As AcadEntity
  43.     Dim nCount As Integer
  44.    
  45.     nCount = oSel.Count - 1
  46.     ReDim objs(nCount)
  47.    
  48.     For i = 0 To nCount
  49.         Set objs(i) = oSel(i)
  50.     Next i
  51.    
  52.     ToArray = objs
  53.    
  54. End Function
  55. Public Property Get Count() As Integer
  56. '获取选择集实体个数
  57. On Error Resume Next
  58.     Count = oSel.Count
  59.    
  60. End Property
  61. Public Property Get Name() As String
  62. '获取选择集名称
  63. On Error Resume Next
  64.     Name = sName
  65.    
  66. End Property
  67. Public Property Get Item(ByVal Index) As AcadEntity
  68. '获取选择集实体
  69. On Error Resume Next
  70.     Set Item = oSel(Index)
  71.    
  72. End Property
  73. Public Sub AddItems(ByVal objs)
  74. '向选择集加入实体
  75. On Error Resume Next
  76.    
  77.     If IsArray(objs) Then
  78.         oSel.AddItems objs
  79.     ElseIf IsObject(objs) Then
  80.         Dim ents(0) As AcadEntity
  81.         Set ents(0) = objs
  82.         oSel.AddItems ents
  83.     End If
  84.    
  85. End Sub
  86. Public Sub RemoveItems(ByVal objs)
  87. '在选择集中移除实体
  88. On Error Resume Next
  89.    
  90.     If IsArray(objs) Then
  91.         oSel.RemoveItems objs
  92.     ElseIf IsObject(objs) Then
  93.         Dim ents(0) As AcadEntity
  94.         Set ents(0) = objs
  95.         oSel.RemoveItems ents
  96.     End If
  97.    
  98. End Sub
  99. Public Sub Clear()
  100. '清空选择集
  101. On Error Resume Next
  102.    
  103.     Select Case sName
  104.     Case "PICKFIRST"
  105.         GetPickfirstSel
  106.     Case "CURRENT"
  107.         GetActiveSel
  108.     Case Else
  109.         Init sName
  110.     End Select
  111.    
  112.     oSel.Clear
  113.    
  114. End Sub
  115. Public Sub Update()
  116. On Error Resume Next
  117.    
  118.     oSel.Update
  119. End Sub
  120. Public Function GetSel() As AcadSelectionSet
  121. '获取选择集
  122. On Error Resume Next
  123.    
  124.     Set GetSel = oSel
  125.    
  126. End Function
  127. Public Sub GetPickfirstSel()
  128. '获取Pickfirst选择集
  129. On Error Resume Next
  130.         
  131.     NullFilter
  132.     If Not oSel Is Nothing Then oSel.Delete
  133.     sName = "PICKFIRST"
  134.     ThisDrawing.SelectionSets(sName).Delete
  135.     Set oSel = ThisDrawing.PickfirstSelectionSet
  136.    
  137. End Sub
  138. Public Sub GetActiveSel()
  139. '获取Active选择集
  140. On Error Resume Next
  141.         
  142.     NullFilter
  143.     If Not oSel Is Nothing Then oSel.Delete
  144.     sName = "CURRENT"
  145.     ThisDrawing.SelectionSets(sName).Delete
  146.     Set oSel = ThisDrawing.ActiveSelectionSet
  147.    
  148. End Sub
  149. Public Sub SetFilterType(ParamArray FilterType())
  150. '设置过滤器类型
  151. On Error Resume Next
  152.    
  153.     Dim nCount As Integer
  154.     nCount = UBound(FilterType)
  155.    
  156.     Dim ft() As Integer
  157.     ReDim ft(nCount)
  158.    
  159.     For i = 0 To nCount
  160.         ft(i) = FilterType(i)
  161.     Next i
  162.    
  163.     TlsFt = ft
  164.    
  165. End Sub
  166. Public Sub SetFilterData(ParamArray FilterData())
  167. '设置过滤器
  168. On Error Resume Next
  169.    
  170.     Dim nCount As Integer
  171.     nCount = UBound(FilterData)
  172.    
  173.     Dim fd()
  174.     ReDim fd(nCount)
  175.    
  176.     For i = 0 To nCount
  177.         fd(i) = FilterData(i)
  178.     Next i
  179.    
  180.     TlsFd = fd
  181.    
  182. End Sub
  183. Public Sub SetFilter(ParamArray Filter())
  184. '设置过滤器
  185. On Error Resume Next
  186.    
  187.     Dim i
  188.     Dim n As Integer
  189.     Dim nCount As Integer
  190.     nCount = (UBound(Filter) + 1) / 2 - 1
  191.    
  192.     Dim ft() As Integer, fd()
  193.     ReDim ft(nCount), fd(nCount)
  194.    
  195.     For i = 0 To nCount
  196.         n = i * 2
  197.         ft(i) = Filter(n)
  198.         fd(i) = Filter(n + 1)
  199.     Next i
  200.    
  201.     TlsFt = ft
  202.     TlsFd = fd
  203. End Sub
  204. Public Sub AppendFilter(ParamArray Filter())
  205.     Dim n As Integer, oCount As Integer, nCount As Integer
  206.     oCount = UBound(TlsFt)
  207.     nCount = (UBound(Filter) + 1) / 2
  208.     n = oCount + nCount
  209.     ReDim Preserve TlsFt(n), TlsFd(n)
  210.     For i = 0 To nCount - 1
  211.         n = oCount + i + 1
  212.         TlsFt(n) = Filter(i * 2)
  213.         TlsFd(n) = Filter(i * 2 + 1)
  214.     Next i
  215. End Sub
  216. Public Sub SelectObjectOnScreen()
  217. On Error Resume Next
  218.         
  219.     If IsArray(TlsFt) Then
  220.         oSel.SelectOnScreen TlsFt, TlsFd
  221.     Else
  222.         oSel.SelectOnScreen
  223.     End If
  224.    
  225. End Sub
  226. Public Sub Selectobject(ByVal Mode As AcSelect, Optional ByVal Point1, Optional ByVal Point2)
  227. On Error Resume Next
  228.         
  229.     If IsArray(TlsFt) Then
  230.         If IsMissing(Point1) Then
  231.             oSel.Select Mode, , , TlsFt, TlsFd
  232.         Else
  233.             oSel.Select Mode, Point1, Point2, TlsFt, TlsFd
  234.         End If
  235.     Else
  236.         If IsMissing(Point1) Then
  237.             oSel.Select Mode
  238.         Else
  239.             oSel.Select Mode, Point1, Point2
  240.         End If
  241.     End If
  242.    
  243. End Sub
  244. Public Sub SelectObjectAtPoint(ByVal Point)
  245. On Error Resume Next
  246.         
  247.     If IsArray(TlsFt) Then
  248.         oSel.SelectAtPoint Point, TlsFt, TlsFd
  249.     Else
  250.         oSel.SelectAtPoint Point
  251.     End If
  252.    
  253. End Sub
  254. Public Sub SelectObjectByPolygon(ByVal Mode As AcSelect, Optional ByVal Points, Optional ByVal Point2)
  255. On Error Resume Next
  256.         
  257.     If IsArray(TlsFt) Then
  258.         oSel.SelectByPolygon Mode, Points, TlsFt, TlsFd
  259.     Else
  260.         oSel.SelectByPolygon Mode, Points
  261.     End If
  262.    
  263. End Sub
  264. Public Property Let Visible(ByVal Value As Boolean)
  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.Visible = Value
  271.     Next i
  272.    
  273. End Property
  274. Public Property Let Layer(ByVal Value As String)
  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.Layer = Value
  281.     Next i
  282.    
  283. End Property
  284. Public Property Let LineType(ByVal Value As String)
  285. On Error Resume Next
  286.     If IsNull() Then Exit Property
  287.    
  288.     Dim i As AcadEntity
  289.     For Each i In oSel
  290.         i.LineType = Value
  291.     Next i
  292.    
  293. End Property
  294. Public Property Let Color(ByVal Value As ACAD_COLOR)
  295. On Error Resume Next
  296.     If IsNull() Then Exit Property
  297.    
  298.     Dim i As AcadEntity
  299.     For Each i In oSel
  300.         i.Color = Value
  301.     Next i
  302.    
  303. End Property
  304. Public Sub Move(Optional ByVal Point1, Optional ByVal Point2)
  305. On Error Resume Next
  306.     If IsNull() Then Exit Sub
  307.    
  308.     If IsMissing(Point1) Then Point1 = CreatePoint()
  309.     If IsMissing(Point2) Then Point2 = CreatePoint()
  310.    
  311.     Dim i As AcadEntity
  312.     For Each i In oSel
  313.         i.Move Point1, Point2
  314.     Next i
  315.    
  316. End Sub
  317. Public Function Copy(Optional ByVal Point1, Optional ByVal Point2)
  318. On Error Resume Next
  319.     If IsNull() Then Exit Sub
  320.    
  321.     If IsMissing(Point1) Then Point1 = CreatePoint()
  322.     If IsMissing(Point2) Then Point2 = CreatePoint()
  323.    
  324.     Dim objs() As AcadEntity
  325.     Dim i
  326.     ReDim objs(Count - 1)
  327.    
  328.     For i = 0 To Count
  329.         Set objs(i) = oSel(i).Copy
  330.         objs(i).Move Point1, Point2
  331.     Next i
  332.    
  333.     Copy = objs
  334.    
  335. End Function
  336. Public Sub Rotate(Optional ByVal BasePoint, Optional ByVal RotationAngle As Double = 1#)
  337. On Error Resume Next
  338.     If IsNull() Then Exit Sub
  339.    
  340.     If IsMissing(BasePoint) Then BasePoint = CreatePoint()
  341.         
  342.     Dim i As AcadEntity
  343.     For Each i In oSel
  344.         i.Rotate BasePoint, RotationAngle
  345.     Next i
  346. End Sub
  347. Public Sub Rotate3D(Optional ByVal Point1, Optional ByVal Point2, Optional ByVal RotationAngle As Double = 1#)
  348. On Error Resume Next
  349.     If IsNull() Then Exit Sub
  350.    
  351.     If IsMissing(Point1) Then Point1 = CreatePoint()
  352.     If IsMissing(Point2) Then Point2 = CreatePoint()
  353.    
  354.     Dim i As AcadEntity
  355.     For Each i In oSel
  356.         i.Rotate3D Point1, Point2, RotationAngle
  357.     Next i
  358. End Sub
  359. Public Sub ScaleAll(Optional ByVal BasePoint, Optional ByVal ScaleFactor As Double = 1)
  360. On Error Resume Next
  361.     If IsNull() Then Exit Sub
  362.    
  363.     If IsMissing(BasePoint) Then BasePoint = CreatePoint()
  364.    
  365.     Dim i As AcadEntity
  366.     For Each i In oSel
  367.         i.ScaleEntity BasePoint, ScaleFactor
  368.     Next i
  369. End Sub
  370. Public Sub Mirror(Optional ByVal Point1, Optional ByVal Point2)
  371. On Error Resume Next
  372.     If IsNull() Then Exit Sub
  373.    
  374.     If IsMissing(Point1) Then Point1 = CreatePoint()
  375.     If IsMissing(Point2) Then Point2 = CreatePoint()
  376.    
  377.     Dim i As AcadEntity
  378.     For Each i In oSel
  379.         i.Mirror Point1, Point2
  380.     Next i
  381. End Sub
  382. Public Sub Mirror3D(Optional ByVal Point1, Optional ByVal Point2, Optional ByVal Point3)
  383. On Error Resume Next
  384.     If IsNull() Then Exit Sub
  385.     If IsMissing(Point1) Then Point1 = CreatePoint()
  386.     If IsMissing(Point2) Then Point2 = CreatePoint()
  387.     If IsMissing(Point3) Then Point3 = CreatePoint()
  388.    
  389.     Dim i As AcadEntity
  390.     For Each i In oSel
  391.         i.Mirror3D Point1, Point2, Point3
  392.     Next i
  393. End Sub
  394. Public Sub Highlight(Optional ByVal HighlightFlag As Boolean = True)
  395. On Error Resume Next
  396.    
  397.     Dim i As AcadEntity
  398.     For Each i In oSel
  399.         i.Highlight HighlightFlag
  400.     Next i
  401. End Sub
  402. Public Sub Delete()
  403. On Error Resume Next
  404.    
  405.     oSel.Erase
  406. End Sub
  407. Public Sub CopyObjects(Optional ByVal Owner, Optional ByVal IdPairs)
  408. On Error Resume Next
  409.     If IsNull() Then Exit Sub
  410.     If IsMissing(Owner) Then
  411.         If IsMissing(IdPairs) Then
  412.             ThisDrawing.CopyObjects ToArray
  413.         Else
  414.             ThisDrawing.CopyObjects ToArray, , IdPairs
  415.         End If
  416.     Else
  417.         If IsMissing(IdPairs) Then
  418.             ThisDrawing.CopyObjects ToArray, Owner
  419.         Else
  420.             ThisDrawing.CopyObjects ToArray, Owner, IdPairs
  421.         End If
  422.     End If
  423. End Sub
  424. Public Sub GetBoundingBox(ByRef MinPoint, ByRef MaxPoint)
  425. On Error Resume Next
  426.     Dim i
  427.     Dim d1, d2, p1, p2
  428.    
  429.     If IsNull() Then Exit Sub
  430.    
  431.     oSel(0).GetBoundingBox d1, d2
  432.    
  433.     For i = 1 To Count - 1
  434.    
  435.         oSel(i).GetBoundingBox p1, p2
  436.         
  437.         If p1(0) < d1(0) Then d1(0) = p1(0)
  438.         If p1(1) < d1(1) Then d1(1) = p1(1)
  439.         If p2(0) > d2(0) Then d2(0) = p2(0)
  440.         If p2(1) > d2(1) Then d2(1) = p2(1)
  441.         
  442.     Next i
  443.    
  444.     MinPoint = d1
  445.     MaxPoint = d2
  446.    
  447. End Sub
  448. Public Function CreatePoint(Optional ByVal X As Double = 0#, Optional ByVal Y As Double = 0#, Optional ByVal Z As Double = 0#)
  449.    
  450.     Dim pnt(2) As Double
  451.     pnt(0) = X: pnt(1) = Y: pnt(2) = Z
  452.    
  453.     CreatePoint = pnt
  454.    
  455. End Function
  456. Public Function ToBlock(Optional ByVal InsertionPoint, Optional ByVal Name As String = "*U") As String
  457. On Error GoTo ErrHandle
  458.     If IsMissing(InsertionPoint) Then InsertionPoint = CreatePoint()
  459.    
  460.     If IsNull() Then Exit Function
  461.    
  462.     Dim oBlock As AcadBlock
  463.     Set oBlock = ThisDrawing.Blocks.Add(InsertionPoint, Name)
  464.     CopyObjects oBlock
  465.    
  466.     ToBlock = oBlock.Name
  467.    
  468. ErrHandle:
  469. End Function

本帖被以下淘专辑推荐:

发表于 2004-11-12 15:54:00 | 显示全部楼层
沙发,谢谢了,能介绍一下吗?
发表于 2004-11-12 16:52:00 | 显示全部楼层
可否介绍一下,有哪些功能的增强?
 楼主| 发表于 2004-11-12 19:30:00 | 显示全部楼层
比较一下下面两段代码,功能是一样的 Sub test1()
Dim ss As New TlsSel
ss.Init
ss.SetFilterType 0
ss.SetFilterData "Line"
ss.SelectObjectOnScreen
End Sub Sub test2()
On Error Resume Next
Dim ss As AcadSelectionSet
ThisDrawing.SelectionSets("TlsSel").Delete
Set ss = ThisDrawing.SelectionSets.Add("TlsSel")
Dim ft(0) As Integer, fd(0)
ft(0) = 0: fd(0) = "Line"
ss.SelectOnScreen ft, fd
ss.Delete
End Sub
发表于 2004-11-12 20:24:00 | 显示全部楼层
收下了,仔细研究研究。简化了多。
发表于 2004-11-12 20:49:00 | 显示全部楼层
建议增加一些常用属性的修改,如颜色、线型、图层等。以前象移动、复制等功能。这也是我以前想做的。
因为AX的选择集就这一点和LISP的选择集不同,操作起来也不方便。
 楼主| 发表于 2004-11-12 22:21:00 | 显示全部楼层
本帖最后由 作者 于 2004-11-13 13:05:04 编辑

按照老大的意见已做更改,见一楼代码,大家看看还有什么要加的?
 楼主| 发表于 2004-11-12 22:31:00 | 显示全部楼层
这是测试代码
  1. Sub test1()
  2.        Dim ss As New TlsSel
  3.        ss.Init "TlsSel1"
  4.        ss.SetFilterType 0, 8
  5.        ss.SetFilterData "Line", "0"
  6.        ss.SelectObjectOnScreen
  7.        ss.GetBoundingBox p1, p2
  8. '       OutputPoint p1
  9. '       OutputPoint p2
  10.        ss.Move p1, p2
  11. End Sub
发表于 2004-11-13 09:58:00 | 显示全部楼层
不错,有所增强
发表于 2004-11-14 18:07:00 | 显示全部楼层
不错也
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-11-22 21:09 , Processed in 0.188292 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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