Skip to content

Create a progress bar in Revit StatusBar for Revit API developers.

License

Notifications You must be signed in to change notification settings

ricaun-io/ricaun.Revit.UI.StatusBar

Repository files navigation

ricaun.Revit.UI.StatusBar

Revit 2019 Visual Studio 2022 Nuke License MIT Build Release

ricaun.Revit.UI.StatusBar

Package to create a progress bar in Revit StatusBar for Revit API developers.

ProgressBar

This project was generated by the ricaun.AppLoader Revit plugin.

RevitProgressBarUtils

The RevitProgressBarUtils has some utility methods to Run a loop and update the RevitProgressBar.

RevitProgressBarUtils.Run("Revit Elements", elements, (element) =>
{
    System.Console.WriteLine(element.Name);
});
int repeat = 100000;
RevitProgressBarUtils.Run("Revit Repeat", repeat, (i) =>
{
    System.Console.WriteLine(i);
});

RevitProgressBar

The RevitProgressBar class gives more control like the cancel button, it's implements the IDisposable interface, so it can be used in a using statement.

using (var revitProgressBar = new RevitProgressBar())
{
    revitProgressBar.Run("Revit Elements", elements, (element) =>
    {
        System.Console.WriteLine(element.Name);
    }
}

or...

using (var revitProgressBar = new RevitProgressBar())
{
    revitProgressBar.SetCurrentOperation("Revit Elements");
    foreach (var element in elements)
    {
        revitProgressBar.Increment();
        System.Console.WriteLine(element.Name);
    }
}

Cancel Button

The RevitProgressBar class allows to set a cancel button, in the constructor or using the SetHasCancelButton method.

using (var revitProgressBar = new RevitProgressBar())
{
    revitProgressBar.SetCurrentOperation("Revit Elements");
    revitProgressBar.SetHasCancelButton(true);
    revitProgressBar.Run(elements, (element) =>
    {
        System.Console.WriteLine(element.Name);
    });
    if (revitProgressBar.IsCancelling())
    {
        // RevitProgressBar Canceled
    }
}

or...

using (var revitProgressBar = new RevitProgressBar(true))
{
    revitProgressBar.Run("Revit Elements", elements, (element) =>
    {
        System.Console.WriteLine(element.Name);
    });
    if (revitProgressBar.IsCancelling())
    {
        // RevitProgressBar Canceled
    }
}

Methods and Configurations

The RevitProgressBar has some methods to configurations the progress bar behavior.

SetCurrentOperation - Set the current operation name.

revitProgressBar.SetCurrentOperation("CurrentOperation");

SetCurrentValue - Set the current value of the progress bar.

revitProgressBar.SetCurrentValue(0);

SetMinimumValue - Set the minimum value of the progress bar.

revitProgressBar.SetMinimumValue(0);

SetMaximumValue - Set the maximum value of the progress bar.

revitProgressBar.SetMaximumValue(100);

SetIsIndeterminate - Set the progress bar to indeterminated.

revitProgressBar.SetIsIndeterminate(true);

SetHasCancelButton - Set the cancel button to show.

revitProgressBar.SetHasCancelButton(true);

Cancel - Cancel the revit progress bar requested. (Run methods gonna be breaked.)

revitProgressBar.Cancel();

IsCancelling - Check if the cancel button was pressed.

if (revitProgressBar.IsCancelling()) { 
    // RevitProgressBar Canceled
}

InitializeMilliseconds - RevitProgressBar only appears after past this property value in milliseconds.

revitProgressBar.InitializeMilliseconds = 250;

RefreshMilliseconds - RevitProgressBar only updates the progress bar after this property value in milliseconds.

revitProgressBar.RefreshMilliseconds = 50;

Revit Commands Example

There are some code examples in the Commands.

CommandRevit

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using ricaun.Revit.UI.StatusBar;

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

            RevitProgressBarUtils.Run(uiapp.Application.VersionName, 100, (i) =>
            {
                System.Threading.Thread.Sleep(i);
            });

            return Result.Succeeded;
        }
    }
}

CommandRevitCancel

This command has a simple implementation with a cancel button.

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using ricaun.Revit.UI.StatusBar;

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

            using (var revitProgressBar = new RevitProgressBar(true))
            {
                revitProgressBar.Run(uiapp.Application.VersionName, 100, (i) =>
                {
                    System.Threading.Thread.Sleep(i);
                });

                if (revitProgressBar.IsCancelling())
                {
                    // RevitProgressBar Canceled
                }
            }

            return Result.Succeeded;
        }
    }
}

CommandRevitCancelTransaction

This commmand copy the selected elements 100 times with the cancel button to rollback.

using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using ricaun.Revit.UI.StatusBar;

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

            UIDocument uidoc = uiapp.ActiveUIDocument;
            Document document = uidoc.Document;
            View view = uidoc.ActiveView;
            Selection selection = uidoc.Selection;

            var elementIds = selection.GetElementIds();

            if (elementIds.Count == 0)
            {
                TaskDialog.Show("Revit", "Select elements to copy.");
                return Result.Failed;
            }

            using (var revitProgressBar = new RevitProgressBar(true))
            {
                using (Transaction transaction = new Transaction(document))
                {
                    transaction.Start("Copy Elements");

                    revitProgressBar.Run("Copy Elements", 100, (i) =>
                    {
                        ElementTransformUtils.CopyElements(document, elementIds, XYZ.BasisX * (i + 1));
                    });

                    if (revitProgressBar.IsCancelling())
                        transaction.RollBack();
                    else
                        transaction.Commit();
                }
            }

            return Result.Succeeded;
        }
    }
}

Utils

BalloonUtils

The BalloonUtils class has some utility methods to show a balloon in Revit UI.

BalloonUtils.Show("Message", "Title/Category");

Release

References

This project was inspired by OptionsBar.

License

This project is licensed under the MIT Licence.


Do you like this project? Please star this project on GitHub!