c#里怎么连接CAD?^_^万分感谢
<p>在VB里的连接CAD函数怎么移植到C#下啊?万分感谢</p><p> Sub 连接AutoCAD()<br/> On Error Resume Next<br/> Acadapp = GetObject(, "AutoCAD.Application")<br/> If Err.Number Then<br/> Err.Clear()<br/> Acadapp = CreateObject("AutoCAD.Application")<br/> If Err.Number Then<br/> MsgBox("不能运行AutoCAD,请检查是否安装了AutoCAD 2004")<br/> Exit Sub<br/> End If<br/> End If<br/> Acadapp.Visible = True '界面可视<br/> AcadApp.WindowState = autocad.AcWindowState.acMax '界面最大化<br/> AppActivate(Acadapp.Caption) '显示AutoCAD界面<br/> End Sub</p> <p><font face="Verdana">class AutoCADConnector: IDisposable<br/> {<br/> public AcadApplication _application;</font></p><font face="Verdana"><p><br/> private bool _initialized;</p>
<p> private bool _disposed;<br/> </p>
<p> public AutoCADConnector()</p>
<p> {</p>
<p> try</p>
<p> {</p>
<p> // Upon creation, attempt to retrieve running instance</p>
<p> _application = (AcadApplication)Marshal.GetActiveObject("AutoCAD.Application.16");</p>
<p> <br/> <br/> <br/> </p>
<p> }</p>
<p> catch</p>
<p> {</p>
<p> try</p>
<p> {</p>
<p> // Create an instance and set flag to indicate this</p>
<p> _application = new AcadApplicationClass();<br/> </p>
<p> _initialized = true;</p>
<p> }</p>
<p> catch</p>
<p> {</p>
<p> throw;</p>
<p> }</p>
<p> }</p>
<p> }</p>
<p> // If the user doesn't call Dispose, the</p>
<p> // garbage collector will upon destruction</p>
<p> ~AutoCADConnector()</p>
<p> {</p>
<p> Dispose(false);</p>
<p> }</p>
<p> </p>
<p> public AcadApplication Application</p>
<p> {</p>
<p> get</p>
<p> {</p>
<p> // Return our internal instance of AutoCAD</p>
<p> return _application;</p>
<p> }</p>
<p> }</p>
<p> </p>
<p> // This is the user-callable version of Dispose.</p>
<p> // It calls our internal version and removes the</p>
<p> // object from the garbage collector's queue.</p>
<p> public void Dispose()</p>
<p> {<br/> </p>
<p> Dispose(true);</p>
<p> GC.SuppressFinalize(this);</p>
<p> }</p>
<p> </p>
<p> // This version of Dispose gets called by our</p>
<p> // destructor.</p>
<p> protected virtual void Dispose(bool disposing)</p>
<p> {</p>
<p> // If we created our AutoCAD instance, call its</p>
<p> // Quit method to avoid leaking memory.</p>
<p> if (!this._disposed && _initialized)<br/> {<br/> <br/> try<br/> {</p>
<p> if (Marshal.GetActiveObject("AutoCAD.Application.16").Equals(this._application))<br/> {<br/> _application.Quit();<br/> }<br/> <br/> }<br/> <br/> catch<br/> {</p>
<p> }<br/> <br/> <br/> }<br/> _disposed = true; <br/> <br/> <br/> <br/> </p>
<p> }<br/> }</font></p>
页:
[1]