Python

Using Python Configuration Manager

Q How do I determine whether a configuration variable is defined?

Q How do I get the value of a configuration variable?

Q How do I get the paths defined by a multi-path variable?

A Here's a small Python program that uses the ConfigurationManager class to answer those questions.

Python Implementation

ConfigurationManager provides methods to test whether a configuration variable is defined and to get the value of that variable.

get_configuration_variable_value

However, there's a problem caused by the way that method ConfigurationManager.GetVariable() is defined. It copies the variable's value into a Bentley.WString class. That class is not Pythonic because it's a non-iterable string, and we need an iterable string for subsequent operations. I wrote a function to wrap ConfigurationManager.GetVariable() and return a Python string …

def get_configuration_variable_value (
	cfg_var_name: str,
	var_level: ConfigurationVariableLevel = ConfigurationVariableLevel.eUser)->(bool, str):
    ''' Get a configuration variable value as a Python string.

    Wrapper around ConfigurationManager.GetVariable() which get a non-Pythonic WString.
    Returns: tuple(bool, str)
    '''
    status: BentleyStatus = 0
    # GetVariable obtains a Bentley WString, which is not Pythonic: it is not iterable.
    # Convert it immediately to a Python string.
    if ConfigurationManager.IsVariableDefined(cfg_var_name):
        temp_value = WString()
        status = ConfigurationManager.GetVariable(temp_value, cfg_var_name, var_level)
        variable_value = str(temp_value)
        return (status == BentleyStatus.eSUCCESS, variable_value)
    else:
        msg = f"Configuration variable '{cfg_var_name}' is not defined"
        MessageCenter.ShowErrorMessage(msg, msg, False)
    return (False, "")

Evaluate a Configuration Variable

evaluate_configuration_variable() extracts the value of a configuration variable. It returns a list of Python strings …

def evaluate_configuration_variable (cfg_var_name: str)->List[str]:
    ''' Attempt to get the value of a MicroStation configuration variable.

    Test whether the named variable exists, if successful get its value.
    Returns: a list of string values.
    If the variable is a multi-path then the list contains a member for each path;
    if the variable points to a single path then the list contains a single member.
    If the list is empty then the variable name is invalid or the variable doesn't exist.'''
    succeeded = False
    variable_value = str()
    (succeeded, variable_value) = get_configuration_variable_value(cfg_var_name)
    msg = str()
    variable_values = list()
    if succeeded:
        msg = f"'{cfg_var_name}' = '{variable_value}'"
        MessageCenter.ShowInfoMessage(msg, msg, False)
        variable_values = variable_value.split(PATH_SEPARATOR)
    else:
        msg = f"Unable to get value of configuration variable '{cfg_var_name}'"
        MessageCenter.ShowErrorMessage(msg, msg, False)
    return variable_values

Trace the Paths in a Multi-Path Variable

Some configuration variables have contents that contain several folder paths. Examples include MS_CELL and MS_RFDIR. Folders are separated by a semi-colon character (;). To see if a string is multi-path, we search the string for that separator and then split the string using the same character …

PATH_SEPARATOR = ";"
def is_multi_path (path_variable_value: str)->bool:
    ''' Determine whether a variable value contains a list of paths.
    e.g. path1;path2; ...
    The MicroStation path separator in a configuration variable value is the semi-colon. '''
    return PATH_SEPARATOR in path_variable_value
def trace_multi_paths(var_name: str, path_variable_value: str):
    ''' Send the contents of a multi-path configuration variable value to the message center.'''
    msg = str()
    if is_multi_path(path_variable_value):
        for count, p in enumerate(path_variable_value.split(PATH_SEPARATOR)):
            msg = f"'{var_name}' path [{count}] '{p}'"
            MessageCenter.ShowDebugMessage(msg, msg, False)
    else:
        msg = "'{var_name}' is not multi path"
        MessageCenter.ShowDebugMessage(msg, msg, False)
Download la_solutions_configuration_manager.zip

Unpack the ZIP file and copy the Python files into a folder that MicroStation knows about.

Python Manager

Use MicroStation's Python Manager to find and execute the script.

Questions

Post questions about MicroStation Python programming to the MicroStation Programming Forum.