Script which generates various 2d elements like point, line, line string, shape
and adds the elements to the current file/model.
Each unique element type to be a separate function.
Set element properties like Color, Level, Weight to random values and Style=0.
Here are the key MicroStation Python APIs and concepts you’ll learn from this example:
1. Accessing the Active Model
A MicroStation session may access many models. The following script gets the Active Model Reference from a session.
ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
2. Working with Units of Resolution (UOR)
Converts distances to master units, ensuring geometry is scaled correctly.
dgnModel = ACTIVEMODEL.GetDgnModel() modelInfo = dgnModel.GetModelInfo() __, uorPerMast = modelInfo.StorageUnit.ConvertDistanceFrom (modelInfo.UorPerStorage, modelInfo.MasterUnit)
3. Creating Geometry: Point Element
Defines a helper function to generate Point element at a given location.
PointStringHandler is used to construct a Point element.
def create_point (): ...
PointStringHandler.CreatePointStringElement(eeh, None, points, rt, True, True, dgnModel)
4. Creating Geometry: Line Element
Defines a helper function to generate Line element from a set of points.
LineHandler is used to construct a Line element.
def create_line (): ...
LineHandler.CreateLineElement(eeh, None, segment, dgnModel.Is3d(), dgnModel)
5. Creating Geometry: Line String Element
Defines a helper function to generate Line String element from a set of points.
LineStringHandler is used to construct a Line String element.
def create_line_string (): ...
LineStringHandler.CreateLineStringElement(eeh, None, points, dgnModel.Is3d(), dgnModel)
6. Creating Geometry: Shape Element
Defines a helper function to generate Shape element from a set of points.
ShapeHandler is used to construct a Shape element.
def create_shape (): ...
ShapeHandler.CreateShapeElement(eeh, None, points, dgnModel.Is3d(), dgnModel)
7. Applying Element Symbology
Demonstrates how to set Color, Weight, Style using ElementPropertiesSetter.
propSetter = ElementPropertiesSetter() propSetter.SetColor(color)
propSetter.SetWeight(weight)
propSetter.SetLineStyle(style, None) propSetter.Apply(eeh)
import random from MSPyBentley import * from MSPyECObjects import * from MSPyBentleyGeom import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * def set_element_properties (eeh, color, weight, style=0): # Set element properties propertiesSetter = ElementPropertiesSetter() propertiesSetter.SetColor(color) propertiesSetter.SetWeight(weight) propertiesSetter.SetLinestyle(style, None) propertiesSetter.Apply(eeh) def create_point (): # Generate random position for the point x = random.uniform(0.0, 100.0) * uorPerMast y = random.uniform(0.0, 100.0) * uorPerMast point = DPoint3d(x, y, 0.0) # Create the point element using PointStringHandler eeh = EditElementHandle() points = DPoint3dArray() points.append(point) rt = RotMatrixArray() if BentleyStatus.eSUCCESS == PointStringHandler.CreatePointStringElement(eeh, None, points,
rt, True, True, dgnModel): color = random.randint(0, 255) weight = 10 set_element_properties(eeh, color, weight) eeh.AddToModel() else: print(f"Failed to create a Point element") def create_line (): global ACTIVEMODEL global dgnModel global modelInfo global uorPerMast # Generate random start and end points for the line x1 = random.uniform(0.0, 100.0) * uorPerMast y1 = random.uniform(0.0, 100.0) * uorPerMast x2 = random.uniform(0.0, 100.0) * uorPerMast y2 = random.uniform(0.0, 100.0) * uorPerMast segment = DSegment3d(DPoint3d(x1, y1, 0.0), DPoint3d(x2, y2, 0.0)) # Create the line element eeh = EditElementHandle() if BentleyStatus.eSUCCESS == LineHandler.CreateLineElement(eeh, None, segment,
dgnModel.Is3d(), dgnModel): color = random.randint(0, 255) weight = 2 set_element_properties(eeh, color, weight) eeh.AddToModel() else: print(f"Failed to create a Line element") def create_line_string (): global ACTIVEMODEL global dgnModel global modelInfo global uorPerMast # Generate random points for the line string points = DPoint3dArray() for _ in range(5): x = random.uniform(0.0, 100.0) * uorPerMast y = random.uniform(0.0, 100.0) * uorPerMast points.append(DPoint3d(x, y, 0.0)) # Create the line string element eeh = EditElementHandle() if BentleyStatus.eSUCCESS == LineStringHandler.CreateLineStringElement(eeh, None, points,
dgnModel.Is3d(), dgnModel): color = random.randint(0, 255) weight = 2 set_element_properties(eeh, color, weight) eeh.AddToModel() else: print(f"Failed to create a Line String element") def create_shape (): global ACTIVEMODEL global dgnModel global modelInfo global uorPerMast # Generate random points for the shape points = DPoint3dArray() for _ in range(4): x = random.uniform(0.0, 100.0) * uorPerMast y = random.uniform(0.0, 100.0) * uorPerMast points.append(DPoint3d(x, y, 0.0)) points.append(points[0]) # Closing the shape # Create the shape element eeh = EditElementHandle() if BentleyStatus.eSUCCESS == ShapeHandler.CreateShapeElement(eeh, None, points,
dgnModel.Is3d(), dgnModel): color = random.randint(0, 255) weight = 2 set_element_properties(eeh, color, weight) eeh.AddToModel() else: print(f"Failed to create a Shape element") def main (): global ACTIVEMODEL global dgnModel global modelInfo global uorPerMast ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef dgnModel = ACTIVEMODEL.GetDgnModel() modelInfo = dgnModel.GetModelInfo() __, uorPerMast = modelInfo.StorageUnit.ConvertDistanceFrom(modelInfo.UorPerStorage, modelInfo.MasterUnit) # Create elements create_point() create_line() create_line_string() create_shape() if __name__ == "__main__": main ()
What You’ll Learn from This Example:
This example is a great starting point for automating geometry creation in MicroStation using Python. It shows how to:
Try It Yourself:
You can modify the prompt to extend your 2D workflows into 3D by learning how to script solids, surfaces, and meshes. Useful for modeling infrastructure components like bridges, tunnels, or buildings.
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs