# Tagging Elements in view (Python)

**URL:** <https://forum.dynamobim.com/t/tagging-elements-in-view-python/79654>\
**Category:** Developers\
**Created:** [August 8, 2022, 8:13pm UTC](https://forum.dynamobim.com/t/tagging-elements-in-view-python/79654 "2022-08-08T20:13:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Helmy0001](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@Helmy0001](https://forum.dynamobim.com/u/Helmy0001)\
**Post date:** [August 8, 2022, 8:13pm UTC](https://forum.dynamobim.com/t/tagging-elements-in-view-python/79654/1 "2022-08-08T20:13:43Z")

</div>

Hi All,

Have a situation where the code works fine to my needs however, tags are being implemented in areas where elements are not visible are still being tagged. Any help is appreciated. Thanks 🙂

```auto
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')

doc = __revit__.ActiveUIDocument.Document
view = doc.ActiveView

# Start transaction
t = Transaction(doc, 'Tag Elements')
t.Start()

# Get all elements in the view
untagged = FilteredElementCollector(doc, view.Id)\
	.WhereElementIsNotElementType()\
	.WhereElementIsViewIndependent()\
	.ToElements()

# Tag elements
for i in untagged:	
	if i.Category and i.Category.Name in ['Doors', 'Windows']:
		try:
			IndependentTag.Create(doc, view.Id, Reference(i), True, TagMode.TM_ADDBY_CATEGORY, TagOrientation.Horizontal, XYZ(0,0,0))
		except:
			pass
	elif i.Category and i.Category.Name in ['Walls']:
		try:
			IndependentTag.Create(doc, view.Id, Reference(i), True, TagMode.TM_ADDBY_CATEGORY, TagOrientation.Vertical, XYZ(0,0,0))
		except:
			pass

# Commit transaction
t.Commit()

```

---

<div class="post-metadata">

**Author:** ![newshunhk](https://avatars.discourse-cdn.com/v4/letter/n/3bc359/32.png) [@newshunhk](https://forum.dynamobim.com/u/newshunhk)\
**Post date:** [August 9, 2022, 3:47am UTC](https://forum.dynamobim.com/t/tagging-elements-in-view-python/79654/2 "2022-08-09T03:47:18Z")

</div>

try this?

```auto
import clr
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')

doc = __revit__.ActiveUIDocument.Document
view = doc.ActiveView

# Start transaction
t = Transaction(doc, 'Tag Elements')
t.Start()

# Get all elements in the view
untagged = FilteredElementCollector(doc, view.Id)\
	.WhereElementIsNotElementType()\
	.WhereElementIsViewIndependent()\
	.ToElements()

# Tag elements
for i in untagged:
	if not i.IsHidden(view):
		if i.Category and i.Category.Name in ['Doors', 'Windows']:
			try:
				IndependentTag.Create(doc, view.Id, Reference(i), True, TagMode.TM_ADDBY_CATEGORY, TagOrientation.Horizontal, XYZ(0,0,0))
			except:
				pass
		elif i.Category and i.Category.Name in ['Walls']:
			try:
				IndependentTag.Create(doc, view.Id, Reference(i), True, TagMode.TM_ADDBY_CATEGORY, TagOrientation.Vertical, XYZ(0,0,0))
			except:
				pass

# Commit transaction
t.Commit()

```

---

<div class="post-metadata">

**Author:** ![Helmy0001](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@Helmy0001](https://forum.dynamobim.com/u/Helmy0001)\
**Post date:** [August 9, 2022, 9:08am UTC](https://forum.dynamobim.com/t/tagging-elements-in-view-python/79654/3 "2022-08-09T09:08:21Z")

</div>

Sorry might have explained this wrong. The issue is that when I have an elevation for example and doors are behind a wall, they are sill being tagged although they are not showing. Not sure if clipping would be useful in a situation like this.  
Thanks

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [August 9, 2022, 1:27pm UTC](https://forum.dynamobim.com/t/tagging-elements-in-view-python/79654/4 "2022-08-09T13:27:03Z")

</div>

That’s expected behavior. From Revit’s perspective, “Visible” means “In View”, not necessarily that it’s visible on screen. Determining whether something is actually visible on screen becomes much more difficult.

---

<div class="post-metadata">

**Author:** ![Helmy0001](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@Helmy0001](https://forum.dynamobim.com/u/Helmy0001)\
**Post date:** [August 9, 2022, 1:37pm UTC](https://forum.dynamobim.com/t/tagging-elements-in-view-python/79654/5 "2022-08-09T13:37:27Z")

</div>

I guess that would require a bounding box to crop view extents?

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [August 9, 2022, 1:58pm UTC](https://forum.dynamobim.com/t/tagging-elements-in-view-python/79654/6 "2022-08-09T13:58:33Z")

</div>

That would help with “fringe” elements that may be just outside the visible crop, but it won’t do anything for elements hidden by other elements. That would require checking all elements (either their geometry or bounding box) against all other elements (geometry or bounding box again) in terms of location in the view direction to determine whether anything is masked by another object and therefore “not visible”. As you can imagine, this gets pretty computationally heavy pretty quickly.
