Problem: It exports the same gbXML file but with different names

Hello I developed this code,

I need to export many gbxml files wach with different window configuration
the code is exporting files, but it is the same file with different naming, I think the problem is that it exports the last energy model created, so I need include a script that delete the energy model then create it again inside the loop

Please help

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

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]

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()
else:
    raise ValueError("One or both of the specified parameters not found.")

Main script

def main():
# Loop through each window and create variations
for window in windows:
for width in width_list:
for height in height_list:
# Modify the dimensions of the window
modify_window_dimensions(window, width, height)

            # Export the variation to gbXML format
            filename = "window_variation_{}_{}.gbxml".format(width, height)
            path = IN[3]
            GBopt=GBXMLExportOptions()
            GBopt.ExportEnergyModelType=ExportEnergyModelType.BuildingElement
            doc.Export(path, filename, GBopt)

Run the main script

main()

Formatting on your Python code has been lost. Set up the formatted text and paste your code into that so that, or share your .dyn directly. Would also help to have a sample Revit file to run the code on, as otherwise the initial setup of that could take as long as the code authoring itself.

Sure, I uploaded the files on this link; Folder2023

  • 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

  • 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]

  • 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()
    
  • else:
    
  •     raise ValueError("One or both of the specified parameters not found.")
    
  • Main script

  • def main():

  • # Loop through each window and create variations
    
  • for window in windows:
    
  •     for width in width_list:
    
  •         for height in height_list:
    
  •             # Modify the dimensions of the window
    
  •             modify_window_dimensions(window, width, height)
    
  •             # Export the variation to gbXML format
    
  •             filename = "window_variation_{}_{}.gbxml".format(width, height)
    
  •             path = IN[3]
    
  •             GBopt=GBXMLExportOptions()
    
  •             GBopt.ExportEnergyModelType=ExportEnergyModelType.BuildingElement
    
  •             doc.Export(path, filename, GBopt)
    
  • Run the main script

  • main()

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

windows = UnwrapElement(IN[0])

width_list = IN[1]
height_list = IN[2]

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)
    
    width_parameter.Set(width/304.8)
    height_parameter.Set(height/304.8)
    
    TransactionManager.Instance.TransactionTaskDone()
else:
    raise ValueError("One or both of the specified parameters not found.")

def main():
for window in windows:
for width in width_list:
for height in height_list:
# Modify the dimensions of the window
modify_window_dimensions(window, width, height)

            filename = "window_variation_{}_{}.gbxml".format(width, height)
            path = IN[3]
            GBopt=GBXMLExportOptions()
            GBopt.ExportEnergyModelType=ExportEnergyModelType.BuildingElement
            doc.Export(path, filename, GBopt)

main()
Blockquote

Hello @rr79664

you can find information about formatting code here

Try to add
doc.Regenerate() at the end of the modify_window_dimensions function

1 Like

Thank you for your reply, I tried to include it as you advised, but still the same problem, I think I should delete the energy model then create it again, but I am not sure how
Here is the code


# 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

# 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]

# 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.")

# Main script
def main():
    # Loop through each window and create variations
    for window in windows:
        for width in width_list:
            for height in height_list:
                # Modify the dimensions of the window
                modify_window_dimensions(window, width, height)
                
                # Export the variation to gbXML format
                filename = "window_variation_{}_{}.gbxml".format(width, height)
                path = IN[3]
                GBopt=GBXMLExportOptions()
                GBopt.ExportEnergyModelType=ExportEnergyModelType.BuildingElement
                doc.Export(path, filename, GBopt)
                

# Run the main script
main()
1 Like

Hi @rr79664

The main issue is that the Energy Model is not dynamically updated with changes in the Revit model. You will have to create (and dispose) a new energy model for each variation

You may be able or match the window element to the Energy Model EnergyAnalysisOpening class and change the width and height however I do not know how this will affect the associated EnergyAnalysisSurface which may also require it’s geometry to be adjusted to the new opening

