MicroStation Python: Font List


 

Welcome to the world of MicroStation Python! This wiki offers a Python code snippet that showcases how to retrieve a comprehensive list of True Type Fonts currently loaded within your MicroStation session. This information is invaluable for tasks such as font selection, font management, and custom font-related applications.

 

Get Font List in MicroStation with Python

Let us dive into, retrieving a list of True Type Fonts. Refer to the Python Manager wiki to create and load a python project. Name your project as "FontList.py" and save it to your preferred directory. Open the project in the editor for writing your Python script.

 

Here is the complete script.

 

from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *
import os 
import pandas as pd


# Get Font List function
def GetFontList():
    font_list = []

    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    if ACTIVEMODEL is None:
        return False
    
    loadDgnFile  = ACTIVEMODEL.GetDgnFile()
    if loadDgnFile is None:
        return False

    fontList = DgnFontManager.CreateKnownFontList(loadDgnFile)

    for i in range (len(fontList)):
        dgnFont = fontList.GetEntry(i)
        # Check if True Type font
        if (DgnFontType.eTrueType == dgnFont.GetType()):
            print(f"Font Name (TTF): {dgnFont.GetName()}")
            font_list.append(dgnFont.GetName())

    return font_list


# Main function
def main():
    dgn_file = ISessionMgr.GetActiveDgnFile()
    file_path = dgn_file.GetFileName().GetWCharCP()
    directory_path = os.path.dirname(file_path)
    file_name_with_extension = os.path.basename(file_path)
    file_name = os.path.splitext(file_name_with_extension)[0]
    file_extension = os.path.splitext(file_name_with_extension)[1]
    report_extension = ".csv"
    report_file_name = os.path.join(directory_path, file_name + report_extension)

    fList = []
    fList = GetFontList()

    if (len(fList) > 0):
        df = pd.DataFrame(fList)
        df.to_csv(report_file_name, index=False)    
        print(f"Report File Name: {report_file_name}")


# main
if __name__ == "__main__":
    print ("***** Report TTF Fonts *****")
    main()

 

Run/Execute project

Load the project "FontList.py" from the Python Manager dialog and Run/Execute the python script.

 

All available True Type Fonts are extracted and exported to a CSV file using the Pandas | DataFrame.

Font List

 
 
Happy coding!

 

 

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