Files
AddinManager/AddInManager/AIM.cs

163 lines
5.0 KiB
C#
Raw Permalink Normal View History

2025-09-04 09:53:20 +08:00
using System;
using System.Windows;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace AddInManager
{
public sealed class AIM
{
public Result ExecuteCommand(ExternalCommandData data, ref string message, ElementSet elements, bool faceless)
{
if (ActiveCmd != null && faceless)
{
return RunActiveCommand(data, ref message, elements);
}
var mainWindow = new Wpf.MainWindow(this);
var dialogResult = mainWindow.ShowDialog();
if (dialogResult != true)
{
return Result.Cancelled;
}
// 窗口关闭后,检查是否有选中的命令需要执行
if (ActiveCmd != null && ActiveCmdItem != null)
{
return RunActiveCommand(data, ref message, elements);
}
return Result.Succeeded; // 如果没有命令要执行,返回成功
}
public string ActiveTempFolder { get; set; } = string.Empty;
private Result RunActiveCommand(ExternalCommandData data, ref string message, ElementSet elements)
{
2026-01-16 21:03:48 +08:00
// 防御性检查:确保 ActiveCmd 不为空
if (this.ActiveCmd == null)
{
MessageBox.Show("错误ActiveCmd 为 null");
return Result.Failed;
}
// 防御性检查:确保 ActiveCmdItem 不为空
if (this.ActiveCmdItem == null)
{
MessageBox.Show("错误ActiveCmdItem 为 null");
return Result.Failed;
}
2025-09-04 09:53:20 +08:00
var filePath = ActiveCmd.FilePath;
2026-01-16 21:03:48 +08:00
// 检查文件是否存在
if (!System.IO.File.Exists(filePath))
{
MessageBox.Show($"错误:找不到文件 {filePath}");
return Result.Failed;
}
2025-09-04 09:53:20 +08:00
var assemLoader = new AssemLoader();
2026-01-16 21:03:48 +08:00
Result result = Result.Failed; // 默认失败
2025-09-04 09:53:20 +08:00
try
{
assemLoader.HookAssemblyResolve();
2026-01-16 21:03:48 +08:00
2025-09-04 09:53:20 +08:00
var assembly = assemLoader.LoadAddinsToTempFolder(filePath, false);
2026-01-16 21:03:48 +08:00
if (assembly == null)
2025-09-04 09:53:20 +08:00
{
2026-01-16 21:03:48 +08:00
message = "Assembly 加载失败,返回了 null";
2025-09-04 09:53:20 +08:00
result = Result.Failed;
}
else
{
ActiveTempFolder = assemLoader.TempFolder;
2026-01-16 21:03:48 +08:00
string className = ActiveCmdItem.FullClassName;
if (string.IsNullOrEmpty(className)) throw new Exception("类名为空");
var instanceObj = assembly.CreateInstance(className);
if (instanceObj == null)
2025-09-04 09:53:20 +08:00
{
2026-01-16 21:03:48 +08:00
message = $"无法创建实例: {className}。请检查类名是否正确或是否有无参构造函数。";
2025-09-04 09:53:20 +08:00
result = Result.Failed;
}
else
{
2026-01-16 21:03:48 +08:00
var externalCommand = instanceObj as IExternalCommand;
if (externalCommand == null)
{
message = $"{className} 没有实现 IExternalCommand 接口";
result = Result.Failed;
}
else
{
ActiveEC = externalCommand;
result = ActiveEC.Execute(data, ref message, elements);
}
2025-09-04 09:53:20 +08:00
}
}
}
catch (Exception ex)
{
2026-01-16 21:03:48 +08:00
MessageBox.Show($"执行异常:\n{ex.Message}\n\n堆栈:\n{ex.StackTrace}");
2025-09-04 09:53:20 +08:00
result = Result.Failed;
}
finally
{
2026-01-16 21:03:48 +08:00
// 确保清理工作安全执行
try
{
assemLoader.UnhookAssemblyResolve();
assemLoader.CopyGeneratedFilesBack();
}
catch { /* 忽略清理时的错误,防止掩盖主异常 */ }
2025-09-04 09:53:20 +08:00
}
return result;
}
public static AIM Instance
{
get
{
if (m_inst == null)
{
lock (typeof(AIM))
{
2026-01-02 09:37:33 +08:00
m_inst ??= new AIM();
2025-09-04 09:53:20 +08:00
}
}
return m_inst;
}
}
private AIM()
{
AddinManager = new AddinManager();
ActiveCmd = null;
ActiveCmdItem = null;
ActiveApp = null;
ActiveAppItem = null;
}
public IExternalCommand ActiveEC { get; set; }
public Addin ActiveCmd { get; set; }
public AddinItem ActiveCmdItem { get; set; }
public Addin ActiveApp { get; set; }
public AddinItem ActiveAppItem { get; set; }
public AddinManager AddinManager { get; set; }
private static volatile AIM m_inst;
}
}