Python - List Management with spaces & tags

I’ve got this far in python by reading through alot of posts on here about it, and hoping someone can help me tie the last couple of bits together.

It add tags to MEP Spaces on multiple views, but doesnt avoiding duplicates/ existing tags.

i wanted to try and do most if it in one python node, to learn a bit more.

so i’m feeding in a couple of views that contain spaces and i’ve left one tag on each, so i can avoid tagging them again.

my first problem is i can’t seem to keep the list of tag space Ids & space Ids in the same list structure as the spaces and tags in views (split per view). (i thought keeping inside the first for loop would do this).

So firstly how can i keep the list structures the same? Hoping this will help with the next issue!

Secondly how can i create a new list with the tagged spaces removed, so it can go through to create the tags. assuming its going to be along the lines of ‘if taggedElem.Id == SpaceId’, but i cant seem to tie the lists together.

If anyone can help me or give me some pointers, it would be much apricated! :pray:

Python Code

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

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

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
TB = []
doc = DocumentManager.Instance.CurrentDBDocument
#The inputs to this node will be stored as a list in the IN variables.
views = UnwrapElement(IN[0])
tag = UnwrapElement(IN[1])
#catid = UnwrapElement(IN[3])

#bic = System.Enum.ToObject(BuiltInCategory,catid)

output = []
viewspaces = []
viewtags = []
taggedelements = []
taggedelementID = []
spacesID = []
newspaces = []

TransactionManager.Instance.EnsureInTransaction(doc)

#find elements in views
for v in views:
	spaceCollector = (FilteredElementCollector(doc, v.Id).OfCategory(BuiltInCategory.OST_MEPSpaces).ToElements())
	tagCollector = (FilteredElementCollector(doc, v.Id).OfClass(SpatialElementTag).ToElements())
	viewspaces.append(spaceCollector)
	viewtags.append(tagCollector)
			
#filter elements with tags	
	for tag in tagCollector:
		tagID = doc.GetElement(tag.Id)
		taggedSpace = tagID.Space
		taggedElem = doc.GetElement(taggedSpace.Id)
		taggedelements.append(taggedElem)
		taggedelementID.append(taggedElem.Id)

	for space in spaceCollector:
		spaceID = doc.GetElement(space.Id)
		spacesID.append(spaceID.Id)
			
#add tags to elements
	for e in spaceCollector:
		doc.Create.NewSpaceTag(e,UV(e.Location.Point.X,e.Location.Point.Y),v)
		output.append(e)
TransactionManager.Instance.EnsureInTransaction(doc)

#OUT = taggedspace#, taggedspaces
OUT = (viewspaces, viewtags), (spacesID, taggedelementID)

Hello,
try this

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

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

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
TB = []
doc = DocumentManager.Instance.CurrentDBDocument
#The inputs to this node will be stored as a list in the IN variables.
views = UnwrapElement(IN[0])
tag = UnwrapElement(IN[1])

viewspaces = []
viewtags = []

TransactionManager.Instance.EnsureInTransaction(doc)

#find elements in views
for v in views:
	spaceCollector = FilteredElementCollector(doc, v.Id).OfCategory(BuiltInCategory.OST_MEPSpaces).ToElements()
	tagCollector = FilteredElementCollector(doc, v.Id).OfClass(SpatialElementTag).ToElements()
	viewspaces.append(spaceCollector)
	#
	spaceIds_already_tagged = [t.Space.Id for t in tagCollector]

	for e in spaceCollector:
		if e.Id not in spaceIds_already_tagged:
			newtag = doc.Create.NewSpaceTag(e,UV(e.Location.Point.X,e.Location.Point.Y),v)
			tagCollector.append(newtag)
	viewtags.append(tagCollector)
	
TransactionManager.Instance.EnsureInTransaction(doc)

OUT = viewspaces, viewtags
2 Likes

Thank you very much @c.poupin !! so much simpler than the way i was trying to do it.

Occasionally i get an error on the append at the end, but i #'d those last two lines out and it still works. I just cant get the list of tags out at the end.

not the end of the world but would be interesting to know why, and seems strange how it does work fine sometimes :person_shrugging:

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed. 
Traceback (most recent call last):
  File "<string>", line 41, in <module>
AttributeError: 'List[Element]' object has no attribute 'append'
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

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

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

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
TB = []
doc = DocumentManager.Instance.CurrentDBDocument
#The inputs to this node will be stored as a list in the IN variables.
views = UnwrapElement(IN[0])
tag = UnwrapElement(IN[1])

viewspaces = []
viewtags = []

TransactionManager.Instance.EnsureInTransaction(doc)

#find elements in views
for v in views:
	spaceCollector = FilteredElementCollector(doc, v.Id).OfCategory(BuiltInCategory.OST_MEPSpaces).ToElements()
	tagCollector = FilteredElementCollector(doc, v.Id).OfClass(SpatialElementTag).ToElements()
	viewspaces.append(spaceCollector)
	#
	spaceIds_already_tagged = [t.Space.Id for t in tagCollector]

	for e in spaceCollector:
		if e.Id not in spaceIds_already_tagged:
			newtag = doc.Create.NewSpaceTag(e,UV(e.Location.Point.X,e.Location.Point.Y),v)
		#	tagCollector.append(newtag)
		#viewtags.append(tagCollector)
	
TransactionManager.Instance.EnsureInTransaction(doc)

OUT = viewspaces, viewtags

What version of Revit and Dynamo are you using?

ahh ok, i’m on 2.31 at the moment. Will that be it?

the traceback error is just… strange :face_with_raised_eyebrow:

try to replace this

by

			tagCollector.Add(newtag)
		viewtags.append(tagCollector)
1 Like

i did try that based on a post you made in another topic, but that didnt work.

but i copy/pasted that in and it seems to work fine!

Thanks again @c.poupin, this has been a huge help! :pray: :pray: