Change Phase of Revit Link in View Template
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I am trying to automate changing the phase of a Revit Link inside of a View Template.
Here is what happens when I create a new view using the Revit user interface. This same issue happens when I use the Revit API to automatically create the views in a project setup script.
- The new view from my model shows the New Construction phase (pic 1).
- However, the architectural model Revit Link has it set to a different phase, even when it is marked as "By Host View" (pic 2).
This means I have to change all my view templates to "Custom" for each Revit Link and manually set the right phase for each.
I have been able to use the following code to successfully change the Phase Filter in the Revit Link Display Setting, but have not been able to change the Phase. It seems this may not be exposed in the Revit API.
Are there any ideas why this may not be working?
using (Transaction tx = new Transaction(doc, "Set Revit Link Phase in View Template"))
{
tx.Start();
try
{
// Find the view template by name
ViewPlan viewPlan = doc.ActiveView as ViewPlan;
ElementId viewTemplateId = viewPlan.ViewTemplateId;
Autodesk.Revit.DB.View viewTemplate = doc.GetElement(viewTemplateId) as Autodesk.Revit.DB.View;
if (viewTemplate != null)
{
//System.Windows.MessageBox.Show("viewTemplate not null");
if (viewTemplate.IsTemplate)
{
//System.Windows.MessageBox.Show("ViewTemplate is template");
}
else
{
System.Windows.MessageBox.Show("View template is not a template");
return Result.Failed;
}
}
else
{
System.Windows.MessageBox.Show("View template not found");
return Result.Failed;
}
// Find the Revit Link instance by name or other criteria
FilteredElementCollector linkCollector = new FilteredElementCollector(doc);
RevitLinkInstance linkInstance = linkCollector.OfClass(typeof(RevitLinkInstance))
.Cast<RevitLinkInstance>()
.FirstOrDefault(); // Modify to filter by specific link name if needed
if (linkInstance == null)
{
System.Windows.MessageBox.Show("Error", "No Revit Link instance found.");
return Result.Failed;
}
// Find the desired phase by name
Autodesk.Revit.DB.Phase targetPhase = new FilteredElementCollector(doc)
.OfClass(typeof(Autodesk.Revit.DB.Phase))
.Cast<Autodesk.Revit.DB.Phase>()
.FirstOrDefault(p => p.Name == "Phase 1A"); // Replace with desired phase name
if (targetPhase == null)
{
System.Windows.MessageBox.Show("Phase 'Phase 1A' not found.");
return Result.Failed;
}
// Set the PhaseId of the Revit Link instance
Autodesk.Revit.DB.Document linkDoc = linkInstance.GetLinkDocument();
System.Windows.MessageBox.Show(linkDoc.Title);
Parameter phaseCreatedParameter = linkInstance.get_Parameter(BuiltInParameter.PHASE_CREATED);
System.Windows.MessageBox.Show(phaseCreatedParameter.Definition.Name + " - " + phaseCreatedParameter.AsElementId());
Autodesk.Revit.DB.Element phaseCreated = linkDoc.GetElement(phaseCreatedParameter.AsElementId());
if (phaseCreated != null)
{
System.Windows.MessageBox.Show(phaseCreated.ToString() + " - " + targetPhase.ToString());
}
else
{
System.Windows.MessageBox.Show("phaseCreated is null - " + targetPhase.ToString());
}
linkInstance.get_Parameter(BuiltInParameter.PHASE_CREATED).Set(targetPhase.Id);
System.Windows.MessageBox.Show(phaseCreatedParameter.Definition.Name + " - " + phaseCreatedParameter.AsElementId());
// Ensure the view template's phase filter allows the link to display
PhaseFilter phaseFilter = new FilteredElementCollector(doc)
.OfClass(typeof(PhaseFilter))
.Cast<PhaseFilter>()
.FirstOrDefault(pf => pf.Name == "Show All"); // Use appropriate phase filter
if (phaseFilter != null)
{
viewTemplate.get_Parameter(BuiltInParameter.VIEW_PHASE_FILTER).Set(phaseFilter.Id);
}
// Reset Revit Link overrides in the view template to use "By Host View"
Category linkCategory = doc.Settings.Categories.get_Item(BuiltInCategory.OST_RvtLinks);
if (linkCategory != null)
{
OverrideGraphicSettings overrides = new OverrideGraphicSettings();
viewTemplate.SetCategoryOverrides(linkCategory.Id, overrides); // Reset to default
}
// Attempt to reset link-specific overrides (workaround for phase mapping)
OverrideGraphicSettings linkOverrides = new OverrideGraphicSettings();
viewTemplate.SetElementOverrides(linkInstance.Id, linkOverrides); // Reset link-specific overrides
//// Diagnostic: Verify settings
//string diagMessage = $"View Template: {viewTemplate.Name}\n" +
// $"Phase: {doc.GetElement(viewTemplate.get_Parameter(BuiltInParameter.VIEW_PHASE).AsElementId()).Name}\n" +
// $"Phase Filter: {doc.GetElement(viewTemplate.get_Parameter(BuiltInParameter.VIEW_PHASE_FILTER).AsElementId()).Name}\n" +
// $"Revit Link: {linkInstance.Name}\n" +
// $"Link Phase (Created): {doc.GetElement(linkInstance.get_Parameter(BuiltInParameter.PHASE_CREATED).AsElementId()).Name}";
//Autodesk.Revit.UI.TaskDialog.Show("Diagnostics", diagMessage);
}
catch (Exception ex)
{
System.Windows.MessageBox.Show($"Failed to set Revit Link phase: {ex.Message} \n {ex.StackTrace}");
return Result.Failed;
}
tx.Commit();
}