Combine strings via python

Hello Dynos,

my code is a bit off…

so dynamo works well, i just want to combine 3 Parameters to one!


BUT in python i got not the right way… zip funktion makes a list instat of concat

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms)
rooms = collector.WhereElementIsNotElementType().ToElements()

#Bauteil
param1 = []

for i in rooms:
	for j in i.Parameters:
		p = (i.LookupParameter("Bauteil").AsString())
		param1.append(p)
		
#Geschoss
param2 = []

for i in rooms:
	for j in i.Parameters:
		p = (i.LookupParameter("Geschoss").AsString())
		param2.append(p)

#Top
param3 = []

for i in rooms:
	for j in i.Parameters:
		p = (i.LookupParameter("Geschoss").AsString())
		param3.append(p)

#combine 
output = []

for i in zip(param1,param2,param3):
	output.append(i)

OUT = output

how to make my code working and more smooth (comprehensive)

KR

Andreas

Hello, I don’t know if you are looking for a more complete way
occasionally

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Walls)
walls = collector.WhereElementIsNotElementType().ToElements()

param_c=[]
for i in walls:
	p = str(i.LookupParameter("Longueur").AsDouble())
	q = str(i.LookupParameter("Surface").AsDouble())
	r = str(i.LookupParameter("Volume").AsDouble())
	param_c.append('test '+ p +" "+ q +" "+ r)
OUT = param_c

cordially
christian.stan

3 Likes

Perhaps edit the for loop to:

output.append(i[0]+i[1]+i[2])

3 Likes

Hey,

To quote the GOAT…" LookupParameter will only retrieve the first parameter of the given name. In order to use it, you must be absolutely certain that only one parameter with the given name exists."

Some other thoughts… Please bear in mind I’m an amateur and I’m sure real coders will point out my failings :slight_smile:

Hope that is useful,

Mark

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *

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

doc = DocumentManager.Instance.CurrentDBDocument

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms)
rooms = collector.WhereElementIsNotElementType().ToElements()

# i would always do lots of commenting to help future you :)

# it is also good for saving useful bits of code
## params = element.GetOrderedParameters()

# i find this structure quite good, there is a full style guide
# called PEP8if you are interested :)

# declaring variables
separator = "_"

BIP_roomName = BuiltInParameter.ROOM_NAME
BIP_roomNo = BuiltInParameter.ROOM_NUMBER
BIP_roomHeight = BuiltInParameter.ROOM_HEIGHT

BIP_List = [BIP_roomName, BIP_roomNo, BIP_roomHeight]


# a def can be reused over and over, reducing duplication
# if you need to you can also use clases
def paramValues(room):
    # AsValueString will get the value whatever the output type
    vals_List = [(room.get_Parameter(BIP).AsValueString()) for BIP in BIP_List]
    # there are loads of useful python methods such as join
    return separator.join(vals_List)

# list comprehension reduces the space taken
# i like to keep variables human readable, avoiding 'i in walls' etc.
OUT = [(paramValues(room)) for room in rooms]


image

4 Likes

Hello!

Can i just add to the above that it is always worth considering weather you want this data to be retrievable later and amend your initial code with some delimeters. If you do you should be placing deliminators into your joined string for later deciphering.

My example shown below is not great (But it worked for what i needed) but it shows the intended data retreival at a later date.

# Input
input_list = IN[0]  # The modified format list

# Initialize lists for list1 and list2
list1 = []
list2 = []

# Iterate over the items in the input list
for item in input_list:
    # Split the item by " | " to separate the groups
    groups = item.split(" | ")
    
    # Initialize lists for the current item's list1 and list2
    item_list1 = []
    item_list2 = []
    
    # Iterate over the groups
    for group in groups:
        # Split each group by " ~ " to get the items
        items = group.split(" ~ ")
        
        # Append the first item to the current item's list1 and the second item to list2
        item_list1.append(items[0])
        item_list2.append(items[1])
    
    # Append the current item's list1 and list2 to the overall lists
    list1.append(item_list1)
    list2.append(item_list2)

# Output
OUT = (list1, list2)  # Tuple containing the original list1 and list2
2 Likes

@christian.stan ,

for me intresting is to declare AsString also as str … than it works …

collector = FilteredElementCollector(doc).OfCategory(BuiltInCategory.OST_Rooms)
rooms = collector.WhereElementIsNotElementType().ToElements()


param_c=[]

for i in rooms:
	p = str(i.LookupParameter("Bauteil").AsString())
	q = str(i.LookupParameter("Geschoss").AsString())
	r = str(i.LookupParameter("Top").AsString())
	a = "M03" + p + q + r
	param_c.append(a)
	
OUT = param_c

to get rid of “None” or replace it via f.e. “xxx” will be the next task …

KR

Andreas

1 Like

hello
look method String.replace() Python String replace() Method
cordially
christian.stan