CAD插件程序 开发
凡是根据参数生成固定图形的,CAD插件程序都可以胜任。
效果展示:
1,命令行:
2,右键菜单:
3,工具条,CAD内嵌界面,和winform一样简单
闲话少说,直接上原理和代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | 0, 项目效果查看: a:在CAD2006的命令行输入netload,加载Test.dll b1:在命令行输入helloworld可以看到命令功能 b2:右键可以看到右键菜单,画一个红色的圆 b3:左边工具面板多了一个工具条,有个界面可以输入各种参数来画一个组合图形 1,建一个xindows窗体程序项目,设置输出为类库 2,引用acdbmgd.dll和acmgd.dll 3,引用如下命名空间 using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Windows.ToolPalette; using Autodesk.AutoCAD.Windows; 4,在Form1上规划界面和后台代码。重点看代码如何实现 5,计划是CAD上增加一个面板按钮,点击按钮就打开Form1来自动画图。 这里我们加一个用户控件,拖一个按钮,按钮方法写: Form1 modalForm = new Form1(); Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(modalForm); 注意这里仅仅是打开的窗体的方式选择了AutoCAD的方式。 想建立一整套工具栏,就多拉几个按钮。按4的方法写好每个按钮执行的代码(可以要个Form1这样的界面,也可以不要,自由选择) 6,把这个项目导出还需要一个类来辅助 先看初始化Initialize()和Terminate()方法。这里给CAD加了一个面板工具栏和右键菜单,以及一些命令行 |
1,这个class1类分别演示了命令行、右键菜单、工具条的实现。注释得非常清楚了,就不做多解释。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | using System; using System.Collections.Generic; using System.Linq; using System.Text; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Windows.ToolPalette; using Autodesk.AutoCAD.Windows; namespace Test { public class Class1 : Autodesk.AutoCAD.Runtime.IExtensionApplication { ContextMenuExtension m_ContextMenu; //定义右键菜单 PaletteSet palSet; //定义工具栏按钮 //初始化方法,这里加了一个面板工具栏和右键菜单。如果不要右键菜单,注释即可 public void Initialize() { AddContextMenu(); //添加面板工具栏 AddPalette(); //添加右键菜单 } //卸载方法 public void Terminate() { RemoveContextMenu(); } //有CommandMethod标注,是提供给CAD使用的命令 [CommandMethod( "HelloWorld" )] public void HelloWorld() { //这段代码的作用是弹出一个提示 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage( "深渊码奴编写:第一个外部门程序CAD!" ); } #region 添加一个右键菜单,并实现画一个圆的功能 /// <summary>点击响应事件,创建一个圆 /// /// </summary> /// <param name="o"></param> /// <param name="e"></param> private void MyMenuItem_OnClick( object o, EventArgs e) { using (DocumentLock doclock = Application.DocumentManager.MdiActiveDocument.LockDocument()) { //创建一个红色的圆 Database db = HostApplicationServices.WorkingDatabase; using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; Circle cir = new Circle( new Point3d(10, 10, 0), Vector3d.ZAxis, 100); cir.ColorIndex = 1; btr.AppendEntity(cir); trans.AddNewlyCreatedDBObject(cir, true ); trans.Commit(); } } } /// <summary>添加右键菜单项 /// /// </summary> private void AddContextMenu() { m_ContextMenu = new ContextMenuExtension(); m_ContextMenu.Title = "深渊码奴的自定义菜单" ; Autodesk.AutoCAD.Windows.MenuItem mi; mi = new Autodesk.AutoCAD.Windows.MenuItem( "创建圆" ); //关联菜单项的处理函数 mi.Click += MyMenuItem_OnClick; m_ContextMenu.MenuItems.Add(mi); Application.AddDefaultContextMenuExtension(m_ContextMenu); } /// <summary>移除菜单项 /// /// </summary> private void RemoveContextMenu() { if (m_ContextMenu != null ) { Application.RemoveDefaultContextMenuExtension(m_ContextMenu); m_ContextMenu = null ; } } #endregion [CommandMethod( "ShowModalForm" )] public void ShowModalForm() { Form1 modalForm = new Form1(); Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(modalForm); } [CommandMethod( "AddPalette" )] public void AddPalette() { Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor; try { if (palSet == null ) { palSet = new Autodesk.AutoCAD.Windows.PaletteSet( "我的面板集" ); palSet.Style = PaletteSetStyles.ShowTabForSingle; palSet.Style = PaletteSetStyles.NameEditable; palSet.Style = PaletteSetStyles.ShowPropertiesMenu; palSet.Style = PaletteSetStyles.ShowAutoHideButton; palSet.Style = PaletteSetStyles.ShowCloseButton; palSet.Opacity = 90; palSet.MinimumSize = new System.Drawing.Size(300, 300); System.Windows.Forms.UserControl myPageCtrl = new ModelessForm(); //注意这里是加载自己写的用户控件 //myPageCtrl.Dock = System.Windows.Forms.DockStyle.Fill; palSet.Add( "我的页面" , myPageCtrl); palSet.Visible = true ; } } catch { ed.WriteMessage( "创建面板集错误" ); } } } } |
2,CAD展现工具条,添加一个用户控件,取名为ModelessForm。两行代码显示Form1窗体而已。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; namespace Test { public partial class ModelessForm : UserControl { public ModelessForm() { InitializeComponent(); } private void button1_Click( object sender, EventArgs e) { Form1 modalForm = new Form1(); Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(modalForm); } } } |
3,Form1窗体的功能,是根据长宽画一个矩形,矩形的四个角分别画一个圆。因为在CAD里运行,所以使用的类库和正常C#的不同。这里是实现的重点,根据需要写自己的代码。class1类是一个框架,基本不用改。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Windows.ToolPalette; using Autodesk.AutoCAD.Windows; namespace Test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } float C; //长 float K; //宽 /// <summary> 画CAD图,根据计算出的坐标 /// /// </summary> void drawCAD() { Point3d p0 = new Point3d(0,0,0); Point3d p1 = new Point3d(C,0,0); Point3d p2 = new Point3d(C, K, 0); Point3d p3 = new Point3d(0, K, 0); using (DocumentLock doclock = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.LockDocument()) { // Database db = HostApplicationServices.WorkingDatabase; using (Transaction trans = db.TransactionManager.StartTransaction()) { BlockTable bt = trans.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; //画4条直线的矩形 List<Line> lins = new List<Line>(); lins.Add( new Line(p0, p1)); lins.Add( new Line(p1, p2)); lins.Add( new Line(p2, p3)); lins.Add( new Line(p3, p0)); foreach (Line line in lins) { btr.AppendEntity(line); trans.AddNewlyCreatedDBObject(line, true ); } //画圆,这里直接用矩形的四个点来画 List<Point3d> points = new List<Point3d>(); points.Add(p0); points.Add(p1); points.Add(p2); points.Add(p3); List<Circle> cirs = new List<Circle>(); foreach (Point3d p3d in points) { int R = 20; cirs.Add( new Circle(p3d, Vector3d.ZAxis, R)); } foreach (Circle cir in cirs) { cir.ColorIndex = 1; btr.AppendEntity(cir); trans.AddNewlyCreatedDBObject(cir, true ); } trans.Commit(); } } } private void button1_Click( object sender, EventArgs e) { this .C = Convert.ToInt32( this .textBox1.Text); //int转成float一定可以,所以可以把int的值直接赋给float this .K = Convert.ToInt32( this .textBox2.Text); this .drawCAD(); this .Close(); } } } |
http://files.cnblogs.com/files/zkp2010/CAD%E6%8F%92%E4%BB%B6%E5%BC%80%E5%8F%91%E2%80%94%E2%80%94%E4%BB%A5CAD2006%E4%B8%BA%E4%BE%8B.rar
CAD插件技术真心不难,无非是画点线条,CAD内部能实现的,C#调用acdbmgd.dll和acmgd.dll也能实现。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 上周热点回顾(3.3-3.9)