Python

Explanation of BeFileName

BeFileName represents a file name. It includes the device name, folder, file title and file extension. It provides functions to manipulate and examine the filename and to work with files on disk by name. See DgnFile for a class that works with open files. This implementation handles file names up to MAX_PATH-1 in length (MAX_PATH = 260).

To get a string representing a file or folder from BeFileName use the Pythonic (but undocumented) __str__ magic method …

msg = f"How to extract a string from BeFileName '{beFileName}'"

Alternatively, use the GetWCharCP method. For an explanation, see below.

Python Path and Pathlib

Python provides modules os.path and pathlib …

You will find plenty of documentation and examples for those on the web.

Python Implementation of BeFileName

from MSPyBentley import BeFileName, WString

BeFileName stores a file path in four pieces …

You can construct a BeFileName from a file path …

beFileName = BeFileName(str(dgnFile.GetFileName()))

Split a full file name into its components: device, path, name and extension …

beFileName.ParseName(dev, fullDir, name, ext)

Build a full file name from its components: device, path, name and extension …

beFileName.BuildName(dev, fullDir, newName, ext)

Because of the BeFileName class predeliction for MSWChar characters, you may need to force conversion by casting like this …

beFileName.BuildName(str(dev), str(fullDir), str(newName), str(ext))

Test whether a file exists. BeFileName.DoesPathExist() is a class method …

if BeFileName.DoesPathExist(beFileName.GetWCharCP()):
    msg = f"File exists"
    MessageCenter.ShowInfoMessage(msg, beFileName.GetWCharCP(), False)

Test whether a string represents a directory. BeFileName.IsDirectory(validPath) is a class method …

if BeFileName.IsDirectory(validPath):
    msg = f"Path is a directory"
    MessageCenter.ShowInfoMessage(msg, validPath, False)

Notes on the Python Implementation

Often, the class uses Bentley's WString to pass file data. You may need to convert to a Python string by casting using str().

Usage

BeFileName test is intended to be run from MicroStation's Python Manager.

Download BeFileName_test ZIP

Unpack the ZIP file and copy the Python file 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 programming to the MicroStation Programming Forum.

Extracting a String

BeFileName is a Python class, and implements magic method __str__. Consequently, you can write something like this …

msg = f"BeFileName implements __str__ '{beFileName}'"

Another way to extract a string from a BeFileName instance, is to use the BeFileName.GetWCharCP() method …

msg = f"How to extract a string from BeFileName '{beFileName.GetWCharCP()}'"

What's that about? MicroStation Python is mostly a wrapper around the C++ MicroStationAPI. C++ doesn't have magic methods, so to get a string from an object you must use a method. Strings in C++ are objects that derive from the C++ standard library std::string, and contain arrays of char (8-bit) or WChar (wide-char or 16-bit) characters. To liberate the string from its container you must call an extractor such as string::c_str(). Bentley chose to create its own method name to get a wide-character string GetWCharCP(), and that's arrived in MicroStation Python unchanged.