Oct 10, 2022

Create Views with Revit API + python

We need to create Views for many different tools. So let's learn learn how to get correct ViewTypes and create views.

❓How to create Views with Revit API?

When you’re creating a view in Revit, there are a few things to keep in mind. First, you need to know what kind of view you want to create:

  • ViewPlan (FloorPlan, Ceiling Plan, StructuralPlan, AreaPlan)

  • ViewSection (Section, Callout, Detail)

  • View3D (Isometric, Perspective)

  • ViewDrafting

  • ViewSheet

  • Legend View

Once you have an idea of what kind of view you want to create, it's time to check Revit API documentation for specific methods on how to create such view.

πŸ‘‡ Here are their syntaxes. You can see in cyan color from what class it comes. (e.g. ViewPlan Class)

Let's look at each one of them to create views

0️⃣ Get all ViewFamilyTypes

But before that we need to prepare our code by getting all of the ViewFamilyTypes.

You will see across all of the methods, we always need to provide viewFamilyTypeId. So let's start by getting and filtering all of View Types so we can use it later on.

Usually you will need to create some User control so they can choose specific ViewType, but I will just use a random one for this post.

#🎴 ALL VIEW TYPES
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()

#πŸ”Ž FILTER CERTAIN VIEW TYPES
view_types_plans      = [vt for vt in view_types if vt.ViewFamily == ViewFamily.FloorPlan]
view_types_sections   = [vt for vt in view_types if vt.ViewFamily == ViewFamily.Section]
view_types_3D         = [vt for vt in view_types if vt.ViewFamily == ViewFamily.ThreeDimensional]
view_types_legends    = [vt for vt in view_types if vt.ViewFamily == ViewFamily.Legend]
view_types_drafting   = [vt for vt in view_types if vt.ViewFamily == ViewFamily.Drafting]
view_types_elevations = [vt for vt in view_types if vt.ViewFamily == ViewFamily.Elevation]
view_types_ceil_views = [vt for vt in view_types if vt.ViewFamily == ViewFamily.CeilingPlan]
view_types_structural = [vt for vt in view_types if vt.ViewFamily == ViewFamily.StructuralPlan]
view_types_area       = [vt for vt in view_types if vt.ViewFamily == ViewFamily.AreaPlan]

1️⃣ How to Create ViewPlan?

Let's start by making a few ViewPlans.

ViewPlan is every horizontally cut plan that we use in Revit, such as Floor Plan, Ceiling Plan, Structural Plan, Area Plan...

And they all use the same method.

ViewPlan.Create(doc, 
                viewFamilyTypeId, 
                levelId)

I have already shown you how to get viewFamilyTypes so now it's about getting other arguments.
You can Copy-Paste these snippets and they will create views in your project!

Create FloorPlan
# -*- coding: utf-8 -*-
#⬇ IMPORTS
from Autodesk.Revit.DB import *

#πŸ“¦ VARIABLES
doc          = __revit__.ActiveUIDocument.Document
active_view  = doc.ActiveView
active_level = doc.ActiveView.GenLevel

#🎴 ALL VIEW TYPES
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()

#πŸ”Ž FILTER VIEW TYPES
view_types_plans = [vt for vt in view_types if vt.ViewFamily == ViewFamily.FloorPlan]
floor_plan_type  = view_types_plans[0]


#🎯 Create Floor Plan
with Transaction(doc,'Create Floor Plan') as t:
    t.Start()
    view = ViewPlan.Create(doc, floor_plan_type.Id, active_level.Id)
    t.Commit()

__author__ = 'πŸ™‹β€β™‚οΈ Erik Frits'
Create CeilingPlan
# -*- coding: utf-8 -*-
#⬇ IMPORTS
from Autodesk.Revit.DB import *

#πŸ“¦ VARIABLES
doc          = __revit__.ActiveUIDocument.Document
active_view  = doc.ActiveView
active_level = doc.ActiveView.GenLevel

#🎴 ALL VIEW TYPES
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()

#πŸ”Ž FILTER VIEW TYPES
view_types_ceil_views = [vt for vt in view_types if vt.ViewFamily == ViewFamily.CeilingPlan]
ceil_plan_type  = view_types_ceil_views [0]


#🎯 Create Create Ceiling Plan
with Transaction(doc,'Create Ceiling Plan') as t:
    t.Start()
    view = ViewPlan.Create(doc, ceil_plan_type.Id, active_level.Id)
    t.Commit()

__author__ = 'πŸ™‹β€β™‚οΈ Erik Frits'
Create AreaPlan
# -*- coding: utf-8 -*-
#⬇ IMPORTS
from Autodesk.Revit.DB import *

#πŸ“¦ VARIABLES
doc          = __revit__.ActiveUIDocument.Document
active_view  = doc.ActiveView
active_level = doc.ActiveView.GenLevel

#🎴 ALL VIEW TYPES
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()

