【C#】AutoCAD二次开发笔记

学习视频地址:CAD二次开发视频C#语言

作者源码地址:CAD二次开发代码

一、环境

本人使用的IDE为VS2017,CAD为AutoCAD2019,在建立项目的时候需要注意Framework版本的选择,由于CAD为2019,对应的Framework为4.7,而Visual Studio自带的选项里“工具->获取工具和功能”,可以直接下载,下载后在 “01环境测试->右键属性”更改即可。

 二、配置环境

需要导入以下三个库(库在AutoCAD 2019的文件夹里):

accoremgd.dll
Acdbmgd.dll
Acmgd.dll

同时增加五个命名空间:

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;

同时修改 “01环境测试->右键属性”调试启动外部程序:

三、简单测试

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _01环境测试
{
    public class Class1
    {
        [CommandMethod("TestDemo")] //指令名称
        public void TestDemo()
        {   
            // 声明命令行对象
            Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
            // 向命令行输出一段文字
            ed.WriteMessage("我是来学习CAD的");
        }
    }
}

 运行后会自动打开cad,然后在cad的命令行里输入` NETLOAD `然后在该项目的bin/debug中找到01环境测试.dll文件载入即可,接着在命令行里输入`TestDemo `运行效果如下:

四、调试过程(避免重启CAD浪费时间)

加断点,然后通过`debug`模式都会重新打开AutoCAD,相当浪费时间做开发;为解决这个问题,该视频给出一个解决方案:

新建一个类库LoadX,通过这个类库去生成原dll库,加载至CAD中后,可以修改原dll库而不用重新打开CAD。

using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Colors;
using System.IO;
using System.Reflection;

namespace LoadX
{
    public class LoadX
    {
        private Action cmd;
        [CommandMethod("DD")]
        public void ReloadX()
        {
            #region Hello
            string dllName = "test.dll";
            string className = "test.cmd";
            string methodName = "hello";
            #endregion

            #region
            var adapterFileInfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
            var targetFilePath = Path.Combine(adapterFileInfo.DirectoryName, dllName);
            var targetAssembly = Assembly.Load(File.ReadAllBytes(targetFilePath));
            var targetType = targetAssembly.GetType(className);
            var targetMethod = targetType.GetMethod(methodName);
            var targetObject = Activator.CreateInstance(targetType);
            cmd = () => targetMethod.Invoke(targetObject, null);

            try
            {
                cmd?.Invoke();
            }
            catch(System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, "Tips");
            }
            #endregion
        }
    }

    public class CmdInfo
    {
        public string DllName { get; set; }
        public string ClassName { get; set; }
        public string MethodName { get; set; }
    }
}

并将原cs文件中的生成文件路径修改为xload中的生成路径;

 运行LoadX.cs后,将在LoadX的bin文件夹中出现test.dll和LoadX.dll两个动态链接库,在CAD中加载LoadX.dll库即可。

 

此时如要修改test.cs,点击重新生存test.dll库,这里不能再修改LoadX.dll,然后在CAD里运行相同命令行,发现内容已经有所修改。

原理:加载一个总的动态链接库,然后该动态链接库指向各命令语句,修改各命令语句的库时就不会影响该总库,从而达到调试的结果。

  • 6
    点赞
  • 47
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值