Welcome to the world of MicroStation Python! In this wiki, we’ll dive into practical techniques for working with raster attachments in MicroStation using Python. You’ll learn how to:
Whether you’re automating workflows or enhancing your design process, this tutorial will help you harness the power of Python to streamline raster management in MicroStation.
Refer to the Python Editor wiki to create and load a python project. Name your project as “RasterClipBoundary.py” and save it to your preferred directory. Open the project in the editor for writing your Python script.
The script begins by importing several modules offering functions and classes to interact with MicroStation.
from MSPyBentley import * from MSPyECObjects import * from MSPyBentleyGeom import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import *
Get DGN Rasters
The get_dgn_rasters() function scans all raster attachments in the active DGN model, reports metadata, detects raster with clip boundaries, applies the raster’s placement transform, and adds the transformed clip shape to the model. Here is a breakdown of what it does:
raster.OpenParams.FileName
raster.OpenParams.Filespec
raster.Description
Putting it all Together: The main Function
The main function calls the get_dgn_rasters() function which scans all raster attachments.
Here is the complete script.
from MSPyBentley import * from MSPyECObjects import * from MSPyBentleyGeom import * from MSPyDgnPlatform import * from MSPyDgnView import * from MSPyMstnPlatform import * def get_dgn_rasters(): """ Retrieves all raster elements in the active DGN model, prints their info, and if clipped, adds the clip boundary shape to the model. """ ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef if ACTIVEMODEL is None: print("No active model.") return for raster in DgnRasterCollection.GetRasters(ACTIVEMODEL): print(f"\nRaster Name: {raster.OpenParams.FileName}") print(f"Raster Full Name Path: {raster.OpenParams.Filespec.GetWCharCP()}") print(f"Raster Description: {raster.Description}") if raster.ClipState: print("Raster is clipped...") # Get the clip boundary element and apply the raster's transform clip_elem_eh = raster.ClipProperties.Boundary.ClipElement if clip_elem_eh is not None: eeh = EditElementHandle(clip_elem_eh, True) eeh.GetDisplayHandler().ApplyTransform(eeh, TransformInfo(raster.GetTransform())) eeh.AddToModel() print("Raster clip shape added to model...") def main(): get_dgn_rasters() if __name__ == "__main__": main()
Load the project “RasterClipBoundary.py” from the Python Editor dialog and Run/Execute the python script.
Clip shapes are added to model and console logs for each raster.
Before:
After:
Console logs
Raster Name: ALK1.png
Raster Full Name Path: D:\MSPY\Examples\Raster\ALK1.png
Raster Description: AL Khobar 20
Raster is clipped...
Raster clip shape added to model...
Raster Name: ALK2.png
Raster Full Name Path: D:\MSPY\Examples\Raster\ALK2.png
Raster Description: AL Khobar 10
Happy coding!
Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs