Label placement

2D label placement for engineering drawings: given a set of labels, each belonging to a leader and keeping clear of blocked regions, Arranger.Run works out how far to move each label so that none overlaps another or encroaches on the blocked regions.

It depends on AutoCAD and Tekla for nothing. The geometry it works in is geometry in the plane, and its own types live in the GeometryHelper.Arranging namespace.

Visual Examples

AutoCAD Integration

Here are some examples of labels arranged inside AutoCAD to avoid overlaps and blocked regions:

Greedy
Greedy

Tekla Structures Integration

Here is an example of reinforcement marks before and after arrangement:

Before Arrangement After Arrangement
Before Arrangement After Arrangement
Arranged Marks Avoiding Dimension Obstacles
Tekla Result Detail

Quick Start

var leader = new GeoLine2(0.0, 0.0, 2000.0, 0.0);

var items = new List<ArrangeItem>
{
    new ArrangeItem
    {
        // The label's box: centre, width, height, rotation angle (radians, counter-clockwise)
        Box    = new GeoRectangle2(new GeoPoint2(1000.0, 0.0), 2000.0, 1000.0),
        // The leader: the candidate positions spread out from its midpoint
        Leader = leader,
        // The least gap between the label's edge and the leader, for this label alone (default 50)
        Offset = 50.0,
        // What the label must not overlap
        BlockPolygons = new List<GeoPolygon2>(),
        BlockLines    = new List<GeoLine2>()
    }
};

// One result for each item, in the same order. The items themselves are left as they were given.
ArrangeResult[] results = Arranger.Run(items);

for (int i = 0; i < items.Count; i++)
{
    GeoPoint2 newCentre = items[i].Box.Center + results[i].Translation;
    bool placed = results[i].Placed; // false: no clear place was found, and the label overlaps something
}

Arranger.Run only reads the items. What becomes of each comes back as an ArrangeResult: Translation, how far to move the label, and Placed, whether it ends up clear of every other label and of every block. The same list can therefore be run again, with other options or on another thread, to compare. A null entry is passed over and answered with default: not moved, not placed.

A run goes over the labels twice. The first pass places every label under every constraint. The labels it leaves overlapping something are tried once more with the block lines lifted, keeping clear of the labels already placed. Placed is judged afterwards, on the final layout as a whole, so a label that another one fell back onto is not reported clear.

To fine-tune the placement, pass ArrangeOptions:

var options = new ArrangeOptions
{
    RowGap              = 20.0,
    PerpendicularLevels = 3
};

ArrangeResult[] results = Arranger.Run(items, options);

ArrangeOptions is the shared configuration for the entire list. Offset is set per ArrangeItem because each label may require a different offset:

var smallTextLabel = new ArrangeItem
{
    Box    = new GeoRectangle2(new GeoPoint2(1000.0, 0.0), 2000.0, 1000.0),
    Leader = leader,
    Offset = 50.0   // small text, closely sticks to the leader
};

var largeTextLabel = new ArrangeItem
{
    Box    = new GeoRectangle2(new GeoPoint2(1000.0, 0.0), 4000.0, 2000.0),
    Leader = leader,
    Offset = 200.0  // large text, must move further away
};

ArrangeResult[] results = Arranger.Run(new List<ArrangeItem> { smallTextLabel, largeTextLabel }, options);

A label can stand one gap off above its leader and another below it. OffsetTop is the gap on the side of the leader that faces up in the drawing and OffsetBottom the gap on the side that faces down, 50 each unless set. Which side faces up does not depend on which way the leader was drawn, and a vertical leader, to within the angle of the options' Tolerance, has its top on the left, where the text of a vertical dimension stands. Every gap has to be a finite number:

var dimensionText = new ArrangeItem
{
    Box          = new GeoRectangle2(new GeoPoint2(1000.0, 0.0), 2000.0, 1000.0),
    Leader       = leader,
    OffsetTop    = 20.0,   // above the leader: close to it
    OffsetBottom = 300.0   // below it: well clear
};

