Wall Type change using Python

I am trying to develop my code to change the window sizes and wall types.
How ever it is a loop so that I have lists for window dimensions and wall types, at the end I will get exported gbXML files for all the options regarding the window modifications and the export to gbXML the code is working, but I have problem in changing the wall types and to access the wall type class

# Import necessary modules
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

import os

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

# Get the window elements from Dynamo
windows = UnwrapElement(IN[0])

# Get the lists of width and height values from Dynamo
width_list = IN[1]
height_list = IN[2]

# Get the list of walls from Dynamo
walls = UnwrapElement(IN[3])

# Get the list of wall type names from Dynamo
wall_type_names = IN[4]

path = IN[5]

# Function to modify window dimensions
def modify_window_dimensions(window, width, height):
    width_parameter = window.LookupParameter('Width')
    height_parameter = window.LookupParameter('Height')
    
    if width_parameter and height_parameter:
        # Start a transaction to modify the model
        TransactionManager.Instance.EnsureInTransaction(doc)
        
        # Set the width and height parameters of the window
        width_parameter.Set(width/304.8)
        height_parameter.Set(height/304.8)
        
        # Commit the changes to the model
        TransactionManager.Instance.TransactionTaskDone()
        doc.Regenerate()
    else:
        raise ValueError("One or both of the specified parameters not found.")

# Function to change wall type
def change_wall_type(wall, wall_type_name):
    # Get all wall types in the document
    collector = FilteredElementCollector(doc)
    wall_types = collector.OfClass(WallType).ToElements()
    
    # Find the wall type with the specified name
    wall_type = None
    for wt in wall_types:
        if wt.Name == wall_type_name:
            wall_type = wt
            break
    
    if not wall_type:
        raise ValueError("Wall type with the specified name not found.")
    
    # Start a transaction to modify the model
    TransactionManager.Instance.EnsureInTransaction(doc)
    
    # Change the wall type of the wall
    wall.WallType = wall_type
    
    # Commit the changes to the model
    TransactionManager.Instance.TransactionTaskDone()
    doc.Regenerate()

# Main script
def main():
    # Loop through each window and create variations
    for width in width_list:
        for height in height_list:
            # Modify the dimensions of all windows for this variation
            for window in windows:
                modify_window_dimensions(window, width, height)
                
            # Change the wall type for all walls for this variation
            for i, wall in enumerate(walls):
                change_wall_type(wall, wall_type_names[i])
                
            # Export the variation to gbXML format
            filename = "DO_Window_{}_{}.gbxml".format(width, height)

            # Delete any existing Energy Analysis Detail Model
            eadm = Autodesk.Revit.DB.Analysis.EnergyAnalysisDetailModel.GetMainEnergyAnalysisDetailModel(doc)
            if eadm is not None:
                doc.Delete(eadm.Id)

            # Create a new Energy Analysis Detail Model
            energyOptions = Autodesk.Revit.DB.Analysis.EnergyAnalysisDetailModelOptions()
            energyOptions.EnergyModelType = Autodesk.Revit.DB.Analysis.EnergyModelType.BuildingElement
            eadm = Autodesk.Revit.DB.Analysis.EnergyAnalysisDetailModel.Create(doc, energyOptions)

            # Export the gbXML file
            GBopt = GBXMLExportOptions()
            GBopt.ExportEnergyModelType = ExportEnergyModelType.BuildingElement
            doc.Export(path, filename, GBopt)
            doc.Regenerate()

# Run the main script
main()
OUT = "Exported Successfully"

Can someone advise.

Which Revit/Dynamo version?

Instead of wt.Name, use wt.get_Name()

if wt.get_Name() == wall_type_name:

1 Like

Revit 2023

I tried, now it gives this error

What’s the reasoning for having a for loop under a for loop under a for loop for windows, widths, heights? That’s going to update each window with every permutation of the provided values but only the last one will show in the model. Is this to have one GBXML of each variation?

