找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 629|回复: 9

[VBA程序]:如何实现选择

[复制链接]
发表于 2003-6-14 11:11:46 | 显示全部楼层 |阅读模式

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

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

×
如何用程序实现操作CAD图纸放大缩小,自动选择整个图纸
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
发表于 2003-6-14 14:50:53 | 显示全部楼层

  1.   [FONT=courier new]
  2. Sub SelAll()
  3.     On Error Resume Next
  4.     Dim mySel As AcadSelectionSet
  5.    
  6.     ThisDrawing.SelectionSets("tempSel").Delete
  7.     Set mySel = ThisDrawing.SelectionSets.Add("tempSel")
  8.    
  9.     mySel.Select acSelectionSetAll
  10.     mySel.Highlight True
  11.    
  12.         
  13. End Sub

  14. Sub Example_ZoomAll()
  15.     ' This example creates several objects in model space and
  16.     ' then performs a variety of zooms on the drawing.
  17.    
  18.     ' Create a Ray object in model space
  19.     Dim rayObj As AcadRay
  20.     Dim basePoint(0 To 2) As Double
  21.     Dim SecondPoint(0 To 2) As Double
  22.     basePoint(0) = 3#: basePoint(1) = 3#: basePoint(2) = 0#
  23.     SecondPoint(0) = 1#: SecondPoint(1) = 3#: SecondPoint(2) = 0#
  24.     Set rayObj = ThisDrawing.ModelSpace.AddRay(basePoint, SecondPoint)
  25.    
  26.     ' Create a polyline object in model space
  27.     Dim plineObj As AcadLWPolyline
  28.     Dim points(0 To 5) As Double
  29.     points(0) = 3: points(1) = 7
  30.     points(2) = 9: points(3) = 2
  31.     points(4) = 3: points(5) = 5
  32.     Set plineObj = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)
  33.     plineObj.Closed = True

  34.     ' Create a line object in model space
  35.     Dim lineObj As AcadLine
  36.     Dim startPoint(0 To 2) As Double
  37.     Dim endPoint(0 To 2) As Double
  38.     startPoint(0) = 0: startPoint(1) = 0: startPoint(2) = 0
  39.     endPoint(0) = 2: endPoint(1) = 2: endPoint(2) = 0
  40.     Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)
  41.    
  42.     ' Create a circle object in model space
  43.     Dim circObj As AcadCircle
  44.     Dim centerPt(0 To 2) As Double
  45.     Dim radius As Double
  46.     centerPt(0) = 20: centerPt(1) = 30: centerPt(2) = 0
  47.     radius = 3
  48.     Set circObj = ThisDrawing.ModelSpace.AddCircle(centerPt, radius)

  49.     ' Create an ellipse object in model space
  50.     Dim ellObj As AcadEllipse
  51.     Dim majAxis(0 To 2) As Double
  52.     Dim center(0 To 2) As Double
  53.     Dim radRatio As Double
  54.     center(0) = 5#: center(1) = 5#: center(2) = 0#
  55.     majAxis(0) = 10: majAxis(1) = 20#: majAxis(2) = 0#
  56.     radRatio = 0.3
  57.     Set ellObj = ThisDrawing.ModelSpace.AddEllipse(center, majAxis, radRatio)

  58. ' ZoomAll
  59.     MsgBox "Perform a ZoomAll", , "ZoomWindow Example"
  60.     ZoomAll
  61.    
  62. ' ZoomWindow
  63.     MsgBox "Perform a ZoomWindow using the following coordinates:" & vbCrLf & _
  64.            "1.3, 7.8, 0" & vbCrLf & _
  65.            "13.7, -2.6, 0", , "ZoomWindow Example"
  66.            
  67.     Dim point1(0 To 2) As Double
  68.     Dim point2(0 To 2) As Double
  69.     point1(0) = 1.3: point1(1) = 7.8: point1(2) = 0
  70.     point2(0) = 13.7: point2(1) = -2.6: point2(2) = 0
  71.     ZoomWindow point1, point2
  72.    
  73. ' ZoomScaled
  74.     MsgBox "Perform a ZoomScaled using:" & vbCrLf & _
  75.            "Scale Type: acZoomScaledRelative" & vbCrLf & _
  76.            "Scale Factor: 2", , "ZoomWindow Example"
  77.     Dim scalefactor As Double
  78.     Dim scaletype As Integer
  79.     scalefactor = 2
  80.     scaletype = acZoomScaledRelative
  81.     ZoomScaled scalefactor, scaletype
  82.    
  83. ' ZoomExtents
  84.     MsgBox "Perform a ZoomExtents", , "ZoomWindow Example"
  85.     ZoomExtents
  86.    
  87. ' ZoomPickWindow
  88.     MsgBox "Perform a ZoomPickWindow", , "ZoomWindow Example"
  89.     ZoomPickWindow
  90.    
  91. ' ZoomCenter
  92.     MsgBox "Perform a ZoomCenter using:" & vbCrLf & _
  93.            "Center 3, 3, 0" & vbCrLf & _
  94.            "Magnification: 10", , "ZoomWindow Example"
  95.     Dim zcenter(0 To 2) As Double
  96.     Dim magnification As Double
  97.     zcenter(0) = 3: zcenter(1) = 3: zcenter(2) = 0
  98.     magnification = 10
  99.     ZoomCenter zcenter, magnification
  100.    
  101. End Sub


  102.   [/FONT]
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2003-6-14 15:29:26 | 显示全部楼层
Sub SelAll()
    On Error Resume Next
    Dim mySel As AcadSelectionSet
   
    ThisDrawing.SelectionSets("tempSel").Delete
    Set mySel = ThisDrawing.SelectionSets.Add("tempSel")
   
    mySel.Select acSelectionSetAll
    mySel.Highlight True        
