Need Help on Python script to adjust Tags position

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

Heya,

I understand that professional coders use ChatGPT to assist their work. It’s giving someone who really understands this stuff a headstart with a decent outline they can debug. Unfortunately, even a misplaced comma will break all your code, so you won’t ever be able to just run it.

My approach, from scratch, would be to make a very simple piece of code work… Say your filtered element collector, then slowly build it up.

The FEC right now doesn’t work (for me anyway).

The code that works for me is :

FilteredElementCollector(doc, doc.ActiveView.Id).OfCategory(BuiltInCategory.OST_WallTags)

By outputting the code a bit at a time, I was able to understand that OfClass(FamilyInstance) is only for ‘non-annotation’ elements… Which is why everything else returned empty.

I’m not sure why OST_Tags doesn’t return anything, and I can’t find any documentation on it. However, I was able to find a forum post which gave me a clue, and use https://www.revitapidocs.com to see that there is an OST_Walls category. Trying that, worked. But then we would need to cycle through all kinds of tags, so if we’re reviewing a detail drawing we would probably use:

FilteredElementCollector(doc, doc.ActiveView.Id).OfClass(IndependentTag)

But that’s only for this example, it wouldn’t work for Room tags for example.

You can see how building all this up gets us to where we want to be, for now, while ChatGPT is still improving, I think you are probably best working to learn the basics of coding in Dynamo.

Searching this forum will give you working examples you can build upon to learn from, which I think, right now, will be more productive for you.

I hope that helps,

Mark

5 Likes

Learn to code.
Revit API 2023 (revitapidocs.com)

3 Likes