Added some methods:
- Rename views by sheet number, view type, detail number and an additional shared parameter
- If view name already exists in the project rename the view that is “blocking” the name
- Register/unregister with taskdialog
Also tried to get it working in Dynamo, while the registration works properly i gave up because error reporting and debugging is impossible…
from Autodesk.Revit.DB import IUpdater, UpdaterId, ElementId, UpdaterRegistry
from Autodesk.Revit.DB import Element, ElementCategoryFilter, BuiltInCategory, ChangePriority,Element, ElementId, ElementParameterFilter, ParameterValueProvider, FilterStringEquals, BuiltInParameter
from Autodesk.Revit.UI import TaskDialog,TaskDialogCommonButtons,TaskDialogResult
from Autodesk.Revit.DB import Transaction
from System import Guid
# Define the SimpleUpdater
class SimpleUpdater(IUpdater):
def __init__(self, doc):
self._doc = doc
def Execute(self, data):
from Autodesk.Revit.DB import ViewType, FilteredElementCollector, BuiltInCategory, BuiltInParameter
def find_unique_name(name, views):
"""Returns a unique name by appending an incrementing number."""
counter = 1
new_name = name + " - old"
while any(view.Name == new_name for view in views):
new_name = "{} - old({})".format(name, counter)
counter += 1
return new_name
print("Updater was triggered!")
# Combine both element lists
element_ids = list(data.GetAddedElementIds()) + list(data.GetModifiedElementIds())
# Fetch all views for the name check
existing_views = FilteredElementCollector(self._doc).OfCategory(BuiltInCategory.OST_Views).WhereElementIsNotElementType().ToElements()
for id in element_ids:
viewport = self._doc.GetElement(id)
view_id = viewport.ViewId
view = self._doc.GetElement(view_id)
view_type = view.ViewType # Get view type
# Fetch parameters
try:
sheet = self._doc.GetElement(viewport.SheetId)
sheet_number_param = sheet.get_Parameter(BuiltInParameter.SHEET_NUMBER)
detail_number_param = viewport.get_Parameter(BuiltInParameter.VIEWPORT_DETAIL_NUMBER)
suffix_param = view.LookupParameter('View Name Suffix (AFRY)')
if sheet_number_param and detail_number_param:
sheet_number = sheet_number_param.AsString()
detail_number = detail_number_param.AsString()
suffix_value = "" if not suffix_param else suffix_param.AsString()
# Determine naming based on view type
if view_type == ViewType.Section:
type_name = "Section"
elif view_type == ViewType.FloorPlan:
type_name = "FloorPlan"
else:
type_name = view_type.ToString() # Default to view type name
# Construct the new view name
new_name = "{} - {} - {}{}".format(sheet_number, type_name, detail_number, " - {}".format(suffix_value) if suffix_value else "")
# Check if the new name is already in use
for existing_view in existing_views:
if existing_view.Name == new_name:
# Find a unique name for the blocking view
unique_name = find_unique_name(new_name, existing_views)
existing_view.Name = unique_name
print("Existing view renamed to: " + unique_name)
break
# Set the new name to the current view
view.Name = new_name
print("New view name set to: " + new_name)
else:
print("Failed to retrieve sheet or detail number for view: " + view.Name)
except Exception as e:
print("Error renaming the view: {}".format(e))
def GetUpdaterId(self):
# Return the unique identifier for this updater
return self.updater_id
def GetUpdaterName(self):
return 'SimpleUpdaterName'
def GetAdditionalInformation(self):
return 'A simple updater for testing purposes'
def GetChangePriority(self):
return ChangePriority.Annotations
def Initialize(self):
# This is where you can add trigger conditions for the updater
pass
def Uninitialize(self):
pass
# Get the current document and application
doc = __revit__.ActiveUIDocument.Document
app = __revit__.Application
# Create an instance of the updater
updater = SimpleUpdater(doc)
# Create a unique Guid for the updater
guid_string = "676d4886-3106-4b53-b1ac-9183947d3cdf"
guid = Guid(guid_string)
# Create an UpdaterId using the AddInId of the current application and the unique Guid
updater_id = UpdaterId(app.ActiveAddInId, guid)
# Set the identifier in the updater instance
updater.updater_id = updater_id
# Create a filter for views
viewport_filter = ElementCategoryFilter(BuiltInCategory.OST_Viewports)
# Get the ElementId for the VIEWPORT_DETAIL_NUMBER parameter
param_id = ElementId(BuiltInParameter.VIEWPORT_DETAIL_NUMBER)
# If the updater is already registered, ask the user if they want to unregister it
if UpdaterRegistry.IsUpdaterRegistered(updater_id, doc):
dialog_result = TaskDialog.Show("Updater Options",
"The updater is already registered. Do you want to unregister it?",
TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No)
# Depending on the user's choice, unregister the updater
if dialog_result == TaskDialogResult.Yes:
# User chose to unregister the updater
UpdaterRegistry.UnregisterUpdater(updater_id, doc)
TaskDialog.Show('Success', 'Updater has been unregistered!')
else:
# If the updater is not registered, then register it
UpdaterRegistry.RegisterUpdater(updater, doc, True)
# Add trigger for the modification of the VIEWPORT_DETAIL_NUMBER parameter
UpdaterRegistry.AddTrigger(updater_id, viewport_filter, Element.GetChangeTypeParameter(param_id))
# Add trigger for the addition of a new viewport to a sheet
UpdaterRegistry.AddTrigger(updater_id, viewport_filter, Element.GetChangeTypeElementAddition())
TaskDialog.Show('Success', 'Updater has been registered and trigger has been set!')