-
Notifications
You must be signed in to change notification settings - Fork 0
/
RenameSheetsToMatchViewNames.cs
92 lines (75 loc) · 2.71 KB
/
RenameSheetsToMatchViewNames.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
namespace CanBIM
{
using System;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
public partial class ThisApplication
{
/// <summary>
/// Renames all sheets that contain exactly one floor plan to match the name of that view
/// Copy the whole method into the macro file on your machine,
/// or download this file and add it to the solution in the macro editor
/// </summary>
public void RenameSheetsToMatchViewNames()
{
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
// Retrieve all sheets, cast them to type ViewSheet and save them in a list
// It is important to use ToList or ToElements when modifying elements
// from a FilteredElementCollector
var sheets = new FilteredElementCollector(doc)
.OfClass(typeof(ViewSheet))
.Cast<ViewSheet>().ToList();
// Initialize a counter that will identify how many sheets have been modified
int renamedSheetsCount = 0;
// Open a transaction that allows changes to be made to the model
using (Transaction trans = new Transaction(doc, "Rename sheets"))
{
// Start the transaction
trans.Start();
// Loop through each sheet retrieved above
foreach (var sheet in sheets)
{
// Get all the views placed on the sheet
var viewsOnSheet = sheet.GetAllPlacedViews();
// If there is not exactly 1 view on the sheet,
// Skip this sheet and continue to the next one
if (viewsOnSheet.Count != 1)
{
continue;
}
// Get the id of the view
// The single call is used because we know there is only 1
// based on the check above
var viewId = viewsOnSheet.Single();
// Get the view from the id, and cast it to type View
var view = (View)doc.GetElement(viewId);
// If the view is not a floor plan,
// skip the sheet and continue to the next one
if (view.ViewType != ViewType.FloorPlan)
{
continue;
}
// Open a sub-transaction for the upcoming modification to the sheet
using (SubTransaction subTrans = new SubTransaction(doc))
{
// Start the sub-transaction
subTrans.Start();
// Set the sheet name to the view name
sheet.Name = view.Name;
// Commit the change made
subTrans.Commit();
// Increment the sheet counter by 1
renamedSheetsCount++;
}
// End of foreach loop
}
// Commit the transaction
trans.Commit();
}
// Show a message indicating how many sheets were modified
TaskDialog.Show("Done", "Renamed " + renamedSheetsCount + " sheets");
}
}
}