Script which generates 10 random rectangular shapes and adds the shapes to the current
file/model.
Some Shape elements to have a fill color.
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: Rectangular Shapes
Defines a helper function to generate the corner points of a rectangle centered at a given point.
def random_rectangle_points(center, width, height): ...
4. Generating Random Geometry
Uses Python’s random module to vary the position and size of each rectangle.
random.uniform(0, 100), random.uniform(5, 30)
5. Handle to Shape element
Set a writeable "handle" to an MSElement using EditElementHandle.
eeh = EditElementHandle()
6. Creating Shape Elements
Constructs a shape element from a set of points and adds it to the model using ShapeHandler.
ShapeHandler.CreateShapeElement(eeh, None, pts, dgnModel.Is3d(), dgnModel)
7. Applying Fill and Color
Demonstrates how to set Color and Fill to elements using ElementPropertiesSetter.
propSetter = ElementPropertiesSetter() propSetter.SetColor(random.randint(1, 254)) propSetter.SetFillColor(random.randint(1, 254)) propSetter.Apply(eeh)
8. Adding Elements to the Model
Adds the newly created shape to the active model.
eeh.AddToModel()
from MSPyBentley import * from MSPyECObjects import * from MSPyBentleyGeom import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * import random def random_rectangle_points(center, width, height): """Returns a DPoint3dArray for a rectangle centered at 'center'.""" hw = width / 2.0 hh = height / 2.0 pts = DPoint3dArray() pts.append(DPoint3d(center.x - hw, center.y - hh, center.z)) pts.append(DPoint3d(center.x + hw, center.y - hh, center.z)) pts.append(DPoint3d(center.x + hw, center.y + hh, center.z)) pts.append(DPoint3d(center.x - hw, center.y + hh, center.z)) pts.append(DPoint3d(center.x - hw, center.y - hh, center.z)) # Close the shape return pts # Get the active model and UOR scaling factor ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef dgnModel = ACTIVEMODEL.GetDgnModel() modelInfo = dgnModel.GetModelInfo() __, uorPerMast = modelInfo.StorageUnit.ConvertDistanceFrom (modelInfo.UorPerStorage, modelInfo.MasterUnit) for i in range(10): # Random center in a 0..100, 0..100 area, random width/height 5..30 cx = random.uniform(0, 100) * uorPerMast cy = random.uniform(0, 100) * uorPerMast width = random.uniform(5, 30) * uorPerMast height = random.uniform(5, 30) * uorPerMast center = DPoint3d(cx, cy, 0.0) pts = random_rectangle_points(center, width, height) eeh = EditElementHandle() if BentleyStatus.eSUCCESS == ShapeHandler.CreateShapeElement(eeh, None, pts, dgnModel.Is3d(), dgnModel): # Randomly assign a fill color to some shapes if random.choice([True, False]): propSetter = ElementPropertiesSetter() # Use a random color index (1-254), 0 is usually background propSetter.SetColor(random.randint(1, 254)) propSetter.SetFillColor(random.randint(1, 254)) propSetter.Apply(eeh) eeh.AddToModel()
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 explore other shapes, add text labels, or even group elements. For example:
“Create 5 circles with random radii and assign each a unique Color and Level.”
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs