- 积分
- 2821
- 明经币
- 个
- 注册时间
- 2024-7-28
- 在线时间
- 小时
- 威望
-
- 金钱
- 个
- 贡献
-
- 激情
-
|

楼主 |
发表于 2025-9-17 17:57:02
|
显示全部楼层
 - 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}");
- }
- }
- // 可选:添加一个命令以便手动重新加载所有插件
- [CommandMethod("RELOADPLUGINS")]
- public void ReloadPluginsCommand()
- {
- LoadPluginFiles();
- }
- // 可选:添加一个命令以便加载特定类型的插件
- [CommandMethod("LOADPLUGINTYPE")]
- 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[selectedType];
- 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}");
- }
- }
- }
- }
|
|