Element is tagged in the view?

Element is tagged in the view?

Anonymous
Not applicable
1,708 Views
1 Reply
Message 1 of 2

Element is tagged in the view?

Anonymous
Not applicable

Hi All, 

 

I made the following method to check if an element is already tagged. I just want to do the check in the active view, is it possible:

 

public bool ElementIsTagged(
            Document doc, 
            Element ele, 
            BuiltInCategory bicTag, 
            BuiltInCategory bicElements)
        {
            // Coleccion de los tags
            List<Element> tagColl
                = new FilteredElementCollector(doc)
                .OfCategory(bicTag)
                .Where(x => x is IndependentTag)
                .ToList();

            // Coleccion de los elementos
            List<Element> eleColl
                = new FilteredElementCollector(doc)
                .OfCategory(bicElements)
                .ToList();

            // Extrae las ID de los elementos que estan taggeados en el modelo.
            List<ElementId> ids = new List<ElementId>();

            foreach(Element e in tagColl)
            {
                IndependentTag tag = e as IndependentTag;

                ElementId id = tag.TaggedLocalElementId;

                int aux1 = tag.TaggedLocalElementId.IntegerValue;
                
                bool aux2 = ids.Contains(tag.TaggedLocalElementId);

                if ((tag.TaggedLocalElementId.IntegerValue != -1) 
                    && (ids.Contains(tag.TaggedLocalElementId) == false))
                {
                    ids.Add(tag.TaggedLocalElementId);
                }
            }

            // Transforma a una lista de Unique Items
            List<ElementId> uniqueIds = ids.Distinct().ToList();

            // Se extraen los ids de los elementos
            List<ElementId> eleIds = new List<ElementId>();

            foreach (Element e in eleColl)
            {
                eleIds.Add(e.Id);
            }

            // Se compara si los elementos estan taggeados
            List<ElementId> idEleTag = new List<ElementId>();

            foreach(ElementId id in eleIds)
            {
                if (uniqueIds.Contains(id))
                {
                    idEleTag.Add(id);
                }
            }

            // COMPARACION - Si el elemento esta en la lista de elementos taggeados, entrega "true"
            // de lo contrario, entrega "false".

            if (idEleTag.Contains(ele.Id))
            {
                return true;
            }
            else
            {
                return false;
            }

        }

Thanks in advance!

 

CHeers

0 Likes
Accepted solutions (1)
1,709 Views
1 Reply
Reply (1)
Message 2 of 2

Anonymous
Not applicable
Accepted solution

I found the solution: 

 

// Coleccion de los tags
            List<Element> tagColl
                = new FilteredElementCollector(doc, doc.ActiveView.Id)
                .OfCategory(bicTag)
                .Where(x => x is IndependentTag)
                .ToList();

            // Coleccion de los elementos
            List<Element> eleColl
                = new FilteredElementCollector(doc, doc.ActiveView.Id)
                .OfCategory(bicElements)
                .ToList();

Hope this will be useful for someone else.

 

Cheers!