望前辈出手修改打包这些代码!!using Autodesk.AutoCAD.ApplicationServices;
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace CadPluginToolbar
{
public partial class PluginToolbar : Form
{
private TabControl tabControl;
private TextBox txtSearch;
private Button btnSettings, btnReload;
public PluginToolbar()
{
PluginManager.LoadPlugins();
InitializeComponent();
BuildInterface();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.Text = "插件工具栏";
this.Size = new Size(350, 700);
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - 370, 50);
this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
this.TopMost = true;
// 搜索和按钮区域
var topPanel = new Panel { Dock = DockStyle.Top, Height = 40 };
txtSearch = new TextBox { Location = new Point(10, 10), Size = new Size(200, 20), PlaceholderText = "搜索命令..." };
txtSearch.TextChanged += TxtSearch_TextChanged;
btnReload = new Button { Text = "🔄", Location = new Point(215, 10), Size = new Size(25, 20) };
btnReload.Click += BtnReload_Click;
btnSettings = new Button { Text = "⚙", Location = new Point(245, 10), Size = new Size(25, 20) };
btnSettings.Click += BtnSettings_Click;
topPanel.Controls.AddRange(new Control[] { txtSearch, btnReload, btnSettings });
// 选项卡区域
tabControl = new TabControl { Dock = DockStyle.Fill };
this.Controls.Add(tabControl);
this.Controls.Add(topPanel);
this.ResumeLayout(false);
}
private void BuildInterface()
{
tabControl.TabPages.Clear();
var categories = PluginManager.GetCategories();
var searchText = txtSearch.Text.ToLower();
foreach (var category in categories)
{
var commands = PluginManager.GetAllCommands()
.Where(c => c.Category == category)
.Where(c => string.IsNullOrEmpty(searchText) ||
c.Name.ToLower().Contains(searchText) ||
c.Description.ToLower().Contains(searchText))
.ToList();
if (commands.Any())
{
var tabPage = new TabPage(category);
BuildCommandPanel(tabPage, commands);
tabControl.TabPages.Add(tabPage);
}
}
// 添加所有命令选项卡
var allCommandsTab = new TabPage("所有命令");
var allCommands = PluginManager.GetAllCommands()
.Where(c => string.IsNullOrEmpty(searchText) ||
c.Name.ToLower().Contains(searchText) ||
c.Description.ToLower().Contains(searchText))
.ToList();
BuildCommandPanel(allCommandsTab, allCommands);
tabControl.TabPages.Add(allCommandsTab);
}
private void BuildCommandPanel(TabPage tabPage, System.Collections.Generic.List<PluginCommand> commands)
{
var flowPanel = new FlowLayoutPanel
{
Dock = DockStyle.Fill,
AutoScroll = true,
WrapContents = false,
FlowDirection = FlowDirection.TopDown
};
foreach (var cmd in commands.OrderBy(c => c.Name))
{
var button = new Button
{
Text = $"{cmd.Icon} {cmd.Name}",
Size = new Size(300, 40),
Margin = new Padding(5),
TextAlign = ContentAlignment.MiddleLeft,
Font = new Font("微软雅黑", 9),
Tag = cmd
};
var toolTip = new ToolTip();
toolTip.SetToolTip(button, $"{cmd.Description}\n命令: {cmd.Command}");
button.Click += (s, e) => ExecuteCommand(cmd);
flowPanel.Controls.Add(button);
}
tabPage.Controls.Add(flowPanel);
}
private void ExecuteCommand(PluginCommand cmd)
{
try
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (cmd.IsLisp && !string.IsNullOrEmpty(cmd.LispCode))
{
// 执行LISP代码
doc.SendStringToExecute($"(load \"{EscapeLisp(cmd.LispCode)}\") ", true, false, true);
}
else
{
// 执行CAD命令
doc.SendStringToExecute(cmd.Command + " ", true, false, true);
}
}
catch (Exception ex)
{
MessageBox.Show($"执行命令失败: {ex.Message}");
}
}
private string EscapeLisp(string lispCode)
{
return lispCode.Replace("\"", "\\\"");
}
private void TxtSearch_TextChanged(object sender, EventArgs e)
{
BuildInterface();
}
private void BtnReload_Click(object sender, EventArgs e)
{
PluginManager.LoadPlugins();
BuildInterface();
}
private void BtnSettings_Click(object sender, EventArgs e)
{
var editor = new PluginEditor();
editor.ShowDialog();
BuildInterface();
}
}
}
加载命令:
[CommandMethod("PTOOL")]
public static void ShowPluginToolbar()
{
var toolbar = new PluginToolbar();
Application.ShowModelessDialog(toolbar);
}
[CommandMethod("PEDIT")]
public static void ShowPluginEditor()
{
var editor = new PluginEditor();
Application.ShowModalDialog(editor);
}
网友答: 又是AI写的代码,你就拿这个考验老干部呢,哪个老干部经受不住这样的考验网友答: 跑到lisp区发c#的东西,没礼貌网友答: 弄好了;你可以试试看
我的cad是2026的 不支持net4.8;请你自测
网友答: cad2026的版本亲测可用
2025应该也是支持的
using System;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
namespace CadPluginToolbar
{
public partial class PluginToolbar : Form
{
private TabControl tabControl;
private TextBox txtSearch;
private Button btnSettings, btnReload;
public PluginToolbar()
{
PluginManager.LoadPlugins();
InitializeComponent();
BuildInterface();
}
private void InitializeComponent()
{
this.SuspendLayout();
this.Text = "插件工具栏";
this.Size = new Size(350, 700);
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(Screen.PrimaryScreen.WorkingArea.Right - 370, 50);
this.FormBorderStyle = FormBorderStyle.SizableToolWindow;
this.TopMost = true;
// 搜索和按钮区域
var topPanel = new Panel { Dock = DockStyle.Top, Height = 40 };
txtSearch = new TextBox { Location = new Point(10, 10), Size = new Size(200, 20), PlaceholderText = "搜索命令..." };
txtSearch.TextChanged += TxtSearch_TextChanged;
btnReload = new Button { Text = "🔄", Location = new Point(215, 10), Size = new Size(25, 20) };
btnReload.Click += BtnReload_Click;
btnSettings = new Button { Text = "⚙", Location = new Point(245, 10), Size = new Size(25, 20) };
btnSettings.Click += BtnSettings_Click;
topPanel.Controls.AddRange(new Control[] { txtSearch, btnReload, btnSettings });
// 选项卡区域
tabControl = new TabControl { Dock = DockStyle.Fill };
this.Controls.Add(tabControl);
this.Controls.Add(topPanel);
this.ResumeLayout(false);
}
private void BuildInterface()
{
tabControl.TabPages.Clear();
var categories = PluginManager.GetCategories();
var searchText = txtSearch.Text.ToLower();
foreach (var category in categories)
{
var commands = PluginManager.GetAllCommands()
.Where(c => c.Category == category)
.Where(c => string.IsNullOrEmpty(searchText) ||
c.Name.ToLower().Contains(searchText) ||
c.Description.ToLower().Contains(searchText))
.ToList();
if (commands.Any())
{
var tabPage = new TabPage(category);
BuildCommandPanel(tabPage, commands);
tabControl.TabPages.Add(tabPage);
}
}
// 添加所有命令选项卡
var allCommandsTab = new TabPage("所有命令");
var allCommands = PluginManager.GetAllCommands()
.Where(c => string.IsNullOrEmpty(searchText) ||
c.Name.ToLower().Contains(searchText) ||
c.Description.ToLower().Contains(searchText))
.ToList();
BuildCommandPanel(allCommandsTab, allCommands);
tabControl.TabPages.Add(allCommandsTab);
}
private void BuildCommandPanel(TabPage tabPage, System.Collections.Generic.List<PluginCommand> commands)
{
var flowPanel = new FlowLayoutPanel
{
Dock = DockStyle.Fill,
AutoScroll = true,
WrapContents = false,
FlowDirection = FlowDirection.TopDown
};
foreach (var cmd in commands.OrderBy(c => c.Name))
{
var button = new Button
{
Text = $"{cmd.Icon} {cmd.Name}",
Size = new Size(300, 40),
Margin = new Padding(5),
TextAlign = ContentAlignment.MiddleLeft,
Font = new Font("微软雅黑", 9),
Tag = cmd
};
var toolTip = new ToolTip();
toolTip.SetToolTip(button, $"{cmd.Description}\n命令: {cmd.Command}");
button.Click += (s, e) => ExecuteCommand(cmd);
flowPanel.Controls.Add(button);
}
tabPage.Controls.Add(flowPanel);
}
private void ExecuteCommand(PluginCommand cmd)
{
try
{
Document doc = Application.DocumentManager.MdiActiveDocument;
if (cmd.IsLisp && !string.IsNullOrEmpty(cmd.LispCode))
{
// 执行LISP代码
doc.SendStringToExecute($"(load \"{EscapeLisp(cmd.LispCode)}\") ", true, false, true);
}
else
{
// 执行CAD命令
doc.SendStringToExecute(cmd.Command + " ", true, false, true);
}
}
catch (Exception ex)
{
MessageBox.Show($"执行命令失败: {ex.Message}");
}
}
private string EscapeLisp(string lispCode)
{
return lispCode.Replace("\"", "\\\"");
}
private void TxtSearch_TextChanged(object sender, EventArgs e)
{
BuildInterface();
}
private void BtnReload_Click(object sender, EventArgs e)
{
PluginManager.LoadPlugins();
BuildInterface();
}
private void BtnSettings_Click(object sender, EventArgs e)
{
var editor = new PluginEditor();
editor.ShowDialog();
BuildInterface();
}
}
}
加载命令:
[CommandMethod("PTOOL")]
public static void ShowPluginToolbar()
{
var toolbar = new PluginToolbar();
Application.ShowModelessDialog(toolbar);
}
[CommandMethod("PEDIT")]
public static void ShowPluginEditor()
{
var editor = new PluginEditor();
Application.ShowModalDialog(editor);
}
网友答: 又是AI写的代码,你就拿这个考验老干部呢,哪个老干部经受不住这样的考验网友答: 跑到lisp区发c#的东西,没礼貌网友答: 弄好了;你可以试试看
我的cad是2026的 不支持net4.8;请你自测
网友答: cad2026的版本亲测可用
2025应该也是支持的