void Interaction_GluingShapes(NGlueShapesEventArgs args) { // safely get the ports collection NPortCollection ports = (NPortCollection)args.Shape2D.GetChild(NShape.PortsChild, false); if (ports == null && ports.Count == 0) { return; } // get the anchor point in page coordinates NPoint anchorInPage = args.ConnectBegin ? args.Shape1D.GetEndPointInPage() : args.Shape1D.GetBeginPointInPage(); // get the nearest port NPort neartestPort = ports[0]; double neartestDistance = NGeometry2D.PointsDistance(anchorInPage, neartestPort.GetLocationInPage()); for (int i = 1; i < ports.Count; i++) { NPort curPort = ports[i]; double curDistance = NGeometry2D.PointsDistance(anchorInPage, curPort.GetLocationInPage()); if (curDistance < neartestDistance) { neartestDistance = curDistance; neartestPort = curPort; } } // connect begin or end if (args.ConnectBegin) { args.Shape1D.GlueBeginToPort(neartestPort); } else { args.Shape1D.GlueEndToPort(neartestPort); } // cancel the event so that the diagram does not perform default connection args.Cancel = true; }
/// <summary> /// Creates a custom shape that is essentially a group consisting of three other shapes each with different filling. /// You need to use groups to have shapes that mix different fill, or stroke styles. /// </summary> /// <returns></returns> protected NShape CreateCoffeeCupShape() { // create the points and paths from which the shape consits NPoint[] cupPoints = new NPoint[] { new NPoint(45, 268), new NPoint(63, 331), new NPoint(121, 331), new NPoint(140, 268) }; NGraphicsPath handleGraphicsPath = new NGraphicsPath(); handleGraphicsPath.AddClosedCurve(new NPoint[] { new NPoint(175, 295), new NPoint(171, 278), new NPoint(140, 283), new NPoint(170, 290), new NPoint(128, 323) }, 1); NGraphicsPath steamGraphicsPath = new NGraphicsPath(); steamGraphicsPath.AddCubicBeziers(new NPoint[] { new NPoint(92, 270), new NPoint(53, 163), new NPoint(145, 160), new NPoint(86, 50), new NPoint(138, 194), new NPoint(45, 145), new NPoint(92, 270) }); steamGraphicsPath.CloseFigure(); // calculate some bounds NRectangle handleBounds = handleGraphicsPath.ExactBounds; NRectangle cupBounds = NGeometry2D.GetBounds(cupPoints); NRectangle steamBounds = steamGraphicsPath.ExactBounds; NRectangle geometryBounds = NRectangle.Union(cupBounds, handleBounds, steamBounds); // normalize the points and paths by transforming them to relative coordinates NRectangle normalRect = new NRectangle(0, 0, 1, 1); NMatrix transform = NMatrix.CreateBoundsStretchMatrix(geometryBounds, normalRect); transform.TransformPoints(cupPoints); handleGraphicsPath.Transform(transform); steamGraphicsPath.Transform(transform); // create the cup shape NDrawPolygon cupPolygon = new NDrawPolygon(normalRect, cupPoints); cupPolygon.Relative = true; NShape cupShape = new NShape(); cupShape.Init2DShape(); cupShape.Geometry.Fill = new NColorFill(NColor.Brown); cupShape.Geometry.Add(cupPolygon); cupShape.SetBounds(geometryBounds); // create the cup handle NDrawPath handlePath = new NDrawPath(normalRect, handleGraphicsPath); handlePath.Relative = true; NShape handleShape = new NShape(); handleShape.Init2DShape(); handleShape.Geometry.Fill = new NColorFill(NColor.LightSalmon); handleShape.Geometry.Add(handlePath); handleShape.SetBounds(geometryBounds); // create the steam NDrawPath steamPath = new NDrawPath(steamGraphicsPath.Bounds, steamGraphicsPath); steamPath.Relative = true; NShape steamShape = new NShape(); steamShape.Init2DShape(); steamShape.Geometry.Fill = new NColorFill(new NColor(50, 122, 122, 122)); steamShape.Geometry.Add(steamPath); steamShape.SetBounds(geometryBounds); // group the shapes as a single group NGroup group; NBatchGroup batch = new NBatchGroup(m_DrawingDocument); batch.Build(cupShape, handleShape, steamShape); batch.Group(null, out group); // alter some properties of the group group.SelectionMode = ENGroupSelectionMode.GroupOnly; group.SnapToShapes = false; return(group); }