Error In Python Script

Hello Everyone,

I am creating python script to apply view template.
Everything working well but at line 78 there is error (I have upload error image for your reference).
Could you please help to solve this error?

Thank you.

import clr

import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')

import System
from System.Collections.Generic import *

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Transactions import TransactionManager 

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk 
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

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

def tolist(obj1):
	if hasattr(obj1,"__iter__"): return obj1
	else: return [obj1]

#Preparing input from dynamo to revit
element = UnwrapElement(IN[0])

#Do some action in a Transaction

TransactionManager.Instance.EnsureInTransaction(doc)

# Define the overall all plan view template name
template_name_11 = 'B-150_OVERALL IBMS PLANS_300'
template_name_12 = 'E-150_OVERALL POWER PLANS_300'
template_name_13 = 'E-250_OVERALL CONTAINMENT & EARTHING PLANS_300'
template_name_14 = 'E-350_OVERALL LIGHTING PLANS_300'
template_name_15 = 'E-450_OVERALL LIGHTNING PROTECTION PLANS_300'
template_name_16 = 'T-150_OVERALL FIRE DETECTION & ALARM PLANS_300'
template_name_17 = 'T-250_OVERALL STRUCTURED CABLING PLANS_300'
template_name_18 = 'T-350_OVERALL MASS NOTIFICATION PLANS_300'
template_name_19 = 'T-450_OVERALL ROOM MANAGEMENT SYSTEM PLANS_300'


# Collect all floor plan views in the document
collector = FilteredElementCollector(doc)
floor_plan_views = collector.OfCategory(BuiltInCategory.OST_Views).OfClass(ViewPlan).ToElements()


# Loop through the floor plan views which contains 'OVERALL' & 'IBMS'
for view in floor_plan_views:
    if 'OVERALL' in view.Name:
     if 'IBMS' in view.Name:
        # Check if the view template exists
        view_templates = FilteredElementCollector(doc).OfClass(View).ToElements()
        template = None
        
              
        for vt in view_templates:
            if vt.Name == template_name_11:
                template = vt
                break


        if template:
            # Apply the view template to the current view
            view.ViewTemplateId= template.Id


TransactionManager.Instance.TransactionTaskDone()

OUT = view.ViewTemplateId

Add an output to confirm your list of views and templates. I’m guessing you have an item in there somewhere that isn’t what you think it is - likely a view type or incompatible view template.

EDIT: I also can’t run your code as the indenting is off at the ‘IBMS’ check. The code still runs but that whole loop is being skipped. Are you expecting to update multiple views or just one? Because right now you’re only returning the final view.

Here’s a bit of cleaned up code that might get you started:

# Collect all floor plan views in the document
collector = FilteredElementCollector(doc)
floor_plan_views = collector.OfCategory(BuiltInCategory.OST_Views).OfClass(ViewPlan).ToElements()

view_templates = FilteredElementCollector(doc).OfClass(View).ToElements()
out = []
# Loop through the floor plan views which contains 'OVERALL' & 'IBMS'
for view in floor_plan_views:
    template = None
    if 'OVERALL' in view.Name and 'IBMS' in view.Name:
        
        for vt in view_templates:
            if vt.Name == template_name_11:
                template = vt
                break   

        if template:
            # Apply the view template to the current view
            view.ViewTemplateId= template.Id
            
        out.append([view,template,view.ViewTemplateId])

TransactionManager.Instance.TransactionTaskDone()

OUT = out

Right now I’m returning the view, the template, and the Id just so you have an idea of which views you’re dealing with.

Hello @Nick_Boyts,

Thank you for your quick response.

I am expecting to apply view template to multiple views.
Currently old view template is applied for those views & now trying to apply new view template.
I am at learning stage and trying to create python code.

Appreciate for your help.

I have updated python code as below but giving error at line 75 (refer attached snip)

import clr

import sys
sys.path.append('C:\Program Files (x86)\IronPython 2.7\Lib')

import System
from System.Collections.Generic import *

clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
clr.ImportExtensions(Revit.GeometryConversion)
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager 
from RevitServices.Transactions import TransactionManager 

clr.AddReference("RevitAPI")
clr.AddReference("RevitAPIUI")

import Autodesk 
from Autodesk.Revit.DB import *
from Autodesk.Revit.UI import *

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

def tolist(obj1):
	if hasattr(obj1,"__iter__"): return obj1
	else: return [obj1]

#Preparing input from dynamo to revit
element = UnwrapElement(IN[0])

#Do some action in a Transaction

TransactionManager.Instance.EnsureInTransaction(doc)


