zxy600 发表于 2010-4-7 15:08:00

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

在VB里的连接CAD函数怎么移植到C#下啊?万分感谢
Sub 连接()
      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

guanxiancad2006 发表于 2010-7-30 12:47:00

classAutoCADConnector: IDisposable
    {
      publicAcadApplication _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 =newAcadApplicationClass();
               
               _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;
         
         
            
               
      }
         }
页: [1]
查看完整版本: c#里怎么连接CAD?^_^万分感谢