Revit model management in Excel

Model management can involve some tedious tasks. Cleaning up the mess created by an unruly user when he import all categories of an old Revit model is probably the most tedious of these tasks.

When someone import elements from another model, we quickly end up with thousand of view templates, filters, and other user created views which can become totally unmanageable.

Here come the BIM Manager, who spend two tedious days sorting these views and campaigning against view proliferation.

To help address this problem, I create a small piece of code for exporting every view, template and filter to three CSV files.

CSV Files

To read these files in a meaningful way, I use PowerPivot in Excel to create some kind of a small database, with two relationships :

Relationships

We can then create tables displaying how filters and views are used, like how many filters are used, or where the templates are applied.

Filters Usage

Templates Usage

Once loaded in the PowerPivot tool, this data allows us to quickly identify which template or filter are used and delete the unwanted ones.

The entire source code is available below, please feel free to use it for your own projects.

public void ExportViewTemplatesList()
{
	Document doc = this.ActiveUIDocument.Document;
			
	//find all view
	IEnumerable<View> views = 
from elem in new FilteredElementCollector(doc).OfClass(typeof(View))
let view = elem as View
select view;
			
	//Create a text file for exporting
	List<string> lines = new List<string>();
	//Add the first line
	lines.Add("TemplateName");
			
	foreach (View view in views) {
		if (view.IsTemplate)
		{
			lines.Add(view.Name);
		}
	}
			
	lines = lines.Distinct().ToList();
	string exportpath = @"templates.csv";
	File.WriteAllLines(exportpath,lines.ToArray(),Encoding.UTF8);
}

public void ExportFiltersList()
{
	Document doc = this.ActiveUIDocument.Document;
			
	//find all filters
	IEnumerable<ParameterFilterElement> filters = 
from elem in new FilteredElementCollector(doc)
.OfClass(typeof(ParameterFilterElement))
let filter = elem as ParameterFilterElement
select filter;
			
	//Create a text file for exporting
	List<string> lines = new List<string>();
	//Add the first line
	lines.Add("FilterName");
			
	foreach (ParameterFilterElement filter in filters) 
	{
		lines.Add(filter.Name);
	}
			
	lines = lines.Distinct().ToList();
	string exportpath = @"filters.csv";
	File.WriteAllLines(exportpath,lines.ToArray(),Encoding.UTF8);
}

public void ExportViewsList()
{
	Document doc = this.ActiveUIDocument.Document;
			
	//find all view
	IEnumerable<View> views = 
from elem in new FilteredElementCollector(doc).OfClass(typeof(View))
let view = elem as View
select view;
			
	//Create a text file for exporting
	List<string> lines = new List<string>();
	//Add the first line
	lines.Add("ViewName;ViewType;IsTemplate;"
		+"TemplateName;LevelName;FilterName");
			
	foreach (View v in views) 
	{
		Level level = v.GenLevel;	
		string levelName = "";
		if (level != null)
		{
			levelName  =  level.Name;
		}
		
		//Get view filters
		ICollection<ElementId> filterIds;
		try {
			filterIds = v.GetFilters();
		} catch (Autodesk.Revit.Exceptions.InvalidOperationException) {
			filterIds = new List<ElementId>() ;
		}
				
		string templateName = "";
				
		if (v.ViewTemplateId.IntegerValue != -1)
		{
			templateName  =  doc.GetElement(v.ViewTemplateId).Name;
		}
				
		string viewinfos =
			v.ViewName + ";" +
			v.ViewType + ";" +
			v.IsTemplate.ToString()  + ";" +
			templateName  + ";" +
			levelName;
				
		if (filterIds.Count != 0)
		{
			foreach (ElementId filterId in filterIds)
			{
				string filterName = doc.GetElement(filterId).Name;
				string line  = viewinfos   + ";" + filterName;
				lines.Add(line);
			}
		}
		else
		{
			lines.Add(viewinfos);
		}
	}
			
	string exportpath = @"views.csv";
	File.WriteAllLines(exportpath,lines.ToArray(),Encoding.UTF8);
}

5 thoughts on “Revit model management in Excel

  1. Pingback: Revit - BIM | Pearltrees

  2. hi,

    i copy your code in one of my Revit project file. unfortunately, i got an error when build the solution. the error @ File.WriteAllLines(exportpath,lines.ToArray(),Encoding.UTF8);”

    the name “Encoding” is not existing within the urrent context.
    the name “File” is not existing within the urrent context.

    • Hello,

      You have to add “using System.IO;” and “using System.Text” along with the other “using” calls at the beginning of your code.

  3. Hello,

    I’m wondering how to use this in our Revit file? We are having the same issue with duplicated filters that do the same thing and would like to consolidate.

    Would like to be able to see which view templates are using which filters, and ideally, the settings of the filters (but this last part is not mandatory).

    Thanks,
    Zach

    • Hello Zachary,

      By now, I would use the new Revit nodes available in Dynamo 1.2. You could extract all view templates, their associated filters and settings, and consolidate all of this in Excel. This would give you far more flexibility than the code I wrote.

Leave a Reply to Simon Moreau Cancel reply

Your email address will not be published. Required fields are marked *