⽤C#来开发CAD插件,含源代码
转⾃原⽂
CAD插件看起来很神秘,其实⼀个合格码农经过⼏天就能快速掌握。没什么秘密,开发CAD插件和winform⼀样简单学⼏个类库⽤法就是(只是太多⼈不喜欢知识分享),在CAD⾥展现界⾯和winform略有不同(整个项⽬⼯程在⽂章的最后有下载)。
学习CAD插件开发的动机是为了薪⽔,由于公司是做显⽰屏和触摸屏的,养了⼀堆CAD的设计⼯程师拿着8K以上的薪⽔,当时我做为信息系统开发⼈员才拿4K,4个⼈要开发维护整个公司(2万⼈,⼏⼗个部门)的系统,感觉严重不平衡,便挑战⼀下设计⼯程师,借此提⾼⾃⾝价值。
适⽤案例,公司做⼿机屏要放UV胶来贴合,⽤CAD画⼀个边框加⼀些线条。看着设计⼯程师们画画CAD也有那么多薪⽔,⼼⾥不平衡就搞出⼀个按照参数⾃动出图的东西。当然实际的项⽬⽐我放出的复杂得多,这⾥只是讲讲CAD插件技术,不涉及实际项⽬。总之⽆论多复杂,凡是根据参数⽣成固定图形的,CAD插件程序都可以胜任。
效果展⽰:
1,命令⾏:
2,右键菜单:
3,⼯具条,CAD内嵌界⾯,和winform⼀样简单
闲话少说,直接上原理和代码:
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类分别演⽰了命令⾏、右键菜单、⼯具条的实现。注释得⾮常清楚了,就不做多解释。
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("创建⾯板集错误");
}
}
}
}
View Code
2,CAD展现⼯具条,添加⼀个⽤户控件,取名为ModelessForm。两⾏代码显⽰Form1窗体⽽已。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;4k电影源代码
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);
}
}
}
View Code
3,Form1窗体的功能,是根据长宽画⼀个矩形,矩形的四个⾓分别画⼀个圆。因为在CAD⾥运⾏,所以使⽤的类库和正常C#的不同。这⾥是实现的重点,根据需要写⾃⼰的代码。class1类是⼀个框架,基本不⽤改。
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()

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。