#πŸ”Ž FILTER VIEW TYPES
view_types_area = [vt for vt in view_types if vt.ViewFamily == ViewFamily.AreaPlan]
area_plan_type  = view_types_area[0]


#🎯 Create Area Plan
with Transaction(doc,'Create Area Plan') as t:
    t.Start()
    view = ViewPlan.Create(doc, area_plan_type.Id, active_level.Id)
    t.Commit()

__author__ = 'πŸ™‹β€β™‚οΈ Erik Frits'
Create StructuralView
# -*- coding: utf-8 -*-
#⬇ IMPORTS
from Autodesk.Revit.DB import *

#πŸ“¦ VARIABLES
doc          = __revit__.ActiveUIDocument.Document
active_view  = doc.ActiveView
active_level = doc.ActiveView.GenLevel

#🎴 ALL VIEW TYPES
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()

#πŸ”Ž FILTER VIEW TYPES
view_types_structural = [vt for vt in view_types if vt.ViewFamily == ViewFamily.StructuralPlan]
struc_plan_type  = view_types_structural[0]


#🎯 Create Structural Plan
with Transaction(doc,'Create Structural Plan') as t:
    t.Start()
    view = ViewPlan.Create(doc, struc_plan_type.Id, active_level.Id)
    t.Commit()

__author__ = 'πŸ™‹β€β™‚οΈ Erik Frits'

2️⃣ How to Create ViewSection?

Sections are a little harder to make because we need to work with BoundingBox

πŸ‘‡ Here is the method to create a Section.

ViewSection.Create(doc, 
                   viewFamilyTypeId, 
                   sectionBox)

We have everything except for secitonBox. It might be tricky to create one, but it makes sense. Because when we create sections, we have a line where it cuts, and then it's view range is extruded, so it creates a sectionBox shape.

# -*- coding: utf-8 -*-
#⬇ IMPORTS
from Autodesk.Revit.DB import *

#πŸ“¦ VARIABLES
doc          = __revit__.ActiveUIDocument.Document
active_view  = doc.ActiveView
active_level = doc.ActiveView.GenLevel

#🎴 ALL VIEW TYPES
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()

#πŸ”Ž FILTER VIEW TYPES
view_types_sections = [vt for vt in view_types if vt.ViewFamily == ViewFamily.Section]
view_type_section   = view_types_sections[0]

#🟦 BOUNDING BOX
origin   = XYZ(5,5,0)
pt_start = XYZ(0,0,0)
pt_end   = XYZ(10,10,0)
vector   = pt_end - pt_start

offset = 5
W = 5
H = 5
D = 5

# SECTION - DIRECTION
fc = -1
if pt_start.X > pt_end.X or (pt_start.X == pt_end.X and pt_start.Y < pt_end.Y):
    fc = 1

# SECTION - ORIENTATION
curvedir = fc * vector.Normalize()

t        = Transform.Identity
t.Origin = origin
t.BasisX = curvedir
t.BasisY = XYZ.BasisZ
t.BasisZ = curvedir.CrossProduct(XYZ.BasisZ)

# SECTION - CROPBOX
sectionBox           = BoundingBoxXYZ()
sectionBox.Transform = t                            #apply orientation
sectionBox.Min       = XYZ(W*-0.5 -offset, 0 -offset, D*-0.5 -offset)
sectionBox.Max       = XYZ(W* 0.5 +offset, H +offset, D* 0.5 +offset)


#🎯 Create Structural Plan
with Transaction(doc,'Create Structural Plan') as t:
    t.Start()
    view_section = ViewSection.CreateSection(doc, view_type_section.Id, sectionBox)
    t.Commit()

__author__ = 'πŸ™‹β€β™‚οΈ Erik Frits'

3️⃣How to Create View3D

Creating 3D Views with Revit API is straight-forward. We just need to decide between Isometric or Perspective.
πŸ‘‡ Here are their methods.

View3D.CreateIsometric(doc, 
                       viewFamilyTypeId)
View3D.CreatePerspective(doc, 
                         viewFamilyTypeId)
Create Isometric 3D View
# -*- coding: utf-8 -*-
#⬇ IMPORTS
from Autodesk.Revit.DB import *

#πŸ“¦ VARIABLES
doc          = __revit__.ActiveUIDocument.Document
active_view  = doc.ActiveView
active_level = doc.ActiveView.GenLevel

#🎴 ALL VIEW TYPES
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()

#πŸ”Ž FILTER VIEW TYPES
view_types_3D = [vt for vt in view_types if vt.ViewFamily == ViewFamily.ThreeDimensional]
view_type_3D  = view_types_3D[0]

#🎯 Create 3D - Isometric
with Transaction(doc,'Create 3D Isometric') as t:
    t.Start()
    view3D = View3D.CreateIsometric(doc, view_type_3D.Id) 
    t.Commit()

__author__ = 'πŸ™‹β€β™‚οΈ Erik Frits'
Create Isometric 3D View
# -*- coding: utf-8 -*-
#⬇ IMPORTS
from Autodesk.Revit.DB import *

