Hi everyone,
I created a function in Python to open a shared parameter file, read it, search for strings that represent Revit parameter names (user input) and then return their corresponding GUIDS and names through a dictionary.
The syntax used for opening the shared parameter file in Python 3:
with open(originalSharedParameterFile, "r", encoding="utf-16") as fileToRead:
While using Dynamo I couldn’t reproduce the same behavior. According to IronPython documentation 2. Built-in Functions — IronPython 2.7.2b1 documentation there is no third argument encoding
so I couldn’t use the same strategy. Somehow the find()
method was returning something completely different.
I did find a workaround to make it work, but I don’t want to use it because everything has to be done manually:
- Opening the original shared parameter file;
- Copy its content;
- Create a new .txt file;
- Save it;
- Point the function to the new file.
I also know that there are Dynamo packages outputting results that I am looking for, but I would like to understand what can be done to make my code work. I am assuming that this is only a problem with the shared parameter encoding.
def find_parameterGuid(parameterToCheck, sharedParameterFile):
"""Check the Guid corresponding to the parameter name. """ \
"""If one Guid is not found, it returns a string """ \
"""describing it."""
messageToUser = []
parameterNames = []
parameterGuids = []
with open(sharedParameterFile, "r") as fileToRead:
lines = fileToRead.readlines()
for line in lines:
# Find the position of the last TAB after the parameter name
# It should start at index 43 since it is the position of the
# First TAB IMMEDIATELY before the parameter name
startingIndexOfGuid = 6
startingIndexOfTab = 43
for p in parameterToCheck:
stopingIndexOfTab = line.find("\t", startingIndexOfTab)
startingIndexOfParameter = line.find(p, startingIndexOfTab, stopingIndexOfTab)
# Here we evaluate the result as different of -1. The -1 represents
# That no index was found, thus no parameter found. By consequence if
# Is NOT -1, than the parameter was found
if startingIndexOfParameter != -1:
guid = line[startingIndexOfGuid: startingIndexOfTab -1]
parameterName = line[startingIndexOfTab: stopingIndexOfTab]
# The a similar principle is here applied. If there is NO
# White space in the checked parameter then return a similar result
# Otherwise return parameters that have a white space in their strings
if p.find(" ") == -1 and parameterName.find(" ") == -1:
parameterNames.append(parameterName)
parameterGuids.append(guid)
elif p.find(" ") != -1 and parameterName.find(" ") != -1:
parameterNames.append(parameterName)
parameterGuids.append(guid)
# Creating the dictionary that is the base for all the operations
# First create a iterator combining the two lists
zip_iterator = zip(parameterNames, parameterGuids)
# Then use dict to convert the iterator to a dictionary
sharedPar_dictionary = dict(zip_iterator)
# Also set a message to the user if the parameters they have chosen
# Are NOT in shared parameter files. This can be obtained
# If I check the result of this IF statement against
# The user input
for p in parameterToCheck:
if p not in parameterNames:
messageToUser.append(p + " NOT in the selected shared parameters"\
"text file. Please check this before continuing."\
" Script not ran.")
# What this definition returns
if len(messageToUser) == 0:
return sharedPar_dictionary
else:
return messageToUser
Thank you in advance