pmq 发表于 2024-10-24 19:37:19

VB.NET [源码] CAD顶部菜单和停靠面板 侧边栏菜单

本帖最后由 pmq 于 2025-4-19 15:48 编辑

VB.NET [源码] CAD顶部下拉菜单,可以自定义配置文件
停靠面板侧边栏菜单
开发环境 Microsoft Visual Studio 2022, AutoCAD2026
也可以用其它版本,只是引用对应的CAD的三个运行库
accoremgd、acdbmgd、acmgd

抖音搜索用户 GCPX515 查看使用视频:












2024.11.08更新



Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.Windows
Imports System.IO
Imports System.Windows.Forms

Public Class NestedToolbarMenu

    ' 定义命令和宏的配置类
    Public Class CommandConfig
      Public Property Title As String
      Public Property Macro As String
      Public Property SubCommands As List(Of CommandConfig) ' 子菜单
    End Class

    ' 主方法:创建嵌套菜单形式的 PaletteSet
    <CommandMethod("CreateNestedToolbar")>
    Public Sub CreateNestedToolbar()
      ' 配置文件路径
      Dim filePath As String = "C:\Asur\System\commands.txt"
      Dim commands As List(Of CommandConfig) = LoadNestedCommandsFromTextFile(filePath)

      If commands Is Nothing OrElse commands.Count = 0 Then
            MsgBox("未在配置文件中找到有效命令。")
            Exit Sub
      End If

      ' 创建 PaletteSet(可停靠窗口)
      Dim paletteSet As New PaletteSet("Custom Toolbar") With {
            .Style = PaletteSetStyles.ShowPropertiesMenu Or PaletteSetStyles.ShowAutoHideButton, ' 移除了无效的 Pinned 样式
            .MinimumSize = New Drawing.Size(1200, 25)
      }

      ' 创建一个 Panel 来容纳工具条
      Dim panel As New Panel() With {
            .Dock = DockStyle.Fill
      }

      ' 创建工具条
      Dim toolStrip As New ToolStrip() With {
            .Dock = DockStyle.Top
      }

      ' 遍历命令列表,添加到工具条
      For Each command In commands
            Dim dropDownButton As New ToolStripDropDownButton() With {
                .Text = command.Title
            }

            ' 如果有子命令,添加到下拉菜单中
            If command.SubCommands IsNot Nothing AndAlso command.SubCommands.Count > 0 Then
                For Each subCommand In command.SubCommands
                  Dim subButton As New ToolStripMenuItem() With {
                        .Text = subCommand.Title
                  }

                  ' 设置子命令的点击事件
                  AddHandler subButton.Click, Sub(sender, e)
                                                    Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.SendStringToExecute(subCommand.Macro & vbCr, True, False, False)
                                                End Sub

                  dropDownButton.DropDownItems.Add(subButton)
                Next
            End If

            toolStrip.Items.Add(dropDownButton)
      Next

      ' 将工具条添加到 Panel
      panel.Controls.Add(toolStrip)

      ' 将 Panel 添加到 PaletteSet
      paletteSet.Add("Nested Toolbar", panel)
      paletteSet.Size = New Drawing.Size(1200, 25)
      paletteSet.Dock = DockSides.Top'面板停靠在窗口的顶部
      ' 显示 PaletteSet
      paletteSet.Visible = True
    End Sub

    ' 从文本文件加载嵌套命令配置
    Private Function LoadNestedCommandsFromTextFile(filePath As String) As List(Of CommandConfig)
      Dim commands As New List(Of CommandConfig)
      Dim currentParent As CommandConfig = Nothing

      Try
            If File.Exists(filePath) Then
                Dim lines = File.ReadAllLines(filePath)

                For Each line In lines
                  ' 跳过空行和注释
                  If String.IsNullOrWhiteSpace(line) OrElse line.StartsWith("#") Then Continue For

                  ' 判断是否是子命令(通过缩进)
                  If line.StartsWith("    ") Then ' 检测 4 个空格的缩进
                        ' 子命令
                        Dim parts = line.Trim().Split("|"c)
                        If parts.Length = 2 AndAlso currentParent IsNot Nothing Then
                            currentParent.SubCommands.Add(New CommandConfig() With {
                              .Title = parts(0).Trim(),
                              .Macro = parts(1).Trim()
                            })
                        End If
                  Else
                        ' 主命令
                        Dim parts = line.Split("|"c)
                        If parts.Length = 2 Then
                            Dim parentCommand As New CommandConfig() With {
                              .Title = parts(0).Trim(),
                              .Macro = parts(1).Trim(),
                              .SubCommands = New List(Of CommandConfig)()
                            }
                            commands.Add(parentCommand)
                            currentParent = parentCommand
                        End If
                  End If
                Next
            Else
                MsgBox("未找到配置文件:" & filePath)
            End If
      Catch ex As Exception
            MsgBox("读取配置文件时出错:" & ex.Message)
      End Try

      Return commands
    End Function

