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 This solution was provided by Jan Šlegr, a veteran MicroStation developer.

Python Implementation

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

def extract_vertices():
    ssm : SelectionSetManager = SelectionSetManager.GetManager()
    if not ssm.IsActive():
        MessageCenter.StatusMessage = "No selected element"
        return

    eh = EditElementHandle()
    status = element = ssm.GetElement(0, eh)
    if (status != BentleyStatus.eSUCCESS):
        MessageCenter.StatusMessage = "Failed to get element"
        return

    if not isinstance(eh.GetHandler(), LineHandler) and not isinstance(eh.GetHandler(), LineStringHandler):
        MessageCenter.StatusMessage = "Element is not a line or line string"
        return

    curve_vector : CurveVector = ICurvePathQuery.ElementToCurveVector(eh)

    # Get the start and end points of the curve
    start = DPoint3d()
    end = DPoint3d()
    curve_vector.GetStartEnd(start, end)
    print ("Start + end points: ", start, end)

    # Get the points of the curve
    for primitive in curve_vector:
        points = primitive.GetLineString()
        for point in points:
            print("Point: ", point)

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.