I have developed a Python script (despite not being familiar with Python, I relied on Chat GPT for assistance) that aims to adjust the position of overlapping tag headers. Specifically, if a tag overlaps with another tag, it will be moved in the Y direction until it finds a clear space.
Although the script runs without any errors, it does not appear to be functioning as intended. Unfortunately, I am unsure of the cause of this issue. If anyone could provide assistance, I would greatly appreciate it.
Below the script.
import clr
clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")
clr.AddReference("RevitServices")
from Autodesk.Revit.UI.Selection import ObjectType
from Autodesk.Revit.UI import UIApplication
from Autodesk.Revit.DB import *
from RevitServices.Persistence import DocumentManager
# Get the current Revit application and document
app = DocumentManager.Instance.CurrentUIApplication.Application
doc = DocumentManager.Instance.CurrentDBDocument
# Get all the tags in the active view
tags = FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(FamilyInstance).OfCategory(BuiltInCategory.OST_Tags)
# Sort the tags by their Y position
sorted_tags = sorted(tags, key=lambda x: x.TagHeadPosition.Y)
# Define a function to check if two tags overlap
def tags_overlap(tag1, tag2):
# Get the bounding boxes of the tags
box1 = tag1.get_BoundingBox(doc.ActiveView)
box2 = tag2.get_BoundingBox(doc.ActiveView)
# Check if the bounding boxes overlap
if box1.Intersects(box2):
return True
else:
return False
# Define a function to adjust the Y position of a tag
def adjust_tag_position(tag):
# Get the current Y position of the tag
current_y = tag.TagHeadPosition.Y
# Loop through the sorted tags and check if there is an overlap
for other_tag in sorted_tags:
if other_tag == tag:
continue
if tags_overlap(tag, other_tag):
# If there is an overlap, adjust the Y position of the tag
new_y = other_tag.TagHeadPosition.Y + other_tag.Symbol.get_Parameter(BuiltInParameter.TAG_OFFSET_Y).AsDouble() + tag.Symbol.get_Parameter(BuiltInParameter.TAG_OFFSET_Y).AsDouble() + tag.Symbol.get_Parameter(BuiltInParameter.TAG_HEAD_TO_TAG).AsDouble() + 0.01
tag.TagHeadPosition = XYZ(tag.TagHeadPosition.X, new_y, tag.TagHeadPosition.Z)
# Update the sorted tags list with the new Y position
sorted_tags = sorted(sorted_tags, key=lambda x: x.TagHeadPosition.Y)
# Recursively check if there are any further overlaps
adjust_tag_position(tag)
break
# Loop through the sorted tags and adjust their position if necessary
for tag in sorted_tags:
adjust_tag_position(tag)
# Return the updated tags
OUT = sorted_tags