In this wiki, we'll delve into the world of MicroStation Python development, specifically focusing on crafting a user interface (UI) using the TKINTER library. This powerful combination allows you to create custom dialogs and tools within the MicroStation environment, enhancing productivity and user experience.
Before we dive into the code, it's essential to understand that TKINTER is a standard Python library for creating graphical user interfaces. It's relatively easy to learn and use, making it a popular choice for rapid UI development.
Refer to the Python Manager wiki to create and load a python project. Name your project as "DialogDemo.py" and save it to your preferred directory. Open the project in the editor for writing your Python script.
Understanding the Code
Let's break down the Python code step-by-step:
The script begins by importing several modules for interacting with MicroStation and creating the TKINTER UI:
colorchooser: A TKINTER module for selecting colors.
from MSPyBentley import * from MSPyBentleyGeom import * from MSPyECObjects import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * import ctypes from tkinter import * from tkinter import colorchooser
Creating the UI
The main function constructs the TKINTER UI:
Key Points
Here is the complete script
from MSPyBentley import * from MSPyBentleyGeom import * from MSPyECObjects import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * import ctypes from tkinter import * from tkinter import colorchooser root = Tk() # Message box function def MsgBox (title, text, style): return ctypes.windll.user32.MessageBoxW (0, text, title, style) # RGB to String function def rgb_to_string(rgb_tuple): return ', '.join(map(str, rgb_tuple)) # Hex to RGB function def hex_to_rgb(hex_color): # Remove the hash symbol if present hex_color = hex_color.lstrip('#') # Convert hex to RGB rgb = tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4)) return rgb # Get Level Name List def GetLevelList (): level_List = [] ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef levelCache = ACTIVEMODEL.GetDgnFile().GetLevelCache() it = iter(levelCache) for level in it: levelName = level.GetName() level_List.append(levelName) return level_List # Place Line Button def PlaceLine(): global my_color global selected_level if my_color is None: my_color = "#646464" rgb_color = hex_to_rgb(my_color) rgb_str = rgb_to_string (rgb_color) level_name = str(selected_level.get()) keyin_str = "ACTIVE COLOR " + rgb_str + "; ACTIVE LEVEL " + level_name keyin_str = keyin_str + "; ACTIVE WEIGHT 2; PLACE LSTRING" #print(keyin_str) PyCadInputQueue.SendKeyin(keyin_str) root.destroy() # Get color function def GetColor(): global my_color global color_label1 my_color = colorchooser.askcolor()[1] my_color = my_color.upper() color_label1.config(text=my_color) color_label1.config(bg=my_color) # Main function def main(): global my_color global color_label1 global selected_level #root = Tk() root.title("Dialog Demo...") # Color my_color = "#646464" # Create the widgets color_label = Label(root, text="Color: ") color_label1 = Label(root, text=my_color, bg=my_color) color_button = Button(root, text="Pick A Color", command=GetColor) # Use grid to place the widgets color_label.grid(row=0, column=0, padx=10, pady=10, sticky="ew") color_label1.grid(row=0, column=1, padx=10, pady=10, sticky="ew") color_button.grid(row=0, column=2, padx=10, pady=10, sticky="ew") # Level levelList = GetLevelList () level_label = Label(root, text="Level: ") selected_level = StringVar() selected_level.set(levelList[0]) level_option = OptionMenu(root, selected_level, *levelList) # Use grid to place the widgets level_label.grid(row=1, column=0, padx=10, pady=10, sticky="ew") level_option.grid(row=1, column=1, padx=10, pady=10, sticky="ew") place_button = Button(root, text="Place Line", command=PlaceLine) # Use grid to place the widgets place_button.grid(row=2, column=1, padx=10, pady=10, sticky="ew") # Configure column weights to spread out evenly root.grid_columnconfigure(0, weight=1) root.grid_columnconfigure(1, weight=1) root.grid_columnconfigure(2, weight=1) root.mainloop() if __name__ == "__main__": main()
Select project "DialogDemo.py" from the Python Manager dialog and Run/Execute the python script.
The following UI loads allowing User to create a Line String element with the selected Color, Level properties.
Explore additional TKINTER widgets and layout options to create more complex and visually appealing UIs with MicroStation integration.
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs