yangyin
2024-08-20 98e49c0dd42840a094837f7acae532bc237a719a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
namespace Yw.WinFrmUI.HydroL2d
{
    /// <summary>
    /// 管道
    /// </summary>
    public class Pipe : Link
    {
        /// <summary>
        /// 线色
        /// </summary>
        public Color? LineColor { get; set; }
 
        /// <summary>
        /// 线宽
        /// </summary>
        public float? LineWidth { get; set; }
 
        /// <summary>
        /// 悬停线色
        /// </summary>
        public Color? HoveredLineColor { get; set; }
 
        /// <summary>
        /// 悬停线宽
        /// </summary>
        public float? HoveredLineWidth { get; set; }
 
        /// <summary>
        /// 选择线色
        /// </summary>
        public Color? SelectedLineColor { get; set; }
 
        /// <summary>
        /// 选择线宽
        /// </summary>
        public float? SelectedLineWidth { get; set; }
 
        private Coordinate _coordinate { get; set; }//坐标信息
 
        //获取坐标位置
        private Coordinate GetCoordinate(Graphics g)
        {
            var ws = this.LineWidth.HasValue ? this.LineWidth.Value : CacheHelper.HydroL2d.Pipe.Line.Width;
            var wz = ws / g.PageScale;
            var angle = (float)this.StartPosition.AngleClockWise(this.EndPosition);
            var xp = this.StartPosition.GetXPoint(this.EndPosition);
            var size = new SizeF(xp.X - this.StartPosition.X, 2 * wz);
            return Coordinate.GetCoordinate(this.StartPosition, angle, size, StringAlignment.Near, StringAlignment.Center);
        }
 
        /// <summary>
        /// 绘制
        /// </summary>
        public override void Draw(Graphics g)
        {
            _coordinate = GetCoordinate(g);
 
            if (this.Hovered)
            {
                var fromCachePen = true;
                var pen = CacheHelper.PipeHoveredLinePen;
                if (this.HoveredLineColor.HasValue && this.HoveredLineWidth.HasValue)
                {
                    pen = new Pen(this.HoveredLineColor.Value, this.HoveredLineWidth.Value);
                    fromCachePen = false;
                }
                var penWidth = pen.Width;
                pen.Width = penWidth / g.PageScale;
                g.DrawLines(pen, this.Positions.ToArray());
                pen.Width = penWidth;
                if (!fromCachePen)
                {
                    pen.Dispose();
                }
            }
            else if (this.Selected)
            {
                var fromCachePen = true;
                var pen = CacheHelper.PipeSelectedLinePen;
                if (this.SelectedLineColor.HasValue && this.SelectedLineWidth.HasValue)
                {
                    pen = new Pen(this.SelectedLineColor.Value, this.SelectedLineWidth.Value);
                    fromCachePen = false;
                }
                var penWidth = pen.Width;
                pen.Width = penWidth / g.PageScale;
                g.DrawLines(pen, this.Positions.ToArray());
                pen.Width = penWidth;
                if (!fromCachePen)
                {
                    pen.Dispose();
                }
            }
            else
            {
                var fromCachePen = true;
                var pen = CacheHelper.PipeLinePen;
                if (this.LineColor.HasValue && this.LineWidth.HasValue)
                {
                    pen = new Pen(this.LineColor.Value, this.LineWidth.Value);
                    fromCachePen = false;
                }
                var penWidth = pen.Width;
                pen.Width = penWidth / g.PageScale;
                g.DrawLines(pen, this.Positions.ToArray());
                pen.Width = penWidth;
                if (!fromCachePen)
                {
                    pen.Dispose();
                }
            }
        }
 
        /// <summary>
        /// 包含
        /// </summary>
        public override bool Contains(PointF pt)
        {
            if (_coordinate == null)
            {
                return false;
            }
            var pa = _coordinate.Origin.RotatePointF(pt, _coordinate.Angle, false);
            return _coordinate.BeforeRectf.Contains(pa);
        }
 
        /// <summary>
        /// 相交
        /// </summary>
        public override bool Intersect(RectangleF rectf)
        {
            var positions = this.Positions;
            foreach (var position in positions)
            {
                if (rectf.Contains(position))
                {
                    return true;
                }
            }
            return false;
 
        }
 
 
    }
}