C#+Visual Studio 2022为AutoCAD 2022开发插件并显示在Ribbon选项卡

1.插件功能开发

(1)建立C#类库项目,添加必要引用,都是autocad二次开发相关的,要注意对引用的库修改其"复制文件"属性为false

(2)项目调试使用"属性"打开"启用外部程序",指定为机器上autocad2022的acad.exe所在路径。

(3)建立一个新类,开发功能。

CommandMethod("CheckArea",CommandFlags.UsePickSet)\] //指令名称 public void CheckArea() { // 获取当前文档和数据库 Document doc =Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument; Database db = HostApplicationServices.WorkingDatabase; Editor ed = doc.Editor; //插件功能代码 } 2.Ribbon选显卡功能开发 在类里面添加一个方法: \[CommandMethod("RibbonDemo")

public void RibbonDemo()

{

// 获取程序集的加载路径

string currentDLLLocation = Path.GetDirectoryName(this.GetType().Assembly.Location) + "\\";

// 获取cad的Ribbon界面

RibbonControl ribbonCtrl = ComponentManager.Ribbon;

// 添加选项卡

RibbonTab tab = new RibbonTab();

tab.Title = "老旧小区插建房屋日照测算";

tab.Id = "Acad.RibbonId1";

tab.IsActive = true;

ribbonCtrl.Tabs.Add(tab);

// 给选项卡添加面板

RibbonPanelSource panelSource = new RibbonPanelSource();

panelSource.Title = "日照测算";

RibbonPanel ribbonPanel = new RibbonPanel();

ribbonPanel.Source = panelSource;

tab.Panels.Add(ribbonPanel);

// 命令按钮

RibbonButton pLineBtn = new RibbonButton();

string imgFileNameLineBtn = string.Format("{0}\\{1}\\{2}.png", currentDLLLocation, "Images", "big");

Uri uriLineBtn = new Uri(imgFileNameLineBtn);

BitmapImage bitmapImgeLineBtn = new BitmapImage(uriLineBtn);

// 按钮图片 16 * 16

pLineBtn.Image = bitmapImgeLineBtn;

// 按钮图片 32 * 32

pLineBtn.LargeImage = bitmapImgeLineBtn;

pLineBtn.ShowImage = true;

pLineBtn.Size = RibbonItemSize.Large;

pLineBtn.Orientation = System.Windows.Controls.Orientation.Vertical;

pLineBtn.Text = "面积测算";

pLineBtn.ShowText = true;

//添加提示对象

RibbonToolTip toolTipLine = new RibbonToolTip();

toolTipLine.Title = "日照测算";

toolTipLine.Content = "根据选定的区域测算插建日照房数量和面积是否达标";

toolTipLine.Command = "CHECKAREA";

toolTipLine.ExpandedContent = "CheckArea命令是一个自定义插件,其根据选定的区域测算插建日照房数量和面积是否达标。";

string imgToolTipFileNameLineBtn = string.Format("{0}\\{1}\\{2}.png", currentDLLLocation, "Images", "big");

Uri toolTipUriLineBtn = new Uri(imgToolTipFileNameLineBtn);

BitmapImage toolTipBitmapImgeLineBtn = new BitmapImage(toolTipUriLineBtn);

toolTipLine.ExpandedImage = toolTipBitmapImgeLineBtn;

pLineBtn.ToolTip = toolTipLine;

// 给按钮关联命令

pLineBtn.CommandHandler = new RibbonCommandHandler();

pLineBtn.CommandParameter = "CheckArea" + " ";

// 添加直线命令按钮

panelSource.Items.Add(pLineBtn);

}

3.配置插件自启动

在"C:\Program Files\Autodesk\ApplicationPlugins"目录下创建文件夹 " RibbonUI.bundle ",将生成的 debug 文件夹(包含Image)拷贝入 " RibbonUI.bundle " ,在此文件夹中,新建PackageContents.xml,填入以下代码

<?xml version="1.0" encoding="utf-8" ?>

<ApplicationPackage SchemaVersion="1.0" AppVersion="1.0"

ProductCode="{xxx}"

Name="ribbon测试插件"

>

<CompanyDetails

Name="****"

Email="****"

/>

<RuntimeRequirements OS="Win32|Win64" Platform="AutoCAD*" />

<Components>

<ComponentEntry ModuleName="./debug/XXXX.dll">

<Commands>

<Command Local="RibbonDemo" Global="RibbonDemo" StartupCommand="True"/>

</Commands>

</ComponentEntry>

</Components>

</ApplicationPackage>

参考链接:https://blog.csdn.net/wang2543311/article/details/124477198

相关推荐
Kyln.Wu2 小时前
【python实用小脚本-187】Python一键批量改PDF文字:拖进来秒出新文件——再也不用Acrobat来回导
python·pdf·c#
hqwest2 小时前
C#WPF实战出真汁01--项目介绍
开发语言·c#·wpf
小牛壮士3 小时前
Tokenizer(切词器)的不同实现算法
开发语言·算法·c#
Dm_dotnet1 天前
WPF Stylet可以如何实现导航功能?
c#
谷宇.1 天前
【Unity3D实例-功能-移动】角色行走和奔跑的相互切换
游戏·unity·c#·unity3d·游戏开发·游戏编程
羊子雄起1 天前
GitHub宕机时的协作方案
运维·vscode·github·visual studio
ts码农2 天前
Aspose使用
c#·aspose
晨非辰2 天前
#C语言——学习攻略:自定义类型路线--结构体--结构体类型,结构体变量的创建和初始化,结构体内存对齐,结构体传参,结构体实现位段
c语言·开发语言·经验分享·学习·其他·学习方法·visual studio
专注VB编程开发20年2 天前
c#,vb.net全局多线程锁,可以在任意模块或类中使用,但尽量用多个锁提高效率
java·前端·数据库·c#·.net
岩屿2 天前
.NET 应用程序 Linux下守护进程脚本编写
linux·运维·服务器·c#·.net