Create a Centre Line with VBA

Questions similar to these appear in the Be Communities MicroStation Programming Forum.

Q How do I create a centre line between two existing lines? That is, I have two similar lines that are approximately 'parallel' and I want to construct a new line that runs mid-way between those lines.

A This article describes a MicroStation VBA project that provide two separate ways to create a centre-line. The macro …

  1. Lets you pick two lines
  2. Calculates a set of chords using one of two methods
    • Method 1: Between corresponding vertices
    • Method 2: By stepping along one line and calculating a perpendicular to the second line
  3. Constructs a new line-string that connects the centre point of each chord
Create Centre Line dialog

If you want to get started with using this macro, go to Download.

Example Line Pairs

The starting point is a pair of similar lines that are 'parallel' (but we use the term parallel very loosely). In this example the lines have the same number of vertices …

Create Centre Line start

Method 1: Chords by Vertex

The VBA macro first gets the vertices from each line, then constructs a set of chords (lines) that start at a vertex of the first line and ends at the corresponding vertex of the second line …

Centre Line Transient Chords by Vertex

Method 2: Chords by Perpendicular

The VBA macro steps along the first line by a fixed distance and gets the point at each step. From each point it drop a perpendicular onto the second line and gets the point of intersection. It constructs the chord from the pair of coordinates found at each step …

Centre Line Transient Chords by Perpendicular

The macro calculates the centre point of each chord. From that set of points it constructs a new line (VBA LineStringElement) that connects the mid-point of each chord …

Centre Line Complete

Method 2: Curves and Arcs

Method 2 works for DGN elements that don't implement the IVertexList interface. For example, arcs and curves. However, an element must provide the point-at-distance method (DistanceAtPoint) which is part of the TraversableElement object.

Here are two arc elements …

Arc Elements

Here is the result of applying Method 2 to those arcs …

Arc Elements Centre Line

Method 2: Maximum and Minimum Separation

While calculating the chords, the macro keeps a record of the maximum and minimum lengths. The result is displayed in the dialog box.

Notes for Programmers

The macro shows how to pick the initial line-strings using a locate class that Implements ILocateCommandEvents. The calculation of the chords and their mid-points is straightforward. The macro creates the chord LineElements as transients.

Class clsChords does the work of calculating chords and creating the centre-line. It provides two different methods to analyse the lines and generate the centre-line …

When using Method 1, the initial lines must be oriented in the same direction. Otherwise, the chords become twisted and the centre line will not be what you expect. The button labelled Reverse Line 2 lets you reverse the direction of the second line.

When using Method 2, the initial line orientation is unimportant. It's also much more adaptable to different DGN elements: it works with almost any line or curve. For example, here's a line-string and a B-Spline curve …

Centre Line B-Spline and Line-String Centre Line B-Spline Complete Download CreateCentreLine.ZIP

Download the Create Centre Line ZIP archive, and unpack it to a suitable location such as \Workspace\Standards\macros. Start the macro using the key-in …

vba run [CreateCentreLine]modMain.Main

Questions

Post questions about MicroStation programming to the MicroStation Programming Forum.