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.
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 …
ElementHandle
) from the selection
CurveVector
from the element
CurveVector
is a list of at least one CurvePrimitive
CurvePrimitive
from the CurveVector
DPoint3d
N points = 2
DPoint3d
vertices
N points = len(list)
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)
Create a CurveVector
from the element …
curve_vector: CurveVector = ICurvePathQuery.ElementToCurveVector(eh)
A CurveVector
is a list of CurvePrimitive
s.
Get the CurvePrimitive
s 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: # AnalyseCurvePrimitive
extracted from line nPoints = 2 case ICurvePrimitive.eCURVE_PRIMITIVE_TYPE_LineString: # AnalyseCurvePrimitive
extracted from line-string points = primitive.GetLineString() for n, point in enumerate(points): PrintDPointToMessageBox(f"Point[{n}]", point, uors) nPoints += 1
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.
Use MicroStation's Python Manager to find and execute the script.
Post questions about MicroStation programming to the MicroStation Programming Forum.