Welcome to the world of MicroStation Python! This wiki will walk you through the process of getting and setting properties of an Element.
Element Properties Get/Set
To get started, refer to the Python Manager wiki for instructions on creating and loading a new Python project. Name your project "ElmPropGetSet.py" and save it in a convenient location. Once created, open the project in your preferred code editor to begin writing your Python script.
To retrieve element properties,
To set element properties,
Here is the complete script.
from MSPyBentley import * from MSPyBentleyGeom import * from MSPyECObjects import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * # Element Properties Set function def ElmPropertiesSet(): ACTIVEMODEL = ISessionMgr.GetActiveDgnModel() eeh = EditElementHandle(8114, ACTIVEMODEL) elemRef = eeh.GetElementRef() propSetter = ElementPropertiesSetter() lvlCache = ISessionMgr.GetActiveDgnFile().GetLevelCache() lvlHandle = lvlCache.GetLevelByName("Level 21", False) propSetter.SetLevel(lvlHandle.GetLevelId()) propSetter.SetColor(0xFFFFFFFF) # -1 doesn't work propSetter.SetWeight(0xFFFFFFFF) propSetter.SetLinestyle(0x7FFFFFFF, None) if True == propSetter.Apply(eeh): eeh.ReplaceInModel(elemRef) # Element Properties Get function def ElmPropertiesGet(): ACTIVEMODEL = ISessionMgr.GetActiveDgnModel() eh = ElementHandle(8114, ACTIVEMODEL) propQuery = ElementPropertiesGetter(eh) print (f"Level ID: ", propQuery.Level) lvlCache = ISessionMgr.GetActiveDgnFile().GetLevelCache() lvlHandle = lvlCache.GetLevel(propQuery.Level) print (f"Level Name: ", lvlHandle.GetName()) print (f"Color ID: ", propQuery.Color) print (f"Weight ID: ", propQuery.Weight) print (f"Style ID: ", propQuery.LineStyle) # Main function def main(): ElmPropertiesGet () ElmPropertiesSet () # Main if __name__ == "__main__": print("***** Element Properties Get/Set *****") main()
Run/Execute project
Load the project “ElmPropGetSet.py” from the Python Manager dialog and Run/Execute the python script.
The Python script above, searches for an element by its ID and displays element properties Level, Color, Weight, Line Style in the MicroStation Text Editor.
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs