Must run twice to see the added family parameter

This code add a family parameter to the selected titleblock family, but I must run it twice to see the new parameter.

One the first run of the script a success message is displayed claiming that the parameter was added. But when I do Edit Family and look at the parameters in the Family Types window, it is not there.

from Autodesk.Revit.DB import *
from pyrevit import script
from Autodesk.Revit.UI import TaskDialog

# Get the current Revit document
doc = __revit__.ActiveUIDocument.Document

# Get the currently selected elements
selection = __revit__.ActiveUIDocument.Selection.GetElementIds()

if not selection:
    TaskDialog.Show("Error", "No elements selected.")
    script.exit()

# Loop through selected elements and ensure it's a title block
for element_id in selection:
    element = doc.GetElement(element_id)
    
    # Check if the element is a Title Block (usually it has the Title Blocks category)
    if element.Category and element.Category.Name == "Title Blocks":
        titleblock = element
        
        # Ensure the element is a FamilyInstance
        if isinstance(titleblock, FamilyInstance):
            # Get the FamilySymbol (the type of the family instance)
            family_symbol = titleblock.Symbol
            
            # Access the Family object from the FamilySymbol
            family = family_symbol.Family
            
            if not family:
                TaskDialog.Show("Error", "Unable to access the family object.")
                script.exit()
            
            # Open the family for editing
            family_doc = doc.EditFamily(family)
            
            if family_doc:
                # Start a transaction to modify the family
                with Transaction(family_doc, "Add KeyplanAreaToggle Parameter") as t:
                    t.Start()
                    
                    # Check for shared parameter file
                    shared_params_file = doc.Application.SharedParametersFilename
                    
                    if shared_params_file:
                        shared_params = doc.Application.OpenSharedParameterFile()
                        
                        if not shared_params:
                            TaskDialog.Show("Error", "Unable to open the shared parameters file.")
                            script.exit()
                        
                        # Search for the shared parameter "KeyplanAreaToggle"
                        param_def = None
                        for group in shared_params.Groups:
                            for definition in group.Definitions:
                                if definition.Name == "KeyplanAreaToggle":
                                    param_def = definition
                                    break
                            if param_def:
                                break
                        
                        if param_def:
                            # Add the parameter to the family
                            family_manager = family_doc.FamilyManager
                            family_manager.AddParameter(param_def, BuiltInParameterGroup.PG_DATA, False)
                        else:
                            TaskDialog.Show("Error", "Shared parameter 'KeyplanAreaToggle' not found.")
                            script.exit()
                    else:
                        TaskDialog.Show("Error", "No shared parameter file is set.")
                        script.exit()
                    
                    t.Commit()
                
                # Save the family
                try:
                    family_doc.SaveAs("C:\\Temp\\ModifiedFamily.rfa")  # Save as a temporary file
                except Exception as ex:
                    TaskDialog.Show("Error", "Failed to save the family: {}".format(ex))
                    script.exit()
                
                # Reload the family
                try:
                    with Transaction(doc, "Reload Family") as reload_txn:
                        reload_txn.Start()
                        new_family_doc_path = "C:\\Temp\\ModifiedFamily.rfa"
                        family_loaded = doc.LoadFamily(new_family_doc_path)
                        reload_txn.Commit()
                    
                    if not family_loaded:
                        TaskDialog.Show("Error", "The family could not be reloaded into the project.")
                        script.exit()
                except Exception as ex:
                    TaskDialog.Show("Error", "Failed to reload the family: {}".format(ex))
                    script.exit()
                
                # Close the temporary family document
                family_doc.Close(False)
                
                TaskDialog.Show("Success", "Parameter 'KeyplanAreaToggle' added successfully to the titleblock family.")
            else:
                TaskDialog.Show("Error", "Unable to open the family document for editing.")
                script.exit()
        else:
            TaskDialog.Show("Error", "The selected element is not a FamilyInstance (Titleblock).")
            script.exit()
    else:
        TaskDialog.Show("Error", "The selected element is not a titleblock.")
        script.exit()

How would I get the script to make the new parameter visible in the family types window after running it the first time?

Haven’t sifted through everything but try a Document.Regenerate() at the end. It might be completing correctly, but you’re just not seeing the results.

1 Like

Thank you @aaronrumple

It tried it and got
IronPython Traceback:
… line 114, in
Exception: Modification of the document is forbidden. Typically, this is because there is no open transaction; consult documentation for Document.IsModified for other possible causes.

when I put Regenerate() at the end of the script. Same result when I put it within the transaction.

Revit API says Regenerate() is run when a transaction.commit executes.

The original script did not include binding the parameter. I have addressed that and believe I have resolved this issue.

Thanks, everyone!

1 Like