What views use which view templates? (2012 edition)

Steve wrote at Revit OpEd:

When I travel back and forth between Revit versions, say between 12 and 13, I find myself missing things. For example I wanted to see (in 2012) how many views were assigned to view templates and which ones were really being used. In 2013 we can add that parameter to a View List, 2012 not so much. In 2013 we can select a view template and see how many views are assigned to it, in 2012…you get the idea. I wish life and project conditions didn’t make it hard to “just upgrade”. Just do it, as soon as you can!

Sometimes when you can’t upgrade your RVT to a newer version of Revit, you can use the Revit API to accomplish something similar (or even better!) than what you would do in the newer release of Revit. Here is a Revit 2012 macro that outputs a text file listing all view templates with the count and names of the views that use it as the default view template:

'Architectural Elevation' is Default View Template for 4 views

Elevation East, Elevation North, Elevation West, Elevation South,

'Architectural Plan' is Default View Template for 2 views

FloorPlan Level 1, FloorPlan Level 2,

'Architectural Reflected Ceiling Plan' is Default View Template for 2 views

CeilingPlan Level 1, CeilingPlan Level 2,

'Site Plan' is Default View Template for 1 views

FloorPlan Site,

'Structural Framing Plan' is Default View Template for 0 views

'Structural Framing Elevation' is Default View Template for 0 views

'Architectural Section' is Default View Template for 0 views

'Site Section' is Default View Template for 0 views

'Structural Section' is Default View Template for 0 views

'Architectural Presentation 3D' is Default View Template for 0 views

'Architectural Presentation Elevation' is Default View Template for 0 views

'Export to Civil Engineering' is Default View Template for 0 views
public void TemplateUse()
{
    Document doc = this.ActiveUIDocument.Document;
    Dictionary<string, IList<string>> dict = new Dictionary<string, IList<string>>();
    foreach (View viewTemplate in (from v in new FilteredElementCollector(doc).OfClass(typeof(View)).Cast<View>() where v.IsTemplate select v))
    {
        IList<string> viewsUsed = new List<string>();
        foreach (View view in (from v in new FilteredElementCollector(doc).OfClass(typeof(View)).Cast<View>() where !(v.IsTemplate) select v))
        {
            Parameter p = view.get_Parameter("Default View Template");
            if (p != null)
            {
                ElementId id = p.AsElementId();
                if (id != ElementId.InvalidElementId)
                {
                    Element e = doc.get_Element(p.AsElementId());
                    if (e.Name == viewTemplate.Name)
                        viewsUsed.Add(view.ViewType.ToString() + " " + view.Name);
                }
            }
        }
        dict.Add(viewTemplate.Name, viewsUsed);
    }

    // sort the dictionary based on the number of views in each list
    var items = from pair in dict orderby pair.Value.Count descending select pair;

    string filename = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "ViewTemplateUsage.txt");

    using (StreamWriter sr = new StreamWriter(filename, false))
    {
        foreach (KeyValuePair<string, IList<string>> pair in items)
        {
            sr.WriteLine("'" + pair.Key + "' is Default View Template for " + pair.Value.Count + " views");
            string s = "";
            foreach (string name in pair.Value)
            {
                s += name + ", ";
            }
            sr.WriteLine(s);
            sr.WriteLine();
        }
    }
    Process.Start(filename);
}
Advertisements

5 thoughts on “What views use which view templates? (2012 edition)

  1. Hello. I’m new to macros. I’ve created a module with macro in c# (Revit 2012), erased the default code, and replaced it with what you have created above, and I get a number of errors upon build. Am I missing something in the process? Thank you!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s