明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1833|回复: 1

c#里怎么连接CAD?^_^万分感谢

[复制链接]
发表于 2010-4-7 15:08:00 | 显示全部楼层 |阅读模式

在VB里的连接CAD函数怎么移植到C#下啊?万分感谢

 Sub 连接AutoCAD()
        On Error Resume Next
        Acadapp = GetObject(, "AutoCAD.Application")
        If Err.Number Then
            Err.Clear()
            Acadapp = CreateObject("AutoCAD.Application")
            If Err.Number Then
                MsgBox("不能运行AutoCAD,请检查是否安装了AutoCAD 2004")
                Exit Sub
            End If
        End If
        Acadapp.Visible = True '界面可视
        AcadApp.WindowState = autocad.AcWindowState.acMax '界面最大化
        AppActivate(Acadapp.Caption) '显示AutoCAD界面
    End Sub

发表于 2010-7-30 12:47:00 | 显示全部楼层

class  AutoCADConnector: IDisposable
    {
      public  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)
          {
             
              try
              {

                  if (Marshal.GetActiveObject("AutoCAD.Application.16").Equals(this._application))
                  {
                      _application.Quit();
                  }
                 
              }
            
             catch
              {

             }
             
                      
          }
          _disposed = true; 
         
        
            
               

      }
         }

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 23:34 , Processed in 0.178628 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表