Python Shell and Revit API Remove Working Views Script

One of the things that I do before sharing our Revit model with consultants is to clean it up from as many unnecessary views as possible. That considerably reduces the size of the model and it is usually considered a good practice to do so. The truth is that often you can leave just a single view in the project because all that maters is the geometry. However when sharing model while in DDs and CDs I normally leave only the production sheets and only the views that are placed on the sheets. Now, you can go over those and delete them manually and if you have tons of views of all kinds that might take a while or you could write a very short Python script for that particular purpose. So, below is the scrip that I use to remove all the views from the project that are not on the sheets. Enjoy Python Shell and Revit API…

PS (I also remove views by attributes with this script and if you look at the commented lines you will see what I am talking about, but as is for the purpose of simplicity the Python script below gets rid of all views that are not on sheets)

Warning! Make sure you don’t run this script on your central model – detach it from central first. Because once the views are gone – they are GONE! Don’t tell me that  I didn’t warn you – use it at your own risk.

Download Revit IronPython Shell script 

import clr
import math
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
from Autodesk.Revit.DB import *app = __revit__.Application
doc = __revit__.ActiveUIDocument.Document
uidoc = __revit__.ActiveUIDocument
collector1 = FilteredElementCollector(doc)
collector1.OfCategory(BuiltInCategory.OST_Views)
elSet1 = collector1.ToElements()
par1 = 'Sheet Number'
#par2 = 'Production/Working'
viewSet = list()for el in elSet1:
if el.CanBePrinted:
if el.get_Parameter(par1)==None:
#if el.get_Parameter(par1)!=startswith('A'):
viewSet.append(el)
print 'Working Views Deleted:'
print len(viewSet)
t = Transaction(doc, 'Delete Views')
t.Start()
for el in viewSet:
doc.Delete(el)
t.Commit()
Share →

3 Responses to Remove Working Views With Python Shell and Revit API

  1. Daren Thomas says:

    Cool! Nice to know people are actually using the RPS for useful stuff!

  2. Unknown says:

    You can actually just go to: View – User Interface – Browser Organisation – not on sheets.

    Easily select the lot in your now re-sorted Project Browser and hit “Delete”.

Leave a Reply

Your email address will not be published. Required fields are marked *

* Copy This Password *

* Type Or Paste Password Here *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>