Offset sets both sides at once. It only sets, holding nothing of its own, so it cannot be read, and set after a side it overwrites that side too: new ArrangeItem { Offset = 50.0, OffsetTop = 20.0 } stands 20 off above and 50 below, new ArrangeItem { OffsetTop = 20.0, Offset = 50.0 } 50 off on both sides.

Side keeps a label to one side of its leader: ArrangeSide.Top, the side that faces up, the left of a vertical leader; ArrangeSide.Bottom, the side that faces down; or ArrangeSide.Both, the default, either. Kept to one side, the label is tried on the rows of that side alone, and with no free place there it is left on the first of them and reported not Placed, however free the other side:

var levelMark = new ArrangeItem
{
    Box       = new GeoRectangle2(new GeoPoint2(1000.0, 0.0), 2000.0, 1000.0),
    Leader    = leader,
    OffsetTop = 20.0,
    Side      = ArrangeSide.Top   // above the leader, never below it
};

A gap as wide as it goes on the other side is not the same thing: it only makes that side the last one tried, and the label still goes there when the near side is full.

Candidate Positions Generation

Every label is tried at a set of discrete candidate positions, expanding from the midpoint of its leader; ArrangeItem.GetPlacePoints(options) lists them:

  • Perpendicular Translation — each level in PerpendicularLevels creates a row of labels on either side of the leader, or on the one side Side keeps the label to. The first row on each side lies half the label height plus the gap of that side off the leader: OffsetTop above, OffsetBottom below. Each subsequent level adds the label height plus RowGap.
  • Longitudinal Sliding — in each row, the label slides parallel to the leader in both directions, up to a maximum of half the leader's length plus LongitudinalOvershootRatio times the label width.

The rows of both sides come nearest first, each straight across the middle of the leader and then a step back and a step forward along it in turn. Two rows as far off, one on each side, as every pair is when both sides have the same gap, are tried together, place by place. A label has no more than MaximumCandidates candidates in all.

Each row slides twenty equal steps each way, the last at the full reach, or, where a twentieth of the reach, within the point tolerance, comes to less than 0.1, steps of the label's height, the last of them no further than the point tolerance past the reach. The steps are counted, not added up one after another, so a scene moved elsewhere is tried at the same places, moved, to within the rounding of where it lies: a label on a leader 60 long had 234 places at the origin and 246 900 along X, its last slide coming and going with the rounding of the sum, and has 246 at both. A reach falling within rounding of the switch itself still goes either way: one of 1.999, the switch less the point tolerance, gives 246 places at the origin and 54 or 66 half a million out. So does a block standing within rounding of the edge of a label's reach: a label 20 by 10 on a leader from (0, 0) to (100, 0), 5 off it, with a block line from (185, -5) to (185, 5), goes to (-3.25, 10) at the origin and to (0, 10) at 2 of 20 000 places a thousand to ten million off it. A label as large as MinimumBoxSize has its places however its corners round: measured along its leader and across it, it counts as large enough to within the point tolerance. Of 200 000 labels 20, 25 or 30 by 10 at the default 10, on leaders 20 to 119 long at a random angle a third of the time and along an axis otherwise, half of them turned to their leader and half not, 6 606 had no places at all near the origin, measured as their corners rounded, and 8 181 of as many drawn alike up to ten million off it.

A label 600 by 250 on a leader 2 000 long, 50 off above and 150 below, with the default options, has 246 candidates: six rows of 41, a step of 72.5 apart. The first row above lies 175 off the leader and the first below 275, and as the gaps differ the rows are tried one at a time, 175 above, 275 below, 445 above, 545 below, 715 and 815:

The candidate places of a label 600 by 250 on a leader 2000 long, OffsetTop 50 and OffsetBottom 150: six rows of 41 places, numbered in the order they are tried; the label dashed where it was given and blue at the first place of each side; the red dot is the middle of the leader

Greedy Placement

The labels are placed greedily, one after another. A label once placed is never taken up again: it stands as a block for every label after it. The same items and options always give the same result.

