I thought I would give a follow up to this as I have further developed it for inclusion in an add-in package, but realized the ElementId > View.Id does not work since you may be trying to change references that were created Long before the View you want to change them too.
To remedy this, I reviewed that noticed that the Viewers have about half as many parameters as a "real" view has. I have elected to look for the "Discipline" parameter as the Reference Views do not contain that parameter. There are others, but that was just the first one that didn't show up on the list. I also added a handler for checking that the View Types are the same.
private void BtnChange_Click(object sender, RoutedEventArgs e)
{
//Use a try so an exception doesn't crash revit
try
{
//Cast the Old and New views from the Combobox Selected Values
View OldView = (View)ComboBoxOldReference.SelectedValue;
View NewView = (View)ComboBoxNewReference.SelectedValue;
//Use a Filter Rule to Collect ONLY views that have the View Name of the OldView
FilterRule rule = ParameterFilterRuleFactory.CreateEqualsRule(new ElementId(BuiltInParameter.VIEW_NAME), OldView.Name, true);
//Create a Filter from the Filter Rule
ElementParameterFilter filter = new ElementParameterFilter(rule);
//Use a Collecter with the Filter to get Viewers that are not ElementTypes
if (OldView.ViewType == NewView.ViewType)
{
using (FilteredElementCollector fec = new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Viewers).WherePasses(filter).WhereElementIsNotElementType())
{
//Create a new Transaction to make make changes to the Document
using (Transaction Trans = new Transaction(doc))
{
//Start the Transaction and Name it for the Undo/rdo Menu
Trans.Start("Change View Reference");
//Iterate each Viewer returned by the Collector
foreach (Element viewer in fec.ToElements())
{
//The Viewers do not contain the same parameters as the actual views, so we check for one of them
if (viewer.GetParameters("Discipline").Count == 0)
{
ReferenceableViewUtils.ChangeReferencedView(doc, viewer.Id, NewView.Id);
}
}
//Commit the Transaction to save the changes
Trans.Commit();
}
}
//Set the Dialog result to true so the Command will keep the changes
DialogResult = true;
//Close the Form
Close();
}
else
{
TaskDialog.Show("View Type Mismatch", "Current View and New View are not of the Same View Type\n\nCurrent View Type: "+OldView.ViewType.ToString()+"\nNew View Type: "+NewView.ViewType.ToString());
}
}
//Catch and Display any Exeptions that are thrown
catch (Exception ex)
{
TaskDialog.Show("Error", ex.ToString());
//Set the Dialog result to False so the Command will not keep any changes
DialogResult = false;
//Close the Form
Close();
}
}
Sean Page, AIA, NCARB, LEED APPartner, Computational Designer, Architect