明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
楼主: zyx1029

CAD插件自动加载

  [复制链接]
发表于 2025-9-15 21:33:30 | 显示全部楼层
会不会有使用限制
回复 支持 反对

使用道具 举报

发表于 2025-9-16 08:01:17 | 显示全部楼层
都是高手,都有好事
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-9-16 10:36:18 | 显示全部楼层
liwen888888 发表于 2025-9-15 21:33
会不会有使用限制

我是没有设置使用限制。要有也就是CAD版本不匹配。2023CAD肯定是没有问题的。
回复 支持 反对

使用道具 举报

发表于 2025-9-17 08:06:40 | 显示全部楼层

感谢大佬分享
回复 支持 反对

使用道具 举报

发表于 2025-9-17 10:37:53 | 显示全部楼层
能不能改成 自定义文件夹路径
回复 支持 反对

使用道具 举报

发表于 2025-9-17 13:41:59 | 显示全部楼层
方便发源码么
回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-9-17 17:57:02 | 显示全部楼层

  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using Autodesk.AutoCAD.ApplicationServices;
  6. using Autodesk.AutoCAD.EditorInput;
  7. using Autodesk.AutoCAD.Runtime;
  8. using SystemException = System.Exception; // 为System.Exception创建别名

  9. namespace VLXAutoLoader
  10. {
  11.     public class VLXLoader : IExtensionApplication
  12.     {
  13.         // 定义支持的插件类型及其文件夹和加载命令
  14.         private readonly Dictionary<string, (string folder, string loadCommand)> _pluginTypes =
  15.             new Dictionary<string, (string, string)>
  16.         {
  17.             { ".vlx", ("VLX", "(load \"{0}\")") },
  18.             { ".lsp", ("LSP", "(load \"{0}\")") },
  19.             { ".fas", ("FAS", "(load \"{0}\")") },
  20.             { ".arx", ("ARX", "(arxload \"{0}\")") },
  21.             { ".dll", ("DLL", "") } // DLL文件需要特殊处理
  22.         };

  23.         // 初始化方法 - 在AutoCAD加载DLL时调用
  24.         public void Initialize()
  25.         {
  26.             // 订阅文档创建事件,确保在有活动文档时才加载插件
  27.             Application.DocumentManager.DocumentCreated += OnDocumentCreated;

  28.             // 如果已经有活动文档,直接尝试加载插件
  29.             if (Application.DocumentManager.MdiActiveDocument != null)
  30.             {
  31.                 LoadPluginFiles();
  32.             }
  33.         }

  34.         // 清理方法 - 在AutoCAD卸载DLL时调用
  35.         public void Terminate()
  36.         {
  37.             // 取消事件订阅
  38.             Application.DocumentManager.DocumentCreated -= OnDocumentCreated;
  39.         }

  40.         // 文档创建事件处理
  41.         private void OnDocumentCreated(object sender, DocumentCollectionEventArgs e)
  42.         {
  43.             LoadPluginFiles();
  44.         }

  45.         // 加载插件文件的主要方法
  46.         private void LoadPluginFiles()
  47.         {
  48.             try
  49.             {
  50.                 // 获取当前DLL所在目录
  51.                 string dllDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
  52.                 Editor editor = Application.DocumentManager.MdiActiveDocument?.Editor;

  53.                 // 遍历所有支持的插件类型
  54.                 foreach (var pluginType in _pluginTypes)
  55.                 {
  56.                     string extension = pluginType.Key;
  57.                     string folderName = pluginType.Value.folder;
  58.                     string loadCommand = pluginType.Value.loadCommand;

  59.                     // 构建插件文件夹路径
  60.                     string pluginDirectory = Path.Combine(dllDirectory, folderName);

  61.                     // 检查插件文件夹是否存在
  62.                     if (!Directory.Exists(pluginDirectory))
  63.                     {
  64.                         editor?.WriteMessage($"{folderName}文件夹未找到: {pluginDirectory}\n");
  65.                         continue;
  66.                     }

  67.                     // 获取所有该类型的插件文件
  68.                     string[] pluginFiles = Directory.GetFiles(pluginDirectory, $"*{extension}");

  69.                     if (pluginFiles.Length == 0)
  70.                     {
  71.                         editor?.WriteMessage($"{folderName}文件夹中没有找到{extension}文件\n");
  72.                         continue;
  73.                     }

  74.                     // 获取当前文档
  75.                     Document doc = Application.DocumentManager.MdiActiveDocument;

  76.                     if (doc == null)
  77.                     {
  78.                         Application.ShowAlertDialog("没有活动文档,无法加载插件文件");
  79.                         return;
  80.                     }

  81.                     // 加载每个插件文件
  82.                     foreach (string pluginFile in pluginFiles)
  83.                     {
  84.                         try
  85.                         {
  86.                             if (extension == ".dll")
  87.                             {
  88.                                 // 特殊处理DLL文件
  89.                                 LoadDllFile(pluginFile, editor);
  90.                             }
  91.                             else
  92.                             {
  93.                                 // 使用相应的命令加载插件文件
  94.                                 string formattedPath = pluginFile.Replace("\\", "/");
  95.                                 string command = string.Format(loadCommand, formattedPath);
  96.                                 doc.SendStringToExecute(command + "\n", true, false, false);

  97.                                 // 命令行输出提示信息
  98.                                 editor?.WriteMessage($"已加载{extension}文件: {Path.GetFileName(pluginFile)}\n");
  99.                             }
  100.                         }
  101.                         catch (SystemException ex)
  102.                         {
  103.                             Application.ShowAlertDialog($"加载{extension}文件时出错 {pluginFile}: {ex.Message}");
  104.                         }
  105.                     }
  106.                 }
  107.             }
  108.             catch (SystemException ex)
  109.             {
  110.                 Application.ShowAlertDialog($"初始化插件加载器时出错: {ex.Message}");
  111.             }
  112.         }

  113.         // 加载DLL文件的方法
  114.         private void LoadDllFile(string dllPath, Editor editor)
  115.         {
  116.             try
  117.             {
  118.                 // 使用反射加载DLL
  119.                 Assembly.LoadFrom(dllPath);
  120.                 editor?.WriteMessage($"已加载DLL文件: {Path.GetFileName(dllPath)}\n");
  121.             }
  122.             catch (SystemException ex)
  123.             {
  124.                 // 使用SystemException而不是Exception
  125.                 throw new SystemException($"无法加载DLL文件 {dllPath}: {ex.Message}");
  126.             }
  127.         }

  128.         // 可选:添加一个命令以便手动重新加载所有插件
  129.         [CommandMethod("RELOADPLUGINS")]
  130.         public void ReloadPluginsCommand()
  131.         {
  132.             LoadPluginFiles();
  133.         }

  134.         // 可选:添加一个命令以便加载特定类型的插件
  135.         [CommandMethod("LOADPLUGINTYPE")]
  136.         public void LoadPluginTypeCommand()
  137.         {
  138.             Document doc = Application.DocumentManager.MdiActiveDocument;
  139.             Editor ed = doc.Editor;

  140.             // 创建关键字选项
  141.             var keywords = new List<string>(_pluginTypes.Keys);
  142.             string kw = string.Join("/", keywords);

  143.             // 提示用户选择插件类型
  144.             PromptKeywordOptions pko = new PromptKeywordOptions($"\n请选择要加载的插件类型 [{kw}]: ");
  145.             foreach (var keyword in keywords)
  146.             {
  147.                 pko.Keywords.Add(keyword);
  148.             }
  149.             pko.AllowNone = true;

  150.             PromptResult pr = ed.GetKeywords(pko);
  151.             if (pr.Status != PromptStatus.OK) return;

  152.             string selectedType = pr.StringResult;

  153.             try
  154.             {
  155.                 // 获取当前DLL所在目录
  156.                 string dllDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

  157.                 // 获取选中的插件类型信息
  158.                 var pluginType = _pluginTypes[selectedType];
  159.                 string folderName = pluginType.folder;
  160.                 string loadCommand = pluginType.loadCommand;

  161.                 // 构建插件文件夹路径
  162.                 string pluginDirectory = Path.Combine(dllDirectory, folderName);

  163.                 // 检查插件文件夹是否存在
  164.                 if (!Directory.Exists(pluginDirectory))
  165.                 {
  166.                     ed.WriteMessage($"{folderName}文件夹未找到: {pluginDirectory}\n");
  167.                     return;
  168.                 }

  169.                 // 获取所有该类型的插件文件
  170.                 string[] pluginFiles = Directory.GetFiles(pluginDirectory, $"*{selectedType}");

  171.                 if (pluginFiles.Length == 0)
  172.                 {
  173.                     ed.WriteMessage($"{folderName}文件夹中没有找到{selectedType}文件\n");
  174.                     return;
  175.                 }

  176.                 // 加载每个插件文件
  177.                 foreach (string pluginFile in pluginFiles)
  178.                 {
  179.                     try
  180.                     {
  181.                         if (selectedType == ".dll")
  182.                         {
  183.                             // 特殊处理DLL文件
  184.                             LoadDllFile(pluginFile, ed);
  185.                         }
  186.                         else
  187.                         {
  188.                             // 使用相应的命令加载插件文件
  189.                             string formattedPath = pluginFile.Replace("\\", "/");
  190.                             string command = string.Format(loadCommand, formattedPath);
  191.                             doc.SendStringToExecute(command + "\n", true, false, false);

  192.                             // 命令行输出提示信息
  193.                             ed.WriteMessage($"已加载{selectedType}文件: {Path.GetFileName(pluginFile)}\n");
  194.                         }
  195.                     }
  196.                     catch (SystemException ex)
  197.                     {
  198.                         Application.ShowAlertDialog($"加载{selectedType}文件时出错 {pluginFile}: {ex.Message}");
  199.                     }
  200.                 }
  201.             }
  202.             catch (SystemException ex)
  203.             {
  204.                 Application.ShowAlertDialog($"加载{selectedType}类型插件时出错: {ex.Message}");
  205.             }
  206.         }
  207.     }
  208. }

回复 支持 反对

使用道具 举报

 楼主| 发表于 2025-9-17 17:58:04 | 显示全部楼层

自己根据CAD版本编译吧。
回复 支持 反对

使用道具 举报

发表于 2025-9-18 10:18:33 | 显示全部楼层
zyx1029 发表于 2025-9-17 17:58
自己根据CAD版本编译吧。

非常感谢无私奉献
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-6 05:30 , Processed in 0.165240 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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