找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 5301|回复: 10

[原创]:一个选择集的增强类

[复制链接]
发表于 2004-11-18 22:40:09 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

  1. 'TlsSel类
  2. 'By 雪山飞狐
  3. '模块名---TlsSel
  4. ' You may use the code included in this module in any way,
  5. ' provided that both the above copyright notice and the
  6. ' release of liability (stated below) appear in all copies.
  7. Private oSel As AcadSelectionSet
  8. Private TlsFt, TlsFd
  9. Private sName As String

  10. Public Sub NullFilter()
  11. '清空过滤器
  12.     TlsFt = Null
  13.     TlsFd = Null

  14. End Sub

  15. Private Function IsNull() As Boolean

  16.     If oSel Is Nothing Then
  17.         IsNull = True
  18.     ElseIf oSel.Count = 0 Then
  19.         IsNull = True
  20.     Else
  21.         IsNull = False
  22.     End If
  23.    
  24. End Function

  25. Public Sub Init(Optional ByVal Name As String = "TlsSel")
  26. '创建选择集
  27. On Error Resume Next
  28.    
  29.     NullFilter
  30.     If Not oSel Is Nothing Then oSel.Delete
  31.     sName = Name
  32.     ThisDrawing.SelectionSets(sName).Delete
  33.     Set oSel = ThisDrawing.SelectionSets.Add(sName)
  34.    
  35. End Sub

  36. Private Sub Class_Terminate()
  37. '类析构时清除选择集
  38. On Error Resume Next
  39.    
  40.     If Not oSel Is Nothing Then oSel.Delete
  41.    
  42. End Sub

  43. Public Function ToArray()
  44. '转化选择集为对象数组输出
  45. On Error Resume Next
  46.   
  47.     Dim i
  48.     Dim objs() As AcadEntity
  49.     Dim nCount As Integer
  50.    
  51.     nCount = oSel.Count - 1
  52.     ReDim objs(nCount)
  53.    
  54.     For i = 0 To nCount
  55.         Set objs(i) = oSel(i)
  56.     Next i
  57.    
  58.     ToArray = objs
  59.    
  60. End Function

  61. Public Property Get Count() As Integer
  62. '获取选择集实体个数
  63. On Error Resume Next

  64.     Count = oSel.Count
  65.    
  66. End Property

  67. Public Property Get Name() As String
  68. '获取选择集名称
  69. On Error Resume Next

  70.     Name = sName
  71.    
  72. End Property


  73. Public Property Get Item(ByVal Index) As AcadEntity
  74. '获取选择集实体
  75. On Error Resume Next

  76.     Set Item = oSel(Index)
  77.    
  78. End Property

  79. Public Sub Add(ByVal obj)
  80. '向选择集加入单个实体
  81. On Error Resume Next
  82.    
  83.     Dim objs(0) As AcadEntity
  84.    
  85.     Set objs(0) = obj
  86.     oSel.AddItems pbjs
  87.    
  88. End Sub

  89. Public Sub AddItems(ByVal objs)
  90. '向选择集加入实体数组
  91. On Error Resume Next
  92.    
  93.     oSel.AddItems objs
  94.    
  95. End Sub

  96. Public Sub Remove(ByVal obj)
  97. '在选择集中移除单个实体
  98. On Error Resume Next
  99.    
  100.     Dim objs(0) As AcadEntity
  101.     Set objs(0) = obj
  102.     oSel.RemoveItems objs
  103.    
  104. End Sub


  105. Public Sub RemoveItems(ByVal objs)
  106. '在选择集中移除实体数组
  107. On Error Resume Next
  108.    
  109.     oSel.RemoveItems objs
  110.    
  111. End Sub

  112. Public Sub Clear()
  113. '清空选择集
  114. On Error Resume Next
  115.    
  116.     Select Case sName
  117.     Case "PICKFIRST"
  118.         GetPickfirstSel
  119.     Case "CURRENT"
  120.         GetActiveSel
  121.     Case Else
  122.         Init sName
  123.     End Select
  124.    
  125.     oSel.Clear
  126.    
  127. End Sub

  128. Public Sub Update()
  129. On Error Resume Next
  130.    
  131.     oSel.Update

  132. End Sub

  133. Public Function GetSel() As AcadSelectionSet
  134. '获取选择集
  135. On Error Resume Next
  136.    
  137.     Set GetSel = oSel
  138.    
  139. End Function

  140. Public Sub GetPickfirstSel()
  141. '获取Pickfirst选择集
  142. On Error Resume Next
  143.         
  144.     NullFilter
  145.     If Not oSel Is Nothing Then oSel.Delete
  146.     sName = "PICKFIRST"
  147.     ThisDrawing.SelectionSets(sName).Delete
  148.     Set oSel = ThisDrawing.PickfirstSelectionSet
  149.    
  150. End Sub

  151. Public Sub GetActiveSel()
  152. '获取Active选择集
  153. On Error Resume Next
  154.         
  155.     NullFilter
  156.     If Not oSel Is Nothing Then oSel.Delete
  157.     sName = "CURRENT"
  158.     ThisDrawing.SelectionSets(sName).Delete
  159.     Set oSel = ThisDrawing.ActiveSelectionSet
  160.    
  161. End Sub

  162. Public Sub SetFilterType(ParamArray FilterType())
  163. '设置过滤器类型
  164. On Error Resume Next
  165.    
  166.     Dim i
  167.     Dim nCount As Integer
  168.     nCount = UBound(FilterType)
  169.    
  170.     Dim ft() As Integer
  171.     ReDim ft(nCount)
  172.    
  173.     For i = 0 To nCount
  174.         ft(i) = FilterType(i)
  175.     Next i
  176.    
  177.     TlsFt = ft
  178.    
  179. End Sub

  180. Public Sub SetFilterData(ParamArray FilterData())
  181. '设置过滤器数据
  182. On Error Resume Next

  183.     Dim i
  184.     Dim nCount As Integer
  185.     nCount = UBound(FilterData)
  186.    
  187.     Dim fd()
  188.     ReDim fd(nCount)
  189.    
  190.     For i = 0 To nCount
  191.         fd(i) = FilterData(i)
  192.     Next i
  193.    
  194.     TlsFd = fd
  195.    
  196. End Sub

  197. Public Sub SetFilter(ParamArray Filter())
  198. '设置过滤器
  199. On Error Resume Next
  200.    
  201.     Dim i
  202.     Dim n As Integer
  203.     Dim nCount As Integer
  204.     nCount = (UBound(Filter) + 1) / 2 - 1
  205.    
  206.     Dim ft() As Integer, fd()
  207.     ReDim ft(nCount), fd(nCount)
  208.    
  209.     For i = 0 To nCount
  210.         n = i * 2
  211.         ft(i) = Filter(n)
  212.         fd(i) = Filter(n + 1)
  213.     Next i
  214.    
  215.     TlsFt = ft
  216.     TlsFd = fd

  217. End Sub


  218. Public Sub SelectObjectOnScreen()
  219. On Error Resume Next
  220.         
  221.     If IsArray(TlsFt) Then
  222.         oSel.SelectOnScreen TlsFt, TlsFd
  223.     Else
  224.         oSel.SelectOnScreen
  225.     End If
  226.    
  227. End Sub

  228. Public Sub Selectobject(ByVal Mode As AcSelect, Optional ByVal Point1, Optional ByVal Point2)
  229. On Error Resume Next
  230.         
  231.     If IsArray(TlsFt) Then
  232.         If IsMissing(Point1) Then
  233.             oSel.Select Mode, , , TlsFt, TlsFd
  234.         Else
  235.             oSel.Select Mode, Point1, Point2, TlsFt, TlsFd
  236.         End If
  237.     Else
  238.         If IsMissing(Point1) Then
  239.             oSel.Select Mode
  240.         Else
  241.             oSel.Select Mode, Point1, Point2
  242.         End If
  243.     End If
  244.    
  245. End Sub

  246. Public Sub SelectObjectAtPoint(ByVal Point)
  247. On Error Resume Next
  248.         
  249.     If IsArray(TlsFt) Then
  250.         oSel.SelectAtPoint Point, TlsFt, TlsFd
  251.     Else
  252.         oSel.SelectAtPoint Point
  253.     End If
  254.    
  255. End Sub

  256. Public Sub SelectObjectByPolygon(ByVal Mode As AcSelect, Optional ByVal Points, Optional ByVal Point2)
  257. On Error Resume Next
  258.         
  259.     If IsArray(TlsFt) Then
  260.         oSel.SelectByPolygon Mode, Points, TlsFt, TlsFd
  261.     Else
  262.         oSel.SelectByPolygon Mode, Points
  263.     End If
  264.    
  265. End Sub

  266. Public Property Let Visible(ByVal Value As Boolean)
  267. On Error Resume Next

  268.     If IsNull() Then Exit Property
  269.    
  270.     Dim i As AcadEntity
  271.     For Each i In oSel
  272.         i.Visible = Value
  273.     Next i
  274.    
  275. End Property


  276. Public Property Let Layer(ByVal Value As String)
  277. On Error Resume Next

  278.     If IsNull() Then Exit Property
  279.    
  280.     Dim i As AcadEntity
  281.     For Each i In oSel
  282.         i.Layer = Value
  283.     Next i
  284.    
  285. End Property

  286. Public Property Let LineType(ByVal Value As String)
  287. On Error Resume Next

  288.     If IsNull() Then Exit Property
  289.    
  290.     Dim i As AcadEntity
  291.     For Each i In oSel
  292.         i.LineType = Value
  293.     Next i
  294.    
  295. End Property

  296. Public Property Let Color(ByVal Value As ACAD_COLOR)
  297. On Error Resume Next

  298.     If IsNull() Then Exit Property
  299.    
  300.     Dim i As AcadEntity
  301.     For Each i In oSel
  302.         i.Color = Value
  303.     Next i
  304.    
  305. End Property

  306. Public Sub Move(Optional ByVal Point1, Optional ByVal Point2)
  307. On Error Resume Next

  308.     If IsNull() Then Exit Sub
  309.     If IsMissing(Point1) Then Point1 = CreatePoint()
  310.     If IsMissing(Point2) Then Point2 = CreatePoint()
  311.    
  312.     Dim i As AcadEntity
  313.     For Each i In oSel
  314.         i.Move Point1, Point2
  315.     Next i
  316.    
  317. End Sub

  318. Public Function Copy(Optional ByVal Point1, Optional ByVal Point2)
  319. On Error Resume Next

  320.     If IsNull() Then Exit Sub
  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.     If IsMissing(BasePoint) Then BasePoint = CreatePoint()
  340.         
  341.     Dim i As AcadEntity
  342.     For Each i In oSel
  343.         i.Rotate BasePoint, RotationAngle
  344.     Next i

  345. End Sub

  346. Public Sub Rotate3D(Optional ByVal Point1, Optional ByVal Point2, Optional ByVal RotationAngle As Double = 1#)
  347. On Error Resume Next

  348.     If IsNull() Then Exit Sub
  349.     If IsMissing(Point1) Then Point1 = CreatePoint()
  350.     If IsMissing(Point2) Then Point2 = CreatePoint()
  351.    
  352.     Dim i As AcadEntity
  353.     For Each i In oSel
  354.         i.Rotate3D Point1, Point2, RotationAngle
  355.     Next i

  356. End Sub

  357. Public Sub ScaleAll(Optional ByVal BasePoint, Optional ByVal scalefactor As Double = 1)
  358. On Error Resume Next

  359.     If IsNull() Then Exit Sub
  360.     If IsMissing(BasePoint) Then BasePoint = CreatePoint()
  361.    
  362.     Dim i As AcadEntity
  363.     For Each i In oSel
  364.         i.ScaleEntity BasePoint, scalefactor
  365.     Next i

  366. End Sub

  367. Public Sub Mirror(Optional ByVal Point1, Optional ByVal Point2)
  368. On Error Resume Next

  369.     If IsNull() Then Exit Sub
  370.     If IsMissing(Point1) Then Point1 = CreatePoint()
  371.     If IsMissing(Point2) Then Point2 = CreatePoint()
  372.    
  373.     Dim i As AcadEntity
  374.     For Each i In oSel
  375.         i.Mirror Point1, Point2
  376.     Next i

  377. End Sub

  378. Public Sub Mirror3D(Optional ByVal Point1, Optional ByVal Point2, Optional ByVal Point3)
  379. On Error Resume Next

  380.     If IsNull() Then Exit Sub
  381.     If IsMissing(Point1) Then Point1 = CreatePoint()
  382.     If IsMissing(Point2) Then Point2 = CreatePoint()
  383.     If IsMissing(Point3) Then Point3 = CreatePoint()
  384.    
  385.     Dim i As AcadEntity
  386.     For Each i In oSel
  387.         i.Mirror3D Point1, Point2, Point3
  388.     Next i

  389. End Sub

  390. Public Sub Highlight(Optional ByVal HighlightFlag As Boolean = True)
  391. On Error Resume Next
  392.    
  393.     Dim i As AcadEntity
  394.     For Each i In oSel
  395.         i.Highlight HighlightFlag
  396.     Next i

  397. End Sub

  398. Public Sub Delete()
  399. On Error Resume Next
  400.    
  401.     oSel.Erase

  402. End Sub


  403. Public Sub CopyObjects(Optional ByVal Owner, Optional ByVal IdPairs)
  404. On Error Resume Next

  405.     If IsNull() Then Exit Sub

  406.     If IsMissing(Owner) Then
  407.         If IsMissing(IdPairs) Then
  408.             ThisDrawing.CopyObjects ToArray
  409.         Else
  410.             ThisDrawing.CopyObjects ToArray, , IdPairs
  411.         End If
  412.     Else
  413.         If IsMissing(IdPairs) Then
  414.             ThisDrawing.CopyObjects ToArray, Owner
  415.         Else
  416.             ThisDrawing.CopyObjects ToArray, Owner, IdPairs
  417.         End If
  418.     End If

  419. End Sub

  420. Public Sub GetBoundingBox(ByRef MinPoint, ByRef MaxPoint)
  421. On Error Resume Next

  422.     Dim i
  423.     Dim d1, d2, p1, p2
  424.    
  425.     If IsNull() Then Exit Sub
  426.    
  427.     oSel(0).GetBoundingBox d1, d2
  428.    
  429.     For i = 1 To Count - 1
  430.    
  431.         oSel(i).GetBoundingBox p1, p2
  432.         
  433.         If p1(0) < d1(0) Then d1(0) = p1(0)
  434.         If p1(1) < d1(1) Then d1(1) = p1(1)
  435.         If p2(0) > d2(0) Then d2(0) = p2(0)
  436.         If p2(1) > d2(1) Then d2(1) = p2(1)
  437.         
  438.     Next i
  439.    
  440.     MinPoint = d1
  441.     MaxPoint = d2
  442.    
  443. End Sub

  444. Public Function ToBlock(Optional ByVal InsertionPoint, Optional ByVal Name As String = "*U") As String
  445. On Error GoTo ErrHandle

  446.     If IsNull() Then Exit Function
  447.     If IsMissing(InsertionPoint) Then InsertionPoint = CreatePoint()
  448.    
  449.     Dim oBlock As AcadBlock
  450.     Set oBlock = ThisDrawing.Blocks.Add(InsertionPoint, Name)
  451.     CopyObjects oBlock
  452.    
  453.     ToBlock = oBlock.Name
  454.    
  455. ErrHandle:

  456. End Function

  457. Function CreateArray(ByVal TypeName As VbVarType, ParamArray ValArray())
  458.     Dim nCount As Integer
  459.     Dim i
  460.     Dim mArray
  461.    
  462.     nCount = UBound(ValArray)
  463.    
  464.     Select Case TypeName
  465.     Case vbDouble
  466.         Dim dArray() As Double
  467.         ReDim dArray(nCount)
  468.         mArray = dArray
  469.     Case vbInteger
  470.         Dim nArray() As Integer
  471.         ReDim nArray(nCount)
  472.         mArray = nArray
  473.     Case vbString
  474.         Dim sArray() As String
  475.         ReDim sArray(nCount)
  476.         mArray = sArray
  477.     Case vbVariant
  478.         Dim vArray()
  479.         ReDim vArray(nCount)
  480.         mArray = vArray
  481.     End Select
  482.    
  483.     For i = 0 To nCount
  484.         mArray(i) = ValArray(i)
  485.     Next i
  486.    
  487.     CreateArray = mArray
  488. End Function

  489. Public Function CreatePoint(Optional ByVal X As Double = 0#, Optional ByVal Y As Double = 0#, Optional ByVal Z As Double = 0#)
  490.    
  491.     Dim pnt(2) As Double
  492.     pnt(0) = X: pnt(1) = Y: pnt(2) = Z
  493.    
  494.     CreatePoint = pnt
  495.    
  496. End Function

论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
 楼主| 发表于 2004-11-18 22:43:00 | 显示全部楼层
下面是一段测试

  1. Sub test()
  2.     Dim ss As New TlsSel
  3.     ss.Init
  4.     ss.SetFilter 0, "Insert", 2, "1"
  5.     ss.Selectobject acSelectionSetAll
  6.     MsgBox ss.Count
  7. End Sub
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2004-11-19 08:59:05 | 显示全部楼层
很好的一个类!请问引用块的过滤数据类型和数据是什么?谢谢

Public Sub SetFilter(ParamArray Filter())
'设置过滤器
On Error Resume Next
   
    Dim i
    Dim n As Integer
    Dim nCount As Integer
    nCount = (UBound(Filter) + 1) / 2 - 1
   
    Dim ft() As Integer, fd()
    ReDim ft(nCount), fd(nCount)
   
    For i = 0 To nCount
        n = i * 2
        ft(i) = Filter(n)
        fd(i) = Filter(n + 1)
    Next i
   
    TlsFt = ft
    TlsFd = fd

End Sub
这个函数不太明白,为什么nCount = (UBound(Filter) + 1) / 2 - 1??
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

 楼主| 发表于 2004-11-19 09:36:27 | 显示全部楼层
(UBound(Filter) + 1)是Filter数组的元素个数
设置过滤器前先在命令行键入"(entget(car(entsel)))",看看对应实体的DXF组码
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2004-11-20 00:56:56 | 显示全部楼层
好程序!!怎么没有加分?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 1个

财富等级: 恭喜发财

发表于 2005-3-18 20:02:50 | 显示全部楼层
类是不是就是一种数据类型,自定义的?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2005-6-7 18:41:30 | 显示全部楼层
doc1.CopyObjects ToArray(acadselect)


Public Function ToArray(ByVal osel As AcadSelectionSet)
'转化选择集为对象数组输出
On Error Resume Next

    Dim i
    Dim objs() As AcadEntity
    Dim nCount As Integer

    nCount = osel.Count - 1
    ReDim objs(nCount)

    For i = 0 To nCount
        Set objs(i) = osel(i)
    Next i

    ToArray = objs
End Function

toarry拿出来放到其它的程序中调用时,执行到toarry=objs时就出错,提示“对象不在数据库中”是怎么回事?
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2009-3-7 11:10:15 | 显示全部楼层
ddddddddddddd
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2009-7-24 16:50:21 | 显示全部楼层
好顶一个
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|申请友链|Archiver|手机版|小黑屋|辽公网安备|晓东CAD家园 ( 辽ICP备15016793号 )

GMT+8, 2024-12-22 19:52 , Processed in 0.291092 second(s), 47 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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