大家好,今天我继续给各位介绍利用c#进行autocad的二次开发。在这一讲中,主要介绍上一讲例子中存在的问题。
在上一次的例子中我是通过引用autocad 2004 type library来进行c#与autocad之间的通信,但这种方法存在两个致命的缺点。第一个缺点是每次调试程序的时候c#都要重新启动autocad,如果调试的次数非常多(比如跟踪错误然后调试),那么编程的效率就很低,因为启动一次cad还是需要较长的时间。相对于第一个缺点,第二个缺点则更要命。由于.net本身的问题,interop.autocad.dll文件(就是通过它才实现了c#与autocad之间的通信)存在着一些bug,因此虽然有时你的代码是完全正确的,但c#编译器还是抛出莫名其妙的错误。那不是完蛋了吗?我曾经有一阶段就因为这两个要命的东东差一点放弃了c#而想改学objectarx了,呵呵,不过还是运气好,我偶尔一次在网上看了一篇外国人写的文章,他专门介绍了这两个问题的解决办法。下面就来解决这两个问题。
首先来看第二个难题,按以下步骤来进行:
1. 随便用visual studio .net建立一个c#应用程序,然后按照上一篇文章中的设置加入autocad 2004 type library,然后不加入任何代码,编译你的程序。
2. 在visual studio .net命令行工具下用ildasm.exe(这个工具可以在visual studio .net安装光盘中找到)把interop.autocad.dll文件(这个文件在步骤1中生成的项目的bin\release文件夹中)编译成中间语言interop. autocad.il。注意:在步骤1中建立的项目的编译设置为release模式。
ildasm.exe /source interop.autocad.dll /output=interop. autocad.il
又要注意了:把ildasm.exe,interop.autocad.dll放在同一目录下。
3.在记事本中打开interop. autocad.il文件,然后查找结尾是“sinkhelper”而开头为 ".class private auto ansi sealed _dacad“的语句,把语句中的private 改为public,然后保存interop. autocad.il文件。
4.使用ilasm.exe把interop. autocad.il文件编译为interop.autocad.dll文件,同样是在visual studio .net命令行工具下进行。
ilasm.exe /resource=interop.autocad.res /dll interop.autocad.il /output=interop. autocad.dll
interop.autocad.res文件是在步骤1中生成的。
5.显然你不愿意每次编写应用程序时都通过上一篇文章中介绍的方法来加入interop. autocad.dll,那太麻烦了。你可以用下面的方法来让程序自动加入该文件:找到c:\program files\microsoft.net\ primary interop assemblies 文件夹,然后把上面生成的
interop.autocad.dll文件拷贝进去。
好了,第二个问题解决了,接下来看第一个。
在vba中,编程者可以使用getobject函数来获得当前活动的autocad对象,但在c#中却没有,为了这个函数我几乎把msdn给翻遍了,然后去各种c#论坛问各位高手,结果都没得到解决,呵呵,可能国内使用c#的人比较少吧。还是在老外的论坛上看到了一篇就是讲这个问题的文章才把这个难题给解决了。使用下面的语句就可以获得当前活动的autocad对象了:
(acadapplication)marshal.getactiveobject("autocad.application.16")
(对于cad2000和cad2002,则把16改为15)
当然以上语句必须在autocad打开的情况下才能使用,否则会发生错误,对于autocad没打开的情况,可以使用上一篇文章的方法来处理。完整的连接autocad与c#的源程序如下所示:
using system;
using autocad;
using system.runtime.interopservices;
namespace acadexample
{
public class autocadconnector : idisposable
{
private acadapplication _application;
private bool _initialized;
private bool _disposed;
public autocadconnector()
{
try
{
// upon creation, attempt to retrieve running instance
_application = (acadapplication)marshal.getactiveobject("autocad.application.16");
}
catch
{
try
{
// create an instance and set flag to indicate this
_application = new acadapplicationclass();
_initialized = true;
}
catch
{
throw;
}
}
}
// if the user doesn't call dispose, the
// garbage collector will upon destruction
~autocadconnector()
{
dispose(false);
}
public acadapplication application
{
get
{
// return our internal instance of autocad
return _application;
}
}
// this is the user-callable version of dispose.
// it calls our internal version and removes the
// object from the garbage collector's queue.
public void dispose()
{
dispose(true);
gc.suppressfinalize(this);
}
// this version of dispose gets called by our
// destructor.
protected virtual void dispose(bool disposing)
{
// if we created our autocad instance, call its
// quit method to avoid leaking memory.
if(!this._disposed && _initialized)
_application.quit();
_disposed = true;
}
}
}
利用visual studio.net 把上面的程序编译成一个类库,你就可以在以后的程序中使用它了,下面的这个例子说明了它的用法。(首先把acadexample类库包含在项目中)
using system;
using acadexample;
using autocad;
namespace consoleapplication6
{
class class1
{
[stathread]
static void main(string[] args)
{
using (autocadconnector connector = new autocadconnector())
{
console.writeline(connector.application.activedocument.name);
}
console.readline();
}
}
}
这个例子是在c#窗口中显示autocad中当前文档的标题。
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器