AutoCAD VBA添加菜单
# 给cad添加自定义菜单

1 Private Sub AddBar() 2 Dim NewMenuItem As AcadPopupMenuItem 3 Dim TheMacro As String 4 Dim MI As Integer 5 6 On Error Resume Next 7 Dim currMenuGroup As AcadMenuGroup 8 Set currMenuGroup = Application.MenuGroups.Item(0) 9 'Create the new menu 10 Set NewMenu = currMenuGroup.Menus.Add("批量绘图") 11 If Err.Number Then 12 Err.Clear 13 For Each NewMenu In currMenuGroup.Menus 14 If NewMenu.Name = "批量绘图" Then Exit For 15 Next 16 End If 17 18 'Add a menu item to the new menu 19 'Assign the macro string the VB equivalent of "ESC ESC _open " 20 'TheMacro = Chr(3) & Chr(3) & Chr(95) & "-vbarun ""GeoSection.dvb!DZPM.GeoSection""" & Chr(32) 21 TheMacro = Chr(3) & Chr(3) & Chr(95) & "-vbarun ""MainSub""" & Chr(32) 22 Set NewMenuItem = NewMenu.AddMenuItem(NewMenu.Count + 1, "批量绘图", TheMacro) 23 If Err.Number Then Err.Clear 24 'Display the menu on the menu bar 25 NewMenu.InsertInMenuBar (ThisDrawing.Application.MenuBar.Count + 1) 26 If Err.Number Then Err.Clear 27 End Sub
写几个事件驱动菜单显示

1 Private Sub AcadDocument_BeginCommand(ByVal CommandName As String) 2 If StrComp(Left$(CommandName, 3), "VBA", 1) <> 0 And UCase$(CommandName) <> "APPLOAD" Then Exit Sub 3 If NewMenu Is Nothing Then AddBar 4 End Sub 5 6 Private Sub AcadDocument_EndCommand(ByVal CommandName As String) 7 If StrComp(Left$(CommandName, 3), "VBA", 1) <> 0 And UCase$(CommandName) <> "APPLOAD" Then Exit Sub 8 If NewMenu Is Nothing Then AddBar 9 End Sub 10 11 Public Sub MainSub() 12 Dim frm As New UserForm1 13 Call UserForm1.Show 14 End Sub
最终效果
增加c# 调用的代码
1 [Autodesk.AutoCAD.Runtime.CommandMethod("QIM")] 2 public void AddMenuBar() 3 { 4 dynamic comAcadApp = Autodesk.AutoCAD.ApplicationServices.Application.AcadApplication; 5 int icount = comAcadApp.MenuGroups.Item(0).Menus.Count; 6 for (int i = 0; i < icount; i++) 7 { 8 if (comAcadApp.MenuGroups.Item(0).Menus.Item(i).Name=="测试菜单") 9 { 10 Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("\"测试菜单\" MENU 已经存在了!"); 11 return; 12 } 13 } 14 dynamic pmParent = comAcadApp.MenuGroups.Item(0).Menus.Add("测试菜单"); 15 dynamic pm = pmParent.AddSubMenu(pmParent.Count + 1, "OPEN_CLOSE"); 16 17 dynamic pmi0 = pmParent.AddMenuItem(pmParent.Count + 1, "退出","Exit "); 18 19 dynamic pmi1 = pm.AddMenuItem(pm.Count + 1, "打开", "Open "); 20 dynamic pmi2 = pm.AddMenuItem(pm.Count + 1, "打开02","Close "); 21 pmParent.InsertInMenuBar(comAcadApp.MenuBar.Count + 1); 22 Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("\"测试菜单\" MENU 添加成功!"); 23 }
标签:
Autocad VBA.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!