How to Create a New Level in MicroStation with Python


 

When working on complex projects in MicroStation, organizing different elements into levels can enhance efficiency and clarity. This guide will walk you through creating a new level programmatically using Python.

 

Import Modules

The script begins by importing several modules offering functions and classes to interact with MicroStation.

from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *

 

The Function

The core of our script is the createLevel() function, which checks if the level already exists and creates if not. It then writes the changes to the level cache.

def createLevel(levelName):
    ...

 

1.  Active DGN File

The first step is to obtain the active design file, which is the file you are currently working on in MicroStation.

dgnFile = ISessionMgr.GetActiveDgnFile()
if dgnFile is None:
    return False

 

2.  Retrieve the Level Cache

Next retrieve level cache from active design file.

levelCache = dgnFile.GetLevelCache()
if levelCache is None:
    return False

 

3.  Check if Level Name exists.

If Level Name exists in the design file, get the Level Handle.

LevelHandle = levelCache.GetLevelByName(levelName)

 

4.  Create New Level

If Level Name is not in the design file, create the new Level and write the changes to the level cache.

if not LevelHandle.IsValid():
    LevelHandle = levelCache.CreateLevel(levelName, LEVEL_NULL_CODE, 
                                          LEVEL_NULL_ID)
    if not LevelHandle.IsValid():
        return False

    levelCache.Write()

 

5.  Return Level ID

Finally, return the Level ID, which confirms that the level creation is successful.

return LevelHandle.GetLevelId()

 

Automating Multiple Levels

To create multiple levels, we can use a loop within the main function. This loop creates ten levels named "Level_1" through "Level_10" and opens the Level Manager dialog. With this function, you can now add new levels to your MicroStation model with ease, keeping your project organized and manageable.

def main():
    for i in range(1, 11):
        sLevelName = "Level_" + str(i)
        iLevelId = createLevel(sLevelName)
    
    PyCadInputQueue.SendCommand("LEVELMANAGER DIALOG OPEN")

 

Here is the complete script.

import os
import math
import sys
from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *


# Create Level Function
def createLevel(levelName):
    # Get Active DGN File
    dgnFile = ISessionMgr.GetActiveDgnFile()
    if dgnFile is None:
        return False

    # Get Level Cache from DGN File
    levelCache = dgnFile.GetLevelCache()
    if levelCache is None:
        return False

    # If Level Name exists, get Level Handle
    LevelHandle = levelCache.GetLevelByName(levelName)

    # If Level Handle is invalid, create Level
    if not LevelHandle.IsValid():
        # Create New Level
        LevelHandle = levelCache.CreateLevel(levelName, LEVEL_NULL_CODE, LEVEL_NULL_ID)
        if not LevelHandle.IsValid():
            return False
        
        # Write Level Cache changes
        levelCache.Write()

    # Return Level ID of newly created Level
    return LevelHandle.GetLevelId()


# Main Function
def main():
    # Create Levels 1 to 10
    for i in range(1, 11):
        sLevelName = "Level_" + str(i)
        print (sLevelName)

        # Call the createLevel function
        iLevelId = createLevel (sLevelName)
        print (iLevelId)
    
    PyCadInputQueue.SendCommand("LEVELMANAGER DIALOG OPEN")


if __name__ == "__main__":
    print ("Create Levels...")
    main()

 

Extend the above code to include CRUD (Create, Read, Update, Delete) Level operations by exploring the documentation.

Happy coding!

 

 

Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs