Removing Revit Line Patterns with C# Macros

Sometimes you might encounter an element within Revit giving you grief.

Recently for me it was a line pattern that had been transferred across from an old template. I didn’t want to spend the time to re-create all the old line patterns in a new template, but that time ended up being lost troubleshooting a fatal error.

Lucky for me that the line patterns were named so inconsistently in the old template or I wouldn’t have even discovered the problem; an unexpected benefit to others not being as meticulous as I can be I suppose.

One by one, I check each line pattern I had imported and discovered there was just one causing the problem. I couldn’t change the pattern definition. I couldn’t rename it. I couldn’t delete it. No matter what I did, Revit would crash.

An audit? No. What about a purge? Still no love.

So what do you do in this situation? I ended up turning to the API to obliterate the pesky line pattern. Dynamo is great but you can make a fantastic toolset based around C# macros and it’s a great way to learn the basic of coding with the API.

public void DeleteLinePattern()
//Get the current document
UIDocument uidoc = this.ActiveUIDocument;
Document doc = uidoc.Document;
my problem line pattern started with a certain prefix, 
so the method i am using is to search for line patterns with that prefix
update your code to prefix that you're looking for
var collector = new FilteredElementCollector(doc)
	.Where(i => i.Name.StartsWith("PREFIX")).ToList();
	List<ElementId> ids = new List<ElementId>();

//Start the transaction that will modify your document
	using(Transaction t = new Transaction(doc,"Delete LinePatterns"))
			foreach (ElementId id in ids)
		catch (Exception)
	TaskDialog.Show("Delete LinePatterns","Deletion complete");

I’m still waiting to hear back from Autodesk as to if I am still at risk of the model becoming corrupt in the future, but in the current state I’m pretty happy as I can continue working without issue.

As you can probably tell, this is quite a simple macro and the API is capable of doing much more. If you’re interested in learning the Revit API, check out these resources on the web

Harry Mattson’s Boost Your BIM

Harry’s Udemy Courses

Danny Bentley’s Channel on Youtube

Jeremy Tammik’s Building Coder

Revit API Docs Online

Autodesk “My First Revit Plugin”

Free C# Courses

ItzAdam5X on Youtube for learning general C# concepts

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: