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("\n加載cadObjArx\n");
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\n");
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\n");
}
[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("\nModel space objects: ");
foreach (ObjectId acObjId in acBlkTblRec)
{
acDoc.Editor.WriteMessage("\n" + acObjId.ObjectClass.DxfName +":"+ acObjId.Handle.Value.ToString());
nCnt = nCnt + 1;
}
if (nCnt == 0)
{
acDoc.Editor.WriteMessage("\nNo objects found.");
}
else
{
acDoc.Editor.WriteMessage("\nTotal {0} objects.", nCnt);
}
}
}
}
}
4 調試,在項目屬性中,設置調試->啟動外部程序,指定cad的啟動路徑,指定工作目錄為當前程序的生成目錄,既可調試。
這裡每次調試後,都要在啟動cad後,通過netload命令加載dll,比較麻煩,可以在上面的工作目錄下添加一個lsp腳本文件cad2012.lsp,裡面添加一條加載的語句(command "netload" "cadObjArx.dll")
這樣每次啟動調試後,cad為自動加載目錄下的cadObjArx.dll。
還有一個問題,因為上面引用了兩個objectArx的庫,系統默認會把這兩個庫復制到生成目錄下,會導致莫名奇妙的加載失敗,設置成不輸出後,就正常了。
5 部署,可以再建一個可執行程序的項目,通過調用com組件的方式來啟動cad和加載dll,也可以用上面那個方式吧lsp腳本文件和要使用的cad文件放在同一目錄下,cad為自動執行腳本
下面通過一個控制台程序啟動cad並加載dll
Console.WriteLine("start..");
string sProgID = "AutoCAD.Application.18.2";
try
{
if (System.Diagnostics.Process.GetProcessesByName("acad").Count() > 0)//判斷當前有沒cad在運行
{
Console.WriteLine("獲取打開的cad");
acApp = (AcadApplication)Marshal.GetActiveObject(sProgID);//獲取當前運行的cad對象
}
else
{
Console.WriteLine("open cad");
acApp = new AcadApplication();//沒有直接建一個
acApp.Visible = true;
}
}
catch (Exception exc)
{
Console.WriteLine(exc.Message);
}
if (acApp != null)
{
Console.WriteLine("netload begin ");
try
{//加載dll
string dllPath = @"D:\workspace\test\cadObjArx\bin\Debug\cadObjArx.dll";
string sCommand = "(command \"netload\" \"{0}\")\n";
dllPath = dllPath.Replace("\\", "\\\\");
acApp.ActiveDocument.SendCommand(string.Format(sCommand, dllPath));
}
catch (Exception exload)
{
Console.WriteLine("netload err:{0}", exload.Message);
}
}