编辑代码

using System;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;

[assembly: CommandClass(typeof(CADPlugin.RibbonButtons))]

namespace CADPlugin
{
    public class RibbonButtons
    {
        [CommandMethod("CreateRibbon")]
        public void CreateRibbon()
        {
            try
            {
                var ribbon = ComponentManager.Ribbon;
                var tab = new RibbonTab
                {
                    Title = "XuangPluginTab",
                    Id = "XuangPluginTab"
                };
                ribbon.Tabs.Add(tab);

                var panelSource = new RibbonPanelSource();
                var panel = new RibbonPanel
                {
                    Source = panelSource,
                    Title = "XuangPluginPanel"
                };
                tab.Panels.Add(panel);

                AddButton(panelSource, "插件介绍", "xuang-plugin");
                AddButton(panelSource, "打散属性块", "burst-block");
                AddButton(panelSource, "框选/全选", "frame-select");
                AddButton(panelSource, "阈值筛选", "filter-selection");
                AddButton(panelSource, "连接文字元件", "connect-text");
            }
            catch (Exception ex)
            {
                Application.ShowAlertDialog($"Error creating ribbon: {ex.Message}");
            }
        }

        private void AddButton(RibbonPanelSource panelSource, string buttonText, string command)
        {
            var button = new RibbonButton
            {
                Text = buttonText,
                CommandParameter = command,
                CommandHandler = new AdskCommandHandler(OnButtonClicked)
            };
            panelSource.Items.Add(button);
        }

        private void OnButtonClicked(object sender, CommandEventArgs e)
        {
            try
            {
                string command = e.GlobalCommandName;
                if (!string.IsNullOrEmpty(command))
                {
                    Document doc = Application.DocumentManager.MdiActiveDocument;
                    doc.SendStringToExecute($"{command} ", true, false, true);
                }
            }
            catch (Exception ex)
            {
                Application.ShowAlertDialog($"Error executing command: {ex.Message}");
            }
        }
    }
}