Filtering For a Specific Line Style

I’m sorry, I tried posting in the Revit API forums about this but maybe I’m not asking it right. I created “No Plot” doc-printing.py and doc-printed.py hooks that search the entire project for elements that have ‘NPLT’ in their type name.

It works for dimensions and text, but not lines. While I wish there were one FilteredElementCollector scheme to collect them all I would be equally happy having two separate FilteredElementCollectors.

Right now I’m assuming I need another filter specifically for collecting lines (detail, model, any) that have the phrase ‘NPLT’ in their style name.

I’ve actually collected all the subcategories of OST_Lines that have ‘NPLT’ in their name, but I have not been able to apply those subcategories to a filter to get the line elements themselves, so I’m thinking that might not be a good way to go about it.

I should note, that I tried just hiding the subcategory using SetCategoryHidden(c,True), but that only worked in Sheet Views, not in other views. (Remember I’ve got text and dimensions being hidden).

I have a bunch of messy code I can paste in here, but I would also happily go to a link if someone has a place I can get started from. I’ve searched all over The Building Coder, Revit API Forums, Google and nothing I’m trying seems to work - I’m looking for a clean start! :pleading_face:

I’m curious why ‘SetCategoryHidden(c,True) only worked in Sheet Views, but not in other views.’ I would think that would work, do the other views have view templates applied? If so, you’ll have to change that in their view templates.

I think I figured it out. It turns out even without a view template I couldn’t hide the category of the linestyle I was filtering for, so I took another approach.

param_id = DB.ElementId(BIP.BUILDING_CURVE_GSTYLE)
param_provider = DB.ParameterValueProvider(param_id)
param_contains = DB.FilterStringContains()
param_rule = DB.FilterStringRule(param_provider,param_contains,'NPLT',False)
param_filter = DB.ElementParameterFilter(param_rule)

line_element_ids = DB.FilteredElementCollector(doc).WherePasses(param_filter).ToElementIds()

This will get all detail lines (and possibly all model lines too, haven’t checked) with the term NPLT in the Line Style name.

I placed the above into doc-printing.py and doc-printed.py scripts. In doc-printing, I hide the collected lines and in doc-printed I unhide the lines.

Similarly, the same code as above but with the built-in-parameter BIP.ALL_MODEL_TYPE_NAME will get dimension strings and text that have NPLT in their type name. I assume any family object that I rename to have NPLT in the name would get caught up in this filter so use with caution. I’ll post my full solution in separate replies in case anyone else wants to create ‘no plot’ functionality.

Here is the doc-printing.py script, place it into a hooks folder inside your .extension folder.

__author__ = 'Stewart Cartmell'

from pyrevit import revit
from pyrevit import DB
from pyrevit import EXEC_PARAMS

# shortcut for DB.BuiltInParameter
BIP = DB.BuiltInParameter
# shortcut for DB.BuiltInCategory
BIC = DB.BuiltInCategory

# current document from event handler
doc = EXEC_PARAMS.event_args.Document

# attempt to get all elements with a type name that contains "NPLT"
param_id = DB.ElementId(BIP.ALL_MODEL_TYPE_NAME)
param_provider = DB.ParameterValueProvider(param_id)
param_contains = DB.FilterStringContains()
param_rule = DB.FilterStringRule(param_provider,param_contains,'NPLT',False)
param_filter = DB.ElementParameterFilter(param_rule)

np_element_ids = DB.FilteredElementCollector(doc).WherePasses(param_filter).ToElementIds()
print('Num Elements Collected: {}'.format(len(np_element_ids)))

param_id = DB.ElementId(BIP.BUILDING_CURVE_GSTYLE)
param_provider = DB.ParameterValueProvider(param_id)
param_contains = DB.FilterStringContains()
param_rule = DB.FilterStringRule(param_provider,param_contains,'NPLT',False)
param_filter = DB.ElementParameterFilter(param_rule)

line_element_ids = DB.FilteredElementCollector(doc).WherePasses(param_filter).ToElementIds()
print('Num Lines Collected: {}'.format(len(line_element_ids)))

