1、环境搭建:安装CAD 和objectArx库,这里安装的是cad2012和objectArx2010 ,vs是2010
  2、新建一个类库项目,引用objectArx安装目录下inc文件夹下的AcDbMgd.dll和AcMgd.dll,这里注意X86和X64系统的区别
  3、添加两个类,一个继承IExtensionApplication接口,这个是dll的入口,cad会从这个类加载程序做一些初始化的操作;另外一个可以写自定义的一些cad命令
  cad的引用:
  using Autodesk.AutoCAD.Runtime;
  using Autodesk.AutoCAD.ApplicationServices;
  using Autodesk.AutoCAD.DatabaseServices;
  using Autodesk.AutoCAD.EditorInput;
  using Autodesk.AutoCAD.Geometry;
  using Autodesk.AutoCAD.Windows;
  using Autodesk.AutoCAD.Interop;
  using Autodesk.AutoCAD.Interop.Common;
  可以添加程序集目录,方便快速加载,其中两个类,第一个类标识为入口继承IExtensionApplication接口,第二个类为自定义的命令
  [assembly: ExtensionApplication(typeof(cadObjArx.CADExetensionCls))]
  [assembly: CommandClass(typeof(cadObjArx.CADCommandS))]
  代码:
  这里只做两个简单的例子,更加深入的需要看cad的开发手册。。。。。
namespace cadObjArx
{
public class CADExetensionCls : IExtensionApplication
{
public void Initialize()
{//加载dll的时候执行相关加载操作
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage(" 加载cadObjArx ");
load();
}
public void Terminate()
{//这个是推出时执行
Document doc = Application.DocumentManager.MdiActiveDocument;
doc.LockDocument(DocumentLockMode.NotLocked, "", "", false);
}
private void load()
{   //这里添加一个工具条,添加一个按钮绑定下面的InitT命令
//这个是通过引用cad的com组件实现的,需要引用cad的Autodesk.AutoCAD.Interop和Autodesk.AutoCAD.Interop.Common两个com组件,
AcadMenuGroups menugroups = (AcadMenuGroups)Application.MenuGroups;
AcadToolbar toolbar = menugroups.Item(0).Toolbars.Add("Test");
AcadToolbarItem item = toolbar.AddToolbarButton(toolbar.Count, "InitT", "测试", "InitT ");
item.SetBitmaps("设置16x16.bmp", "设置32x32.bmp");
toolbar.Dock(AcToolbarDockStatus.acToolbarDockTop);
}
}
public class CADCommandS
{
[CommandMethod("InitT",CommandFlags.Modal)]//特性标识,标识这个是cad命令
public void Init()
{//这里做个测试,命令行中输出一句话,按下上面添加的按钮或者在命令行中输入命令会被执行
Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("测试init ");
}
[CommandMethod("ListEntities")]
public  void ListEntities()
{//这个是遍历当前cad打开的文档中的对象//在命令行中写命令即可执行
Document acDoc = Application.DocumentManager.MdiActiveDocument;
Database acCurDb = acDoc.Database;
using (Transaction acTrans = acCurDb.TransactionManager.StartTransaction())
{
BlockTable acBlkTbl;
acBlkTbl = acTrans.GetObject(acCurDb.BlockTableId, OpenMode.ForRead) as BlockTable;
BlockTableRecord acBlkTblRec;
acBlkTblRec = acTrans.GetObject(acBlkTbl[BlockTableRecord.ModelSpace], OpenMode.ForRead) as BlockTableRecord;
int nCnt = 0;
acDoc.Editor.WriteMessage(" Model space objects: ");
foreach (ObjectId acObjId in acBlkTblRec)
{
acDoc.Editor.WriteMessage(" " + acObjId.ObjectClass.DxfName +":"+ acObjId.Handle.Value.ToString());
nCnt = nCnt + 1;
}
if (nCnt == 0)
{
acDoc.Editor.WriteMessage(" No objects found.");
}
else
{
acDoc.Editor.WriteMessage(" Total {0} objects.", nCnt);
}
}
}
}
}