Package to create a progress bar in Revit StatusBar for Revit API developers.
This project was generated by the ricaun.AppLoader Revit plugin.
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);
});
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);
}
}
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
}
}
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;
There are some code examples in the Commands.
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;
}
}
}
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;
}
}
}
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;
}
}
}
The BalloonUtils
class has some utility methods to show a balloon in Revit UI.
BalloonUtils.Show("Message", "Title/Category");
This project was inspired by OptionsBar.
This project is licensed under the MIT Licence.
Do you like this project? Please star this project on GitHub!