MicroStation Python: Create Basic Elements


 

Welcome to the world of MicroStation Python! In our earlier wikis, we explored how to create Line and Line String elements. In this wiki, we will delve into creating other basic geometric elements in MicroStation using Python.

 

Create Basic Elements in MicroStation using Python

Let us dive into creating basic geometric elements such as Arc, Circle, Ellipse, and Shape using Python. Refer to the Python Manager wiki to create and load a python project. Name your project as "CreateBasicElements.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 *

 

Create an Arc Element

The createArcElement() function takes several parameters for the arc element, including the base point, radius, rotation, start angle, and sweep angle. It uses the ArcHandler.CreateArcElement() method to create the arc element with these parameters. The script includes error checking to ensure the arc element is successfully created and added to the model.

def createArcElement(basePoint, radius=100, rotation=math.pi/4,
                       startAngle=0, sweepAngle=math.pi/2):
    global ACTIVEMODEL

    arc_eeh = EditElementHandle()

    axis1 = axis2 = radius

    # Create Arc element
    status = ArcHandler.CreateArcElement(arc_eeh, None, basePoint, axis1, axis2,
                                            rotation, startAngle, sweepAngle, 
                                            ACTIVEMODEL.Is3d(), ACTIVEMODEL)
    if BentleyStatus.eSUCCESS != status:
        return False

    # Add the Arc element to model
    if BentleyStatus.eSUCCESS != arc_eeh.AddToModel():
        return False
        
    return True

 

Create a Circle Element

The createCircleElement() function takes base point and radius as parameters for the circle element. It uses the EllipseHandler.CreateEllipseElement() method to create the circle element. The script includes error checking to ensure the circle element is successfully created and added to the model.

def createCircleElement(basePoint, radius=50):
    global ACTIVEMODEL

    circle_eeh = EditElementHandle()

    ell = DEllipse3d.FromCenterRadiusXY(basePoint, radius)

    # Create Ellipse element
    status = EllipseHandler.CreateEllipseElement(circle_eeh, None, ell, 
                                                    ACTIVEMODEL.Is3d(), 
                                                    ACTIVEMODEL)
    if BentleyStatus.eSUCCESS != status:
        return False

    # Add the Ellipse element to model
    if BentleyStatus.eSUCCESS != circle_eeh.AddToModel():
        return False
        
    return True

 

Create an Ellipse Element

The createEllipseElement() function takes base point, radius and rotation as parameters for the ellipse element. It uses the EllipseHandler.CreateEllipseElement() method to create the ellipse element. The script includes error checking to ensure the ellipse element is successfully created and added to the model.

def createEllipseElement(basePoint, radius=100, rotation=0):
    global ACTIVEMODEL

    ellipse_eeh = EditElementHandle()

    axis2 = radius
    axis1 = axis2 / 2

    # Create Ellipse element
    status = EllipseHandler.CreateEllipseElement(ellipse_eeh, None, 
                                                    basePoint, 
                                                    axis1, axis2,
                                                    rotation, 
                                                    ACTIVEMODEL.Is3d(), 
                                                    ACTIVEMODEL)
    if BentleyStatus.eSUCCESS != status:
        return False

    # Add the Ellipse element to model
    if BentleyStatus.eSUCCESS != ellipse_eeh.AddToModel():
        return False
        
    return True

 

Create a Shape Element

The createShapeElement() function generates a shape element at a given base point. It defines and sets the necessary coordinates, then uses the ShapeHandler.CreateShapeElement() method to create the shape element. The script includes error checking to ensure the shape element is successfully created and added to the model.

def createShapeElement(basePoint):
    global ACTIVEMODEL

    shape_eeh = EditElementHandle()

    # Set Shape coordinates
    offset = 150
    points = DPoint3dArray()
    points.append (DPoint3d (basePoint.x, basePoint.y, 0.0))
    points.append (DPoint3d (basePoint.x + offset, basePoint.y, 0.0))
    points.append (DPoint3d (basePoint.x + offset, basePoint.y + offset, 0.0))
    points.append (DPoint3d (basePoint.x, basePoint.y + offset, 0.0))
    points.append (DPoint3d (basePoint.x, basePoint.y, 0.0))

    # Create Shape element
    status = ShapeHandler.CreateShapeElement (shape_eeh, None, points, 
                                               ACTIVEMODEL.Is3d(), ACTIVEMODEL)

    if BentleyStatus.eSUCCESS != status:
        return False

    # Add the Shape element to model
    if BentleyStatus.eSUCCESS != shape_eeh.AddToModel():
        return False

    return True

 

