Python

Distinguish between Activate and Open a DGN File

From a user perspective, opening and activating a new DGN file appear the same. For programmers, those are distinct operations …

Open and Activate a New DGN File

ISessionMgr

This example uses two methods provided by ISessionMgr …

Example Python Code

from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyDgnPlatform import *
from MSPyMstnPlatform import *
def  activate_dgn_file(file_path: str, open_mode: DgnFileOpenMode = DgnFileOpenMode.ePreferablyReadWrite)->bool:
    '''
    Opening and activating a DGN file requires two steps...
    1  Use ISessionMgr.FindDesignFile to find a file using a specified path
    2  Switch to the new file using ISessionMgr.SwitchToNewFile

    '''
    _session_mgr = ISessionMgr.GetManager()
    _dgn_model_name = None
    _ALLOW_CANCEL = True
    dgn_file, status = _session_mgr.FindDesignFile(file_path,
                                                  _dgn_model_name,
                                                   GraphicsFileType.eGRAPHICSFILE_CAD,
                                                  _ALLOW_CANCEL)

    _UPDATE_ALL = True
    _RELEASE_REFERENCES = False
    status = _session_mgr.SwitchToNewFile         (dgn_file.GetDocument(),
                                                  _dgn_model_name,
                                                   GraphicsFileType.eGRAPHICSFILE_UStn,
                                                  _UPDATE_ALL,
                                                  _RELEASE_REFERENCES)

    retVal = status == DgnFileStatus.eDGNFILE_STATUS_Success
    return retVal
if __name__ == "__main__":  # check if this script is being run directly (not imported as a module)
    # Choose a file from the same workset as the one you have open in MicroStation.
    # Otherwise MicroStation will pop a modal dialog about the file being from a different workset.
    FILE_PATH = r"L:\ full path to the DGN file you want to activate"
    activate_dgn_file (FILE_PATH)

Copy the above code to a new .py file. Edit the file name and change FILE_PATH to a valid DGN file. Execute the code!

MicroStation Session Manager

The following paragraphs are copied from the C++ MicroStationAPI help manual.

The MicroStation user interface focuses on one design file and one model within that file at a time.

The design file whose views are displayed is called the "MasterFile" and the model is called the "Active Model". A new session begins when a file becomes the MasterFile. When a session begins, the session manager does the following:

A session ends when the Master DGN is closed, a new file is to become the master, or when MicroStation shuts down. When ending a session, the session manager does the following:


Questions

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