Identifying objects that are already tagged (post 2022 api)

Identifying objects that are already tagged (post 2022 api)

TWhitehead_HED
Contributor Contributor
569 Views
3 Replies
Message 1 of 4

Identifying objects that are already tagged (post 2022 api)

TWhitehead_HED
Contributor
Contributor

I have a little plugin that goes through and automatically tags all the doors in a view.  Because of the complexity of the doors this important because only certain families in the door category should be tagged.  This is to make it easier for users. 

 

With 2022 and higher, the property of IndependentTag class has been removed and I'm unable to a simple check as I did before.  Here's a copy of my previous code:

 

using (Transaction trans = new Transaction(doc, "Tag Parent Doors"))
       {
       trans.Start();
       foreach (FamilyInstance door in doors)
            {
             if (new FilteredElementCollector(doc, currentView.Id) //this collector checks if there is a tag associated with the door
                 .OfCategory(BuiltInCategory.OST_DoorTags)
                 .OfClass(typeof(IndependentTag))
                 .Cast<IndependentTag>()
                 .Where(q => q.TaggedLocalElementId == door.Id).Any())
              {
              skipCount++;
         continue; /

The problem is on line 10.  TaggedLocalElementId is not a property of IndependentTag anymore.  

I know this is likely basic to many of you but how would I alter the existing collector to instead use GetTaggedLocalElement() and skip items that contain door.Id within this collector? Or is there a different strategy completely I should look at?

 

I understand that the results from the new method will be a list, but I still should be able to verify that the specific door.Id isn't in that list.  Perhaps there is a way know that the "door" has an associated tag? 

 

Thanks in advance, 

Tom

0 Likes
Accepted solutions (1)
570 Views
3 Replies
Replies (3)
Message 2 of 4

Mohamed_Arshad
Advisor
Advisor
Accepted solution

HI @TWhitehead_HED 

 

     You can use GetTaggedLocalElementIds Method and get Ids of elements which are tagged. Then check whether door.Id is present in the list, if it is present then increase the skip count. Kindly check the below reference code.

 

Reference Code

using (Transaction trans = new Transaction(doc, "Tag Parent Doors"))
            {
                trans.Start();

                foreach (FamilyInstance door in doors)
                {
                    if (new FilteredElementCollector(doc, currentView.Id)
                         .OfCategory(BuiltInCategory.OST_DoorTags)
                         .OfClass(typeof(IndependentTag))
                         .Cast<IndependentTag>()
                         .SelectMany(x => x.GetTaggedLocalElementIds())
                         .Where(x => x == door.Id).Any())
                    {
                        skipCount++;
                        continue;
                    }
                }
            }

 

Hope this will Helps 🙂


Mohamed Arshad K
Software Developer (CAD & BIM)

Message 3 of 4

TWhitehead_HED
Contributor
Contributor

Thank you Mohamed.  I'm going to try this today and see if it resolves the issue.  

0 Likes
Message 4 of 4

TWhitehead_HED
Contributor
Contributor

Again, thank  you Mohamed.  This worked a treat.  The part that was stopping me was the "selectMany".  I knew I needed the method GetTaggedLocalElementIds but didn't know how to implement.  

0 Likes