Skip to content

Commit

Permalink
DialogBoxResolver to cancel dialog box before revit initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
ricaun committed Sep 6, 2024
1 parent 452f03c commit 2e18c3b
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [1.4.0] / 2024-09-05
### Features
- Support `NUnit.Timeout` option to abort application. (default '10' minutes) (Fix: #51)
- Autoclose `VIEWER MODE` warning dialog box when Revit starts. (Fix: #52)
### Application
- Add `DialogBoxResolver` to cancel dialog box before revit initialization.
### Console
- Change `TimeoutMinutesDefault` to 10 minutes in the `RevitTestUtils`.
- Remove deprecated `wait` option and `RevitDebugUtils`.
Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<Project>
<PropertyGroup>
<Version>1.4.0-alpha.1</Version>
<Version>1.4.0-beta</Version>
</PropertyGroup>
</Project>
7 changes: 6 additions & 1 deletion ricaun.RevitTest.Application/Revit/App.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class App : IExternalApplication
private static PipeTestServer PipeTestServer;
private static RevitTaskService RevitTask;
private static RevitBusyService RevitBusyService;
private static DialogBoxResolver DialogBoxResolver;
private static bool IsTestRunning = false;

private const int TestThreadSleepMin = 50;
Expand All @@ -38,6 +39,9 @@ public Result OnStartup(UIControlledApplication application)
RevitTask = new RevitTaskService(application);
RevitTask.Initialize();

DialogBoxResolver = new DialogBoxResolver(application);
DialogBoxResolver.Initialize();

Log.WriteLine();
Log.WriteLine($"{AppUtils.GetInfo()}");

Expand Down Expand Up @@ -219,9 +223,10 @@ public Result OnShutdown(UIControlledApplication application)
});

ribbonPanel?.Remove();

PipeTestServer?.Dispose();
RevitBusyService?.Dispose();

DialogBoxResolver?.Dispose();
RevitTask?.Dispose();

Log.Finish();
Expand Down
64 changes: 64 additions & 0 deletions ricaun.RevitTest.Application/Revit/Utils/DialogBoxResolver.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
using Autodesk.Revit.UI;
using System;

namespace ricaun.RevitTest.Application.Revit.Utils
{
/// <summary>
/// DialogBoxResolver resolve the dialog box by DialogId and override the result to cancel.
/// </summary>
/// <remarks>The resolver only works before Revit initialize.</remarks>
public class DialogBoxResolver : IDisposable
{
private readonly UIControlledApplication application;
private readonly string[] TaskDialogIds;
public DialogBoxResolver(UIControlledApplication application)
{
this.application = application;
this.TaskDialogIds = new string[]
{
"TaskDialog_License_Current_Status_Demo_Mode", // Viewer Mode
"TaskDialog_DNSMTemplate" // Revit Warning
};
}

/// <summary>
/// Initialize the events to resolve the dialog box.
/// </summary>
/// <remarks>The Initialize is ignored if the <see cref="Autodesk.Revit.UI.UIControlledApplication.IsLateAddinLoading"/>.</remarks>
public virtual void Initialize()
{
if (this.application.IsLateAddinLoading) return;
Dispose();
this.application.DialogBoxShowing += OnDialogBoxShowing;
this.application.ControlledApplication.ApplicationInitialized += OnApplicationInitialized;
}

/// <summary>
/// Dispose the events.
/// </summary>
public virtual void Dispose()
{
this.application.DialogBoxShowing -= OnDialogBoxShowing;
this.application.ControlledApplication.ApplicationInitialized -= OnApplicationInitialized;
}

private void OnDialogBoxShowing(object sender, Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs e)
{
if (TaskDialogIds is null) return;
foreach (var dialogId in TaskDialogIds)
{
if (dialogId == e.DialogId)
{
Console.WriteLine($"{nameof(DialogBoxResolver)}: [{e.DialogId}]");
e.OverrideResult((int)TaskDialogResult.Cancel);
return;
}
}
}
private void OnApplicationInitialized(object sender, Autodesk.Revit.DB.Events.ApplicationInitializedEventArgs e)
{
Dispose();
}
}

}
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 2e18c3b

Please sign in to comment.