Revit API: Delete LinePatterns

Revit API: Delete LinePatterns

Remove CAD Imported Line Patterns

Quick CSharp Macro Snippet to remove line patterns that start with “IMPORT” from the active Revit document.

public void deleteLinePatterns()
{
	UIDocument uidoc = this.ActiveUIDocument;
	Document doc = uidoc.Document;

	var collector = new FilteredElementCollector(doc)
		.OfClass(typeof(LinePatternElement))
		.Where(i => i.Name.StartsWith("IMPORT")).ToList();

	List<ElementId> ids = new List<ElementId>();
	for (int i = 0; i < collector.Count(); i++)
	{
		ids.Add(collector[i].Id);
	}

	using(Transaction t = new Transaction(doc,"Remove Pattern"))
	{
		t.Start();
		try
		{
			doc.Delete(ids);
		} 
		catch (Exception) 
		{
			t.RollBack();
			TaskDialog.Show("Imports Removed","Could not remove patterns");
			return;
		}
		t.Commit();
		TaskDialog.Show("Imports Removed","All Imported Patterns removed");
	}
}