Nine marks 1 100 by 320 on the sloping leaders of the bars along a beam 9 000 long, 40 off them, the beam given as a block polygon and the dimension line above it as block lines. Given at the middle of their leaders, every label overlaps the next and the beam; after Arranger.Run all nine are Placed, each above its own leader:

Nine labels at the middle of their leaders, overlapping each other and the beam The same labels after Arranger.Run, each clear of the others, the beam and the dimension line; a thin line joins each to the middle of its leader
As given After Arranger.Run
  • Order — with PlaceMostConstrainedFirst, the default, each label's freedom is counted first: how many of its first FreedomSampleSize candidates, 12 unless set, are clear of the blocks. The label with the fewest goes first. Of two as free, with PlaceFromInsideOut, also the default, the one nearer the centre of all the labels goes first, and without it the one with fewer candidates in all. With PlaceMostConstrainedFirst off, the labels go from the centre outwards, or, with PlaceFromInsideOut off too, from left to right and then bottom to top. Labels that tie keep the order they were given in. How far a label stands from the centre, or how far along, is counted in whole steps of the point tolerance, so that two as far but for the last digits tie wherever the scene lies: of two labels on leaders 10.6 apart, kept to the sides facing each other, whose first places are one and the same, centred at (15, 5.3), the first given goes first and the second slides, at the origin as a million off it, where the last digits of the centre put the second first in a third of the places tried. Two lengths within rounding of half a step can still round to different steps.
  • Look-ahead — a label is tried at its candidates in turn until LookAheadCandidates of them, 3 unless set, are found clear, and takes the one that stands furthest from everything near it; of two as open, the one found first. Clearances up to the point tolerance of the options count as nought: at a tolerance of a thousandth, places 0.0004 and 0.0007 clear are as open, and at a ten-thousandth the second is the more open. Clearances that differ by no more than a thousandth of the tolerance are as open too, so which of two places as open a label takes does not follow where the scene lies: at a thousandth, places 0.5 and 0.50000001 clear go to the one tried first, at the origin as a million off it. The gap one side asks for beyond the gap of the other is not counted as room: a label 20 off above its leader and 300 below it, keeping clear of its own leader, stands 20 clear of it in the first row above and 300 in the first row below, and counted as measured, the side below, the one it is to keep further off, would win whenever places on both sides were weighed.
  • Touching — a place collides with a label already placed as it does with a block, when it comes within the point tolerance of it, and the final layout judges two labels the same way: two labels that touch collide wherever the scene lies. Judged on their boxes as they stood, they collided or not as the rounding of where the scene lay left the boxes overlapping or a hair apart: of the 400 labels of the benchmark Arrange.Dense, crowded a step apart, one went to candidate 57 of its list at the origin, touching a label already placed, and to candidate 61 moved 1 000 along X. It goes to 61 at both.
  • No clear place — a label with none of its candidates clear is left on the first of them, overlapping, for the second pass to try again. Looking for the place with the least overlap instead was measured worse: on 80 crowded labels over 16 seeds it brought the share of clean labels down from 32.3 % to 22.9 %, as stuck labels wandered into quiet regions and pushed out the labels placed well there.

Parameters of each ArrangeItem

Parameter Default Meaning
Box — The label's box, the rectangle that is moved
Leader — The segment the label belongs to; its midpoint is the origin of the candidate positions
Rectangle null The rectangle of the object, to stand along its long side, in place of the Leader; the gaps are measured from its long edges
Offset — Sets OffsetTop and OffsetBottom both at once; it can be set, not read
OffsetTop 50.0 The least gap between the label's edge and the leader on the side that faces up in the drawing; the left of a vertical leader; a finite number
OffsetBottom 50.0 The least gap on the side that faces down; the right of a vertical leader; a finite number
Side Both Which side of the leader the label may stand on: Both, Top (the left of a vertical leader) or Bottom
BlockPolygons empty Regions the label must not overlap
BlockLines empty Segments the label must not overlap; lifted in the second pass, and a label left across one is not Placed

The blocks of all the items are gathered into one set before any label is placed, so every label keeps clear of the blocks of every item, and a block given to many items is tested once.