#πŸ“¦ VARIABLES
doc          = __revit__.ActiveUIDocument.Document
active_view  = doc.ActiveView
active_level = doc.ActiveView.GenLevel

#🎴 ALL VIEW TYPES
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()

#πŸ”Ž FILTER VIEW TYPES
view_types_3D = [vt for vt in view_types if vt.ViewFamily == ViewFamily.ThreeDimensional]
view_type_3D  = view_types_3D[0]

#🎯 Create 3D - Perspective
with Transaction(doc,'Create 3D Perspective') as t:
    t.Start()
    view3D = View3D.CreatePerspective(doc, view_type_3D.Id) 
    t.Commit()

__author__ = 'πŸ™‹β€β™‚οΈ Erik Frits'

4️⃣ ViewDrafting

ViewDrafting is probably the easiest one to create

πŸ‘‡Here is the snippet

ViewDrafting.Create(doc, 
                    viewFamilyTypeId)

So you already know how to creat it.

# -*- coding: utf-8 -*-
#⬇ IMPORTS
from Autodesk.Revit.DB import *

#πŸ“¦ VARIABLES
doc          = __revit__.ActiveUIDocument.Document
active_view  = doc.ActiveView
active_level = doc.ActiveView.GenLevel

#🎴 ALL VIEW TYPES
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()

#πŸ”Ž FILTER VIEW TYPES
view_types_drafting = [vt for vt in view_types if vt.ViewFamily == ViewFamily.Drafting]
view_type_drafting  = view_types_drafting[0]

#🎯 Create Drafting View
with Transaction(doc,'Create Drafting') as t:
    t.Start()

    view =  ViewDrafting.Create(doc, view_type_drafting.Id)
    t.Commit()

__author__ = 'πŸ™‹β€β™‚οΈ Erik Frits'

5️⃣ Create View Sheet

πŸ‘‡Creating ViewSheet is a little different because we don't need to provide viewType but we need to get TitleBlockId instead.

ViewSheet.Create(doc, 
                 title_block.Id)

P.S. If you want to create a ViewSheet without title block you probably need to provide ElementId(-1) . I haven't tried it, but knowing Revit API this should work.

# -*- coding: utf-8 -*-
#⬇ IMPORTS
from Autodesk.Revit.DB import *

#πŸ“¦ VARIABLES
doc          = __revit__.ActiveUIDocument.Document
active_view  = doc.ActiveView
active_level = doc.ActiveView.GenLevel

#🎴 ALL TITLE BLOCKS
all_title_blocks = FilteredElementCollector(doc)\
.OfCategory(BuiltInCategory.OST_TitleBlocks)\
.WhereElementIsElementType().ToElements()

if not all_title_blocks:
    import sys
    print('There are no TitleBlocks in the Project! \nPlease Try Again')
    sys.exit()

selected_title_block = all_title_blocks[0]


#🎯 Create Drafting View
with Transaction(doc,'Create Drafting') as t:
    t.Start()

    new_sheet = ViewSheet.Create(doc, selected_title_block.Id)
    t.Commit()

__author__ = 'πŸ™‹β€β™‚οΈ Erik Frits'

6️⃣ Create Legend View

If you're trying to create a legend in Revit, the bad news is that there are no methods in Revit API for creating them.
The good news is that we can just duplicate any existing legend without detailing and we will have a new legend.

So for your tools where you want to create legends you need to verify that at least 1 Legend exists, so we can duplicate it! Otherwise, stop execution of your script and alert user that project needs to have at least 1 Legend to execute it.

πŸ‘‡I got all Legend views and checked if it's empty, then there are no Legends in the project and I need to alert user.

# -*- coding: utf-8 -*-
#⬇ IMPORTS
from Autodesk.Revit.DB import *
from pyrevit.forms import alert

#πŸ“¦ VARIABLES
doc         = __revit__.ActiveUIDocument.Document
all_views   = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).ToElements()
all_legends = [view for view in all_views if view.ViewType == ViewType.Legend]

#βœ… Check Legend in the Project
if not all_legends:
    alert("There has to be at least 1 Legend View in the project! "
          "Please create one and try again.", exitscript=True)

Once you made sure that you have a legend in the project, take a random one and just duplicate without Detailing.
And there you have it, a new empty Legend View.

#πŸ‘‰ GET RANDOM LEGEND
random_legend = all_legends[0]

#πŸ†• CREATE NEW LEGEND VIEW
new_legend_view_id    = random_legend.Duplicate(ViewDuplicateOption.Duplicate)
new_legend_view       = doc.GetElement(new_legend_view_id)

#πŸ”Ž Change Scale
new_legend_view.Scale = 100

Join Newsletter

πŸ“© You will be added to Revit API Newsletter

Join Us!

which is already read by 7400+ people!

Ready to become Revit Hero for your office? Learn Revit API!

Join this comprehensive course that will guide you step by step on how to create your dream tools for Revit that save time.