获取当前活动的AutoCAD对象 ,下面语句只适合AutoCAD打开的情况,否则会发生错误,对于AutoCAD没有打开的情况后续会阐述。
View Code
namespace AcadExample { public class AutoCADConnector : IDisposable { private AcadApplication _application; private bool _initialized; private bool _dispoased; public AutoCADConnector() { try { //获取当前活动的AutoCAD对象,对于AutoCAD2000和AutoCAD2002,则把16改成15 _application = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.16"); } catch { try { //创建一个新的实例并作标记 _application = new AcadApplication(); _initialized = true; } catch { throw; } } } public AcadApplication Application { get { return _application; } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } //我们自己创建AutoCAD实例,调用此方法 protected virtual void Dispose(bool disposing) { //退出方法,避免内存泄露 if (!this._dispoased && _initialized) { _application.Quit(); _dispoased = true; } } } }