Main Parameters of ArrangeOptions

Parameter Default Meaning
RowGap 20.0 Clearance between two consecutive rows of labels
PerpendicularLevels 3 Number of perpendicular fallback levels to test on each side
LongitudinalOvershootRatio 0.75 Ratio of label width allowed to overshoot beyond the two endpoints of the guide segment
MinimumBoxSize 10.0 Labels smaller than this size are ignored
MinimumMoveDistance 0.1 Translations smaller than this threshold are rounded to zero
NeighbourMargin 50.0 Expanded margin when filtering nearby obstacles
MaximumCandidates 10000 The most candidate positions a label has
PlaceMostConstrainedFirst true Place labels with fewer options first
PlaceFromInsideOut true Prioritize labels close to the area centroid
LookAheadCandidates 3 Number of free positions considered before selection
Tolerance Tolerance.Global Tolerance for geometric comparisons, clearances included; Tolerance.Global as it stands when the options are made, and only then

Default values are in millimeters, matching conventional structural drawings.

Along the long side of a rectangle

A part is a rectangle more often than a line: a beam or a plate seen from the side has a long side its labels are to stand along. Give the item that rectangle as Rectangle, a nullable GeoRectangle2, in place of the Leader:

var part = new GeoRectangle2(new GeoPoint2(1000.0, 500.0), 2000.0, 300.0);   // centre, width, height, angle

var item = new ArrangeItem
{
    Rectangle = part,                                                       // the leader is not needed
    Box       = new GeoRectangle2(part.Center, 100.0, 40.0),                // the label
    Offset    = 50.0,                                                       // the gap off a long edge
};

The candidate positions spread out from the centre of the rectangle, in rows on either side of its long side, each sliding along it, as they do from the midpoint of a leader. The gaps Offset, OffsetTop and OffsetBottom are measured from the long edges: with the rectangle above, the label 40 high and the gap 50, the first rows stand 220 from the middle line, 70 off the edge, one above the rectangle and one below it, and each next row a label's height and the row gap further, 270 and then more. The top is the long edge that faces up in the drawing, and the left of one whose long side stands upright; Side keeps the label to one of them. The long side is the width, or the height where that is greater, and the rectangle may be turned by any angle. A rectangle with no long side leaves the label where it is. Where both a rectangle and a leader are given, the rectangle is used. An item with no rectangle is arranged as it always was, to the bit.

Running inside AutoCAD

GeometryHelper.ArrangeAlgorithms.CadTest builds a DLL file to be loaded into AutoCAD:

dotnet build examples/GeometryHelper.ArrangeAlgorithms.CadTest/GeometryHelper.ArrangeAlgorithms.CadTest.csproj

The output is located at examples/GeometryHelper.ArrangeAlgorithms.CadTest/bin/Debug/net48/GeometryHelper.ArrangeAlgorithms.CadTest.dll. Load this file into AutoCAD using the NETLOAD command, then run the T1_Greedy command. Select LINE or LWPOLYLINE objects, and the plugin will draw the label box before and after arrangement, along with statistics.

The project compiles against three AutoCAD assemblies — accoremgd, acdbmgd, acmgd — committed under src/GeometryHelper.CadConvert/Lib and referenced from there by relative path, so no AutoCAD installation is needed to build. Loading the result still needs AutoCAD, which supplies those assemblies at run time.

Running inside Tekla Structures

GeometryHelper.ArrangeAlgorithms.TeklaTest is a console application that connects to the active Tekla Structures model and drawing to arrange reinforcement marks.

To build and run:

  1. Open Tekla Structures and open a drawing with some reinforcement marks and dimensions selected.
  2. Build the project:
    dotnet build examples/GeometryHelper.ArrangeAlgorithms.TeklaTest/GeometryHelper.ArrangeAlgorithms.TeklaTest.csproj
    
  3. Run the compiled executable:
    examples/GeometryHelper.ArrangeAlgorithms.TeklaTest/bin/Debug/net48/GeometryHelper.ArrangeAlgorithms.TeklaTest.exe
    

Licence

MIT.

See also