Assuming one gbxml per variation, you should only need one transaction per variation as well (maybe two, with one before the export). It would also be easier to define your transaction within main() rather than within the other functions. (Sidenote: you don’t actually need a main function in a python node. Everything inside the node will execute.)

For now, it looks like your code isn’t finding any matching wall types. It’s probably worth writing a little side node to get all wall type names and check for matches. Make sure that portion of code is doing what you expect it to do before moving forward.

1 Like

get_Name needs parantheses
get_Name()

# Import necessary modules
import clr
clr.AddReference('RevitAPI')
clr.AddReference('RevitAPIUI')
import Autodesk
from Autodesk.Revit.DB import *

clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

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

# Get the window elements from Dynamo
windows = UnwrapElement(IN[0])

# Get the lists of width and height values from Dynamo
width_list = IN[1]
height_list = IN[2]

# Get the list of walls from Dynamo
walls = UnwrapElement(IN[3])

# Get the list of wall type names from Dynamo
wall_type_names = IN[4]

path = IN[5]

# Function to modify window dimensions
def modify_window_dimensions(window, width, height):
    width_parameter = window.LookupParameter('Width')
    height_parameter = window.LookupParameter('Height')
    
    if width_parameter and height_parameter:
        # Start a transaction to modify the model
        TransactionManager.Instance.EnsureInTransaction(doc)
        
        # Set the width and height parameters of the window
        width_parameter.Set(width/304.8)
        height_parameter.Set(height/304.8)
        
        # Commit the changes to the model
        TransactionManager.Instance.TransactionTaskDone()
        doc.Regenerate()

# Function to change wall type
def change_wall_type(wall, wall_type_name):
    # Get all wall types in the document
    collector = FilteredElementCollector(doc)
    wall_types = collector.OfClass(WallType).ToElements()
    
    # Find the wall type with the specified name
    wall_type = None
    for wt in wall_types:
        if wt.get_Name() == wall_type_name:
            wall_type = wt
            break
            
    # Start a transaction to modify the model
    TransactionManager.Instance.EnsureInTransaction(doc)
    
    # Change the wall type of the wall
    wall.WallType = wall_type
    
    # Commit the changes to the model
    TransactionManager.Instance.TransactionTaskDone()
    doc.Regenerate()

# Main script
def main():
   
    for width in width_list:
        for height in height_list:
            for wall_type_name in wall_type_names:
            # Modify the dimensions of all windows for this variation
                for window in windows:
                    modify_window_dimensions(window, width, height)
            
           
                    for i,wall in enumerate(walls):
                        change_wall_type(wall, wall_type_name)
       
                        filename = "DO_{}_{}_{}".format(width, height, wall_type_name)

                    eadm = Autodesk.Revit.DB.Analysis.EnergyAnalysisDetailModel.GetMainEnergyAnalysisDetailModel(doc)
                    if eadm is not None:
                        doc.Delete(eadm.Id)

                    energyOptions = Autodesk.Revit.DB.Analysis.EnergyAnalysisDetailModelOptions()
                    energyOptions.EnergyModelType = Autodesk.Revit.DB.Analysis.EnergyModelType.BuildingElement
                    eadm = Autodesk.Revit.DB.Analysis.EnergyAnalysisDetailModel.Create(doc, energyOptions)   
            # Export the gbXML file
                    GBopt = GBXMLExportOptions()
                    GBopt.ExportEnergyModelType = ExportEnergyModelType.BuildingElement
                    doc.Export(path, filename, GBopt)
                    doc.Regenerate()

# Run the main script
main()
OUT = "Exported Successfully"

This code now is working, but it exports different files with the same wall type, the window dimensions are changing, but for the walls it exports files with different naming, but with the same wall type for all files.

1 Like

Thank you for raising this.
Do you have any idea how to change type of other families (for ex- Windows and Doors)?