Purging unused linestyles in Revit with Dynamo and Python

All the best in 2017!

Lately, I’ve been dealing with models with far too many (i.e. more than zero) exploded dwgs inside, and being able to clean up all those leftover linestyles was crucial to me. Once you have to deal with 500+ positions when drafting a single detail, the blow to productivity is far too severe. Purging would be a smart move, yes?

As far as I know, you can’t achieve that with default purge functionality, and to my surprise – it’s not so obvious in Dynamo either. I’ve digged through internet (This thread was very helpful), and at some point I’ve realized that using Python was inevitable. I was reluctant, because my programming skills are basic at best, but as it turns out, “basic” is good enough and I’ve managed to complete my purging script. Here’s how it works:

LinePurge

 

My goal was to do as much as possible in “vanilla” Dynamo and as little as possible with Python. What you can see here is collecting all the linestyles used in project, saving all the unique ones and cross-checking them with list of linestyles available. This is the important distinction, as the first one can be easily done with basic dynamo nodes, the latter had to be achieved with following Python script:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

TransactionManager.Instance.EnsureInTransaction(doc)
lineStyle = doc.Settings.Categories.get_Item(BuiltInCategory.OST_Lines)
lineStyleSubTypes = lineStyle.SubCategories
listNames = []
listId = []

for i in lineStyleSubTypes:
  name = i.Name
  ID = i.Id
  listNames.append(name)
  listId.append(ID)

TransactionManager.Instance.TransactionTaskDone()
OUT = listNames, listId

We start with the list of all available styles, then we throw away those that are used in the project and those with ID’s below zero (non-deletable system linestyles) – ending with list of unused ones. We feed that list to a second Python script that deletes elements by ID’s.

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
from System.Collections.Generic import *

x = IN[0]
Left = list()
Erased = list()

for Id in x:
  try:
    doc.Delete(Id)
    Erased.append(Id)
  except:
    Left.append(Id)
OUT = Erased, Left

…and we’re done. Use with caution!