using System;
using System.Text;
using Autodesk.Revit.DB.Events;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Events;
using FWindSoft.Events;
namespace FWindSoft.Revit
{
[Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
[Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
[Autodesk.Revit.Attributes.Journaling(Autodesk.Revit.Attributes.JournalingMode.NoCommandData)]
public class ExternalApplication : IExternalApplication
{
#region 构造函数
public ExternalApplication()
{
Events = new EventsWrapper();
Registers = new RevitRegisterManager(true);
}
#endregion
#region 私有相关
///
/// 事件包装器(不是每一个事件都进行了包装)
///
public EventsWrapper Events { get;private set; }
private RevitRegisterManager Registers { get; set; }
#endregion
public static ExternalApplication CurrentApp{ get;private set; }
public virtual Result OnStartup(UIControlledApplication application)
{
RevitVersion.AddInManager.CheckCancelAddin();
CurrentApp = this;
UIControlApp = application;
#region 核心区,与ShutDown顺序相反相反
#region 事件绑定
Events.AddBindings(application);
Events.AddBindings(application.ControlledApplication);
#endregion
Registers.AddEvents(this);
AttachExecute(application);
#endregion
//加载application
Idling += ExternalApplication_Idling;
return Result.Succeeded;
}
/*此类由revit初始化
*/
public virtual Result OnShutdown(UIControlledApplication application)
{
CurrentApp = null;
UIControlApp = application;
ShutExecute(application);
Registers.RemoveEvents(this);
#region 事件绑定
Events.RemoveBindings(application);
Events.RemoveBindings(application.ControlledApplication);
#endregion
return Result.Succeeded;
}
private static void ExternalApplication_Idling(object sender, IdlingEventArgs e)
{
RevitCore.InitCore((UIApplication) sender);
if (CurrentApp != null)
{
CurrentApp.Idling -= ExternalApplication_Idling;
}
}
#region 属性相关
///
/// 关联UIControlApp
///
public UIControlledApplication UIControlApp { get; private set; }
#endregion
#region 事件相关扩展
public event EventHandler Idling
{
add { Events["Idling"]?.AddEventHandler(value); }
remove { Events["Idling"]?.RemoveEventHandler(value); }
}
public event EventHandler DocumentOpened
{
add { Events["DocumentOpened"]?.AddEventHandler(value); }
remove { Events["DocumentOpened"]?.RemoveEventHandler(value); }
}
#endregion
#region 自定义扩展
///
/// 附加启动时的初始化设置
///
protected virtual void AttachExecute(UIControlledApplication application)
{
//读一个初始化的配置文件,然后顺序执行配置文件中的各项操作
//1、定义一个命令度配置文件
//2、根据配置文件中的相关设置,再进行执行
DockableWindow win = new DockableWindow();
win.RegisterDockableWindow(CurrentApp.UIControlApp, RevitCustomDockablePanels.Default);
}
///
/// 附加启动时的初始化设置
///
protected virtual void ShutExecute(UIControlledApplication application)
{
}
#endregion
#region 未处理异常捕获
///
/// 不会未处理异常
///
protected void CatchUnhandledException()
{
AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
}
///
/// 去掉捕获未处理异常
///
protected void NoCatchUnhandledException()
{
AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException;
}
private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
StringBuilder builder = new StringBuilder();
var exception = e.ExceptionObject as Exception;
if (exception != null)
{
builder.AppendLine("Messsage:" + exception.Message);
builder.AppendLine("Trace:" + exception.StackTrace);
}
else
{
builder.Append("未处理异常");
}
TaskDialog.Show("捕获异常", builder.ToString());
}
#endregion
}
}