网络编程 | 站长之家 | 网页制作 | 图形图象 | 操作系统 | 冲浪宝典 | 软件教学 | 网络办公 | 邮件系统 | 网络安全 | 认证考试 | 系统进程
Firefox | IE | Maxthon | 迅雷 | 电驴 | BitComet | FlashGet | QQ | QQ空间 | Vista | 输入法 | Ghost | Word | Excel | wps | Powerpoint
asp | .net | php | jsp | Sql | c# | Ajax | xml | Dreamweaver | FrontPages | Javascript | css | photoshop | fireworks | Flash | Cad | Discuz!
当前位置 > 网站建设学院 > 网络编程 > C#应用
Tag:注入,存储过程,分页,安全,优化,xmlhttp,fso,jmail,application,session,防盗链,stream,无组件,组件,md5,乱码,缓存,加密,验证码,算法,cookies,ubb,正则表达式,水印,索引,日志,压缩,base64,url重写,上传,控件,Web.config,JDBC,函数,内存,PDF,迁移,结构,破解,编译,配置,进程,分词,IIS,Apache,Tomcat,phpmyadmin,Gzip,触发器,socket
网络编程:ASP教程,ASP.NET教程,PHP教程,JSP教程,C#教程,数据库,XML教程,Ajax,Java,Perl,Shell,VB教程,Delphi,C/C++教程,软件工程,J2EE/J2ME,移动开发
本月文章推荐
.C#中的非安全编程.
.c# 正确读取存储中文,以及如何获.
.[C#]解决读写包含汉字的txt文件时.
.C#制作MID窗体.
.c#调用oracle存储过程返回结果集.
.关于C#中的DateTime类型的细节问.
.把RichTextBox中的文本保存到Sql.
.网页表单自动填写技术(gmail为例) .
.用.NET开发MSN聊天机器人.
.关于C#中枚举打印机.
.用C#实现FTP搜索引擎.
.借用VB的My,C#照样条条大路通罗.
.代码前置时页面输出脚本的要注意.
.C#箴言:用静态构造函数初始化静.
.c-sharp开发应避免的几个小滥用.
.如何在控件的设计时得到窗体设计.
.C#判断一个string是否可以为数字.
.深入解析C#编程中的事件.
.Active Directory如何用C#进行增.
.如何用.NET Array类的Sort方法分.

利用C#进行AutoCAD的二次开发(二)

发表日期:2003-12-30


       大家好,今天我继续给各位介绍利用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中当前文档的标题。

上一篇:利用C#进行AutoCAD的二次开发(一) 人气:17091
下一篇:如何利用C#创建和调用DLL 人气:42874
浏览全部AutoCAD的内容 Dreamweaver插件下载 网页广告代码 祝你圣诞节快乐 2009年新年快乐