Line Styles Clean up

This post is about how you can clean up the unnecessary lines styles from the document by using IronPython in Revit Dynamo Application. And the idea is if the given keyword contains in line style’s name, the line style 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 Line category object from Category object by using GetCategory method.

# 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 category object
cate = Category.GetCategory(doc, BuiltInCategory.OST_Lines)

Create a dictionary to store Line Style’s Element Id and Name, keywords input can be single or multiple and removed will be the output (line style names).

# name and id pairs
nameIdDict = {}
# keyword - string
keywords = [IN[0]] if not isinstance(IN[0], list) else IN[0]
# output (removed lines names)
removed = []
# each line from line category
for i in cate.SubCategories:
	# each word in keywords
	for k in keywords:
		# keyword in line 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 style’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:

line

# 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 category object
cate = Category.GetCategory(doc, BuiltInCategory.OST_Lines)
# name and id pairs
nameIdDict = {}
# keyword - string
keywords = [IN[0]] if not isinstance(IN[0], list) else IN[0]
# output (removed lines names)
removed = []
# each line from line category
for i in cate.SubCategories:
	# each word in keywords
	for k in keywords:
		# keyword in line 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
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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