Q How do I get line elements using MicroStation Python?
Q How do I get a zero-length line element using MicroStation Python?
A Here is a Python example that gets line elements from a DGN model.
It tests for zero-length lines while building a
list
of
PersistentElementRef
.
Line elements are often used in MicroStation. Lines have two flavours: simple lines and line-strings. A simple line has two vertices, the start and end points. A line-string has many vertices, from two to five thousand. If you want to get a list of vertices from a line-string, read Get Line Vertices.
Line elements are sometimes used to represent a point. In that case the start and end points are coincident, and the line has zero-length.
Although a point element is useful, it's often the case that after use they should be deleted from the DGN model.
As a point is sometimes hard to see, especially in a busy part of a design, it's useful to have a utility that
seeks points.
This example shows how to harvest a list
of zero-length lines.
First, get a list of any element from a DGN model …
elements = dgnModel.GetGraphicElements()
Next, use a Python
list comprehension
to get a list of lines.
That calls the following function IsLine
to test if an element is a line element …
def IsLine (elemRef: PersistentElementRef, dgnModel: DgnModel): """ Test whether a DGN element is a line. """ # Create an element handle and get its Handler eh = ElementHandle (elemRef, dgnModel) handler = eh.GetHandler() # Compare this handler to LineHandler class if isinstance (handler, LineHandler): # Found a line return True #else return False
lines = [elemRef for elemRef in dgnModel.GetGraphicElements() if IsLine(elemRef, dgnModel)]
Finally, use a Python
list comprehension
to get a list of zero-length lines.
That calls the following function IsZeroLengthLine
to test if an element is a line element that has zero length …
def IsZeroLengthLine (elemRef: PersistentElementRef, dgnModel: DgnModel, epsilon: float = 1e-8): """ Test whether a DGN element is a zero-length line. In this example we want elements of a specified type: a DGN line element of zero-length.""" eh = ElementHandle (elemRef, dgnModel) handler = eh.GetHandler() if isinstance (handler, LineHandler): # Found a line: test for zero-length curve = ICurvePathQuery.ElementToCurveVector (eh) lineLength = curve.Length() msg = f"Line [{elemRef.GetElementId()}] length {lineLength}" MessageCenter.ShowDebugMessage (msg, msg, False) # Test the line length against some small quantity epsilon return lineLength < epsilon #else return False
zeroLengthlines = [elemRef for elemRef in dgnModel.GetGraphicElements() if IsZeroLengthLine(elemRef, dgnModel)]
Why perform test lineLength < epsilon
and not lineLength == 0.0
?
When analysing floating-point numbers, we need to
beware rounding and other issues.
Once you have a list of elements, it's straightforward to remove them.
Now you can check to see if your DGN model contains zero-length lines. Watch the messages in the MicroStation Messager Center.
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.