Programmatically connect a label to a polyline

Mar 13, 2012 at 8:20 PM
Edited Mar 14, 2012 at 11:24 AM

I can see how I would connect a polyline with two boxes. However how would I go about for linking a label object to a polyline (pretty much to a point in the middle of the line?).

 

So far I have something like this (it doesn't work :( ):

var line = (Polyline) _LineType.CreateInstance();
line.EndCapStyle = project.Design.CapStyles.Arrow;
 
line.Connect(ControlPointId.FirstVertex, shape1, ControlPointId.Reference);
line.Connect(ControlPointId.LastVertex, shape2, ControlPointId.Reference);
 
var label = (Label)_LabelType.CreateInstance();
label.Connect(10,line,ControlPointId.Reference);
Coordinator
Mar 15, 2012 at 8:48 AM
Edited Mar 15, 2012 at 8:48 AM

First of all, a few words about the behavior of labels:

When connected to other shapes, labels calculate the relative position of their "GluePoint". When the partner shape moves, the label will try to maintain this relative position. You can change the relative position of the label itself to its partner shape (distance and angle) by moving the connected label to the desired position.

So you have to...
- Move the label to the desired position
- Move the gluepoint to the desired position
- Connect the glue point and the reference point of the partner shape

Example:

var line = (Polyline)_LineType.CreateInstance();
line.EndCapStyle = project.Design.CapStyles.Arrow;
line.Connect(ControlPointId.FirstVertex, shape1, ControlPointId.Reference);
line.Connect(ControlPointId.LastVertex, shape2, ControlPointId.Reference);

// Calculate the coordinates of the line's middle

Point a = line.GetControlPointPosition(ControlPointId.FirstVertex);
Point
b = line.GetControlPointPosition(ControlPointId.LastVertex);
int
dX = b.X - a.X;
int
dY = b.Y - a.Y;

Point dstPos = Point.Empty;
dstPos.X = a.X + (int)Math.Round(dX * 0.5f);
dstPos.Y = a.Y + (int)Math.Round(dY * 0.5f);

// Create a Label shape and move the shape and its glue point to the desired position (the
// middle of the line in this case).
// Connect Label and Polyline afterwards

var label = (Label)_LabelType.CreateInstance();
label.MoveTo(dstPos.X, dstPos.Y);
label.MoveControlPointTo(10, dstPos.X, dstPos.Y, ResizeModifiers.None);
label.Connect(10, line, ControlPointId.Reference);

 

Best Regards,
Kurt

Aug 28, 2012 at 2:07 PM

I tried the above code by Kurt, it works perfectly. However, when the polyline has internal vertices, I noticed the code does not work. The label is created in some cases but is not glued to the line. Please, can I get further guidance on this.

Aug 28, 2012 at 2:10 PM

Is it possible to create a polyline label template? 

Coordinator
Aug 28, 2012 at 2:24 PM

I assume you mean that the label is always created but sometimes not connected to the line (correct me if I'm wrong).
Furthermore, I assume that the label's glue point is not on the line's outline when calling the Connect() method.

The code above is an example for a line with only 2 points and for the case that you want the label to be placed in the middle of the line. If you have additional points on your line, you have to calculate the middel of the line with a more sophisticated algorithm than the one above.
For testing, you can connect the label directly to one of the additional points.

If this is not the reason, can you please provide me with a small demo project where the problem is visible?
Upload it here or send it to to our support email address "support(at)dataweb(dot)de".