Remove Curved Grids From List

Hi All

I am trying to filter out the curved grids from a list of grids collected from a sheet.
I have defined a function that creates a boolean list that I think I can then use to mask the curved grids out of the list. I am, however, struggling to figure this step out.
Another way to remove the curved grids could be through the Filtered Element Collector, but I am unsure if this is possible.
My final goal is to develop ribbons so all needs to be python code.
Please see my code below.
Thanks for your help.

# Load the Python Standard and DesignScript Libraries
import clr
import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')
import System
from System import Array
from System.Collections.Generic import *
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

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *
from itertools import islice

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = uiapp.ActiveUIDocument

# INPUTS

# CODE
# Get Viewports on Sheet
all_viewports = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Viewports).WhereElementIsNotElementType().ToElements()

# Get Views in the Viewports
all_views = []
for viewport in all_viewports:
        viewId = viewport.ViewId
        all_views.append(doc.GetElement(viewId))

# Remove non-plan views from List
all_views_clean = []
for view in all_views:
        if not isinstance (view,(Autodesk.Revit.DB.ViewPlan)):
                continue
        if view.ViewType == ViewType.Legend:
                continue
        all_views_clean.append(view)

#Get Grids
grids = []
for view in all_views_clean:
        grids.append(FilteredElementCollector(doc, view.Id).OfCategory(BuiltInCategory.OST_Grids).WhereElementIsNotElementType().ToElements())
        
#Define straight vs curved grid boolean function
def isCurved(grid):
    bool = grid.IsCurved
    return bool

gridBoolList = []
for grid in grids:
       gridBoolList.append(list(map(isCurved,grid)))

Hi @bayowindapo,

I’m not sure if you’re hoping to be able to use this on a list of sheets and that is why you have a list structure as below for your grids in view:
image

But I proceeded with the understanding that this was intentional.

I simply looped over each nested list within the variable ‘grids’ and filtered the whole list using the IsCurved property and appended that to the straight_grids variable. See below:

# Load the Python Standard and DesignScript Libraries
import clr
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
uidoc = uiapp.ActiveUIDocument
# INPUTS

# CODE
# Get Viewports on Sheet
all_viewports = FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_Viewports).WhereElementIsNotElementType().ToElements()

# Get Views in the Viewports
all_views = []
for viewport in all_viewports:
        viewId = viewport.ViewId
        all_views.append(doc.GetElement(viewId))

# Remove non-plan views from List
all_views_clean = []
for view in all_views:
        if not isinstance (view,(Autodesk.Revit.DB.ViewPlan)):
                continue
        if view.ViewType == ViewType.Legend:
                continue
        all_views_clean.append(view)

#Get Grids
grids = []
for view in all_views_clean:
        grids.append(FilteredElementCollector(doc, view.Id).OfCategory(BuiltInCategory.OST_Grids).WhereElementIsNotElementType().ToElements())
        
straight_grids = []
for grid_list in grids:
	straight_grids.append(list(filter(lambda x: x.IsCurved == False, grid_list)))

OUT = straight_grids 

I think you may be right that you could do this with a smarter filtered element collector but I’m no expert with them I’ll leave that to @c.poupin :wink: .

(I also removed some libraries you were importing and didnt need :slight_smile: ).

Hope this helps!

3 Likes

Ahh, thanks so much!
It looks like it works; I’ve never used the lambda function. I’ll read up.
You are somewhat correct. The intention is to use the script to dimension grids on a list of plan views on a sheet.
Yes, it would be great to know if all can be done in the collector. @c.poupin
Thanks again.

1 Like

you can use Linq or Predicate, however Python filter or list comprehension works fine too

Example

3 Likes

Lovely, much more succinct!

Didn’t realise you could filter the collection itself, good to know!

2 Likes

Wow thanks!
Great solution. Will take a while for my brain to digest that. Haha.
Thanks again.

1 Like