End Sub

请Laoyao网友仔细讲讲“Hightlight”属性!我的英文太差了。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2003-6-14 16:38:47 | 显示全部楼层
HELP原文。
Sets the highlight status for the given object, or for all objects in a given selection set。

设定所给OBJECT或SELECTIONSET中所有OBJECT为高亮模式(就是选中后的成为虚线状的样子:6 )。

    ActiveDocument.ModelSpace.Item(1).Highlight True
   
    ActiveDocument.SelectionSets(1).Highlight True
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2003-6-14 18:53:02 | 显示全部楼层
最初由 Laoyao 发布
[B]HELP原文。
Sets the highlight status for the given object, or for all objects in a given selection set。

设定所给OBJECT或SELECTIONSET中所有OBJECT为高亮模式(就是选中后的成为虚线状的样子:6 )。
例... [/B]

谢谢!
On Error Resume Next
Dim mySel As AcadSelectionSet

ThisDrawing.SelectionSets("tempSel").Delete
Set mySel = ThisDrawing.SelectionSets.Add("tempSel")
这几行写的比我的方法(见那个水库的贴子)好,又学一招!
再次感谢Laoyao,向Laoyao学习!!!
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

已领礼包: 181个

财富等级: 日进斗金

发表于 2003-6-16 20:39:21 | 显示全部楼层
在程序中应该尽量少用On Error Resume Next,因为它有可能屏蔽之后产生的错误,使调试因难.
好的方法是先遍历整个选择集,如果选择集不存在,那么就创建一个.
当然如果这种方法繁杂的话,也可以单独编写一个过程或者函数来设置选择集.而在这段代码中加入错误处理机制,尽量避免将错误带到代码外的语句.
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

使用道具 举报

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

使用道具 举报

发表于 2003-11-20 20:15:48 | 显示全部楼层
不过如果VB中不用on error ,又是发生错误时就突然退出了,用了代码又大大增加了,每个sub都要加一段错误陷阱代码。

谁有什么好办法吗?能省点代码的。方便好用的。
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

发表于 2003-11-20 21:01:16 | 显示全部楼层
关于选择的操作,我转贴几个例程:(转自明经通道

  1. [FONT=courier new]
  2. ' 不断提示选择一个对象直到获取了对象或用户取消操作
  3. Public Sub GetEntityEx(ent As Object, pickedPoint, Optional Prompt)

  4.     On Error Resume Next
  5. StartLoop:
  6.     acadApp.ActiveDocument.Utility.GetEntity ent, pt, Prompt
  7.     If Err Then
  8.         If acadApp.ActiveDocument.GetVariable("errno") = 7 Then
  9.             Err.Clear
  10.             GoTo StartLoop
  11.         Else

  12.             Err.Raise vbObjectError + 5, , "用户取消操作"
  13.         End If
  14.     End If

  15. End Sub


  16. ' 返回一个空白选择集
  17. ' 它也可消除烦人的“选择集已经存在”问题

  18. Public Function CreateSelectionSet(Optional ssName As String = "ss") As AcadSelectionSet

  19.     Dim ss As AcadSelectionSet
  20.    
  21.     On Error Resume Next
  22.     Set ss = ThisDrawing.SelectionSets(ssName)
  23.     If Err Then Set ss = ThisDrawing.SelectionSets.Add(ssName)
  24.     ss.Clear
  25.     Set CreateSelectionSet = ss

  26. End Function

  27. ' 对象选择,可实现先选择后操作和先操作后选择
  28. ' 作者:郑立楷
  29. Function GetSelSet() As AcadSelectionSet
  30.     Dim ss As AcadSelectionSet
  31.     Dim ssName As String
  32.     ssName = "PICKFIRST"
  33.     On Error Resume Next
  34.     Set ss = ThisDrawing.SelectionSets.Add(ssName)
  35.     If Err Then
  36.         Set ss = ThisDrawing.SelectionSets(ssName)
  37.         ss.Delete
  38.     End If
  39.     Set ss = ThisDrawing.PickfirstSelectionSet
  40.     If ss.Count = 0 Then
  41.         Set ss = ThisDrawing.SelectionSets(ssName)
  42.         If Err Then Set ss = ThisDrawing.SelectionSets.Add(ssName)
  43.         ss.Clear
  44.         ss.SelectOnScreen
  45.     End If
  46.     Set GetSelSet = ss
  47. End Function


  48. [/FONT]
论坛插件加载方法
发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;
如果你在论坛求助问题,并且已经从坛友或者管理的回复中解决了问题,请把帖子标题加上【已解决】;
如何回报帮助你解决问题的坛友,一个好办法就是给对方加【D豆】,加分不会扣除自己的积分,做一个热心并受欢迎的人!
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-29 13:22 , Processed in 0.383940 second(s), 47 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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