MicroStation Python: Create a Line Element


 

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

 

Create a Line element in MicroStation using Python

Let us dive into creating a Line element program. Refer to the Python Manager wiki to create and load a python project. Name your project as "LineElement.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 createLine().

def createLine():
    ...

 

Let's 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 element

Set a writeable "handle" to an MSElement.

    line_eeh = EditElementHandle()

 

4.  Line Coordinates

We define the start and end point coordinates for the Line element, using DPoint3d object. You can modify these values to create lines of different lengths and directions.

DSegment3d object is created using the defined start and end points. This represents the actual line segment.

    startPoint = DPoint3d(0, 0, 0)
    endPoint   = DPoint3d(5 * mu, 5 * mu, 0)

    seg = DSegment3d(startPoint, endPoint)

 

5.  Create Line Element

The Line element is created using the LineHandler.CreateLineElement() function. This function takes several parameters, that includes Line element handle, Line segments constituting Start and End point coordinates and the Model to which the Line element should be added.

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

    status = LineHandler.CreateLineElement(line_eeh, None, seg, 
                                            ACTIVEMODEL.Is3d(), ACTIVEMODEL)

    if BentleyStatus.eSUCCESS != status:
        return False

 

6.  Add Line Element to Model

The newly created Line element is added to the Model.

The script includes error checking to ensure that the Line element is successfully added to the model.

    if BentleyStatus.eSUCCESS != line_eeh.AddToModel():
	    return False

 

Putting it all Together: The main Function

The main() function simply calls the createLine() function and checks its return value. If the Line 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 Line element function
def createLine():
	# Active Model Reference
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    if ACTIVEMODEL is None:
        return False
	
	# Master Unit from DGN Model
    dgnModel = ACTIVEMODEL.GetDgnModel()
    modelInfo = dgnModel.GetModelInfo()
    mu = modelInfo.GetUorPerStorage()

	# Handle to line element
    line_eeh = EditElementHandle()

	# Set line coordinates
    startPoint = DPoint3d(0, 0, 0)
    endPoint   = DPoint3d(5 * mu, 5 * mu, 0)

	# Line segements
    seg = DSegment3d(startPoint, endPoint)
        
	# Create line element
    status = LineHandler.CreateLineElement(line_eeh, None, seg, 
                                            ACTIVEMODEL.Is3d(), 
		                            ACTIVEMODEL)
    if BentleyStatus.eSUCCESS != status:
        return False
        
	# Add the line element to model
    if BentleyStatus.eSUCCESS != line_eeh.AddToModel():
	    return False

    return True


# Main Function
def main():
    if True != createLine():
        print("Could not create a Line element")

    PyCadInputQueue.SendKeyin("FIT VIEW EXTENDED")


if __name__ == "__main__":
	main()

 

Run/Execute project

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

Voila! A line element should be created in your active MicroStation model with the specified starting and ending points.

image

Extend the above code to set Line properties (Symbology) Level, Color, Weight and Style by exploring the delivered examples.

Happy coding!

 

 

Python: Documentation | API Presentations | FAQs | GitHub | Samples | Wikis | Blogs