鱼C论坛

 找回密码
 立即注册
查看: 59|回复: 2

[已解决]C#开发autoCAD调试的时候,每次都要重启autoCAD,很麻烦,求办法解决

[复制链接]
发表于 3 天前 | 显示全部楼层 |阅读模式
60鱼币
最佳答案
3 天前
可以使用一个套件来动态加载你的插件
  1. using Autodesk.AutoCAD.Runtime;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;

  9. namespace ArxDotNetCourse
  10. {
  11.     public class DebugAdapter
  12.     {
  13.         private Action cmd1;
  14.         public DebugAdapter()
  15.         {
  16.             Reload();
  17.         }

  18.         [CommandMethod("cmd1")]
  19.         public void Cmd1()
  20.         {
  21.             cmd1?.Invoke();
  22.         }

  23.         [CommandMethod("reload1")]
  24.         public void Reload()
  25.         {
  26.             FileInfo adapterFileinfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
  27.             string assemblyPath = Path.Combine(adapterFileinfo.DirectoryName, "FunctionalGraph.dll");
  28.             Assembly targetAssembley = Assembly.Load(File.ReadAllBytes(assemblyPath));
  29.             Type targetType = targetAssembley.GetType("FunctionalGraph.Class1");
  30.             MethodInfo targetMethod = targetType.GetMethod("Cmd1");
  31.             Object targetObject = Activator.CreateInstance(targetType);
  32.             cmd1 = () => targetMethod.Invoke(targetObject, null);
  33.         }
  34.     }
  35. }
复制代码

最佳答案

查看完整内容

可以使用一个套件来动态加载你的插件
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 3 天前 | 显示全部楼层    本楼为最佳答案   
可以使用一个套件来动态加载你的插件
  1. using Autodesk.AutoCAD.Runtime;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text;
  8. using System.Threading.Tasks;

  9. namespace ArxDotNetCourse
  10. {
  11.     public class DebugAdapter
  12.     {
  13.         private Action cmd1;
  14.         public DebugAdapter()
  15.         {
  16.             Reload();
  17.         }

  18.         [CommandMethod("cmd1")]
  19.         public void Cmd1()
  20.         {
  21.             cmd1?.Invoke();
  22.         }

  23.         [CommandMethod("reload1")]
  24.         public void Reload()
  25.         {
  26.             FileInfo adapterFileinfo = new FileInfo(Assembly.GetExecutingAssembly().Location);
  27.             string assemblyPath = Path.Combine(adapterFileinfo.DirectoryName, "FunctionalGraph.dll");
  28.             Assembly targetAssembley = Assembly.Load(File.ReadAllBytes(assemblyPath));
  29.             Type targetType = targetAssembley.GetType("FunctionalGraph.Class1");
  30.             MethodInfo targetMethod = targetType.GetMethod("Cmd1");
  31.             Object targetObject = Activator.CreateInstance(targetType);
  32.             cmd1 = () => targetMethod.Invoke(targetObject, null);
  33.         }
  34.     }
  35. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 3 天前 | 显示全部楼层
解决C# AutoCAD调试需频繁重启的两种方案

  • 方案一:使用附加进程调试
    1. 在VS中点击 调试 -> 附加到进程
    2. 在进程列表中选择正在运行的 acad.exe
    3. 修改代码后,按 Ctrl+Shift+B 重新生成DLL
    4. 在AutoCAD命令行输入 NETUNLOAD 卸载旧模块
    5. 再次输入 NETLOAD 加载新生成的DLL
  • 方案二:自动重载插件(代码实现)
    在项目中添加以下命令,执行RELOAD即可热更新:

    1. [CommandMethod("RELOAD")]
    2. public void ReloadPlugin()
    3. {
    4.     Assembly assembly = Assembly.GetExecutingAssembly();
    5.     Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
    6.    
    7.     try {
    8.         // 卸载当前程序集
    9.         Application.UnloadApplicationContext(Application.ApplicationContext);
    10.         // 重新加载
    11.         Assembly.Load(File.ReadAllBytes(assembly.Location));
    12.         ed.WriteMessage("\n插件重载成功!");
    13.     }
    14.     catch (Exception ex) {
    15.         ed.WriteMessage("\n错误: " + ex.Message);
    16.     }
    17. }
    复制代码



优化建议
  • 在VS项目属性中设置生成后事件,自动复制DLL到AutoCAD支持路径
  • 将调试启动程序设为 acad.exe 并添加参数 /nologo
  • 使用 AppDomain 隔离加载插件(需要设计分层架构)


提示:NETUNLOAD可能需要处理干净的对象引用,建议在Initialize()中注册的菜单/命令要做成可释放的

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 21:58

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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