1、开发环境配置:
(1)安装AutoCAD2018;
(2)安装VS2015;
(3)安装ObjectARX 2018类库;
(4)安装ObjectARX 2018 .NET开发向导(ObjectARXWizards.msi)。
2、利用向导新建一个ObjectARX项目:File>NEW>project,在项目名称输入yunyou.ke.qq.com,确定。
3、确定后弹出如下对话框:
4、确定后生成项目:
5 、向导自动添加两个类,一个继承IExtensionApplication接口,这个是dll的入口,cad会从这个类加载程序做一些初始化的操作;另外一个可以写自定义的一些cad命令。
6、添加COM库的应用。
7、在“myplugin”中补充如下的命名空间引用。
using Autodesk.AutoCAD.Windows;
using AutoCAD;//com库
8、myplugin.cs代码修改如下:
using System; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Windows; using AutoCAD;//com库 // This line is not mandatory, but improves loading performances [assembly: ExtensionApplication(typeof(yunyou.ke.qq.com_1.MyPlugin))] namespace yunyou.ke.qq.com { // This class is instantiated by AutoCAD once and kept alive for the // duration of the session. If you don't do any one time initialization // then you should remove this class. public class MyPlugin : IExtensionApplication { void IExtensionApplication.Initialize() { //加载dll的时候执行相关加载操作 Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("\n加载 云幽学院 yunyou.ke.qq.com\n"); load(); } void IExtensionApplication.Terminate() { //这个是推出时执行 Document doc = Application.DocumentManager.MdiActiveDocument; doc.LockDocument(DocumentLockMode.NotLocked, "", "", false); } private void load() { //这里添加一个工具条,添加一个按钮绑定下面的MyCommand命令 //这个是通过com组件实现需要引用cad的两个com组件. AcadMenuGroups menugroups = (AcadMenuGroups)Application.MenuGroups; AcadToolbar toolbar = menugroups.Item(0).Toolbars.Add("云幽学院 yunyou.ke.qq.com"); AcadToolbarItem item = toolbar.AddToolbarButton(toolbar.Count, "MyCommand", "云幽学院MyCommand", "MyCommand\n"); item.SetBitmaps("16x16.bmp", "32x32.bmp"); toolbar.Dock(AcToolbarDockStatus.acToolbarDockTop); } } }
7、Mycommand.cs
using System; using Autodesk.AutoCAD.Runtime; using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.EditorInput; // This line is not mandatory, but improves loading performances [assembly: CommandClass(typeof(yunyou.ke.qq.com_1.MyCommands))] namespace yunyou.ke.qq.com_1 { // This class is instantiated by AutoCAD for each document when // a command is called by the user the first time in the context // of a given document. In other words, non static data in this class // is implicitly per-document! public class MyCommands { // Modal Command with localized name [CommandMethod("MyGroup", "MyCommand", "MyCommandLocal", CommandFlags.Modal)]//特性标识,标识这个是cad命令 public void Init() { Editor ed = Application.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("云幽学院 yunyou.ke.qq.com\n"); } } }
10、调试,在项目属性中,设置调试->启动外部程序,指定cad的启动路径,指定工作目录为当前程序的生成目录,既可调试。这里每次调试后,都要在启动cad后,通过netload命令加载dll,避免复杂调试、用自己的程序启动AutoCAD 并加载自己开发应用程序等高级方法请见云幽学院的视频课程 yunyou.ke.qq.com 。