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
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
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
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.
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…!!
@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
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.
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
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 !?
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.
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).
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.
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.
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.