Revit Dynamo Python Return ViewFamilyType R22

Figured it out and didn’t want to lose it. If the ViewFamilyType exists it returns it - else it creates it and returns that for Revit.Dynamo.Python R22. There is a BUG in the ViewFamilyType.Name that will not allow reading or setting the .Name property- so there is a work around in there.

Overarching goal is to remove as many unnecessary parameters from Revit as possible and use Native Revit Elements to organize and filter content.

This is the basis for converting view templates to view types, organizing the project browser and organized view filters based on ViewFamilyType names.

# Load the Python Standard and DesignScript Libraries
import sys
import clr
##https://forum.dynamobim.com/t/collecting-all-elements-of-family-types-in-active-view/19838/2
from Autodesk.Revit.DB import FilteredElementCollector
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
#  Drafting Views
from Autodesk.Revit.DB import FilteredElementCollector, BuiltInCategory, BuiltInParameter
from Autodesk.Revit.DB import ViewFamilyType, ViewDrafting, Element
from Autodesk.Revit.DB import ViewFamily
from Autodesk.Revit.DB import Transaction

doc = DocumentManager.Instance.CurrentDBDocument

########################################################
def GetNameByID(oID):                                   ##Get Broken Element.Name as work around GLOBAL ByID.Name
    return doc.GetElement(oID).get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_NAME).AsString()

def GetViewTypeByName(clsViewFamilyType, ViewTypeName=""): ##Returns Default "Drafting View" ViewType or name as specified or creates ViewType by DraftViewTypeName
    ##Trans_GDVBTN = Transaction (doc, 'Drafting View Type')    ##MUST Be in traqnsaction to Create/Name!
    ViewTypes = [ x for x in FilteredElementCollector(doc).OfClass(ViewFamilyType) if x.ViewFamily == clsViewFamilyType ]
  
    found=False                                         ##Found as false
    if not ViewTypeName == "":                          ##If view name is not null
        for ViewType in ViewTypes:                      ##Looking for Viewtype name amongst ViewFamily'ies
            if GetNameByID(ViewType.Id) == ViewTypeName:    ##Get name by ID as FamilyViewType.Name BROKEN - 2022-09-15 check furture versions!
                found=True
                break
    
    if not found:
        ViewType = None                                 ##Clear remnants from loop if not (found)
    
    if ViewType == None:                                ##If no DraftingViewType found
        ViewTypes = ViewTypes[0]                        ##Set to base type
        ViewType = ViewTypes.Duplicate(ViewTypeName)     ##(strName) as ViewFamilyType
        
    ##Trans_GDVBTN.Commit()                             ##Commit transaction - Cannot parallel or sub trans
    
    return ViewType                                     ##Return Viewtype
   
##############
t = Transaction (doc, 'Create Drafting View Type')
t.Start()
#######################ViiewFamily, Name
DVT=GetViewTypeByName(ViewFamily.Drafting,"Test6") ## E.G. Get drafting view
OUT=DVT
t.Commit()

Seems like you should be able to simplify this some.

def GetViewTypeByName(clsViewFamilyType, ViewTypeName=""): ##Returns Default "Drafting View" ViewType or name as specified or creates ViewType by DraftViewTypeName
    ##Trans_GDVBTN = Transaction (doc, 'Drafting View Type')    ##MUST Be in traqnsaction to Create/Name!
    ViewTypes = [ x for x in FilteredElementCollector(doc).OfClass(ViewFamilyType) if x.ViewFamily == clsViewFamilyType ]

    if not ViewTypeName == "":                          ##If view name is not null
        for ViewType in ViewTypes:                      ##Looking for Viewtype name amongst ViewFamily'ies
            if GetNameByID(ViewType.Id) == ViewTypeName:    ##Get name by ID as FamilyViewType.Name BROKEN - 2022-09-15 check furture versions!
                return ViewType  ##just return here
    #if it didn't return then it didn't find it so we know we need to duplicate
    ViewTypes = ViewTypes[0]                        ##Set to base type
    ViewType = ViewTypes.Duplicate(ViewTypeName)     ##(strName) as ViewFamilyType
        
    return ViewType
1 Like

I wasn’t sure if the for loop would leave the last on populated with the end of a list if it didn’t find a match, or is the last element of a compare in a list a NULL?