Get all element instances of all categories in OOTB Dynamo and/or Python

i’m trying to clean up Revit model to be used as project template, lots of junks which are invisible like ref plane, etc

just thinking if i can get all element instances via Python, then i can delete them …, turned out a few issues:

  1. how to convert category from cats to category from bics? seems cats is much less than bics and cats are more “suitable” in my case
    cats = doc.Settings.Categories
    bics = System.Enum.GetValues(BuiltInCategory)

  2. seems the elements below are not all instance elements? some are type elements like line type, group type, etc
    elements = FilteredElementCollector(doc).WhereElementIsNotElementType().OfCategory(bic).ToElements()

for the first point you can go from cats to bics and from bics to cats like this:

Category = IN[0]

Category2BuiltIn = System.Enum.ToObject(BuiltInCategory, Category.Id)


For the second point I don’t know. To select elements in dynamo with OOTB nodes i normally use a combination of ElementTypes (=FamilyInstance) -> All Elements of Element Type but I guess with your API method its more complete. To filter out “bad” elements maybe you should find all the types you dont want (str(item.GetType())) and filter out that way?

Anyways did you already purge the model before running dynamo?

1 Like

thanks Grey.

category from doc.Settings.Categories is not same as category from IN[0] which is actually BuiltIn already (at first i thought category from IN[0] is same as from doc.Settings.Categories).

purge will purge element type only which is not desired, well, as you know that project template needs to preload many types.

Excuse me but I do not understand what type of categories you want :slight_smile:
OUT = UnwrapElement(IN[0])
I think that the dynamo nodes give you some wrapped objects, while python is able to unwrap them.
With doc.settings.Categories and with UnwrapElement you get a Revit.DB.Category, instead with System.Enum.ToObject(BuiltInCategory,etc you get a Revit.DB.BuiltInCategory.

Or are you trying to select all builtin categories? Please excuse me if I do not understand correctly!

As for the purge you can manually choose what to delete and what to keep, obviously by hand. In Dynamo you could filter the elements you do not want to delete with str(item.GetType()) logic as I said in my previous post, or you could check if they have a location…maybe elements that are not instances do not have a location?

thanks Grey.

total categories from doc.Settings.Categories is 283 while total categories from bics = System.Enum.GetValues(BuiltInCategory) is 973.

i want to use that 283 as input to Dynamo but image

in fact, i got similar issue, bounding box from Python cannot be used in Dynamo, did i miss something simple and common?

The python output belongs to the Revit API namespaces, while BoundingBox.ToCuboid (and all elements of category) wants an input belonging to the “dynamo namespace” or seomthing like that. I am not sure if you can convert a revit boundingbox to a dynamo one (you could however export the points and create a bounding box with the dynamo nodes)

a better explanation:

If you want to select every object of every category look here:
FilteredElementCollector(doc).OfCategoryId(catid) and you can put that in a loop

thanks Grey for your great help!

for boundingbox issue, solution by Thomas_Mahon as below:
That’s because you’re calling Dynamo’s Revit Element wrapper method. If you are looking to return ProtoGeometry try ToProtoType() instead.

currently i’m having troubles in connection between Python and Dynamo so need more research on it, especially when and how to use ToDSType, ToRevitType, ToProtoType, anything else?