Q How do I get a configuration variable value using MicroStation Python?
A Here's a small Python program that gets the value of MS_REPORT_OUTPUT
.
While developing another example I needed a folder to write an Excel file.
Wanting to demonstrate MicroStation's configuration capability, I decided to use a
configuration variable
to determine the folder.
MicroStation configuration variable MS_REPORT_OUTPUT
looked a good candidate.
MicrdoStation help describes it:
Default output directory where report results will be exported..
MS_REPORT_OUTPUT
points to a folder.
However, it's not defined by default, so the code should be prepared to assign another folder.
def GetReportFolder ()->(bool, str):
'''
Get a folder where a report file may be written.
If configuration variable MS_REPORT_OUTPUT is defined, it gets that path.
Otherwise, it returns a folder in the operating system TEMP directory.
Returns: tuple (valid, path)
'''
path = WString()
status = ConfigurationManager.GetVariable(path, MS_REPORT_OUTPUT)
if BentleyStatus.eSUCCESS == status:
return (True, str(path))
status = ConfigurationManager.GetLocalTempDirectory (path,"")
if BentleyStatus.eSUCCESS == status:
return (True, str(path))
return (False, "")
The function GetReportFolder
takes no arguments.
Internally it tries to get the value of MS_REPORT_OUTPUT
.
If that value is empty, then the code gets the current temporary directory.
If you're unfamiliar with Python then the returned object may cause you to
raise an eyebrow.
However, you can relax your eyebrow because the return object is a
tuple
that contains two values:
the Boolean flag valid
and the string value path
.
We unpack the tuple using syntax (valid, path)
.
(valid, path) = GetReportFolder()
if valid:
# use path
Post questions about MicroStation programming to the MicroStation Programming Forum.