Tagging on multiple views with Dynamo

One of the most useful built-in automations in Revit is the “Tag All Not Tagged” command. However it tags elements in the active view only. In larger projects we often have dozens of views where we have to repeat the procedure. Therefore I have tried to create a Dynamo/Python script that can work on a view list and tag elements missing annotation.

The Dynamo definition is really simple here. We just have to collect a list of views, that we want to work on, category of the tagged elements and the tag itself.

The actual work is done inside of the Python node. First we have to convert given element category to a BuiltInCategory enum value. Then we will be able to use it as a filter for the FilteredElementCollector.

import System
bic = System.Enum.ToObject(BuiltInCategory,category.Id.IntegerValue) 

The FilteredElementCollector does actually most of the job in this script. We use two of them. First one collects all the elements in the view that potentially need to be tagged. Second collector gathers all tags present.

elementCollector = FilteredElementCollector(doc,v.Id).OfCategory(bic)
tagCollector = FilteredElementCollector(doc,v.Id).OfClass(IndependentTag)

Now, we just have to loop through the elements and check if they are not already tagged. We do that by comparing their id with ids of tag hosts.

notTagged = []
for e in elementCollector:
	tagged = False
	for t in tagCollector:
		if t.TaggedLocalElementId==e.Id:
			tagged = True
	if not tagged:
		notTagged.append(e)

Finally, we create new tags for elements that require them and set tag type to the one specified in the input.

for e in notTagged:
	t = IndependentTag.Create(doc,v.Id,Reference(e),False,TagMode.TM_ADDBY_CATEGORY,TagOrientation.Horizontal,e.Location.Point)
	t.ChangeTypeId(tag.Id)

The complete code can look like below. Also on the bottom of the post you can find a complete Dynamo file for download.

import clr
import System

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

#The inputs to this node will be stored as a list in the IN variables.
views = UnwrapElement(IN[0])
category = UnwrapElement(IN[1])
tag = UnwrapElement(IN[2])

bic = System.Enum.ToObject(BuiltInCategory,category.Id.IntegerValue)

output = []

TransactionManager.Instance.EnsureInTransaction(doc)

for v in views:
	if not v.IsTemplate:
		elementCollector = FilteredElementCollector(doc,v.Id).OfCategory(bic)
		tagCollector = FilteredElementCollector(doc,v.Id).OfClass(IndependentTag)
		notTagged = []
		for e in elementCollector:
			tagged = False
			for t in tagCollector:
				if t.TaggedLocalElementId==e.Id:
					tagged = True
			if not tagged:
				notTagged.append(e)
				
		for e in notTagged:
			t = IndependentTag.Create(doc,v.Id,Reference(e),False,TagMode.TM_ADDBY_CATEGORY,TagOrientation.Horizontal,e.Location.Point)
			t.ChangeTypeId(tag.Id)
			output.append(t)
		
TransactionManager.Instance.TransactionTaskDone()
			
#Assign your output to the OUT variable.
OUT = output
Posts created 34

Leave a Reply

Your email address will not be published.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top