Close Active Project/Document using UIFramework

Close Active Project/Document using UIFramework

ricaun
Advisor Advisor
1,007 Views
4 Replies
Message 1 of 5

Close Active Project/Document using UIFramework

ricaun
Advisor
Advisor

Hello,

 

For some reason Revit API does not allow to close an ActiveUIDocument, and I know is possible to close using press F4 or PostCommand.Close.

 

I want to share a different approach using UIFramework reference.

 

Here is the extension for the UIApplication to enable CloseAllUIDocument and CloseActiveUIDocument.

 

Basically the code close UI document window and use the event DialogBoxShowing to force to save or not the document if the save popup happens.

 

Here is a simple Command to close the ActiveUIDocument.

 

 

[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;
    }
}

 

 

To use the extension your project need to have the UIFramework reference, you can add the reference using the packages below.

 

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

 

or

 

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

 

 

See yaa!

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

Accepted solutions (1)
1,008 Views
4 Replies
Replies (4)
Message 2 of 5

nice3point
Advocate
Advocate

This should definitely be in the default API, why such restrictions🙈

Message 3 of 5

rhanzlick
Advocate
Advocate

Is there a reason you would want to close a UIDocument vs the UIDocument.Document? What’s the nuance/usecase for the former? (Just out of curiosity.)

0 Likes
Message 4 of 5

ricaun
Advisor
Advisor
Accepted solution

@rhanzlick wrote:

Is there a reason you would want to close a UIDocument vs the UIDocument.Document? What’s the nuance/usecase for the former? (Just out of curiosity.)



Is not possible to close the active document, the Remarks in the documentation for the Close method mention that.

 

"The currently active document may not be closed by this function. It can only be closed via Revit's UI."

 

So if you use OpenAndActivateDocument and need to close the active document the Close method does not work.

 

UIDocument uidoc = uiapp.OpenAndActivateDocument("Sample.rvt");
uidoc.Document.Close(false); // Fail to close.

 

Using the extension should work to close the active document using the Revit UI.

 

UIDocument uidoc = uiapp.OpenAndActivateDocument("Sample.rvt");
uiapp.CloseActiveUIDocument();

 

 

 

Luiz Henrique Cassettari

ricaun.com - Revit API Developer

AppLoader EasyConduit WireInConduit ConduitMaterial CircuitName ElectricalUtils

0 Likes
Message 5 of 5

K.Calero_Arup
Contributor
Contributor

@ricaun  Somehow I tested willy nilly and got this working with your extension methods, although I'm passing commandlinks instead of yes/no in the OnDialogBoxShowing handler. So it can be applied to any dialog right? (https://rvtdocs.com/2025/bde54cc2-a445-8610-0ced-1851f04eea18)

0 Likes