# Define the overall all plan view template name
template_name_11 = 'B-150_OVERALL IBMS PLANS_300'
template_name_12 = 'E-150_OVERALL POWER PLANS_300'
template_name_13 = 'E-250_OVERALL CONTAINMENT & EARTHING PLANS_300'
template_name_14 = 'E-350_OVERALL LIGHTING PLANS_300'
template_name_15 = 'E-450_OVERALL LIGHTNING PROTECTION PLANS_300'
template_name_16 = 'T-150_OVERALL FIRE DETECTION & ALARM PLANS_300'
template_name_17 = 'T-250_OVERALL STRUCTURED CABLING PLANS_300'
template_name_18 = 'T-350_OVERALL MASS NOTIFICATION PLANS_300'
template_name_19 = 'T-450_OVERALL ROOM MANAGEMENT SYSTEM PLANS_300'


# Collect all floor plan views in the document
collector = FilteredElementCollector(doc)
floor_plan_views = collector.OfCategory(BuiltInCategory.OST_Views).OfClass(ViewPlan).ToElements()

view_templates = FilteredElementCollector(doc).OfClass(View).ToElements()
out = []
# Loop through the floor plan views which contains 'OVERALL' & 'IBMS'
for view in floor_plan_views:
    template = None
    if 'OVERALL' in view.Name and 'IBMS' in view.Name:
        
        for vt in view_templates:
            if vt.Name == template_name_11:
                template = vt
                break   

        if template:
            # Apply the view template to the current view
            view.ViewTemplateId = template.Id
            
        out.append([view,template,view.ViewTemplateId])

TransactionManager.Instance.TransactionTaskDone()

OUT = out

Hi @nilesh.katkarNS59L

try this:

# Try to start the code
try:
    #import the common language runtime (CLR) so we can access .net libraries in the Python environment
    import clr
     #import System
    import System
    #import the .net List class
    from System.Collections.Generic import List
    #add the Revit API to the CLR
    clr.AddReference('RevitAPI')
    #importing the autodesk namepace to the python environment
    import Autodesk
    #importing all of the Revit DB namespace as DB, to the python environment so we can be sure we catch what we need in development
    from Autodesk.Revit.DB import *
    import Autodesk.Revit.DB as DB
    #import the Revit services namespace to the Python environment
    clr.AddReference('RevitServices')
    import RevitServices
    #import the document manager class to the Python environment
    from RevitServices.Persistence import DocumentManager
    #import the transaction manager class to the Python environment
    from RevitServices.Transactions import TransactionManager
    #the current document
    doc = DocumentManager.Instance.CurrentDBDocument
    
    # Define the overall all plan view template name
    template_name_11 = 'B-150_OVERALL IBMS PLANS_300'
    template_name_12 = 'E-150_OVERALL POWER PLANS_300'
    template_name_13 = 'E-250_OVERALL CONTAINMENT & EARTHING PLANS_300'
    template_name_14 = 'E-350_OVERALL LIGHTING PLANS_300'
    template_name_15 = 'E-450_OVERALL LIGHTNING PROTECTION PLANS_300'
    template_name_16 = 'T-150_OVERALL FIRE DETECTION & ALARM PLANS_300'
    template_name_17 = 'T-250_OVERALL STRUCTURED CABLING PLANS_300'
    template_name_18 = 'T-350_OVERALL MASS NOTIFICATION PLANS_300'
    template_name_19 = 'T-450_OVERALL ROOM MANAGEMENT SYSTEM PLANS_300'
    
    # Create Filter to get FloorPlan Views with conditions 
    filterV= System.Predicate[System.Object](lambda x : x.ViewType == DB.ViewType.FloorPlan\
                                                    and not x.IsTemplate\
                                                    and "OVERALL" in x.Name and 'IBMS' in x.Name)
    # Create Filter to get FloorPlan View Template with conditions 
    filterT= System.Predicate[System.Object](lambda x : x.ViewType == DB.ViewType.FloorPlan\
                                                    and x.IsTemplate\
                                                    and x.Name == template_name_11)
    # Collect all FloorPlan views in the document from filterV                                               
    filtered_Views = List[DB.Element](FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).OfClass(ViewPlan).ToElements()).FindAll(filterV)
    # Collect ViewTemplate in the Document from fiterT
    filtered_Template = List[DB.Element](FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Views).OfClass(ViewPlan).ToElements()).FindAll(filterT)[0]
    
    # Start transaction
    TransactionManager.Instance.EnsureInTransaction(doc)
    for v in filtered_Views:
        v.ViewTemplateId= filtered_Template.Id
        out = "Success"
    # End transaction
    TransactionManager.Instance.TransactionTaskDone()
# Catch errors if occur
except Exception as ex:
    out = repr(ex)
OUT = out

I tested for 2 views…
image

2 Likes

Hello @Kulkul,

This script working as expected.
Appreciate for your help.

Thank you.

2 Likes