liwen888888 发表于 2025-9-15 21:33:30

会不会有使用限制

彳余 发表于 2025-9-16 08:01:17

都是高手,都有好事

zjy2999 发表于 2025-9-16 08:42:16

多谢分享!!!!!!

zyx1029 发表于 2025-9-16 10:36:18

liwen888888 发表于 2025-9-15 21:33
会不会有使用限制

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

清水白粥 发表于 2025-9-17 08:06:40


感谢大佬分享

KEwb 发表于 2025-9-17 10:37:53

能不能改成 自定义文件夹路径

bloodtempt 发表于 2025-9-17 13:41:59

方便发源码么

zyx1029 发表于 2025-9-17 17:57:02

bloodtempt 发表于 2025-9-17 13:41
方便发源码么

using System;
using System.IO;
using System.Collections.Generic;
using System.Reflection;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.Runtime;
using SystemException = System.Exception; // 为System.Exception创建别名

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

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

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

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

      // 文档创建事件处理
      private void OnDocumentCreated(object sender, DocumentCollectionEventArgs e)
      {
            LoadPluginFiles();
      }

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

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

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

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

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

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

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

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

                  // 加载每个插件文件
                  foreach (string pluginFile in pluginFiles)
                  {
                        try
                        {
                            if (extension == ".dll")
                            {
                              // 特殊处理DLL文件
                              LoadDllFile(pluginFile, editor);
                            }
                            else
                            {
                              // 使用相应的命令加载插件文件
                              string formattedPath = pluginFile.Replace("\\", "/");
                              string command = string.Format(loadCommand, formattedPath);
                              doc.SendStringToExecute(command + "\n", true, false, false);

                              // 命令行输出提示信息
                              editor?.WriteMessage($"已加载{extension}文件: {Path.GetFileName(pluginFile)}\n");
                            }
                        }
                        catch (SystemException ex)
                        {
                            Application.ShowAlertDialog($"加载{extension}文件时出错 {pluginFile}: {ex.Message}");
                        }
                  }
                }
            }
            catch (SystemException ex)
            {
                Application.ShowAlertDialog($"初始化插件加载器时出错: {ex.Message}");
            }
      }

      // 加载DLL文件的方法
      private void LoadDllFile(string dllPath, Editor editor)
      {
            try
            {
                // 使用反射加载DLL
                Assembly.LoadFrom(dllPath);
                editor?.WriteMessage($"已加载DLL文件: {Path.GetFileName(dllPath)}\n");
            }
            catch (SystemException ex)
            {
                // 使用SystemException而不是Exception
                throw new SystemException($"无法加载DLL文件 {dllPath}: {ex.Message}");
            }
      }

      // 可选:添加一个命令以便手动重新加载所有插件
      
      public void ReloadPluginsCommand()
      {
            LoadPluginFiles();
      }

      // 可选:添加一个命令以便加载特定类型的插件
      
      public void LoadPluginTypeCommand()
      {
            Document doc = Application.DocumentManager.MdiActiveDocument;
            Editor ed = doc.Editor;

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

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

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

            string selectedType = pr.StringResult;

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

                // 获取选中的插件类型信息
                var pluginType = _pluginTypes;
                string folderName = pluginType.folder;
                string loadCommand = pluginType.loadCommand;

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

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

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

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

                // 加载每个插件文件
                foreach (string pluginFile in pluginFiles)
                {
                  try
                  {
                        if (selectedType == ".dll")
                        {
                            // 特殊处理DLL文件
                            LoadDllFile(pluginFile, ed);
                        }
                        else
                        {
                            // 使用相应的命令加载插件文件
                            string formattedPath = pluginFile.Replace("\\", "/");
                            string command = string.Format(loadCommand, formattedPath);
                            doc.SendStringToExecute(command + "\n", true, false, false);

                            // 命令行输出提示信息
                            ed.WriteMessage($"已加载{selectedType}文件: {Path.GetFileName(pluginFile)}\n");
                        }
                  }
                  catch (SystemException ex)
                  {
                        Application.ShowAlertDialog($"加载{selectedType}文件时出错 {pluginFile}: {ex.Message}");
                  }
                }
            }
            catch (SystemException ex)
            {
                Application.ShowAlertDialog($"加载{selectedType}类型插件时出错: {ex.Message}");
            }
      }
    }
}

zyx1029 发表于 2025-9-17 17:58:04

bloodtempt 发表于 2025-9-17 13:41
方便发源码么

自己根据CAD版本编译吧。

bloodtempt 发表于 2025-9-18 10:18:33

zyx1029 发表于 2025-9-17 17:58
自己根据CAD版本编译吧。

非常感谢无私奉献
页: 1 [2] 3
查看完整版本: CAD插件自动加载