Python code do retrieve all Annotation Types

Hi,

Since I don’t know Python, I asked ChatGPT to develop a code to retrieve all the types of all the annotations at once. It’s for changing all the arrowheads with a few clicks.

The code, that’s failing in line 16, is:

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 *

doc = DocumentManager.Instance.CurrentDBDocument

TransactionManager.Instance.EnsureInTransaction(doc)

collector = FilteredElementCollector(doc)
annotation_categories = collector.OfCategory(BuiltInCategory.OST_AnnotationCategories).WhereElementIsNotElementType().ToElements()

for category in annotation_categories:
    types = collector.OfClass(category.GetTypeId()).ToElements()
    for t in types:
        if t.GetType().ToString() == "Autodesk.Revit.DB.TextNoteType":
            t.SetDuplicateTypeAction(DuplicateTypeAction.UseExistingType)
        else:
            t.SetDuplicateTypeAction(DuplicateTypeAction.Duplicate)
            t.Duplicate()
            
TransactionManager.Instance.TransactionTaskDone()

The error:
Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File “”, line 16, in
AttributeError: ‘type’ object has no attribute ‘OST_AnnotationCategories’

Could someone please help me?

Guess ChatGPT doesn’t do Revit.
OST_AnnotationCategories) isn’t a thing.
Are you after: OST_GenericAnnotation?

Collecting everything that is an annotation is a lot more involved. (Dimensions, symbols, notes, elevation tags, sections, etc. etc. etc.

And you don’t need to wrap collecting information inside a transaction. Waste of time and memory.

Actually I’m after a replacement for and old Rhythm node:

It was so useful!

It isn’t present in Rhythm anymore, and I thought of getting a wider workaround using Python.

PS: I tried to edit the custom node to see if I could update something inside and make it work againg, but it doesn’t edit.

Then you need the OST of all of those different categories.
ChatGPT isn’t going to write that for you. You’re better off going back to the old Rhythm package and starting from there. Of course, you’ll need to learn to code.
BuiltInCategory Enumeration (revitapidocs.com)

You’re probably right. I’m evading learn Python for too long now.

If I may ask, can I list all of the individual categories in the same code, or is it better to do separeted nodes for each category I desire do select?

A sample…

 cat_list = [BuiltInCategory.OST_Rooms, BuiltInCategory.OST_Walls, BuiltInCategory.OST_Windows, BuiltInCategory.OST_Doors]
    typed_list = List[BuiltInCategory](cat_list)
    filter = ElementMulticategoryFilter(typed_list)
    output = FilteredElementCollector(doc).WherePasses(filter).ToElements()
1 Like

Hi Rodrigo,

I’ve asked ChatGPT to double check your code.
See if this helps:

The error message suggests that BuiltInCategory does not have the attribute OST_AnnotationCategories. This could be due to a simple typo or a version mismatch between the Revit API and the code.

To fix the error, you can change BuiltInCategory.OST_AnnotationCategories to BuiltInCategory.OST_Annotation. The OST_Annotation category includes all annotation categories, which should achieve the same result.

Here’s the updated code:

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 *

doc = DocumentManager.Instance.CurrentDBDocument

TransactionManager.Instance.EnsureInTransaction(doc)

collector = FilteredElementCollector(doc)
annotation_categories = collector.OfCategory(BuiltInCategory.OST_Annotation).WhereElementIsNotElementType().ToElements()

for category in annotation_categories:
    types = collector.OfClass(category.GetTypeId()).ToElements()
    for t in types:
        if t.GetType().ToString() == "Autodesk.Revit.DB.TextNoteType":
            t.SetDuplicateTypeAction(DuplicateTypeAction.UseExistingType)
        else:
            t.SetDuplicateTypeAction(DuplicateTypeAction.Duplicate)
            t.Duplicate()
            
TransactionManager.Instance.TransactionTaskDone()

:wink:

(a joke ofcourse. I don’t think getting chatGPT to write you a piece of code only to end up on the Dynamo forum for others to troubleshoot your generated code is best practice. I think ChatGPT is great for learning code as you can ask it simple questions, but writing an entire working code for Revit doesnt really work all that well yet, if ever…)

Thanks for sharing.