Jeremy Tammik has just provided a blog entry at The Building Coder on this very subject The Building Coder: Export, gbXML and Python Tips

2 Likes

Thank you for your reply, the problem changing the windows dimension only will not help, as I need the full geometry to be updated for the energy calculations

An update;
I tried to develop the model to delete the energy model then create it again.
It worked, but again it is exporting the same file I mean it exports the last variation many times.
I think I need to include the export in the loop before deleting every time, but I do not know how it is inside the loop anyhow

# 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]

path = IN[3]

# 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.")

# Main script
def main():
    # Loop through each window and create variations
    for window in windows:
        for width in width_list:
            for height in height_list:
                TransactionManager.Instance.EnsureInTransaction(doc)
                # Modify the dimensions of the window
                modify_window_dimensions(window, width, height)
                
                # Export the variation to gbXML format
                filename = "window_variation_{}_{}.gbxml".format(width, height)

                GBopt = GBXMLExportOptions()
                eadm = Autodesk.Revit.DB.Analysis.EnergyAnalysisDetailModel.GetMainEnergyAnalysisDetailModel(doc)
                if eadm != 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)
                GBopt.ExportEnergyModelType = ExportEnergyModelType.BuildingElement
                doc.Export(path, filename, GBopt)
                TransactionManager.Instance.TransactionTaskDone()
                OUT = [path + filename + ".xml"]

# Run the main script
main()

Think you’ll need a few transactions here.

  1. Update the windows
  2. Delete the old model.
  3. Generate a new model.
  4. Run the export.

That will have to happen per window size.

Also, your loop doesn’t appear to be lacing all window sizes, if that is the goal. Situation: Two windows, call them A and B, with 2 sizes (1, 2, and 3 units), with initial size of 1 unit.

Loop 1:

  • Window A: 1
  • Window B: 1
    Loop 2:
  • Window A: 2
  • Window B: 1
    Loop 3:
  • Window A: 3
  • Window B: 1
    Loop 4:
  • Window A: 3
  • Window B: 1
    Loop5:
  • Window A: 3
  • Window B: 2
    Loop 6:
  • Window A: 3
  • Window B: 3

Notice the lack of Window A and B both being 2? Not sure if this is your intent, and obviously this is going to get even more complex as you add height into the equation. Really you need all permutations of the lists, which is a VERY expensive operation to even calculate the value sets.

I would move any code that only needs to execute once to outside of the loop(s) such as the energy model and export options.
You are overwriting each height/width combination for every window and the OUT variable
Loading the energy model to check if it exists and then overwriting is very inefficient and based on the discussion in the Jeremy Tammik’s blog there does not appear to be a need to delete the model, just generate a new one with each option - there seems to be a limit around 175
Some suggestions:
Add this line to your imports
from Autodesk.Revit.DB.Analysis import *

Indicative refactor of main function (not tested)

# Main script
def main():
    # Energy options
    energyOptions = EnergyAnalysisDetailModelOptions()
    energyOptions.EnergyModelType = EnergyModelType.BuildingElement

    # Energy export options
    GBopt = GBXMLExportOptions()
    GBopt.ExportEnergyModelType = ExportEnergyModelType.BuildingElement

    # Loop through each window and create variations
    output = []
    for width in width_list:
        for height in height_list:
            TransactionManager.Instance.EnsureInTransaction(doc)
            for window in windows:
                # Modify the dimensions of the window
                modify_window_dimensions(window, width, height)
                
            # Export the variation to gbXML format
            filename = f"window_variation_{width}_{height}.xml"
            output.append(path + filename)

            eadm = EnergyAnalysisDetailModel.Create(doc, energyOptions)
            eadm.TransformModel()
            TransactionManager.Instance.TransactionTaskDone()
            
            doc.Export(path, filename, GBopt)

    return output
# Run the main script
OUT = main()
1 Like

Thank you very much, the following code works for me;

# 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]

path = IN[3]

# 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.")

# 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)
                
            # 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 = "Exoprted Successfully"
1 Like