Skip to content

Commit 8306f9a

Browse files
authored
Update README.md
1 parent d7b6913 commit 8306f9a

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

README.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,93 @@ With the ProgressMeter library, you can easily integrate a progress bar into the
1616
**Dark Theme**
1717

1818
![ProgressMeter](./rvt2024/progressmeterdark.gif)
19+
20+
## Code example
21+
```c#
22+
using System.Collections.Generic;
23+
using Autodesk.Revit.Attributes;
24+
using Autodesk.Revit.DB;
25+
using Autodesk.Revit.UI;
26+
using SCADtools.Revit.UI;
27+
using System.Linq;
28+
using System;
29+
30+
namespace SCADtools.ProgressMeterSample
31+
{
32+
[TransactionAttribute(TransactionMode.Manual)]
33+
internal class Sample : IExternalCommand
34+
{
35+
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
36+
{
37+
UIApplication uiapp = commandData.Application;
38+
UIDocument uidoc = uiapp.ActiveUIDocument;
39+
Document doc = uidoc.Document;
40+
41+
//Initialize ProgressMeter
42+
//By default the cancel button will be displayed
43+
//If you don't want to show the cancel button, you must initialize it by passing false as an argument in the constructor
44+
ProgressMeter progressMeter = new ProgressMeter();
45+
46+
try
47+
{
48+
List<Wall> walls = new FilteredElementCollector(doc)
49+
.WhereElementIsNotElementType()
50+
.OfClass(typeof(Wall))
51+
.Cast<Wall>()
52+
.ToList();
53+
54+
int count = walls.Count;
55+
56+
if (count > 0)
57+
{
58+
//Set limit number
59+
progressMeter.SetLimit(count);
60+
61+
//Initialize and make it visible (no display string)
62+
progressMeter.Start();
63+
64+
using (Transaction tr = new Transaction(doc, "ProgressMeter - Updating Wall comments"))
65+
{
66+
tr.Start();
67+
68+
int i = 1;
69+
70+
foreach (Wall wall in walls)
71+
{
72+
//Set display string to be displayed to user
73+
//If the display string is static it can be assigned in the Start() method of the ProgressMeter class
74+
progressMeter.DisplayString = "Updating wall comments " + i.ToString() + " of " + count.ToString();
75+
76+
//Increment Progress Meter
77+
progressMeter.MeterProgress();
78+
79+
//Update comments
80+
Parameter parameter = wall.get_Parameter(BuiltInParameter.ALL_MODEL_INSTANCE_COMMENTS);
81+
parameter.Set("Comment " + i.ToString());
82+
83+
//If the cancel button is pressed
84+
if (progressMeter.StopWorker) break;
85+
86+
i++;
87+
}
88+
89+
tr.Commit();
90+
}
91+
}
92+
93+
return Result.Succeeded;
94+
}
95+
catch (Exception ex)
96+
{
97+
message = ex.Message;
98+
return Result.Failed;
99+
}
100+
finally
101+
{
102+
//Stop and hide the ProgressMeter (IMPORTANT)
103+
progressMeter.Stop();
104+
}
105+
}
106+
}
107+
}
108+
```

0 commit comments

Comments
 (0)