By Joe Ye
If calling Level.Name property to change a level's name, all its corresponding views' name are changed automatically. Here is the code snippet.
Transaction trans = new Transaction(doc);
trans.Start("ChangeLevelName");
FilteredElementCollector collector =
new FilteredElementCollector(doc);
collector.OfClass(typeof(Level));
Level level1 = null;
foreach (Element elem in collector)
{
level1 = elem as Level;
if (level1 != null)
break;
}
level1.Name = "NewName";
trans.Commit();
There is no message box showing to allow users to decide if change the corresponding views’ name.
How can I change a level name like the way Revit UI command behaves?
Solution
There are two ways to change a level name via API.
1. Via the Level.Name property as showed above.
2. Changing the "Name" parameter of the level.
Method 1 changes all corresponding views' name after the level name is changed.
The behavior of method 2 is just like the UI command. It can display a message dialog to accept users' decision if changing corresponding views' name.
Here is the code fragment showing the usage.
Transaction trans = new Transaction(doc);
trans.Start("ChangeLevelName");
FilteredElementCollector collector =
new FilteredElementCollector(doc);
collector.OfClass(typeof(Level));
Level level1 = null;
foreach (Element elem in collector)
{
level1 = elem as Level;
if (level1 != null)
break;
}
//level1.Name = "NewName";
Parameter paramLevel = level1.get_Parameter("Name");
paramLevel.Set("NewName");
trans.Commit();