Putting it all Together: The main Function

The main function initializes the active model and calls the functions to create the geometric elements.

Here is the complete script.

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


# Create Shape Element function
def createShapeElement(basePoint):
    global ACTIVEMODEL

    shape_eeh = EditElementHandle()

    # Set Shape coordinates
    offset = 150
    points = DPoint3dArray()
    points.append (DPoint3d (basePoint.x, basePoint.y, 0.0))
    points.append (DPoint3d (basePoint.x + offset, basePoint.y, 0.0))
    points.append (DPoint3d (basePoint.x + offset, basePoint.y + offset, 0.0))
    points.append (DPoint3d (basePoint.x, basePoint.y + offset, 0.0))
    points.append (DPoint3d (basePoint.x, basePoint.y, 0.0))

    # Create Shape element
    status = ShapeHandler.CreateShapeElement (shape_eeh, None, points, 
                                               ACTIVEMODEL.Is3d(), ACTIVEMODEL)

    if BentleyStatus.eSUCCESS != status:
        return False

    # Add the Shape element to model
    if BentleyStatus.eSUCCESS != shape_eeh.AddToModel():
        return False

    return True


# Create Ellipse Element function
def createEllipseElement(basePoint, radius=100, rotation=0):
    global ACTIVEMODEL

    ellipse_eeh = EditElementHandle()

    axis2 = radius
    axis1 = axis2 / 2

    # Create Ellipse element
    status = EllipseHandler.CreateEllipseElement(ellipse_eeh, None, 
                                                    basePoint, 
                                                    axis1, axis2,
                                                    rotation, 
                                                    ACTIVEMODEL.Is3d(), 
                                                    ACTIVEMODEL)
    if BentleyStatus.eSUCCESS != status:
        return False

    # Add the Ellipse element to model
    if BentleyStatus.eSUCCESS != ellipse_eeh.AddToModel():
        return False
        
    return True


# Create Circle Element function
def createCircleElement(basePoint, radius=50):
    global ACTIVEMODEL

    circle_eeh = EditElementHandle()

    ell = DEllipse3d.FromCenterRadiusXY(basePoint, radius)

    # Create Ellipse element
    status = EllipseHandler.CreateEllipseElement(circle_eeh, None, ell, 
                                                    ACTIVEMODEL.Is3d(), 
                                                    ACTIVEMODEL)
    if BentleyStatus.eSUCCESS != status:
        return False

    # Add the Ellipse element to model
    if BentleyStatus.eSUCCESS != circle_eeh.AddToModel():
        return False
        
    return True


# Create Arc Element function
def createArcElement(basePoint, radius=100, rotation=math.pi/4,
                       startAngle=0, sweepAngle=math.pi/2):
    global ACTIVEMODEL

    arc_eeh = EditElementHandle()

    axis1 = axis2 = radius

    # Create Arc element
    status = ArcHandler.CreateArcElement(arc_eeh, None, basePoint, axis1, axis2,
                                            rotation, startAngle, sweepAngle, 
                                            ACTIVEMODEL.Is3d(), ACTIVEMODEL)
    if BentleyStatus.eSUCCESS != status:
        return False

    # Add the Arc element to model
    if BentleyStatus.eSUCCESS != arc_eeh.AddToModel():
        return False
        
    return True


# Main function
def main():
    # Global variable declaration
    global ACTIVEMODEL

    # Get the active DGN model reference
    ACTIVEMODEL = ISessionMgr.ActiveDgnModelRef
    if ACTIVEMODEL is None:
        return
        
    if True != createArcElement(DPoint3d(0, 0, 0)):
        print("Create Arc element failed...")

    if True != createCircleElement(DPoint3d(200, 0, 0)):
        print("Create Circle element failed...")

    if True != createEllipseElement(DPoint3d(400, 0, 0)):
        print("Create Ellipse element failed...")

    if True != createShapeElement(DPoint3d(600, -75, 0)):
        print("Create Shape element failed...")

    PyCadInputQueue.SendKeyin("FIT VIEW EXTENDED")


if __name__ == "__main__":
    main()

 

Run/Execute project

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

 

Voila! An arc, circle, ellipse, and shape element have been successfully created and added to your active MicroStation model.

 

image

 

 

Enhance the above code to add a fill property to a closed element by referring to the provided examples and documentation.

Happy coding!