MicroStation Python: Create a Line String Element


 

Welcome to the world of MicroStation Python! This wiki will guide you through creating a Line String element in MicroStation using Python.

 

Create a Line String element in MicroStation using Python

Let us dive into creating a Line String element. Refer to the Python Manager wiki to create and load a python project. Name your project as "LineStringElement.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 and to create geometric elements.

from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *

 

The Function

The main function of this script is createLineString().

def createLineString():
    ...

 

Let us break down its steps:

 

1.  Active Model

A MicroStation session may access many models. The following script gets the Active Model Reference from a session.

    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    if ACTIVEMODEL is None:
        return False

 

2.  Get Units of Resolution per Storage

The following script, gets the UOR per Storage unit factor (Double value)

    dgnModel = ACTIVEMODEL.GetDgnModel()
    modelInfo = dgnModel.GetModelInfo()
    mu = modelInfo.GetUorPerStorage()

 

3.  Handle to Line String element.

Set a writeable "handle" to an MSElement.

    ls_eeh = EditElementHandle()

 

4.  Line String Coordinates

We define and set coordinates for the Line String element.

    pt0 = DPoint3d (0, 0, 0)
    pt1 = DPoint3d (0, 0, 0)
    pt2 = DPoint3d (0, 0, 0)
    pt3 = DPoint3d (0, 0, 0)
    pt4 = DPoint3d (0, 0, 0)
    pt5 = DPoint3d (0, 0, 0)

    pt1.x = pt0.x; pt1.y = pt0.y - mu/2; pt1.z = pt0.z;
    pt2.x = pt1.x + mu/2; pt2.y = pt1.y; pt2.z = pt0.z;
    pt3.x = pt2.x; pt3.y = pt2.y - mu/2; pt3.z = pt0.z;
    pt4.x = pt3.x + mu/2; pt4.y = pt3.y; pt4.z = pt0.z;
    pt5.x = pt4.x; pt5.y = pt0.y; pt5.z = pt0.z;

 

5.  Add Coordinates to DPoint3d array.

We define and add coordinates to a DPoint3dArray object.

    linePts = DPoint3dArray()
    linePts.append(pt0)
    linePts.append(pt1)
    linePts.append(pt2)
    linePts.append(pt3)
    linePts.append(pt4)
    linePts.append(pt5)
    linePts.append(pt0)

 

6.  Create Line String element.

The Line String element is created using the LineStringHandler.CreateLineStringElement() function. This function takes several parameters, which includes Line String element handle, Line String coordinates and the Model to which the Line String element should be added.

The script includes error checking to ensure that the Line String element is created successfully.

    status = LineStringHandler.CreateLineStringElement(ls_eeh, None,   
                                                        linePts,
                                                        ACTIVEMODEL.Is3d(), 
                                                        ACTIVEMODEL)
    
    if BentleyStatus.eSUCCESS != status:
        return False

 

7.  Set Element Properties (Symbology) and Add Element to Model

Element Properties (Symbology) Level, Color, Style, Weight are set to the newly created Line String element and element is added to the Model.

The script includes error checking to ensure that the Element Properties are set, and the Line String element is successfully added to the model.

    levelID = createLevel("MyNewLevel")
    color = 2
    lineStyle = 0
    lineWeight = 3

    propertiesSetter = ElementPropertiesSetter()
    propertiesSetter.SetColor(color)
    propertiesSetter.SetLinestyle(lineStyle, None)
    propertiesSetter.SetWeight(lineWeight)
    propertiesSetter.SetLevel(levelID)

    if True == propertiesSetter.Apply(ls_eeh):
        if BentleyStatus.eSUCCESS != ls_eeh.AddToModel():
            return False

 

8.  Create a new Level.

In the above code, the first line levelID = createLevel("MyNewLevel") returns a levelID for Level Name "MyNewLevel". If the Level Name does not exist in the DGN File, a new Level "MyNewLevel" is created before returning the Level ID.

The create new level process is executed through the createLevel() function detailed below.

