Rename Line Patterns
Revit API Macro
Description
The following piece of code retrieves all line patterns matching a naming prefix and modifies their name. This code is an evolution of Matthew Nelson's example. Thanks Matt!
Use at your own risk. Share your thoughts.
public void renameLinePatterns()
{
//Get the current document
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
//Get all line patters starting with PREFIX
var collector = new FilteredElementCollector(doc)
.OfClass(typeof(LinePatternElement))
.Where(i => i.Name.StartsWith("PREFIX")).ToList();
//Add them to a list of ids - this is redundant but makes this code reusable for other purposes
List<ElementId> ids = new List<ElementId>();
for (int i = 0; i < collector.Count(); i++)
{
ids.Add(collector[i].Id);
}
//Start the transaction that will modify your document
using(Transaction t = new Transaction(doc,"Rename LinePatterns"))
{
t.Start();
try
{
foreach (ElementId id in ids)
{
//Get the line pattern element
LinePatternElement lpe = doc.GetElement(id) as LinePatternElement;
if (lpe != null)
{
//Get the line pattern name
string lpeName = lpe.Name;
//Modify the name of the line pattern
lpe.Name = lpeName + "-FOO";
}
}
}
catch (Exception)
{
t.RollBack();
TaskDialog.Show("Rename LinePatterns","Renaming failed");
return;
}
t.Commit();
TaskDialog.Show("Rename LinePatterns","Renaming complete");
}
}
Platform
Revit.
Type
Revit API Macro.