MicroStation Python: Raster Clip Boundary


 

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.

 

Scan Raster Clip Boundary in MicroStation with Python

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.

 

Import Modules

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:

  1. Get Active model: Retrieves the active DGN model using ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef. If there’s no active model, it exits safely.
  2. Raster Enumeration: Iterates through all raster attachments using, DgnRasterCollection.GetRasters(ACTIVEMODEL).
  3. Raster Metadata: For each raster, it prints, File name, Full Path with File name and Description.

raster.OpenParams.FileName

raster.OpenParams.Filespec

raster.Description

  1. Get Raster Clip: If raster is clipped raster.ClipState, retrieve the clip element raster.ClipProperties.Boundary.ClipElement.
  2. Raster Clip Processing: Wrap the clip element to an EditElementHandle(clip_elem_eh, True) and apply the raster transform eeh.GetDisplayHandler().ApplyTransform(eeh, TransformInfo(raster.GetTransform())) to correctly position it in model coordinates.
  3. Add Clip to Model: Add the transformed element to the model eeh.AddToModel().

 

 

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()

 

Run/Execute project

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