line pattern

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)
	.OfClass(typeof(LinePatternElement))
	.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"))
		{
		t.Start();
			
		try
		{
			foreach (ElementId id in ids)
			{
				doc.Delete(id);
			}
		}
		catch (Exception)
				
	t.Commit();
	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
https://boostyourbim.wordpress.com/

Harry’s Udemy Courses
https://www.udemy.com/revitapi/
https://www.udemy.com/revitapi2/
https://www.udemy.com/revit-api-materials/

Danny Bentley’s Channel on Youtube
https://www.youtube.com/channel/UC1Dx-jGyRbvvHzZ8ZyGWF5w

Jeremy Tammik’s Building Coder
https://thebuildingcoder.typepad.com/

Revit API Docs Online
http://www.revitapidocs.com/

Autodesk “My First Revit Plugin”
https://knowledge.autodesk.com/support/revit-products/learn-explore/caas/simplecontent/content/my-first-revit-plug-overview.html

Free C# Courses
https://www.learncs.org/
https://www.sololearn.com/Course/CSharp/

ItzAdam5X on Youtube for learning general C# concepts
https://www.youtube.com/channel/UC9pq4hre8qZI132O4cok5vA