This project has moved and is read-only. For the latest updates, please go here.

Connect Two Picture Shapes

Oct 16, 2013 at 1:04 PM
Edited Oct 16, 2013 at 1:11 PM
Hi there,

I'm trying to connect two picture shapes with a polyline. I'm doing this:
var cp1 = (ControlPointId) startShape.GetControlPointIds(ControlPointCapabilities.Glue).FirstOrDefault();
var cp2 = (ControlPointId)  endShape.GetControlPointIds(ControlPointCapabilities.Glue).FirstOrDefault();

line.Connect(ControlPointId.FirstVertex, startShape, cp1);
line.Connect(ControlPointId.LastVertex, endShape, cp2);
But whenever I do this, I always get an error:
Neither Picture's point -1 nor Picture's point 0 is a glue point. At least one glue point is required for a connection between shapes.
Upon investigating a little further, I noticed that both cp1 and cp2 values are 0 and seems that they have no Glue Control Points.

Am I doing something wrong or picture shapes indeed have no glue points at all?

Best Regards,
Oct 16, 2013 at 4:15 PM
NShape distinguishes between two types of connection points:
  • Active connection points (Glue Points), that stick to a shape/point and follow it.
  • Passive connection points serving as connection target for the glue points.
Typically, shapes have only connection points while lines have two glue points (start and end point).
The Label shape is the only standard shape that has a glue point (you can pin a label shape to other shapes so it will follow them).

In your code, you have to connect the glue points of the line shapes with (passive) connection points of the picture shapes.
var cp1 = (ControlPointId) startShape.GetControlPointIds(__ControlPointCapabilities.Connect__).FirstOrDefault();
var cp2 = (ControlPointId)  endShape.GetControlPointIds(__ControlPointCapabilities.Connect__).FirstOrDefault();

line.Connect(ControlPointId.FirstVertex, startShape, cp1);
line.Connect(ControlPointId.LastVertex, endShape, cp2);
If you do not connect to ControlPointId.Reference (Point-To-Shape connection) but to a 'normal' connection point (Point-to-Point connection), you have to move the line's glue point to the correct position:
Point p = Point.Empty;
p = startShape.GetControlPointPosition(cp1);
line.MoveControlPointTo(ControlPointId.FirstVertex, p.X, p.Y, ResizeModifiers.None);
line.Connect(ControlPointId.FirstVertex, startShape, cp1);

p = endShape.GetControlPointPosition(cp2);
line.MoveControlPointTo(ControlPointId.LastVertex, p.X, p.Y, ResizeModifiers.None);
line.Connect(ControlPointId.LastVertex, endShape, cp2);
Marked as answer by rafaborges on 10/16/2013 at 10:38 AM
Oct 16, 2013 at 6:38 PM
Edited Oct 16, 2013 at 6:42 PM
Amazing! It worked like a charm! Thanks a lot!

Just a very short question: it is possible to "snap" the ControlPointId.LastVertex to the current mouse position while keeping the ControlPointId.FirstVertex anchored on the same position? I see ways to move everything alltoghether, but not a single ControlPoint...

Once again, thanks!

Best Regards,
Oct 16, 2013 at 7:10 PM
Actually was very simple! I didn't find at first the MoveControlPointTo() method...
private void canvas_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
                _currentLine.MoveControlPointTo(ControlPointId.LastVertex, e.X, e.Y, ResizeModifiers.MaintainAspect);