Python

Get Active Cell Library Name

Before looking for a cell library, we must know its name. If we want to see the active cell library, then look in the active DGN file using Cell.GetLibraryName(). That function puts the file name in a BeFileName object that you supply.

If there is no active cell library, then the name is empty. If a cell library is attached, we can get its name …

from MSPyBentley import *
from MSPyDgnPlatform import *
from MSPyMstnPlatform import *

lib_name = BeFileName()
Cell.GetLibraryName(lib_name)
if lib_name.IsEmpty():
    print("Cell Library name is empty")
else:
    # Use the file name
    pass

Get a Reference to the Active Cell Library

We're going to call Cell.GetLibraryObject(). Unfortunately, its documentation is incomplete: it tells us that it returns a tuple but without telling us the contents of that tuple. I believe that the returned value is (error_code, library_object_reference) …

(err_code, lib_reference) = Cell.GetLibraryObject(lib_name, False)

The second bool argument is redundant and can be ignored.

If err_code is zero (eSUCCESS) then the call succeeded and lib_reference is a valid reference to a DGN cell library file object.

I've packed the above logic into two functions …

If you're interested in enumerating the cells in a library, then here's how.

Download la_solutions_cell_library.zip

The code is available in a single Python source file. 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.