# get view_ids of all views (including views within sheets) of printed pages
printed_view_ids = EXEC_PARAMS.event_args.GetViewElementIds() 
printed_views = []
for p in printed_view_ids:
    pview = doc.GetElement(p)
    printed_views.append(pview)

# hide elements and subcategories in all printed views
with revit.Transaction("Hide NPLT Elements",doc):
    for s in printed_views:
        s.HideElements(np_element_ids)
        s.HideElements(line_element_ids)
        #for c in np_subcategories:
        #    s.SetCategoryHidden(c,True)
        #use try-except because the sheet might not have any additional viewports
        try:
            vports = s.GetAllViewports()
            for vportid in vports:
                # get the view id associated with the current viewport 
                view_id = doc.GetElement(vportid).ViewId
                # get the actual view element in order to hide/unhide things inside that view
                view = doc.GetElement(view_id)
                view.HideElements(np_element_ids)
                view.HideElements(line_element_ids)
                #for sc in np_subcategories:
                #    view.SetCategoryHidden(sc,True)
        except:
            pass

And here is the doc-printed.py script. Note there is an issue when combining pages into a single pdf so I don’t rely on the EXEC_PARAMS views printed list.

__author__ = 'Stewart Cartmell'

from pyrevit import revit
from pyrevit import DB
from pyrevit import EXEC_PARAMS

# shortcut for DB.BuiltInParameter
BIP = DB.BuiltInParameter
# shortcut for DB.BuiltInCategory
BIC = DB.BuiltInCategory
# importing doc-printing to gain access to it's sheet list

# current document from event handler
doc = EXEC_PARAMS.event_args.Document

#Note! Using EXEC_PARAMS to get printed views does not work
# for views that are combined into a single pdf when printing
# so we'll collect all sheets and unhide elements there:
#Get all sheets in project, including placeholder sheets
sheetsUnfiltered = DB.FilteredElementCollector(doc) \
                     .OfCategory(DB.BuiltInCategory.OST_Sheets) \
                     .WhereElementIsNotElementType() \
                     .ToElements()
#Create empty list to hold all sheets minus placeholder sheets
sheetlist = []
#Filter out placeholder sheets
for s in sheetsUnfiltered:
    if not s.IsPlaceholder:
        sheetlist.append(s)

# attempt to get all elements with a type name that contains "NPLT"
param_id = DB.ElementId(BIP.ALL_MODEL_TYPE_NAME)
param_provider = DB.ParameterValueProvider(param_id)
param_contains = DB.FilterStringContains()
param_rule = DB.FilterStringRule(param_provider,param_contains,'NPLT',False)
param_filter = DB.ElementParameterFilter(param_rule)

np_element_ids = DB.FilteredElementCollector(doc).WherePasses(param_filter).ToElementIds()
print('Num Elements Collected: {}'.format(len(np_element_ids)))
    
param_id = DB.ElementId(BIP.BUILDING_CURVE_GSTYLE)
param_provider = DB.ParameterValueProvider(param_id)
param_contains = DB.FilterStringContains()
param_rule = DB.FilterStringRule(param_provider,param_contains,'NPLT',False)
param_filter = DB.ElementParameterFilter(param_rule)

line_element_ids = DB.FilteredElementCollector(doc).WherePasses(param_filter).ToElementIds()
print('Num Lines Collected: {}'.format(len(line_element_ids)))

# unhide elements
with revit.Transaction("Unhide NPLT Elements",doc):
    for s in sheetlist:
        s.UnhideElements(np_element_ids)
        s.UnhideElements(line_element_ids)
        try:
            vports = s.GetAllViewports()
            for vportid in vports:
                # get the view id associated with the current viewport 
                view_id = doc.GetElement(vportid).ViewId
                # get the actual view element in order to hide/unhide things inside that view
                view = doc.GetElement(view_id)
                view.UnhideElements(np_element_ids)
                view.UnhideElements(line_element_ids)
        except:
            pass

Always happy to receive insights on how I can optimize.

2 Likes