Check if a Category contains any element

Check if a Category contains any element

mahshid.motie
Contributor Contributor
1,627 Views
6 Replies
Message 1 of 7

Check if a Category contains any element

mahshid.motie
Contributor
Contributor

Hi 🙂

From this post (https://thebuildingcoder.typepad.com/blog/2010/05/categories.html), thanks to Jeremmy, I managed to get the list of all categories in my model. However, there are many categories which no element belongs to them. I want to have only the list of categories (or sub categories) which contain at least one element. How can I get this list?

 

Many thanks in advance 🙂

0 Likes
Accepted solutions (1)
1,628 Views
6 Replies
Replies (6)
Message 2 of 7

recepagah12
Advocate
Advocate

You can iterate the categories in the loop and check if null or size >0. Then you can remove them if you want.

 

I hope this helps,

Recep.

0 Likes
Message 3 of 7

mahshid.motie
Contributor
Contributor

Hi recepagah 🙂

 

Thanks for your answer. Here is my code:

 

//get UIDocument
UIDocument uidoc = commandData.Application.ActiveUIDocument;

//get document
Document doc = uidoc.Document;

try
{

Categories categories = doc.Settings.Categories;

int n = categories.Size;

StringBuilder CategoryInformation = new StringBuilder();
CategoryInformation.Append(n + "," + "Categories and their parents");

foreach (Category c in categories)
{
Category p = c.Parent;

CategoryInformation.Append("\n" + c.Name + "," + c.Id + "," + "parent:" + (null == p ? "<none>" : p.Name));

}
TaskDialog.Show("Revit", CategoryInformation.ToString());

return Result.Succeeded;
}

catch (Exception e)
{
message = e.Message;
return Result.Failed;
}

 

I tried to use your advice but didnt figure out to try it on which parameter of categories? I tried adding c.size, but it doesnt exists.

 

 

 

0 Likes
Message 4 of 7

jeremytammik
Autodesk
Autodesk
Accepted solution

For each category, create a filtered element collector and retrieve the count of elements that match the category and are not types.

  

If this advice is not clear to you, please work through the getting started material before asking any further questions:

 

https://thebuildingcoder.typepad.com/blog/about-the-author.html#2

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder

Message 5 of 7

mahshid.motie
Contributor
Contributor

Thanks a lot. This method works 🙂

0 Likes
Message 6 of 7

mahshid.motie
Contributor
Contributor

But can you explain why it should not be a type?

0 Likes
Message 7 of 7

jeremytammik
Autodesk
Autodesk

You need to know whether element type should be allowed or not.

 

It depends on your needs.

 

If you do not know, you should stop programming Revit right here and now and learn about the difference between types and instances first, before taking a single further step.

 

Here are some hints:

 

https://forums.autodesk.com/t5/revit-api-forum/importinstance-like-in-dynamo/m-p/8741697

 



Jeremy Tammik
Developer Technical Services
Autodesk Developer Network, ADN Open
The Building Coder