Hi, in the general feeling of sharing with the community, is there any place to share short scripts/snippets which users develop to aid our fellow architects? what would be an appropriate place for that?
Here is a small script i wrote with the help of ChatGPT which will take a sheet or sheets which have dependent views on them, and replace said dependent views with “fresh” copies from the original parent view as to achieve consistency between parent and child views on sheets (seeing as hiding or overriding graphics does not carry on from parent to child view).
# Import necessary Revit API and pyRevit libraries
from Autodesk.Revit.DB import FilteredElementCollector, BuiltInCategory, ViewSheet, Viewport, View, ViewDuplicateOption, Transaction, XYZ
from pyrevit import script
# Ensure the active Revit document is correctly defined
doc = __revit__.ActiveUIDocument.Document
# Function to check if a view is a dependent view
def is_dependent_view(view):
return view.GetPrimaryViewId().IntegerValue != -1
# Function to process each sheet
def process_sheet(sheet, summary):
viewports = FilteredElementCollector(doc).OwnedByView(sheet.Id).OfCategory(BuiltInCategory.OST_Viewports).ToElements()
num_views = len(viewports)
num_dependent_views = 0
for vp in viewports:
view_id = vp.ViewId
view = doc.GetElement(view_id)
if is_dependent_view(view):
num_dependent_views += 1
primary_view_id = view.GetPrimaryViewId()
primary_view = doc.GetElement(primary_view_id)
new_view_id = primary_view.Duplicate(ViewDuplicateOption.AsDependent)
new_view = doc.GetElement(new_view_id)
view.Name = "{} old".format(view.Name)
new_view_name = "{} dependent".format(primary_view.Name)
new_view.Name = new_view_name
vp_location = vp.GetBoxCenter()
doc.Delete(vp.Id)
Viewport.Create(doc, sheet.Id, new_view.Id, vp_location)
summary.append((sheet.SheetNumber, view.Name, new_view.Name))
doc.Delete(view.Id)
if new_view.CropBoxVisible:
new_view.CropBoxVisible = False
script.get_output().print_md("Cropbox hidden.")
else:
script.get_output().print_md("Cropbox already hidden")
return num_views, num_dependent_views
# Main function
def main():
uidoc = __revit__.ActiveUIDocument
selection_ids = uidoc.Selection.GetElementIds()
if not selection_ids:
script.get_output().print_md("No sheets selected. Please select at least one sheet.")
return
sheets = [doc.GetElement(id) for id in selection_ids if isinstance(doc.GetElement(id), ViewSheet)]
if len(sheets) != len(selection_ids):
script.get_output().print_md("Selection contains non-sheet elements. Please select only sheets.")
return
summary = []
t = Transaction(doc, 'Duplicate and Replace Dependent Views on Multiple Sheets')
t.Start()
try:
for sheet in sheets:
process_sheet(sheet, summary)
t.Commit()
except Exception as e:
t.RollBack()
script.get_output().print_md("Error during operation: {}".format(str(e)))
return
# Output summary
if summary:
output = script.get_output()
output.print_md("### Operation Summary:")
for sheet_number, old_view, new_view in summary:
output.print_md("- **Sheet {0}**: Replaced '{1}' with '{2}'".format(sheet_number, old_view, new_view))
else:
script.get_output().print_md("No dependent views were replaced.")
if __name__ == "__main__":
main()