def createLevel(levelName):
    dgnFile = ISessionMgr.GetActiveDgnFile()
    if dgnFile is None:
        return False

    levelCache = dgnFile.GetLevelCache()
    if levelCache is None:
        return False

    LevelHandle = levelCache.GetLevelByName(levelName)
    if not LevelHandle.IsValid():
        LevelHandle = levelCache.CreateLevel(levelName, LEVEL_NULL_CODE, 
                                              LEVEL_NULL_ID)
        if not LevelHandle.IsValid():
            return False
        levelCache.Write()

    return LevelHandle.GetLevelId()

 

Putting it all Together: The main Function

The main function simply calls the createStringLine() function and checks its return value. If the Line String element creation fails, an error message is printed.

Here is the complete script.

 

from MSPyBentley import *
from MSPyBentleyGeom import *
from MSPyECObjects import *
from MSPyDgnPlatform import *
from MSPyDgnView import *
from MSPyMstnPlatform import *


# Create Level Function
def createLevel(levelName):
    dgnFile = ISessionMgr.GetActiveDgnFile()
    if dgnFile is None:
        return False

    levelCache = dgnFile.GetLevelCache()
    if levelCache is None:
        return False

    LevelHandle = levelCache.GetLevelByName(levelName)
    if not LevelHandle.IsValid():
        LevelHandle = levelCache.CreateLevel(levelName, LEVEL_NULL_CODE, LEVEL_NULL_ID)
        if not LevelHandle.IsValid():
            return False
        levelCache.Write()

    return LevelHandle.GetLevelId()


# Create Line String element function
def createLineString ():
    # Get the active DGN model reference
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef

    # Get model information from the DGN model
    dgnModel = ACTIVEMODEL.GetDgnModel()
    modelInfo = dgnModel.GetModelInfo() 
    mu = modelInfo.GetUorPerStorage()

    # Handle Line String element
    ls_eeh = EditElementHandle() 

    # Define Line String coordinates
    pt0 = DPoint3d (0, 0, 0)
    pt1 = DPoint3d (0, 0, 0)
    pt2 = DPoint3d (0, 0, 0)
    pt3 = DPoint3d (0, 0, 0)
    pt4 = DPoint3d (0, 0, 0)
    pt5 = DPoint3d (0, 0, 0)

    # Coordinates of Line String element
    pt1.x = pt0.x; pt1.y = pt0.y - mu/2; pt1.z = pt0.z;
    pt2.x = pt1.x + mu/2; pt2.y = pt1.y; pt2.z = pt0.z;
    pt3.x = pt2.x; pt3.y = pt2.y - mu/2; pt3.z = pt0.z;
    pt4.x = pt3.x + mu/2; pt4.y = pt3.y; pt4.z = pt0.z;
    pt5.x = pt4.x; pt5.y = pt0.y; pt5.z = pt0.z;

    # Add coordinates to DPoint3d array
    linePts = DPoint3dArray()
    linePts.append(pt0)
    linePts.append(pt1)
    linePts.append(pt2)
    linePts.append(pt3)
    linePts.append(pt4)
    linePts.append(pt5)
    linePts.append(pt0)

    # Create Line String element
    status = LineStringHandler.CreateLineStringElement(ls_eeh, None, linePts,
                                                       ACTIVEMODEL.Is3d(), 
                                                       ACTIVEMODEL)
    
    if BentleyStatus.eSUCCESS != status:
        return False


   # Set Element Properties and add Element to Model
    levelID = createLevel("MyNewLevel")
    color = 2
    lineStyle = 0
    lineWeight = 3

    propertiesSetter = ElementPropertiesSetter()
    propertiesSetter.SetColor(color)
    propertiesSetter.SetLinestyle(lineStyle, None)
    propertiesSetter.SetWeight(lineWeight)
    propertiesSetter.SetLevel(levelID)
    if True == propertiesSetter.Apply(ls_eeh):
        if BentleyStatus.eSUCCESS != ls_eeh.AddToModel():
            return False
        
    return True


# Main function
def main():
    if True != createLineString():
        print("Could not create a Line String element")

    PyCadInputQueue.SendKeyin("FIT VIEW EXTENDED")


if __name__ == "__main__":
	main()

 

Run/Execute project

Select project "LineStringElement.py" from the Python Manager dialog and Run/Execute the python script.

Voila! A Line String element is created with element Properties (Symbology) set and added to your active MicroStation model.

image

 

Extend the above code to Extrude/Project the Line String element to a Surface or Solid element by exploring the delivered examples.

Happy coding!