Get All Elements of category using API

Hi All,
To get “All Elements of Category” in my rebar container using Revit API just as you would do in dynamo as shown below

image

I used Revit Lookup and followed the steps indicated in the image below to extract necessary parameters and to be able to write my script, but I’m stuck on the last step which is getting All elements of Category

here is my graph and my script:

import sys
import clr

clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *

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

doc = DocumentManager.Instance.CurrentDBDocument

Shaft_Id = UnwrapElement(IN[0]).Id

container = UnwrapElement(IN[1])


rebars = []
for c in container:
    # get rebar type Id
    rebar_Id = c.BarTypeId
    # get rebar Elements
    rebar = doc.GetElement(rebar_Id)
    # get rebar category Id
    rebar_category = rebar.Category.Id
    # get Element category??
    element_cat = doc.GetElement(rebar_category)
    rebars.append(element_cat)
    

OUT = rebars

Any help would be appriciated

Thanks.

You’ll want to use a FilteredElemwntCollector. There are a LOT of example of this on the forum and in Github repos like this one. Since you’ve gotten this far I’m confident you can find one which works for you. :slight_smile:

1 Like

Hi @jacob.small

In my model, I can for example get the catefory of the used family wich is Generic Model by using FilteredElementCollector as shown in the image and the code below, but honestly, I can’t find the correct syntax how to use it inside the rabar container to be able to get Elements Category, however I can iterate through the rebar container and get the rebar Catogory Id as indicated in the script above…!!

all_Shaft = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_GenericModel).WhereElementIsNotElementType().ToElements()

Cat_Id

Thanks.

Which category are you trying to collect content for? I think armature à béton is after the built in category OST_Rebar.

However I may not be entirely interpreting what you’re after. If you paste the .dyn and a sample .rvt it may help provide some context.

1 Like

@jacob.small
I’m trying to get the BuiltInCategory OST_Rebar under Category Id as shown in the image below to get the final result as you would do in dynamo according to the 2nd image

builtincategory

image

Including the specific part of the .dyn file takes too much time and effort, I’ll try to do it and if I can’t I’ll send you all of the file.

Thanks.

So you have the object (rebar) and want to know what category it is from?

This should get you started. Something like rebar.Category.

1 Like

@jacob.small

I’ve the same Element Type for each container Item inside the rebar container and I want to get All Elements Category for those rebars as I mentioned above

I used the code you linked, and I get Category but I can’t find the correct syntax to get the BuiltInCategory which is OST_Rebar??

rebars = []
for c in container:
    # get rebar type Id
    rebar_Id = c.BarTypeId
    # get rebar Elements
    rebar = doc.GetElement(rebar_Id)
    # get rebar category
    cat = rebar.Category
    rebars.append(cat)
    

OUT = rebars

image

Thanks.

Try this:
all_Rebar = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rebar).WhereElementIsNotElementType().ToElements()

1 Like

@jacob.small

I’m completly lost!!.. I tried your code but I got duplicated rebar containers instead Elements of Category within the FiltredElementCollector !!!

To solve this issue please check the attached .dyn and Revit file below.

final_shaft_rebar.dyn (38.0 KB)

Shaft_rebar1.rvt (3.5 MB)

Thanks.

Your script is creating a new set of Rebar every time you click Run. The last python script is reporting all of the rebar in model after each run.

First Run

Second Run

1 Like

@staylor

I know that, and I’ve mentioned that in my last post…it does not bother me for the moment…I’m looking for a solution to my main problem and you have not proposed me anything how to solve it !?

Thanks.

Hi @jacob.small @c.poupin

Have you take a look to my model? …what you suggest me as a solution?

Thanks.

This has to do with how you are generating your rebar. To return the ‘Rebar’ type, you need to use a ‘per bar sketch’, which doesn’t scale well for hopefully obvious reasons.

Rebar containers allow you to populate a lot of rebar at once (as you likely know), with out defining each individual curve. If you use an All Elements Of Category node after pupulating your rebar containers with the Python code you’re using, you’ll see that it returns only rebar containers. If you manually draw a sketch rebar as well you’ll see it returns a rebar object.

The previous FilteredElementCollector element returns the same collection, with the same object types.

The Python
import sys, clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import FilteredElementCollector, BuiltInCategory
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager

doc = DocumentManager.Instance.CurrentDBDocument
all_Rebar = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rebar).WhereElementIsNotElementType().ToElements()

OUT = all_Rebar

If you want to access the individual contents thereof, look into what youc an do with the RebarContainer class, which has a GetRebarContainerIterator, which in turn allows you to iterate over the bars in the container to get contents of each individual bar. These are never stored as individual elements though, as that would be overly restrictive on the file itself (each bar would need 6 additional element IDs, allowing generation of millions’s of element IDs in one go, which would potentially hit the upper limit of element IDs in a pre 2024 project (something I haven’t seen a report of yet - largest has only been 1/2 of the way there).

2 Likes

Sorry, I misread thinking you were implying that the duplication issue was with the code that @jacob.small provided.

I may be stepping way above my paygrade with this. If I understand correctly, a container’s purpose is to simulate having all of the rebar without having each actual bar and overwhelming the model. Would be sort of like standing between two mirrors. You are just seeing your image repeated over and over.

So with that being said, I think if you iterate over each bar in the container, you will end up getting the same Id for every item and not unique individual Ids. You can schedule the containers and that will give you a count and name of the bar types. Not sure what your endgame is for wanting to get all of the bars independently, but in that case you may have to use freeform rebar.

Again, I may be misinterpreting things all together. If so, please ignore my comments and proceed.

1 Like

I hope I can clarify the issue for you.

First of all, in your first snapshot, you are looking at the category of the BarType and not the category of the bar.

Second, RebarContainer is a special type of RebarCollection only available for creation via the API. The category of RebarContainer is OST_Rebar.

Thrid, each individual bar nested into the RebarContainer is a RebarContainerItem. And RebarContainerItem is not a subclass of Revit Element and does not have a categery (RevitAPI docs does not show Category as a property of this class). Therefore, FilteredElementCollector will not collect such items for you. RebarContainerItem Class

Your best bet is to collect and iterate over the RebarContainers after their creation to grab the RebarContainerItems.

1 Like

Thanks for everything,

the main purpouse is to copy the rebars or (rebar container) from the first shaft to the other ones shown in the image below

So the goal of extracting the rebar category from the rebar container is to be able to use this parameter introduced in the script given by @Einar_Raknes in this discussion which talk about copying rebars from a beam to another…but it’s seem to me that it’s not an easy task getting this parameter from the rebar container, so I’ll start a new thread explaining the main issue.

Thanks.