cad二次開發的準備工作:需要在cad的安裝檔案中加載紅色框中的dll檔案。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiI0NXYFhGd192UvwVe0lmdhJ3ZvwFM38CXlZHbvN3cpR2Lc1TPB10QGtWUCpEMJ9CXsxWam9CXwADNvwVZ6l2c052bm9CXUJDT1wkNhVzLcRnbvZ2LcFTQU9ENVJjWxwWbiZXUYpVd1kmYr50MZV3YyI2cKJDT29GRjBjUIF2LcRHelR3LcJzLctmch1mclRXY39TM3kTM0ATM5EDNwETM3EDMy8CX0Vmbu4GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.jpg)
然後就是一堆Using:
接着注意版本的問題,因為visual studio2015用的是新的程式集,是以最好cad也用比較高的版本,我這裡直接用的2016版的cad。
然後就是一個在cad中加載生成的dll檔案的問題。這裡用下面的這個方法:
http://jingyan.baidu.com/article/d8072ac4930316ec94cefd67.html
using System;
using System.Linq;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System.Windows.Forms;
using System.Reflection;
namespace myNetLoad
{
public class MyClass
{
//本程式在AutoCAD的快捷指令是"NL"
[CommandMethod("NL")]
public void myLoad()
{
//AutoCAD指令欄
Editor ed = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument.Editor;
//調用Windows.Forms選擇一個檔案
OpenFileDialog fileDialog = new OpenFileDialog();
//判斷确認按鈕
if (fileDialog.ShowDialog() == DialogResult.OK)
{
//選擇的檔案路徑
string file_dir = fileDialog.FileName;
//在AutoCAD指令欄輸出選擇的檔案路徑
ed.WriteMessage("檔案路徑:" + file_dir);
//打開檔案,将檔案以二進制方式複制到記憶體,自動關閉檔案
byte[] buffer = System.IO.File.ReadAllBytes(file_dir);
//加載記憶體中的檔案
Assembly assembly = Assembly.Load(buffer);
}
}
}
}
注意最後覆寫新的dll檔案的時候注意用新的程式集,即連結方法的最後一步。
接下來開始我們的正文——Hello World以及畫一個圓。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.Colors;
using Autodesk.AutoCAD.EditorInput;
using System.Collections;
using AcadApp = Autodesk.AutoCAD.ApplicationServices.Application;
using System.Reflection;
namespace cad開發
{
public class Class1
{
[CommandMethod("HelloWorld")]
public void HelloWorld()
{
Editor ed = AcadApp.DocumentManager.MdiActiveDocument.Editor;
ed.WriteMessage("Hello World222");
}
[CommandMethod("test")]
public void createCircle()
{ //首先聲明我們要使用的對象
Circle circle; //這個是我們要加入到模型空間的圓
BlockTableRecord btr;//要加入圓,我們必須打開模型空間
BlockTable bt; //要打開模型空間,我們必須通過塊表(BlockTable)來通路它
//我們使用一個名為‘Transaction’的對象,把函數中有關資料庫的操作封裝起來
Transaction trans;
//使用 TransactionManager 的 StartTransaction()成員來開始事務處理
trans = HostApplicationServices.WorkingDatabase.TransactionManager.StartTransaction();
//現在建立圓……請仔細看這些參數——注意建立 Point3d 對象的‘New’和 Vector3d 的靜态成員 ZAxis
circle = new Circle(new Point3d(, , ), Vector3d.ZAxis, );
bt = (BlockTable)trans.GetObject(HostApplicationServices.WorkingDatabase.BlockTableId, OpenMode.ForRead);
//使用目前的空間 Id 來擷取塊表記錄——注意我們是打開它用來寫入
btr = (BlockTableRecord)trans.GetObject(HostApplicationServices.WorkingDatabase.CurrentSpaceId, OpenMode.ForWrite);
//現在使用 btr 對象來加入圓
btr.AppendEntity(circle);
trans.AddNewlyCreatedDBObject(circle, true); //并确定事務處理知道要加入圓!
//一旦完成以上操作,我們就送出事務處理,這樣以上所做的改變就被儲存了……
trans.Commit();
//…然後銷毀事務處理,因為我們已經完成了相關的操作(事務處理不是資料庫駐留對象,可以銷毀)
trans.Dispose();
}
}
}
在cad中先用NETLOAD指令加載myNetLoad.dll,然後在指令框中加載NL指令,然後再把”cad開發.dll”加載進去。然後運作HelloWorld指令,指令框中直接出現Hello World。然後在指令框中輸入test,cad裡就直接畫了一個圓。