Python

Get Line Vertices

Q How do I get a list of vertices from a DGN line element? This question arose on the Be Communities MicroStation Programming site.

A A version of this solution was provided by Jan Šlegr, a veteran MicroStation developer. The code discussed here addresses some minor problems.

Python Implementation

This code expects you to select some elements in the active DGN model. The code analyses the first element found in the contents of the Selection Set …

Selection Set

Check the selection set …

def ElementIsSelected ()->(bool, ElementHandle):
    '''
    Shows how to get an element from a selection set.
    If user has selected at least one element, get its ElementHandle.
    Returns: tuple(True, ElementHandle) if an element is found.
    '''
    valid = False
    msg = str()
    eh = ElementHandle()
    ssm : SelectionSetManager = SelectionSetManager.GetManager()
    if ssm.IsActive():
        #   Get the first element in the selection
        status = ssm.GetElement(0, eh)
        if (status == BentleyStatus.eSUCCESS):
            valid = eh.IsValid()
        else:
            msg = "Failed to get element from selection set"
            MessageCenter.ShowErrorMessage(msg, msg, False)
    else:
        msg = "No selected element"
        MessageCenter.ShowErrorMessage(msg, msg, False)
    return (valid, eh) 

CurveVector

Create a CurveVector from the element …

curve_vector: CurveVector = ICurvePathQuery.ElementToCurveVector(eh) 

A CurveVector is a list of CurvePrimitives.

CurvePrimitive

Get the CurvePrimitives from the CurveVector. For a simple element, such as a line or line-string, there will be a single CurvePrimitive. When analysing linear elements, we must distinguish between a line and a line-string …

for primitive in curve_vector:
    type = primitive.GetCurvePrimitiveType()
    match type:
        case ICurvePrimitive.eCURVE_PRIMITIVE_TYPE_Line:
            # Analyse CurvePrimitive extracted from line
            nPoints = 2
        case ICurvePrimitive.eCURVE_PRIMITIVE_TYPE_LineString:
            # Analyse CurvePrimitive extracted from line-string
            points = primitive.GetLineString()
            for n, point in enumerate(points):
                PrintDPointToMessageBox(f"Point[{n}]", point, uors)
                nPoints += 1
Download la_solutions_get_line_vertices.zip

Download the Get Line Vertices archive, and unpack it to a suitable location.

Unpack the ZIP file and copy the Python file into a folder that MicroStation knows about.

Python Manager

Use MicroStation's Python Manager to find and execute the script.

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.