예제 #1
0
        /// <summary>
        /// Adds an object instance to the internal object list.
        /// </summary>
        /// <param name="obj"><code>DiagramObject</code> instance</param>
        public void AddDiagramObject(DiagramObject obj)
        {
            Log.LogMessageDebug($"Object placed: {obj}");
            if (ErrorLogger.AllowLogging)
            {
                var parameters = obj.GetInternalParameters();
                foreach (string key in parameters.Keys)
                {
                    Log.LogMessageDebug($"{key}: {parameters[key]}");
                }
            }

            DiagramObjects.Add(obj);
        }
예제 #2
0
        public override void Draw(Diagram diagram)
        {
            float usedX1 = X1, usedY1 = Y1;
            float usedX2 = X2, usedY2 = Y2;

            if (AnchorID1 != -1)
            {
                DiagramObject obj = Program.DForm.CurrentDiagram.GetDiagramObject(AnchorID1);
                if (obj != null)
                {
                    if (obj.ObjectID == ObjectTypeID.Atom)
                    {
                        usedX1 = ((Atom)obj).X;
                        usedY1 = ((Atom)obj).Y;
                    }
                }
            }

            if (AnchorID2 != -1)
            {
                DiagramObject obj = Program.DForm.CurrentDiagram.GetDiagramObject(AnchorID2);
                if (obj != null)
                {
                    if (obj.ObjectID == ObjectTypeID.Atom)
                    {
                        usedX2 = ((Atom)obj).X;
                        usedY2 = ((Atom)obj).Y;
                    }
                }
            }

            if (AnchorID1 != -1 || AnchorID2 != -1)
            {
                float off1 = 0, off2 = 0;
                if (AnchorID1 != -1)
                {
                    DiagramObject obj = Program.DForm.CurrentDiagram.GetDiagramObject(AnchorID1);
                    if (obj != null)
                    {
                        if (obj.ObjectID == ObjectTypeID.Atom)
                        {
                            off1 = ((Atom)obj).FontSize * 0.9f;
                        }
                    }
                }

                if (AnchorID2 != -1)
                {
                    DiagramObject obj = Program.DForm.CurrentDiagram.GetDiagramObject(AnchorID2);
                    if (obj != null)
                    {
                        if (obj.ObjectID == ObjectTypeID.Atom)
                        {
                            off2 = ((Atom)obj).FontSize * 0.9f;
                        }
                    }
                }

                double ang1 = Math.Atan2(usedY2 - usedY1, usedX2 - usedX1);
                double ang2 = ang1 - Math.PI;

                usedX1 += (float)Math.Cos(ang1) * off1;
                usedY1 += (float)Math.Sin(ang1) * off1;
                usedX2 += (float)Math.Cos(ang2) * off2;
                usedY2 += (float)Math.Sin(ang2) * off2;
            }

            SKPaint paint = new SKPaint
            {
                Color       = Colour,
                IsAntialias = true,
                StrokeWidth = Thickness,
                StrokeCap   = SKStrokeCap.Round,
                Style       = SKPaintStyle.Stroke
            };

            SKPath path = new SKPath();

            double angle = Math.Atan2(usedY2 - usedY1, usedX2 - usedX1);

            //if (angle < 0d) angle += Math.PI * 2;

            double a1 = angle + (Math.PI / 2);
            double a2 = angle - (Math.PI / 2);

            float dPointX1 = usedX1 + (float)((Thickness * 2) * Math.Cos(a1));
            float dPointY1 = usedY1 + (float)((Thickness * 2) * Math.Sin(a1));
            float dPointX2 = usedX2 + (float)((Thickness * 2) * Math.Cos(a1));
            float dPointY2 = usedY2 + (float)((Thickness * 2) * Math.Sin(a1));

            float dPointX3 = usedX1 + (float)((Thickness * 2) * Math.Cos(a2));
            float dPointY3 = usedY1 + (float)((Thickness * 2) * Math.Sin(a2));
            float dPointX4 = usedX2 + (float)((Thickness * 2) * Math.Cos(a2));
            float dPointY4 = usedY2 + (float)((Thickness * 2) * Math.Sin(a2));

            if (TypeOfBond != BondType.Single)
            {
                Log.LogMessageGeneral($"({usedX1}, {usedY1}) | ({usedX2}, {usedY2}) || ({dPointX1}, {dPointY1}) | ({dPointX2}, {dPointY2}) | ({dPointX3}, {dPointY3}) | ({dPointX4}, {dPointY4})");
            }

            if (BondSubtype == BondStyle.Wedged || BondSubtype == BondStyle.Dashed)
            {
                SKPoint[] points = new SKPoint[]
                {
                    new SKPoint()
                    {
                        X = usedX1, Y = usedY1
                    },
                    new SKPoint()
                    {
                        X = dPointX3, Y = dPointY3
                    },
                    new SKPoint()
                    {
                        X = dPointX4, Y = dPointY4
                    }
                };

                switch (BondSubtype)
                {
                case BondStyle.Wedged:
                    paint.Style = SKPaintStyle.StrokeAndFill;
                    break;

                case BondStyle.Dashed:
                    paint.Style      = SKPaintStyle.StrokeAndFill;
                    paint.PathEffect = SKPathEffect.CreateDash(new float[] { Thickness * 3, Thickness * 2 }, 0);
                    break;

                default:
                    break;
                }

                diagram.DiagramSurface.Canvas.DrawPoints(SKPointMode.Polygon, points, paint);
            }
            else
            {
                switch (TypeOfBond)
                {
                case BondType.Single:
                    path.MoveTo(usedX1, usedY1);
                    path.LineTo(usedX2, usedY2);
                    break;

                case BondType.Double:
                    path.MoveTo(dPointX1, dPointY1);
                    path.LineTo(dPointX2, dPointY2);

                    SKPath path2 = new SKPath();
                    path2.MoveTo(dPointX3, dPointY3);
                    path2.LineTo(dPointX4, dPointY4);

                    diagram.DiagramSurface.Canvas.DrawPath(path2, paint);
                    break;

                case BondType.Triple:
                    path.MoveTo(usedX1, usedY1);
                    path.LineTo(usedX2, usedY2);

                    SKPath p2 = new SKPath(), p3 = new SKPath();
                    p2.MoveTo(dPointX1, dPointY1);
                    p2.LineTo(dPointX2, dPointY2);

                    p3.MoveTo(dPointX3, dPointY3);
                    p3.LineTo(dPointX4, dPointY4);

                    diagram.DiagramSurface.Canvas.DrawPath(p2, paint);
                    diagram.DiagramSurface.Canvas.DrawPath(p3, paint);
                    break;

                case BondType.Aromatic:
                    SKPaint paintDash = new SKPaint
                    {
                        Color       = Colour,
                        IsAntialias = true,
                        StrokeWidth = Thickness,
                        StrokeCap   = SKStrokeCap.Round,
                        PathEffect  = SKPathEffect.CreateDash(new float[] { Thickness * 3, Thickness * 2 }, 0),
                        Style       = SKPaintStyle.Stroke
                    };

                    SKPath pth2 = new SKPath();
                    pth2.MoveTo(dPointX3, dPointY3);
                    pth2.LineTo(dPointX4, dPointY4);

                    diagram.DiagramSurface.Canvas.DrawPath(pth2, paintDash);

                    path.MoveTo(dPointX1, dPointY1);
                    path.LineTo(dPointX2, dPointY2);
                    break;

                default:
                    break;
                }

                //path.Close();
                diagram.DiagramSurface.Canvas.DrawPath(path, paint);
            }
            paint.Dispose();
        }