lixiaojun
2025-04-07 33efd807b5f5aac98ce736ccec002e190e5dd175
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
namespace Yw.WinFrmUI.HydroL2d
{
    /// <summary>
    /// 阀门
    /// </summary>
    public class Valve : 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; }
 
        /// <summary>
        /// 图片
        /// </summary>
        public Image Image { get; set; }
 
        /// <summary>
        /// 宽度
        /// </summary>
        public float? Width { get; set; }
 
        /// <summary>
        /// 高度
        /// </summary>
        public float? Height { get; set; }
 
        /// <summary>
        /// 悬停图片
        /// </summary>
        public Image HoveredImage { get; set; }
 
        /// <summary>
        /// 悬停宽度
        /// </summary>
        public float? HoveredWidth { get; set; }
 
        /// <summary>
        /// 悬停高度
        /// </summary>
        public float? HoveredHeight { get; set; }
 
        /// <summary>
        /// 选择图片
        /// </summary>
        public Image SelectedImage { get; set; }
 
        /// <summary>
        /// 选择宽度
        /// </summary>
        public float? SelectedWidth { get; set; }
 
        /// <summary>
        /// 选择高度
        /// </summary>
        public float? SelectedHeight { get; set; }
 
        private Coordinate _coordinate { get; set; }//坐标信息
 
        //获取坐标位置
        private Coordinate GetCoordinate(Graphics g)
        {
            var ws = this.Width.HasValue ? this.Width.Value : CacheHelper.HydroL2d.Valve.Size.Width;
            var hs = this.Height.HasValue ? this.Height.Value : CacheHelper.HydroL2d.Valve.Size.Height;
            var wz = ws / g.PageScale;
            var hz = hs / 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 * hz);
            return Coordinate.GetCoordinate(this.StartPosition, angle, size, StringAlignment.Near, StringAlignment.Center);
        }
 
        /// <summary>
        /// 绘制
        /// </summary>
        public override void Draw(Graphics g)
        {
            _coordinate = GetCoordinate(g);
 
            #region 画线
 
            if (this.Hovered)
            {
                var fromCachePen = true;
                var pen = CacheHelper.ValveHoveredLinePen;
                if (this.HoveredLineColor.HasValue && this.HoveredLineWidth.HasValue)
                {
                    pen = new Pen(this.HoveredLineColor.Value, this.HoveredLineWidth.Value);
                    fromCachePen = false;
                }
                var penWidth = pen.Width;
                pen.Width = pen.Width / g.PageScale;
                g.DrawLine(pen, this.StartPosition, this.EndPosition);
                pen.Width = penWidth;
                if (!fromCachePen)
                {
                    pen.Dispose();
                }
            }
            else if (this.Selected)
            {
                var fromCachePen = true;
                var pen = CacheHelper.ValveSelectedLinePen;
                if (this.SelectedLineColor.HasValue && this.SelectedLineWidth.HasValue)
                {
                    pen = new Pen(this.SelectedLineColor.Value, this.SelectedLineWidth.Value);
                    fromCachePen = false;
                }
                var penWidth = pen.Width;
                pen.Width = pen.Width / g.PageScale;
                g.DrawLine(pen, this.StartPosition, this.EndPosition);
                pen.Width = penWidth;
                if (!fromCachePen)
                {
                    pen.Dispose();
                }
            }
            else
            {
                var fromCachePen = true;
                var pen = CacheHelper.ValveLinePen;
                if (this.LineColor.HasValue && this.LineWidth.HasValue)
                {
                    pen = new Pen(this.LineColor.Value, this.LineWidth.Value);
                    fromCachePen = false;
                }
                var penWidth = pen.Width;
                pen.Width = pen.Width / g.PageScale;
                g.DrawLine(pen, this.StartPosition, this.EndPosition);
                pen.Width = penWidth;
                if (!fromCachePen)
                {
                    pen.Dispose();
                }
            }
 
 
 
            #endregion
 
            #region 画图片
 
            if (this.Hovered)
            {
                var img = CacheHelper.ValveHoveredImage;
                if (this.HoveredImage != null)
                {
                    img = this.HoveredImage;
                }
                var width = this.HoveredWidth.HasValue ? this.HoveredWidth.Value : CacheHelper.HydroL2d.Valve.HoveredSize.Width;
                var height = this.HoveredHeight.HasValue ? this.HoveredHeight.Value : CacheHelper.HydroL2d.Valve.HoveredSize.Height;
                var isNewImage = false;
                if (img.Width != (int)width || img.Height != (int)height)
                {
                    img = img.CloneC(width, height);
                    isNewImage = true;
                }
 
                var pt = this.StartPosition.GetCenter(this.EndPosition);
                var dx = width / g.PageScale / 2f;
                var dy = height / g.PageScale / 2f;
                var p0 = new PointF(pt.X - dx, pt.Y - dy);
 
                g.DrawImage(img, p0);
 
                if (isNewImage)
                {
                    img.Dispose();
                }
            }
            else if (this.Selected)
            {
                var img = CacheHelper.ValveSelectedImage;
                if (this.SelectedImage != null)
                {
                    img = this.SelectedImage;
                }
                var width = this.SelectedWidth.HasValue ? this.SelectedWidth.Value : CacheHelper.HydroL2d.Valve.SelectedSize.Width;
                var height = this.SelectedHeight.HasValue ? this.SelectedHeight.Value : CacheHelper.HydroL2d.Valve.SelectedSize.Height;
                var isNewImage = false;
                if (img.Width != (int)width || img.Height != (int)height)
                {
                    img = img.CloneC(width, height);
                    isNewImage = true;
                }
 
                var pt = this.StartPosition.GetCenter(this.EndPosition);
                var dx = width / g.PageScale / 2f;
                var dy = height / g.PageScale / 2f;
                var p0 = new PointF(pt.X - dx, pt.Y - dy);
 
                g.DrawImage(img, p0);
 
                if (isNewImage)
                {
                    img.Dispose();
                }
            }
            else
            {
                var img = CacheHelper.ValveImage;
                if (this.Image != null)
                {
                    img = this.Image;
                }
                var width = this.Width.HasValue ? this.Width.Value : CacheHelper.HydroL2d.Valve.Size.Width;
                var height = this.Height.HasValue ? this.Height.Value : CacheHelper.HydroL2d.Valve.Size.Height;
                var isNewImage = false;
                if (img.Width != (int)width || img.Height != (int)height)
                {
                    img = img.CloneC(width, height);
                    isNewImage = true;
                }
 
                var pt = this.StartPosition.GetCenter(this.EndPosition);
                var dx = width / g.PageScale / 2f;
                var dy = height / g.PageScale / 2f;
                var p0 = new PointF(pt.X - dx, pt.Y - dy);
 
                g.DrawImage(img, p0);
 
                if (isNewImage)
                {
                    img.Dispose();
                }
            }
 
 
 
            #endregion
 
        }
 
 
        /// <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;
 
        }
 
    }
}