create views from new levels

# import pyrevit libraries
from Autodesk.Revit.DB import *

from pyrevit import script, revit, DB, forms
from System.Collections.Generic import List

doc = revit.doc

# get all levels, views, and view family types
levels_all = DB.FilteredElementCollector(doc).OfClass(DB.Level).WhereElementIsNotElementType().ToElements()
views_all = DB.FilteredElementCollector(doc).OfClass(DB.View).WhereElementIsNotElementType().ToElements()
vfts_all = DB.FilteredElementCollector(doc).OfClass(DB.ViewFamilyType).WhereElementIsElementType().ToElements()

# extract level names and IDs
level_data = []
for level in levels_all:
    level_data.append(level.Id)

# extract view family types for floor plans
vft_data = []
for vft in vfts_all:
    if vft.ViewFamily == ViewFamily.FloorPlan:
        vft_data.append(vft.Id)

# display a dialog to select levels
selected_levels = forms.SelectFromList.show(
    level_data,
    title="Select levels to create views",
    width=500,
    button_name="Create Views",
    multiselect=True
)

# display a dialog to select a view family type
selected_vft = forms.SelectFromList.show(
    vft_data,
    title="Select a view family type",
    width=500,
    button_name="Create Views"
)


# create views based on the selected levels and view family types
t = Transaction(doc, 'Create Floor Plan Views')
t.Start()

for selected_level in selected_levels:
    level_id = selected_level
    level_name = selected_level

    for vft in vft_data:
        view_name = level_name
        view = DB.ViewPlan.Create(doc, selected_vft, selected_levels)
        view.Name = view_name

t.Commit()

this script is it ok?

Hi @shuvro.arc , welcome to the community!

What are you asking here exactly?

Did you run the script in your pyrevit? does it throw any error, or behaves differently than what you thought?

From a quick look:

  • level_data can be directly created using ToElementIds instead of ToElements
  • pyRevit python library offers a Transaction object that handles the start and commit automatically using with Transaction():
  • you create the level_id, level_name, view_name to then assign the value to view.Name, you can directly to view.Name = selected_level
  • for the ViewPlan.Create method:
    • you always pass the same ViewFamilyType, but loop through all the vtf_data, so there will be many duplicates
    • you should use selected_vft.Id, not the object itself
    • you should use the selected_level.Id instead of selected_levels (the method accepts a single id, not a list of levels)
1 Like