Exemplo n.º 1
0
        private static PortConstraint CreatePortConstraintFromSketch(IEdge e, bool source, bool strong)
        {
            //Get connection port and owner
            IPort port      = source ? e.SourcePort : e.TargetPort;
            INode portOwner = port.Owner as INode;

            if (portOwner != null)
            {
                //Einfachste Loesung:
                //Erzeugt einen strong PortConstraint genau an der port location
                //anschluesse in alle richtungen moeglich
                //        return PortConstraint.create(PortConstraint.ANY_SIDE, strong);
                //alternativ: z.B. Kantenpfad bestimmen und einen PortConstraint
                //erzeugen, dessen Richtung durch den Schnittpunkt zwischen Pfad und Knotenrand gegeben ist.
                //hier nur geradlinige Verbindung zwischen Bends
                PointD portLocation = port.GetLocation();
                PointD seg          = new PointD();
                var    bends        = e.Bends;
                if (bends.Count == 0)
                {
                    // no bends, instead take the endpoint
                    seg = source ? e.TargetPort.GetLocation() : e.SourcePort.GetLocation();
                }
                else
                {
                    IPoint p1 = bends[0].Location;
                    IPoint p2 = bends[bends.Count - 1].Location;
                    seg = source ? new PointD(p1) : new PointD(p2);
                }
                // Some offset for ports, which lie exactly on the border
                RectD enlarged = portOwner.Layout.ToRectD().GetEnlarged(5);

                var generalPath = new GeneralPath(2);
                generalPath.MoveTo(portLocation);
                generalPath.LineTo(seg);

                if (generalPath.FindLineIntersection(enlarged.TopLeft, enlarged.TopRight) < 1)
                {
                    //Erstes Segment verlaesst den Knoten auf der Nordseite
                    //Die tatsaechliche Position des Constraints ergibt sich aus dem Startpunkt der Kante, muss
                    //hier also nicht noch mal angegeben werden, dafuer aber, dass es sich wirklich um einen STRONG constraint
                    //handelt.
                    return(PortConstraint.Create(PortSide.North, strong));
                }
                if (generalPath.FindLineIntersection(enlarged.TopLeft, enlarged.BottomLeft) < 1)
                {
                    //first segment leaves at west...
                    return(PortConstraint.Create(PortSide.West, strong));
                }
                if (generalPath.FindLineIntersection(enlarged.TopRight, enlarged.BottomRight) < 1)
                {
                    //first segment leaves at east...
                    return(PortConstraint.Create(PortSide.East, strong));
                }
                if (generalPath.FindLineIntersection(enlarged.BottomLeft, enlarged.BottomRight) < 1)
                {
                    //first segment leaves at south...
                    return(PortConstraint.Create(PortSide.South, strong));
                }
                //keine intersection mit dem ersten segment, hier waehlen wir den einfachen Weg...
                return(PortConstraint.Create(PortSide.Any, strong));
            }
            return(null);
        }