β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.
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
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
from Autodesk.Revit.DB import *
doc = __revit__.ActiveUIDocument.Document
active_view = doc.ActiveView
active_level = doc.ActiveView.GenLevel
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
view_types_plans = [vt for vt in view_types if vt.ViewFamily == ViewFamily.FloorPlan]
floor_plan_type = view_types_plans[0]
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
from Autodesk.Revit.DB import *
doc = __revit__.ActiveUIDocument.Document
active_view = doc.ActiveView
active_level = doc.ActiveView.GenLevel
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
view_types_ceil_views = [vt for vt in view_types if vt.ViewFamily == ViewFamily.CeilingPlan]
ceil_plan_type = view_types_ceil_views [0]
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
from Autodesk.Revit.DB import *
doc = __revit__.ActiveUIDocument.Document
active_view = doc.ActiveView
active_level = doc.ActiveView.GenLevel
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
view_types_area = [vt for vt in view_types if vt.ViewFamily == ViewFamily.AreaPlan]
area_plan_type = view_types_area[0]
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
from Autodesk.Revit.DB import *
doc = __revit__.ActiveUIDocument.Document
active_view = doc.ActiveView
active_level = doc.ActiveView.GenLevel
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
view_types_structural = [vt for vt in view_types if vt.ViewFamily == ViewFamily.StructuralPlan]
struc_plan_type = view_types_structural[0]
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.
from Autodesk.Revit.DB import *
doc = __revit__.ActiveUIDocument.Document
active_view = doc.ActiveView
active_level = doc.ActiveView.GenLevel
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
view_types_sections = [vt for vt in view_types if vt.ViewFamily == ViewFamily.Section]
view_type_section = view_types_sections[0]
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
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
curvedir = fc * vector.Normalize()
t = Transform.Identity
t.Origin = origin
t.BasisX = curvedir
t.BasisY = XYZ.BasisZ
t.BasisZ = curvedir.CrossProduct(XYZ.BasisZ)
sectionBox = BoundingBoxXYZ()
sectionBox.Transform = t
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)
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
from Autodesk.Revit.DB import *
doc = __revit__.ActiveUIDocument.Document
active_view = doc.ActiveView
active_level = doc.ActiveView.GenLevel
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
view_types_3D = [vt for vt in view_types if vt.ViewFamily == ViewFamily.ThreeDimensional]
view_type_3D = view_types_3D[0]
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
from Autodesk.Revit.DB import *
doc = __revit__.ActiveUIDocument.Document
active_view = doc.ActiveView
active_level = doc.ActiveView.GenLevel
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
view_types_3D = [vt for vt in view_types if vt.ViewFamily == ViewFamily.ThreeDimensional]
view_type_3D = view_types_3D[0]
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.
from Autodesk.Revit.DB import *
doc = __revit__.ActiveUIDocument.Document
active_view = doc.ActiveView
active_level = doc.ActiveView.GenLevel
view_types = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
view_types_drafting = [vt for vt in view_types if vt.ViewFamily == ViewFamily.Drafting]
view_type_drafting = view_types_drafting[0]
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.
from Autodesk.Revit.DB import *
doc = __revit__.ActiveUIDocument.Document
active_view = doc.ActiveView
active_level = doc.ActiveView.GenLevel
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]
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.
from Autodesk.Revit.DB import *
from pyrevit.forms import alert
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]
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.
random_legend = all_legends[0]
new_legend_view_id = random_legend.Duplicate(ViewDuplicateOption.Duplicate)
new_legend_view = doc.GetElement(new_legend_view_id)
new_legend_view.Scale = 100