Python Autodesk.Revit.DB.Viewplan.Create

import clr 
clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes") # Import RevitNodes
import Revit
from Revit.Elements import * # Import Revit elements
clr.AddReference("RevitServices") # Import DocumentManager
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference('RevitAPI') #import clr
from Autodesk.Revit.DB import * # Import ToDSType(bool) extension method
clr.ImportExtensions(Revit.Elements)
import System # Import System
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')

prjViewFamilyTypes = IN[0] # ALL THE FAMILY TYPES IN THE PROJECT
xlsxViewFamilyTypeNames = IN[1] # CUSTOM VIEW TYPES THAT WERE CREATED FOR THE RPOJECT
xlsxSheetNumberDiscipline = IN[2] # THE DISCIPLINE CODE INTHE SHEET NAMES
xlsxSheetName = IN[3] # ALL THE SHEET NAMES THAT MUST BE CREATED FOR EACH LEVEL
xlsxLevels = IN[4] # ALL THE LEVELS THAT WERE CREATED FOR THIS PROJECT
Titleblock = IN[5] # THE TITLEBLOCK FAMILY TYPE THAT NEEDS TO BE USED TO CREATE THE SHEET
prjLevels = IN[6] # ALL THE LEVELS IN THE PROJECT
SheetList = [] # CONTATINER FOR THE SHEETS THAT ARE CREATED

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application


TransactionManager.Instance.EnsureInTransaction(doc) # you need an active transaction as you will create elements
ViewPlan newViewPlan 
newViewPlan = Create(doc, prjViewFamilyTypes[6].Id, prjLevels[0].Id)
newViewPlan.Name = "Level Test"
TransactionManager.Instance.TransactionTaskDone()

OUT = SheetList
>

I am trying to create new floor plans from existing ViewPlanFamilyTypes and Levels I have already created. I am trying to declare a new Autodesk.Revit.DB.ViewPlan so I can use the .Create method: http://www.revitapidocs.com/2017.1/aa8f2c2e-dfd9-fda5-8d61-31580cbce408.htm

Is anyone able to shed light on why I am receiving the following Error Message:
exception

Try removing the instantiation of newViewPlan. Python should understand the variable’s type one initialized by the Create method.

[...]

TransactionManager.Instance.EnsureInTransaction(doc) # you need an active transaction as you will create elements
#ViewPlan newViewPlan <- This shouldn't be necessary.
newViewPlan = Create(doc, prjViewFamilyTypes[6].Id, prjLevels[0].Id)
newViewPlan.Name = "Level Test"
TransactionManager.Instance.TransactionTaskDone()

[...]
1 Like

I can’t say what you should write, but this line is where your issue is, in the Python syntax.

Thank you alvpickmans, that works.

Quick Question. Can you shed some light on how to get the unique element id from prjViewFamilyTypes[6] as it expects ElementID but returns UnknownElement

So this works:
TransactionManager.Instance.EnsureInTransaction(doc) # you need an active transaction as you will create elements
ViewFamilyTypeId = ElementId(prjViewFamilyTypes[6].Id)
LevelId = ElementId(prjLevels[0].Id)
newViewPlan = ViewPlan.Create(doc, ViewFamilyTypeId, LevelId)
newViewPlan.Name = “Temp1234”
TransactionManager.Instance.TransactionTaskDone()

It pretty much depends on your inputs. It seems that you know that the type of plan that you want to create is at index 6.

After quickly checking online I found this great post:

Following the code I created a python definition where by inputing the name of the ViewType to create, it returns its ElemetId. If not matching name, returns -1.

import System

doc = DocumentManager.Instance.CurrentDBDocument


def ViewFamilyIdByName(document, name):
	type = System.Enum.Parse(ViewFamily, name)
	viewFamilyTypes = FilteredElementCollector(document).OfClass(ViewFamilyType).ToElements()
	id = -1
	for viewType in viewFamilyTypes:
		if viewType.ViewFamily == type:
			id = viewType.Id
			break
	return id
 
viewId = ViewFamilyIdByName(doc, "FloorPlan")

With this you could replace your first input IN[0] by the name of the view type you want to create.
Hope it helps!

