使用C#开发CAD,基于ActiveX

本文介绍了如何使用C#通过ActiveX接口与AutoCAD进行交互,创建AutoCADConnector类以连接和操作AutoCAD,包括打开、关闭AutoCAD实例。同时展示了如何利用C#代码绘制直线、矩形并添加尺寸标注的方法,提供了一个简单的按钮点击事件来执行图形绘制操作。
摘要由CSDN通过智能技术生成

建立于AutoCAD的连接

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autodesk.AutoCAD.Interop;
using Autodesk.AutoCAD.Interop.Common;
using System.Runtime.InteropServices;

namespace Mode
{
    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");
                _application.Documents.Add("");
            }
            catch
            {
                try
                {
                    // Create an instance and set flag to indicate this
                    _application = new AcadApplication();
                    _initialized = true;
                }
                catch
                {
                    throw;
                }
            }
          
        }

        // If the user doesn't call Dispose, the
        // garbage collector will upon destruction
        ~AutoCADConnector()
        {
            try { Dispose(false); }
            catch { }
        }

        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)
                _a

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值