明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2432|回复: 9

[VBA]:类似录制宏的VBA代码示例。

[复制链接]
发表于 2003-12-3 23:12:00 | 显示全部楼层 |阅读模式

  1. 第一步,引用:Microsoft Visual Basic for Applications Extensibility 5.3类型库,这是用于扩展VBA功能的组件。
  2. 第二步,在ThisDraiwntg中添加事件的监控,主要有对象的增加、修改、删除操作。以下只检测对象的创建,也只对直张的创建进行监控,当直线创建时,自动往工程中添加一个过程。
  3. Private Sub AcadDocument_ObjectAdded(ByVal Object As Object)
  4.     Select Case Object.ObjectName
  5.         Case "AcDbLine"
  6.             Dim lineObj As AcadLine
  7.             Set lineObj = Object
  8.             Dim ComponentObj As VBComponent
  9.             Set ComponentObj = GetVBComponent(vbext_ct_StdModule)
  10.             If ComponentObj Is Nothing Then
  11.                 Set ComponentObj = Application.VBE.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule)
  12.             End If
  13.             With ComponentObj.CodeModule
  14.                 Dim s As String
  15.                 s = "" & vbCrLf
  16.                 s = "Sub 宏_Line" & GetProcCount(ComponentObj.CodeModule) + 1 & "()" & vbCrLf
  17.                 s = s & "" & vbCrLf
  18.                 s = s & "    ' 宏_Line" & GetProcCount(ComponentObj.CodeModule) + 1 & vbCrLf
  19.                 s = s & "    ' efan2000 记录的宏" & vbCrLf
  20.                 s = s & "" & vbCrLf
  21.                 s = s & "    Dim lineObj As AcadLine" & vbCrLf
  22.                 s = s & "    Dim startPoint(0 To 2) As Double" & vbCrLf
  23.                 s = s & "    Dim endPoint(0 To 2) As Double" & vbCrLf
  24.                 s = s & "" & vbCrLf
  25.                 s = s & "    ' 定义直线的起点和终点" & vbCrLf
  26.                 s = s & "    startPoint(0) = " & lineObj.startPoint(0) & ": startPoint(1) =  " & lineObj.startPoint(1) & ": startPoint(2) = 0" & vbCrLf
  27.                 s = s & "    endPoint(0) = " & lineObj.endPoint(0) & ": endPoint(1) = " & lineObj.endPoint(1) & ": endPoint(2) = 0" & vbCrLf
  28.                 s = s & "" & vbCrLf
  29.                 s = s & "    ' 在模型空间创建直线" & vbCrLf
  30.                 s = s & "    Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)" & vbCrLf
  31.                 s = s & "    Set lineObj = Nothing" & vbCrLf
  32.                 s = s & "End Sub" & vbCrLf
  33.                 .InsertLines .CountOfLines + 1, s
  34.             End With
  35.     End Select
  36. End Sub

  37. '返回当前工程的第一个模块
  38. Public Function GetVBComponent(ByVal ComponentType As vbext_ComponentType) As VBComponent
  39.     Dim i As Integer
  40.     For i = 1 To Application.VBE.ActiveVBProject.VBComponents.Count
  41.         If Application.VBE.ActiveVBProject.VBComponents(i).Type = ComponentType Then
  42.             Set GetVBComponent = Application.VBE.ActiveVBProject.VBComponents(i)
  43.             Exit For
  44.         End If
  45.     Next
  46. End Function

  47. '返回模块中的过程数目
  48. Public Function GetProcCount(ByVal CMObj As CodeModule) As Integer
  49.     Dim i As Integer
  50.     Dim s  As String
  51.     For i = 1 To CMObj.CountOfLines
  52.         If InStr(1, s, CMObj.ProcOfLine(i, vbext_pk_Proc), vbTextCompare) = 0 Then
  53.             s = s & CMObj.ProcOfLine(i, vbext_pk_Proc) & ";"
  54.         End If
  55.     Next
  56.     If s = "" Then Exit Function
  57.     s = Left(s, Len(s) - 1)
  58.     Dim v As Variant
  59.     v = Split(s, ";")
  60.     If Not IsEmpty(v) Then
  61.         GetProcCount = UBound(v) + 1
  62.     End If
  63. End Function

  64. 第三步,这是在事件中自动创建的代码结果。
  65. Sub 宏_Line1()

  66.     ' 宏_Line1
  67.     ' efan2000 记录的宏

  68.     Dim lineObj As AcadLine
  69.     Dim startPoint(0 To 2) As Double
  70.     Dim endPoint(0 To 2) As Double

  71.     ' 定义直线的起点和终点
  72.     startPoint(0) = 83.7160125997048: startPoint(1) = 206.265137503404: startPoint(2) = 0
  73.     endPoint(0) = 259.874137686561: endPoint(1) = 243.32201842691: endPoint(2) = 0

  74.     ' 在模型空间创建直线
  75.     Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)
  76.     Set lineObj = Nothing
  77. End Sub

  78. Sub 宏_Line2()

  79.     ' 宏_Line2
  80.     ' efan2000 记录的宏

  81.     Dim lineObj As AcadLine
  82.     Dim startPoint(0 To 2) As Double
  83.     Dim endPoint(0 To 2) As Double

  84.     ' 定义直线的起点和终点
  85.     startPoint(0) = 259.874137686561: startPoint(1) = 243.32201842691: startPoint(2) = 0
  86.     endPoint(0) = 158.433236658299: endPoint(1) = 136.511009275691: endPoint(2) = 0

  87.     ' 在模型空间创建直线
  88.     Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)
  89.     Set lineObj = Nothing
  90. End Sub

  91. Sub 宏_Line3()

  92.     ' 宏_Line3
  93.     ' efan2000 记录的宏

  94.     Dim lineObj As AcadLine
  95.     Dim startPoint(0 To 2) As Double
  96.     Dim endPoint(0 To 2) As Double

  97.     ' 定义直线的起点和终点
  98.     startPoint(0) = 158.433236658299: startPoint(1) = 136.511009275691: startPoint(2) = 0
  99.     endPoint(0) = 328.59216866062: endPoint(1) = 155.039449391691: endPoint(2) = 0

  100.     ' 在模型空间创建直线
  101.     Set lineObj = ThisDrawing.ModelSpace.AddLine(startPoint, endPoint)
  102.     Set lineObj = Nothing
  103. End Sub

  104. 这仅仅是一个简单的功能,如果能够加以扩充,完全可以实现如在Excel中的录制宏的效果。
发表于 2003-12-4 11:15:00 | 显示全部楼层
厉害厉害~~~~~~
发表于 2005-10-23 17:17:00 | 显示全部楼层

这真是个绝好的贴字

如果能够实现录制宏的功能

我们就不用花很多时间了

只要对宏进行修改就可以了

希望大家都来顶一下!

发表于 2005-10-23 22:11:00 | 显示全部楼层

鼓掌中!!!

发表于 2005-10-24 12:09:00 | 显示全部楼层
不错!
发表于 2005-10-24 14:12:00 | 显示全部楼层

不明白ACAD中为什么不提供这样的功能.

发表于 2005-10-24 18:46:00 | 显示全部楼层
不知具体怎样实现(我没有看懂)?
发表于 2005-10-26 11:57:00 | 显示全部楼层

没看懂,希望楼主讲明具体怎么实现.前面几个鼓掌的看懂了?厉害!比楼主还厉害!

发表于 2005-10-30 22:23:00 | 显示全部楼层
这可是个好贴子哦
发表于 2005-10-31 17:02:00 | 显示全部楼层

看了这个帖子,知道什么是差距了.

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

本版积分规则

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

GMT+8, 2025-2-22 05:21 , Processed in 0.159493 second(s), 23 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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