Automated Data Transformation and Assignment in Dynamo: Extending and Formatting Parameter Values

Hello Experts,
I’m trying to develop a Dynamo script using Python that retrieves the “Mark” parameter for all visible components in the current 3D view, extends each value by appending “1111”, changes the format to “12345678-1234-1234-1234-123456789012”, and then assigns the modified value to a parameter called “Comments”


But it’s not working and I’m getting this error message in the first comment

The code in the first comment

Error meesage
image

Code

Import necessary Dynamo and Revit libraries

import clr
clr.AddReference(‘RevitAPI’)
from Autodesk.Revit.DB import FilteredElementCollector, BuiltInCategory, Transaction

Get the active document

doc = DocumentManager.Instance.CurrentDBDocument

Get the active 3D view

activeView = doc.ActiveView

Get all visible components in the 3D view

allVisibleComponents = FilteredElementCollector(doc, activeView.Id)
.OfCategory(BuiltInCategory.OST_GenericModel)
.WhereElementIsNotElementType()
.ToElements()

Start a transaction for modifying elements

transaction = Transaction(doc, “Extend Mark, Format, and Set Comments”)
transaction.Start()

try:

Loop through each component and update the “Comments” parameter

for component in allVisibleComponents:

Get the existing “Mark” parameter value

markParameter = component.get_Parameter(BuiltInParameter.ALL_MODEL_MARK)

Check if the “Mark” parameter has a value

if markParameter:
markValue = markParameter.AsString()

Extend the “Mark” value by appending “1111”

extendedMark = markValue + “1111”

Change the format of the extended value

formattedValue = “{}-{}-{}-{}-{}”.format(
extendedMark[:8], extendedMark[8:12], extendedMark[12:16],
extendedMark[16:20], extendedMark[20:]
)

Set the “Comments” parameter with the formatted value

commentsParameter = component.get_Parameter(BuiltInParameter.ALL_MODEL_COMMENTS)
if commentsParameter:
commentsParameter.Set(formattedValue)

Commit the transaction

transaction

Hi Sam, welcome :wave:
Your code has no indents (tab/4 spaces)
Use </> in the menu to format code and you could use a IDE like VSCode which can assist
Cleaned code

# Import necessary Dynamo and Revit libraries
import clr

clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import (
    FilteredElementCollector,
    BuiltInParameter,
    BuiltInCategory,
    Transaction,
)

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager

# Get the active document
doc = DocumentManager.Instance.CurrentDBDocument

# Get the active 3D view
activeView = doc.ActiveView

# Get all visible components in the 3D view
allVisibleComponents = (
    FilteredElementCollector(doc, activeView.Id)
    .OfCategory(BuiltInCategory.OST_GenericModel)
    .WhereElementIsNotElementType()
    .ToElements()
)

# Start a transaction for modifying elements
with Transaction(doc, "Extend Mark, Format, and Set Comments") as t:
    t.Start()

    try:
        # Loop through each component and update the "Comments" parameter
        for component in allVisibleComponents:
            # Get the existing "Mark" parameter value
            markParameter = component.get_Parameter(BuiltInParameter.ALL_MODEL_MARK)

            # Check if the "Mark" parameter has a value
            if markParameter:
                markValue = markParameter.AsString()

                # Extend the "Mark" value by appending "1111"
                extendedMark = markValue + "1111"

                # Change the format of the extended value
                formattedValue = "{}-{}-{}-{}-{}".format(
                    extendedMark[:8],
                    extendedMark[8:12],
                    extendedMark[12:16],
                    extendedMark[16:20],
                    extendedMark[20:],
                )

                # Set the "Comments" parameter with the formatted value
                commentsParameter = component.get_Parameter(
                    BuiltInParameter.ALL_MODEL_COMMENTS
                )
                if commentsParameter:
                    commentsParameter.Set(formattedValue)
    except:
        pass
    # Commit the transaction
    t.Commit()

OUT = "What is your output?"
1 Like

Hi Mike,
Thanks!
Output is assigning the modified value to a parameter called “Comments” for the same 3d component