Skip to content

Commit

Permalink
change logic
Browse files Browse the repository at this point in the history
  • Loading branch information
KennanChan committed Dec 21, 2017
1 parent 3800114 commit 6863748
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 22 deletions.
31 changes: 28 additions & 3 deletions RevitAPIToolbox/Common/ParameterizedExternalEventHandler.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
using Autodesk.Revit.UI;
using System.Collections.Generic;
using Autodesk.Revit.UI;

namespace Techyard.Revit.Common
{
public abstract class ParameterizedExternalEventHandler<T> : IExternalEventHandler
{
public T EventParameter { get; internal set; }
private static Queue<T> Parameters { get; } = new Queue<T>();

public abstract void Execute(UIApplication app);
public T EventParameter
{
get
{
lock (Parameters)
{
return Parameters.Dequeue();
}
}
set
{
lock (Parameters)
{
Parameters.Enqueue(value);
}
}
}

public void Execute(UIApplication app)
{
var parameter = EventParameter;
Execute(app, parameter);
}

public abstract void Execute(UIApplication app, T parameter);

public abstract string GetName();
}
Expand Down
20 changes: 2 additions & 18 deletions RevitAPIWheel/ExternalEvent/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,13 @@ internal class App : IExternalApplication
{
public Result OnStartup(UIControlledApplication application)
{
application.Idling += Application_Idling;
ExternalEventWheel.Initialize(application);
return Result.Succeeded;
}

/// <summary>
/// <para>An event that revit signals repeatedly at idle time</para>
/// <para>Revit在空闲时间反复调用的回调方法,当做 white(true){} 无限循环使用</para>
/// </summary>
/// <param name="sender">The revit UI level application</param>
/// <param name="e">Arguments to config the event</param>
private static void Application_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
{
//Internal method for the main process to get a raised external event handler to execute
var handler = ExternalEventWheel.TryGetEvent();
if (null == handler) return;
var app = sender as UIApplication;
if (null == app) return;
handler.Execute(app);
}

public Result OnShutdown(UIControlledApplication application)
{
application.Idling -= Application_Idling;
ExternalEventWheel.Destroy();
return Result.Succeeded;
}
}
Expand Down
55 changes: 54 additions & 1 deletion RevitAPIWheel/ExternalEvent/ExternalEventWheel.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.UI;

namespace Techyard.Revit.Wheel.ExternalEvent
{
Expand Down Expand Up @@ -30,6 +32,57 @@ private ExternalEventWheel(IExternalEventHandlerWheel handler)
/// </summary>
private IExternalEventHandlerWheel Handler { get; }

private static object _app;

/// <summary>
/// Initialize external event system
/// </summary>
/// <param name="app"></param>
public static void Initialize(UIApplication app)
{
if (null != _app) return;
_app = app;
app.Idling += App_Idling;
}

/// <summary>
/// Initialize external event system
/// </summary>
/// <param name="app"></param>
public static void Initialize(UIControlledApplication app)
{
if (null != _app) return;
_app = app;
app.Idling += App_Idling;
}

/// <summary>
/// Stop the external event system
/// </summary>
public static void Destroy()
{
if (null == _app) return;
if (_app is UIApplication)
((UIApplication)_app).Idling -= App_Idling;
else if (_app is UIControlledApplication)
((UIControlledApplication)_app).Idling -= App_Idling;
}

/// <summary>
/// <para>An event that revit signals repeatedly at idle time</para>
/// <para>Revit在空闲时间反复调用的回调方法,当做 white(true){} 无限循环使用</para>
/// </summary>
/// <param name="sender">The revit UI level application</param>
/// <param name="e">Arguments to config the event</param>
private static void App_Idling(object sender, Autodesk.Revit.UI.Events.IdlingEventArgs e)
{
var handler = TryGetEvent();
if (null == handler) return;
var app = sender as UIApplication;
if (null == app) return;
handler.Execute(app);
}

/// <summary>
/// <para>Create an external event to raise</para>
/// <para>创建一个外部事件</para>
Expand Down Expand Up @@ -61,7 +114,7 @@ public void Raise()
/// <para>尝试获取一个待执行的处理程序</para>
/// </summary>
/// <returns>The first external event handler</returns>
internal static IExternalEventHandlerWheel TryGetEvent()
private static IExternalEventHandlerWheel TryGetEvent()
{
lock (Events)
{
Expand Down
1 change: 1 addition & 0 deletions Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="TestExternalEventHandler.cs" />
<Compile Include="TestGeometry.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TestSchema.cs" />
Expand Down
72 changes: 72 additions & 0 deletions Test/TestExternalEventHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Techyard.Revit.Common;

namespace Test
{
[Transaction(TransactionMode.Manual)]
public class TestExternalEventHandler : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
var handler = new CustomEventHandler2();
var event2 = ExternalEvent.Create(handler);
if (ExternalEventManager.Manager.Register(new CustomEventHandler()))
{
ExternalEventManager.Manager.Raise<CustomEventHandler, string>("刘一");
event2.Raise();
ExternalEventManager.Manager.Raise<CustomEventHandler, string>("陈二");
event2.Raise();
ExternalEventManager.Manager.Raise<CustomEventHandler, string>("张三");
event2.Raise();
ExternalEventManager.Manager.Raise<CustomEventHandler, string>("李四");
event2.Raise();
ExternalEventManager.Manager.Raise<CustomEventHandler, string>("王五");
event2.Raise();
ExternalEventManager.Manager.Raise<CustomEventHandler, string>("赵六");
event2.Raise();
ExternalEventManager.Manager.Raise<CustomEventHandler, string>("孙七");
event2.Raise();
ExternalEventManager.Manager.Raise<CustomEventHandler, string>("周八");
event2.Raise();
ExternalEventManager.Manager.Raise<CustomEventHandler, string>("吴九");
event2.Raise();
ExternalEventManager.Manager.Raise<CustomEventHandler, string>("郑十");
event2.Raise();
}
return Result.Succeeded;
}

private class CustomEventHandler : ParameterizedExternalEventHandler<string>
{
private int Count { get; set; }

public override void Execute(UIApplication app, string parameter)
{
TaskDialog.Show("外部事件", $"{GetName()}事件第{++Count}次调用:{parameter}");
}

public override string GetName()
{
return "Test";
}
}

private class CustomEventHandler2 : IExternalEventHandler
{
private int Count { get; set; }

public void Execute(UIApplication app)
{
TaskDialog.Show("外部事件", $"{GetName()}事件第{++Count}次调用");
}

public string GetName()
{
return "Test";
}
}
}
}

0 comments on commit 6863748

Please sign in to comment.