Tagging Elements in view (Python)

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 :slight_smile:

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()

try this?

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()
1 Like

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

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.

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

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.

1 Like