Populating shared/project parameters in conduit run schedules

Take a string, and cut it into multiple strings where ever there is a comma. (In theory your input is a list of parameter names, like “Comments, Sector, Elevation… etc.” So this gets you a list with each parameter name.
params = params.split(',')

This retrieves your current Revit selection

sel = uidoc.Selection.GetElementIds()
sel = [doc.GetElement(x) for x in sel]

Using list comprehension, look up each parameter, and make a list their storage types.
parent_types = [sel[0].LookupParameter(x).StorageType for x in params]

Like I said when I posted this, I threw the code together pretty quick. So I’m pretty sure this line is a remnant from something else I was trying, didn’t end up using, and forgot to remove.
run_list = []

Create a transaction so that we can make changes to the model
TransactionManager.Instance.EnsureInTransaction(doc)

Start our for loop, and create a list to hold parameter values, and get the element ID of the run associated with the conduit element.

for elem in sel:
    param_vals = []
    run = doc.GetElement(elem.RunId)

Start a new for loop, going through our input parameter list.
The enumerate method basically lets us keep track of what index we are at in the list.
for count, param in enumerate(params):
Use that index (count) to check what storage type that parameter is. We need to do this so we can call the right .As...() method.

        if parent_types[count] == StorageType.Double:
            param_vals.append(elem.LookupParameter(param).AsDouble())
        elif parent_types[count] == StorageType.Integer:
            param_vals.append(elem.LookupParameter(param).AsInteger())
        elif parent_types[count] == StorageType.String:
            param_vals.append(elem.LookupParameter(param).AsString())

In retrospect this part isn’t really “Pythonic”. Technically you shouldn’t use list comrehension unless you’re actually making a list.
At any rate, we use the zip function to pair up the parameters, with the values we retrieved, and then apply those parameters to the conduit run.
[run.LookupParameter(p).Set(v) for (p,v) in zip(params,param_vals)]

This last line just closes and commits the transaction.
I’m not sure why I used ForceCloseTransaction here. It’s not a problem, necessarily. But if you started a new transaction after this one, it would make two separate undo items.
TransactionManager.Instance.ForceCloseTransaction()

I’d probably recommend using TransactionManager.Instance.TransactionTaskDone() instead.


In regards to FilteredElementCollector, it can definitely take a couple uses to wrap your head around it. It all comes with time, practice, and a lot of Google. Haha

Copy and pasting from a post I’ve made elsewhere, here are some useful links:

If you haven’t already seen this, I’d read through the Dynamo Python Primer.
Take Dynamo Further 🚀 - Dynamo Python Primer

The primer will touch on this as well, but you’re also going to want to get familiar with navigating the website I’ve been linking to.
https://www.revitapidocs.com/

Finally, Jeremy Tammik’s blog is invaluable. There are a great many explanations and examples for just about everything in the API.
That said, everything here is written in C# so far as I’m aware, so you’ll have to get used to reading that and converting it to Python, but it’s doable with some practice.
https://thebuildingcoder.typepad.com/blog/about-the-author.html#2

In fact, the python primer has a page on using FilteredElementCollector.