This post is about how you can clean up the unnecessary lines patterns from the document by using IronPython in Revit Dynamo Application. And the idea is if the given keyword contains in the line pattern’s name, the line pattern element will be removed from the document.
First import these modules: clr to load .Net Assemblies as IronPython modules which are RevitAPI.dll and RevitServices.dll and from these modules we import DB, DocumentManager and TransactionManager classes. Collect the current document and store in doc. Get LinePatterns Elements from the current document by using FilteredElementCollector Class.
# dynamo version - 1.3.3 import clr clr.AddReference("RevitAPI") clr.AddReference("RevitServices") from Autodesk.Revit.DB import * from RevitServices.Persistence import DocumentManager from RevitServices.Transactions import TransactionManager # current document doc = DocumentManager.Instance.CurrentDBDocument # get line patterns elements line_patterns = FilteredElementCollector(doc).OfClass(LinePatternElement).ToElements()
Create a dictionary to store Line Pattern’s Element Id and Name, keywords input can be single or multiple and removed will be the output (line patterns names).
# name and id pairs nameIdDict = {} # keyword - string keywords = [IN[0]] if not isinstance(IN[0], list) else IN[0] # output (removed line patterns names) removed = [] # each line from line patterns for i in line_patterns: # each word in keywords for k in keywords: # keyword in line pattern name if k in i.Name: # store in dictionary nameIdDict[i.Name] = i.Id # start transaction TransactionManager.Instance.EnsureInTransaction(doc) # each name, id in dictionary for name, id in nameIdDict.items(): # add to output result removed.append(name) # delete from the document doc.Delete(id) # transaction done TransactionManager.Instance.TransactionTaskDone() # output result OUT = removed
Start transaction to make the changes. Line pattern’s Name to add to the output list and Id to use in Delete method. And finally, close the transaction.
Sample usage and the complete code as below:
# dynamo version - 1.3.3 import clr clr.AddReference("RevitAPI") clr.AddReference("RevitServices") from Autodesk.Revit.DB import * from RevitServices.Persistence import DocumentManager from RevitServices.Transactions import TransactionManager # current document doc = DocumentManager.Instance.CurrentDBDocument # line pattern elements line_patterns = FilteredElementCollector(doc).OfClass(LinePatternElement).ToElements() # name and id pairs nameIdDict = {} # keyword - string keywords = [IN[0]] if not isinstance(IN[0], list) else IN[0] # output (removed line pattern names) removed = [] # each line from line patterns for i in line_patterns: # each word in keywords for k in keywords: # keyword in line pattern name if k in i.Name: # store in dictionary nameIdDict[i.Name] = i.Id # start transaction TransactionManager.Instance.EnsureInTransaction(doc) # each name, id in dictionary for name, id in nameIdDict.items(): # add to output result removed.append(name) # delete from the document doc.Delete(id) # transaction done TransactionManager.Instance.TransactionTaskDone() # output result OUT = removed