how to verify label on element using revit api?

how to verify label on element using revit api?

prasannamurumkar
Advocate Advocate
1,882 Views
10 Replies
Message 1 of 11

how to verify label on element using revit api?

prasannamurumkar
Advocate
Advocate

hook is present in bathroom in given project.

Now wants to verify whether this hook is labeled by"HK" using revit api?

how do we that?

 

0 Likes
Accepted solutions (2)
1,883 Views
10 Replies
Replies (10)
Message 2 of 11

BardiaJahan
Advocate
Advocate

Assuming your question is about Tags (IndependentTag class in Revit API): tags are dependents of model elements. This means if you delete the element they will be deleted, then you can retrieve the tag ElementId and Rollback the transaction. Then retrieve the TagText property of the tag and compare it to "HK" or anything else.

Message 3 of 11

prasannamurumkar
Advocate
Advocate

Thank you for reply.

1.able to retrive tag

var doortag = new HashSet<ElementId>(collector1.OfClass(typeof(IndependentTag)).OfCategory(BuiltInCategory.OST_DoorTags).OfType<IndependentTag>().Select(x => x.GetTaggedLocalElement().Id).Where(x => x != null));

2.Able to retrive ElementID of tags.

foreach (ElementId i in doortag)

3.How to retrive Tag text property and how to compare with any particular name?

 

Could you give code for that.

 

0 Likes
Message 4 of 11

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @prasannamurumkar ,

I hope this will help

foreach(ElementId eid in tagElementIds)
                {
                    Element e = doc.GetElement(eid);
                    IndependentTag Doortag = e as IndependentTag;
                    if(Doortag!=null)
                    {
                       if(Doortag.TagText=="Name to compare")
                       {
                            //Your code
                       }
                    }
                }

Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 5 of 11

prasannamurumkar
Advocate
Advocate
 Element e = doc.GetElement(eid);

able to get element.For this

 IndependentTag Doortag = e as IndependentTag;

 Doortag is showing null.

 

0 Likes
Message 6 of 11

naveen.kumar.t
Autodesk Support
Autodesk Support

Hi @prasannamurumkar,

Your code collects the door not the door tag.

You can't cast door as independent tag.

Collect all doortags not doors and from the collected doortags try to get the tagtext.


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 7 of 11

prasannamurumkar
Advocate
Advocate

but how to collect tags tried this one but not getting collect.

IEnumerable<Element> roomTags = new FilteredElementCollector(activeDoc, activeDoc.ActiveView.Id).OfCategory(BuiltInCategory.OST_RoomTags).WhereElementIsNotElementType().ToElements();

is this right?

or sholud try something else?

0 Likes
Message 8 of 11

naveen.kumar.t
Autodesk Support
Autodesk Support
Accepted solution

Hi @prasannamurumkar ,

Here the question is which tags you want to collect?

Do you want to collect Roomtags or DoorTags?

this code will help you to collect RoomTags

 FilteredElementCollector Collector = new FilteredElementCollector(doc,doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_RoomTags).WhereElementIsNotElementType();
                IList<ElementId> eids = Collector.ToElementIds() as IList<ElementId>;
                
                foreach(ElementId eid in eids)
                {
                    Element e = doc.GetElement(eid);
                    RoomTag IT = e as RoomTag;
                    string s=IT.TagText;
                }

and this code will help you to collect DoorTags

FilteredElementCollector Collector = new FilteredElementCollector(doc,doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_DoorTags).WhereElementIsNotElementType();
                IList<ElementId> eids = Collector.ToElementIds() as IList<ElementId>;
                
                foreach(ElementId eid in eids)
                {
                    Element e = doc.GetElement(eid);
                    IndependentTag IT = e as IndependentTag;
                     string s=IT.TagText;
                }

I tested the above codes and both are working fine for me.

 

 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network

Message 9 of 11

prasannamurumkar
Advocate
Advocate

thank you it works fine.

0 Likes
Message 10 of 11

prasannamurumkar
Advocate
Advocate

Hi 

Do we able to get relation between tag and its element?

as in above case  able to get independent tags of particular category.

for eg having 6 doors got 6 door tags in above case

suppose one of door does not have tag then it should able to find particular door does not having tag.

so it is require find relation between element category and element tag category.

 

0 Likes
Message 11 of 11

naveen.kumar.t
Autodesk Support
Autodesk Support
Accepted solution

Hi @prasannamurumkar ,

try using the below code

this code will highlight the elements which are not taggged

IList<ElementId> ElementsWithoutTag = new List<ElementId>();

                foreach(Element e in new FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Doors).OfClass(typeof(FamilyInstance)))
                {
                    if(new FilteredElementCollector(doc).OfClass(typeof(IndependentTag)).Cast<IndependentTag>().FirstOrDefault(q=>q.TaggedLocalElementId==e.Id)==null)
                    {
                        ElementsWithoutTag.Add(e.Id);
                    }
                }
uidoc.Selection.SetElementIds(ElementsWithoutTag);
uidoc.RefreshActiveView();

If this helped solve your problem please mark it as solution, so other users can get this solutions as wellSmiley Happy

 


Naveen Kumar T
Developer Technical Services
Autodesk Developer Network