Skip to content

Instantly share code, notes, and snippets.

@ricaun
Last active February 25, 2025 12:45
Show Gist options
  • Save ricaun/ff6814faf407ee044b93ee8e787f628c to your computer and use it in GitHub Desktop.
Save ricaun/ff6814faf407ee044b93ee8e787f628c to your computer and use it in GitHub Desktop.
Revit api extension to close all or active UI documents.
using Autodesk.Revit.UI;
using System;
using UIFramework;
namespace RevitTest.UIDocument.Tests
{
///<summary>
/// Provides extension methods for closing UI documents in the Revit application.
/// </summary>
public static class UIDocumentCloseExtension
{
/// <summary>
/// Closes all open UI documents in the Revit application.
/// </summary>
/// <param name="uiapp">The UIApplication instance.</param>
/// <param name="saveModified">Indicates whether to save modified documents.</param>
public static void CloseAllUIDocument(this UIApplication uiapp, bool saveModified = false)
{
using (new DialogBoxShowingForceResultYesNo(uiapp, saveModified))
{
foreach (var frameControl in MainWindow.getMainWnd().getAllViews())
{
frameControl.closeWindow();
}
}
}
/// <summary>
/// Closes the active UI document in the Revit application.
/// </summary>
/// <param name="uiapp">The UIApplication instance.</param>
/// <param name="saveModified">Indicates whether to save the modified document.</param>
public static void CloseActiveUIDocument(this UIApplication uiapp, bool saveModified = false)
{
var frameManager = MainWindow.getMainWnd().frameManager;
var activeFrameControl = frameManager.onGetActiveFrame();
if (activeFrameControl is null) return;
var activeFrameHost = activeFrameControl.Content as MFCMDIFrameHost;
var activeDocument = activeFrameHost.document;
var allViews = frameManager.getAllMDIFrames();
using (new DialogBoxShowingForceResultYesNo(uiapp, saveModified))
{
foreach (var frameControl in allViews)
{
var frameHost = frameControl.Content as MFCMDIFrameHost;
if (frameHost.document == activeDocument)
{
frameControl.closeWindow();
}
}
}
}
/// <summary>
/// Helper class to force a specific result for dialog boxes shown in the Revit application.
/// </summary>
public class DialogBoxShowingForceResultYesNo : IDisposable
{
private readonly UIApplication uiapp;
readonly private bool resultYes;
/// <summary>
/// Initializes a new instance of the <see cref="DialogBoxShowingForceResultYesNo"/> class.
/// </summary>
/// <param name="uiapp">The UIApplication instance.</param>
/// <param name="resultYes">Indicates whether to force a 'Yes' result for dialog boxes.</param>
public DialogBoxShowingForceResultYesNo(UIApplication uiapp, bool resultYes)
{
this.uiapp = uiapp;
this.resultYes = resultYes;
uiapp.DialogBoxShowing += OnDialogBoxShowing;
}
/// <summary>
/// Event handler for the DialogBoxShowing event.
/// </summary>
/// <param name="sender">The event sender.</param>
/// <param name="e">The event arguments.</param>
void OnDialogBoxShowing(object sender, Autodesk.Revit.UI.Events.DialogBoxShowingEventArgs e)
{
var uiapp = sender as UIApplication;
var result = resultYes ? TaskDialogResult.Yes : TaskDialogResult.No;
e.OverrideResult((int)result);
}
/// <summary>
/// Disposes the instance and unsubscribes from the DialogBoxShowing event.
/// </summary>
public void Dispose()
{
uiapp.DialogBoxShowing -= OnDialogBoxShowing;
}
}
}
}
@ricaun
Copy link
Author

ricaun commented Feb 19, 2025

This code require UIFramework reference.

Included in the Revit_All_Main_Versions_API_x64 package.

<PackageReference Include="Revit_All_Main_Versions_API_x64" Version="$(RevitVersion).*" IncludeAssets="build; compile" PrivateAssets="All" />

or in the Nice3point.Revit.Api.UIFramework package.

<PackageReference Include="Nice3point.Revit.Api.UIFramework" Version="$(RevitVersion).*"/>

@ricaun
Copy link
Author

ricaun commented Feb 25, 2025

[Transaction(TransactionMode.Manual)]
public class CommandCloseActiveUIDocument : IExternalCommand
{
    public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elementSet)
    {
        UIApplication uiapp = commandData.Application;

        uiapp.CloseActiveUIDocument();

        return Result.Succeeded;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment