From a user perspective, opening and activating a new DGN file appear the same. For programmers, those are distinct operations …
This example uses two methods provided by ISessionMgr …
ISessionMgr.FindDesignFile()
This function works with
MSDocumentManager
to locate and manage files.
In stand-alone MicroStation,
if inFilename is not a complete file path,
this function will look for it in the directories defined by configuration variable MS_DEF
.
ISessionMgr.SwitchToNewFile()
This function closes the current Master DGN, if any, and ends the previous session.
It then begins a new session.
See SessionManager for a description of what happens when a session ends and starts
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!
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:
Post questions about MicroStation Python programming to the MicroStation Programming Forum.