What views use which view templates? (2013 edition and better formatting)

View Templates got a big makeover in Revit 2013, so the previous post needs to be modified slightly to run in Revit 2013. I’ve also improved the format of the output so it looks like this:


---------------- Summary ---------------------------
Architectural Elevation: 4
Architectural Plan: 2
Architectural Reflected Ceiling Plan: 2
Site Plan: 1
Structural Framing Plan: 0
Structural Framing Elevation: 0
Architectural Section: 0
Site Section: 0
Structural Section: 0
Architectural Presentation 3D: 0
Architectural Presentation Elevation: 0
Export to Civil Engineering: 0

---------------- Details ---------------------------
'Architectural Elevation' is View Template for 4 views
Elevation East
Elevation North
Elevation West
Elevation South

'Architectural Plan' is View Template for 2 views
FloorPlan Level 1
FloorPlan Level 2

'Architectural Reflected Ceiling Plan' is View Template for 2 views
CeilingPlan Level 1
CeilingPlan Level 2

'Site Plan' is View Template for 1 views
FloorPlan Site

'Structural Framing Plan' is View Template for 0 views

'Structural Framing Elevation' is View Template for 0 views

'Architectural Section' is View Template for 0 views

'Site Section' is View Template for 0 views

'Structural Section' is View Template for 0 views

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

'Architectural Presentation Elevation' is View Template for 0 views

'Export to Civil Engineering' is View Template for 0 views

The view parameter “Default View Template” has been renamed “View Template” and the API has a new property View.ViewTemplateId. 2013 also obsoleted Document.get_Element, replacing it with Document.GetElement.

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))
        {
            ElementId id = view.ViewTemplateId;
            if (id != ElementId.InvalidElementId)
            {
                Element e = doc.GetElement(id);
                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))
    {
        sr.WriteLine("---------------- Summary ---------------------------");

        foreach (KeyValuePair<string, IList<string>> pair in items)
        {
            sr.WriteLine(pair.Key + ": " + pair.Value.Count);
        }
        sr.WriteLine();

        sr.WriteLine("---------------- Details ---------------------------");
        foreach (KeyValuePair<string, IList<string>> pair in items)
        {
            sr.WriteLine("'" + pair.Key + "' is View Template for " + pair.Value.Count + " views");
            foreach (string name in pair.Value)
            {
                sr.WriteLine(name);
            }
            sr.WriteLine();
        }
    }
    Process.Start(filename);
}    
Advertisements

2 thoughts on “What views use which view templates? (2013 edition and better formatting)

    • Hi Arno,

      You are right. Steve’s observation and the point of this macro is that this functionality is new to 2013. When you are working in an older release and wishing that you had access to a feature in a newer release, sometimes you can use the API to recreate that feature in the older release.

      Harry

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