End Class

'commands.txt
配置文件格式
'===========================

# 主菜单: 绘图
绘图|
    绘直线 Line|_.LINE
    展点 ZD|_.POINT

# 主菜单: 编辑
编辑|
    打断 Br|_.BREAK
    分解 EX|_.EXPLODE


pmq 发表于 2024-10-25 08:57:35

本帖最后由 pmq 于 2024-10-25 14:17 编辑

p-3-ianlcc 发表于 2024-10-24 23:39
请问这个有包含里面的工具插件吗?
还是只是菜单呢?
另外请教有没有2016~2018的版本呢?
ACeL2025.Rar是插件
Asur.Rar是vb.net写的菜单
我这电脑没安装低版本的CAD

5.89 s@R.Kw 03/31 fBt:/ 测量绘图计算工具https://v.douyin.com/iSPKByVY/ 复制此链接,打开Dou音搜索,直接观看视频!
这里有使用视频

pmq 发表于 2024-10-29 18:04:16

zyx1029 发表于 2024-10-29 16:32
谁能告诉一下我这菜鸟    这种程序这么加载到CAD中使用了?DLL那个文件会加载了,菜单文件是放到那里了了? ...


Sur.rar 是自己编程用的一个菜单示例文件
ACeL2025.rar 是一个插件
使用视频抖音
https://www.douyin.com/user/self?from_tab_name=main&showTab=post

zyx1029 发表于 2024-10-31 10:18:03

pmq 发表于 2024-10-29 18:04
Sur.rar 是自己编程用的一个菜单示例文件
ACeL2025.rar 是一个插件
使用视频抖音


插件加到CAD里了,但是你发的源码用Blend for Visual Studio 2022打开后也看不懂,太高级了!插件的代码也没找到在哪里写的了!这么看还是lisp简单一点!

bai2000 发表于 2024-10-24 20:11:53

版本太高了,有低点版本么?

pmq 发表于 2024-10-24 20:14:26

本帖最后由 pmq 于 2024-10-25 14:16 编辑

bai2000 发表于 2024-10-24 20:11
版本太高了,有低点版本么?
CAD什么版本?这个可以运行在 Auto CAD 2019--2025 64位下
5.89 s@R.Kw 03/31 fBt:/ 测量绘图计算工具https://v.douyin.com/iSPKByVY/ 复制此链接,打开Dou音搜索,直接观看视频!

p-3-ianlcc 发表于 2024-10-24 23:39:54

请问这个有包含里面的工具插件吗?
还是只是菜单呢?
另外请教有没有2016~2018的版本呢?

lxl304712346 发表于 2024-10-25 00:02:47

支持!!!!!!!!开发环境是什么

shz9 发表于 2024-10-25 07:36:33

这个功能栏具体功能展示一下看看

yefei812678 发表于 2024-10-25 07:59:10

谢谢分享谢谢分享谢谢分享

pmq 发表于 2024-10-25 08:50:26

本帖最后由 pmq 于 2024-10-25 14:15 编辑

shz9 发表于 2024-10-25 07:36
这个功能栏具体功能展示一下看看
5.89 s@R.Kw 03/31 fBt:/ 测量绘图计算工具https://v.douyin.com/iSPKByVY/ 复制此链接,打开Dou音搜索,直接观看视频!
这里有使用视频

潘成祥2015 发表于 2024-10-25 08:58:40

VB不懂,能教怎么使用吗
页: [1] 2 3
查看完整版本: VB.NET [源码] CAD顶部菜单和停靠面板 侧边栏菜单