Hi alvpickmans,
Thanks, that is great! I have been watching Jeremy Tammic for a while now and he seems to be quite a legend. In his post he also used the native Revit Floor Plan ViewFamilyType, while I want to use my own custom ones and the API does not want to allow me this. I am wondering whether there is a parallel Server Stored Procedure in the background and that the API is not compatible with it? Is there a live database running in the background or does the database get created on export only? Here is the link to the new post I created.

https://forum.dynamobim.com/t/python-autodesk-revit-db-viewplan-create-custom-viewfamilytype-error/22218

Ok, I understand what you are after now. I modified the previous definition to look into the view type (symbol) name and compare it to the input.

import clr
 
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
 
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager

import System

doc = DocumentManager.Instance.CurrentDBDocument


def ViewFamilyTypeIdByName(document, name):

	viewFamilyTypes = FilteredElementCollector(document).OfClass(ViewFamilyType).ToElements()
	id = -1
	for viewType in viewFamilyTypes:
		typeName = viewType.get_Parameter(BuiltInParameter.SYMBOL_NAME_PARAM).AsString()
		if typeName == name:
			id = viewType.Id
			break
	return id
	
OUT = ViewFamilyTypeIdByName(doc, IN[0])
3 Likes

Thank you alvpickmans,

I will try this.

:grinning: WORKED A CHARM! THANK YOU VERY, VERY MUCH!

import clr 
clr.AddReference("ProtoGeometry")
from Autodesk.DesignScript.Geometry import *
clr.AddReference("RevitNodes") # Import RevitNodes
import Revit
from Revit.Elements import * # Import Revit elements
clr.AddReference("RevitServices") # Import DocumentManager
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference('RevitAPI') #import clr
from Autodesk.Revit.DB import * # Import ToDSType(bool) extension method
clr.ImportExtensions(Revit.Elements)
import System # Import System
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')

prjViewFamilyTypes = IN[0] # ALL THE FAMILY TYPES IN THE PROJECT
xlsxViewFamilyTypeNames = IN[1] # CUSTOM VIEW TYPES THAT WERE CREATED FOR THE RPOJECT
xlsxSheetNumberDiscipline = IN[2] # THE DISCIPLINE CODE IN THE SHEET NAMES
xlsxSheetName = IN[3] # ALL THE SHEET NAMES THAT MUST BE CREATED FOR EACH LEVEL
xlsxLevels = IN[4] # ALL THE LEVELS THAT WERE CREATED FOR THIS PROJECT
Titleblock = IN[5] # THE TITLEBLOCK FAMILY TYPE THAT NEEDS TO BE USED TO CREATE THE SHEET
prjLevels = IN[6] # ALL THE LEVELS IN THE PROJECT
SheetList = [] # CONTATINER FOR THE SHEETS THAT ARE CREATED

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
Discipline =[]

name = "PD - DRAINAGE - BELOW"

viewFamilyTypes = FilteredElementCollector(doc).OfClass(ViewFamilyType).ToElements()
id = -1
for viewType in viewFamilyTypes:
	typeName = viewType.get_Parameter(BuiltInParameter.SYMBOL_NAME_PARAM).AsString()
	if typeName == name:
		id = viewType.Id
		break

TransactionManager.Instance.EnsureInTransaction(doc) # you need an active transaction as you will create elements
ViewFamilyTypeId = ElementId(prjViewFamilyTypes[0].Id)
# ViewFamilyTypeId = ElementId(prjViewFamilyTypes[10].Id)       THIS DOES NOT WORK!
LevelId = ElementId(prjLevels[0].Id)
newViewPlan = ViewPlan.Create(doc, id, LevelId)
#newViewPlan = ViewPlan.Create(doc, ViewFamilyTypeId, LevelId)
newViewPlan.Name = "2"
TransactionManager.Instance.TransactionTaskDone()

OUT = Discipline

Glad that worked!

I would recommend to have the name variable as an input so the python script is more flexible (rather than opening to change the variable for each type of view).
That applies as a general rule, everything that might change, is an input :slight_smile:

Either way, please remember to mark the post as solved

1 Like