duheng
2024-12-11 2e8e85f65748ee5a5adf0dcda864aa4167cbf2ef
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
using DevExpress.XtraEditors;
using System.Data;
 
namespace Yw.WinFrmUI.Phart
{
    public partial class PumpPictureBoxCtrl : DevExpress.XtraEditors.XtraUserControl
    {
        public PumpPictureBoxCtrl()
        {
            InitializeComponent();
 
            this.gridView1.SetDefaultEditView();
            this.colDelete.OptionsColumn.AllowEdit = false;
            this.colDelete.OptionsColumn.ReadOnly = false;
            this.gridView1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
        }
 
        #region 页面类
        protected class CurrentViewModel
        {
            public CurrentViewModel() { }
 
            public CurrentViewModel(Yw.Geometry.Point2d rhs)
            {
                this.X = rhs.X;
                this.Y = rhs.Y;
            }
            public CurrentViewModel(double x, double y, int xp, int yp)
            {
                this.X = x;
                this.Y = y;
                this.Xpic = xp;
                this.Ypic = yp;
            }
            public double X { get; set; }
            public double Y { get; set; }
            public int Xpic { get; set; }
            public int Ypic { get; set; }
        }
        #endregion
 
        #region 变量
 
        /// <summary>
        /// 有效范围
        /// </summary>
        private Rectangle? _rect;//矩形
 
        /// <summary>
        /// 是否正在获取等效线
        /// </summary>
        private bool _isCaptureEuqualCurve = false;
 
        /// <summary>
        /// 是否打点等效线
        /// </summary>
        private bool _isCaputueEtaPtInEqupCurve = false;
 
        /// <summary>
        /// 是否打点等效线,产生流量扬程(原水,五号沟泵站金海支线 是这个情况)
        /// </summary>
        public bool IsCaputueEtaPtInEqupCurve
        {
            set
            {
                _isCaputueEtaPtInEqupCurve = value;
            }
        }
 
        /// <summary>
        /// 捕捉曲线类型
        /// </summary>
        private Yw.Ahart.eCurveType _currentCaptureCruve = Yw.Ahart.eCurveType.QH;
 
        /// <summary>
        /// 当前捕捉曲线类型
        /// </summary>
        public Yw.Ahart.eCurveType CurrentCaptureCruve
        {
            get => _currentCaptureCruve;
            set => _currentCaptureCruve = value;
        }
 
 
 
        /// <summary>
        /// 流量单位  (默认m3/h)
        /// </summary>
        private Unit.eUnitQ _unitQ = Unit.eUnitQ.M3H;
 
        /// <summary>
        /// 流量单位 (默认m3/h)
        /// </summary>
        public Unit.eUnitQ UnitQ
        {
            get => _unitQ;
            set => _unitQ = value;
        }
 
 
        private bool _isSelBoundary;
 
        /// <summary>
        /// 允许设置有效范围
        /// </summary>
        public bool IsSelBoundary
        {
            get
            {
                return _isSelBoundary;
            }
            set
            {
                _isSelBoundary = value;
 
            }
        }
 
        private bool _isPickPoint;
 
        /// <summary>
        /// 允许获取点
        /// </summary>
        public bool IsPickPoint
        {
            get
            {
                return _isPickPoint;
            }
            set
            {
                _isPickPoint = value;
            }
        }
 
 
        #endregion
 
        #region 事件
 
        /// <summary>
        /// 刷新曲线点事件
        /// </summary>
        public event Action<List<Yw.Geometry.Point2d>> ReloadPickPointsEvent;
 
        /// <summary>
        /// 刷新框选范围事件
        /// </summary>
        public event Action<Rectangle?> ReloadBoundaryEvent;
 
        #endregion
 
        // 曲线图表坐标
        public double ChartMinX = 0, ChartMaxX = 0;
        public double ChartMinY = 0, ChartMaxY = 0;
 
        /// <summary>
        /// 绑定数据列表
        /// </summary>
        private List<CurrentViewModel> _allClickPoints;
 
        // <summary>
        /// 绑定数据
        /// </summary> 
        public void SetBindingData(string path)
        {
            if (!System.IO.File.Exists(path))
                return;
            this.pictureBox1.Image = Image.FromFile(path);
 
            _rect = null;
            _allClickPoints = new List<CurrentViewModel>();
            this.bindingSource1.DataSource = _allClickPoints;
        }
 
        /// <summary>
        /// 设置图片显示方式
        /// </summary> 
        public void SetPictureSizeMode(PictureBoxSizeMode sizeMode)
        {
            this.pictureBox1.SizeMode = sizeMode;
        }
 
 
        #region 选取点并绘制曲线 
 
        /// <summary>
        /// 开始获取点
        /// </summary>
        public void StartPickPoint()
        {
            _isPickPoint = true;
        }
 
        /// <summary>
        /// 结束选取点
        /// </summary>
        public void EndPickPoint()
        {
            _isPickPoint = false;
        }
 
        /// <summary>
        /// 清除选取点
        /// </summary>
        public void ClearPickPoint()
        {
            if (XtraMessageBox.Show("是否清空点?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) != DialogResult.OK)
                return;
            _allClickPoints.Clear();
            ReloadPickPoints();
            DrawPictureShape();
        }
 
        /// <summary>
        /// 刷新界面
        /// </summary>
        public void RefreshPictureShape(List<Yw.Geometry.Point2d> points = null, Rectangle? picRect = null)
        {
            _rect = picRect;
            if (_rect == null)
            {
                this.pictureBox1.Invalidate();
                return;
            }
            var rect = _rect.Value;
            _allClickPoints.Clear();
            if (points != null && points.Any())
                foreach (var item in points)
                    _allClickPoints.Add(new CurrentViewModel(item));
            ReloadPickPoints();
            foreach (var row in _allClickPoints)
            {
                double ptX = row.X;
                double ptY = row.Y;
                int rX = 0, rY = 0;
                if (Math.Abs(ptX - ChartMinX) < 0.01)
                {
                    rX = rect.Left;//防止分母为0
                }
                else
                {
                    rX = (int)(rect.Left + (rect.Right - rect.Left) / (ChartMaxX - ChartMinX) * (ptX - ChartMinX));
                }
                if (Math.Abs(ptY - ChartMaxY) < 0.01)
                {
                    rY = rect.Top; //防止分母为0
                }
                else
                {
                    rY = (int)(rect.Top - (rect.Bottom - rect.Top) / (ChartMaxY - ChartMinY) * (ptY - ChartMaxY));
                }
                row.Xpic = rX;
                row.Ypic = rY;
            };
            DrawPictureShape();
        }
        #endregion
 
        #region Draw Picture
 
        /// <summary>
        /// 绘图
        /// </summary>
        public void DrawPictureShape()
        {
            try
            {
                var list = _allClickPoints;
                this.pictureBox1.Refresh();
                var picPoints = new List<Yw.Geometry.Point2d>();
                using (Graphics g = this.pictureBox1.CreateGraphics())
                {
                    //绘制边框
                    if (_rect != null)
                    {
                        using (Pen pen = new Pen(Color.Red, 3))
                        {
                            g.DrawRectangle(pen, _rect.Value);
                        }
                    }
 
                    //绘制点
                    if (list != null && list.Any())
                    {
                        var num = 5;
                        foreach (var row in list)
                        {
                            g.DrawLine(new Pen(Color.Black, 3), row.Xpic - num, row.Ypic + num, row.Xpic + num, row.Ypic - num);
                            g.DrawLine(new Pen(Color.Black, 3), row.Xpic - num, row.Ypic - num, row.Xpic + num, row.Ypic + num);
                            picPoints.Add(new Yw.Geometry.Point2d(row.Xpic, row.Ypic));
                        }
 
                        //绘制线
                        if (list.Count >= 4 && !_isCaptureEuqualCurve)
                        {
                            //if (_currentCaptureCruveFitType == AStation.Curve.eFitType.ThroughPoint)
                            //    DrawThrountPoint(picPoints, 0.5f);
                            //else
                            DrawFitPoint(picPoints, 0.5f);
                        }
                    }
                }
            }
            catch (Exception)
            {
 
                throw;
            }
        }
 
        /// <summary>
        /// 画点和线(拟合)
        /// </summary>
        /// <param name="picPoints">图片点</param>
        /// <param name="tension"></param>
        private void DrawFitPoint(List<Yw.Geometry.Point2d> picPoints, float tension)
        {
            if (picPoints == null || picPoints.Count < 4)
                return;
            var fitPoints = picPoints.GetFitPointList();
            if (fitPoints == null || fitPoints.Count < 4)
                return;
            var resultPts = new List<Point>();
            fitPoints.ForEach(x => resultPts.Add(new Point((int)x.X, (int)x.Y)));
            using (Graphics g = this.pictureBox1.CreateGraphics())
            using (Pen pen = new Pen(Color.Red, 3))
                g.DrawCurve(pen, resultPts.ToArray(), tension);
        }
 
        /// <summary>
        /// 绘制通过点
        /// </summary>
        /// <param name="picPoints">图片点</param>
        /// <param name="tension"></param>
        private void DrawThrountPoint(List<Yw.Geometry.Point2d> picPoints, float tension)
        {
            if (picPoints == null || picPoints.Count < 4)
                return;
            var resultPts = new List<PointF>();
            picPoints.ForEach(x => resultPts.Add(new PointF((float)x.X, (float)x.Y)));
            using (Graphics g = this.pictureBox1.CreateGraphics())
            using (Pen pen = new Pen(Color.Red, 3))
                g.DrawCurve(pen, resultPts.ToArray(), tension);
        }
 
        #endregion
 
        #region 图片区域的鼠标操作
 
        private Point? _startPoint;//开始点
 
        /// <summary>
        /// 开始选取范围
        /// </summary>
        public void StartSelectionRange()
        {
            this.pictureBox1.Invalidate();
            _rect = null;
            _startPoint = null;
            _isSelBoundary = true;
            this.Cursor = System.Windows.Forms.Cursors.Cross;
            this.DoubleBuffered = true;
 
            if (_allClickPoints != null)
            {
                _allClickPoints.Clear();
                this.ReloadBoundaryEvent?.Invoke(_rect);
                ReloadPickPoints();
            }
        }
 
        /// <summary>
        /// 鼠标按下
        /// </summary> 
        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (_isSelBoundary)
                {
                    _startPoint = e.Location;
                }
                else if (_isPickPoint)
                {
                    int rX = e.X;
                    int rY = e.Y;
 
                    //精度
                    int xJd = 1, yJd = 1;
                    if (Math.Abs(ChartMaxX - ChartMinX) < 12)
                        xJd = 2;
                    if (Math.Abs(ChartMaxX - ChartMinX) < 1)
                        xJd = 3;
                    double ptX = 0;
                    if (rX == _rect.Value.Left)
                    {
                        ptX = ChartMinX;
                    }
                    else
                    {
                        ptX = UtilsHelper.Regulate(ChartMinX + (ChartMaxX - ChartMinX) / (_rect.Value.Right - _rect.Value.Left) * (rX - _rect.Value.Left), xJd);
                    }
                    if (ptX < 0)
                        ptX = 0;
 
                    Yw.Geometry.Point2d picPoint = null;
                    if (!_isCaputueEtaPtInEqupCurve)
                    {
                        if (Math.Abs(ChartMaxY - ChartMinY) < 12)
                            yJd = 2;
                        if (Math.Abs(ChartMaxY - ChartMinY) < 2)
                            yJd = 3;
 
                        double ptY = 0;
                        if (rY <= _rect.Value.Top)
                        {
                            ptY = ChartMaxY;
                        }
                        else
                        {
                            ptY = UtilsHelper.Regulate(ChartMaxY + (ChartMinY - ChartMaxY) / (_rect.Value.Bottom - _rect.Value.Top) * (rY - _rect.Value.Top), yJd);
                        }
 
                        if (ptY < 0)
                            ptY = 0;
 
                        _allClickPoints.Add(new CurrentViewModel(ptX, ptY, rX, rY));
                        picPoint = new Yw.Geometry.Point2d(ptX, ptY);
                    }
                    else
                    {
                        var dlg = new PickEqupPointDlg();
                        dlg.ReloadDataEvent += (ptY) =>
                        {
                            _allClickPoints.Add(new CurrentViewModel(ptX, ptY, rX, rY));
                            picPoint = new Yw.Geometry.Point2d(ptX, ptY);
                            return true;
                        };
                        dlg.ShowDialog();
                    }
                    ReloadPickPoints();
                    DrawPictureShape();
                }
            }
        }
 
        /// <summary>
        /// 鼠标移动
        /// </summary> 
        private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isSelBoundary && _startPoint != null)
            {
                _rect = GetRectangle(_startPoint.Value, e.Location);
                this.pictureBox1.Invalidate();
            }
        }
 
        //获取矩形
        private Rectangle GetRectangle(Point p1, Point p2)
        {
            int x = p1.X < p2.X ? p1.X : p2.X;
            int y = p1.Y < p2.Y ? p1.Y : p2.Y;
            int width = Math.Abs(p1.X - p2.X);
            int height = Math.Abs(p1.Y - p2.Y);
            return new Rectangle(x, y, width, height);
        }
 
        /// <summary>
        /// 鼠标松开
        /// </summary>    
        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            if (_isSelBoundary)
            {
                _startPoint = null;
                _isSelBoundary = false;
                this.Cursor = System.Windows.Forms.Cursors.Arrow;
                this.DoubleBuffered = false;
                this.ReloadBoundaryEvent?.Invoke(_rect);
            }
        }
 
 
 
 
        /// <summary>
        /// 绘制
        /// </summary>
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            if (_rect != null && _isSelBoundary)
            {
                using (Pen pen = new Pen(Color.Red, 3))
                {
                    e.Graphics.DrawRectangle(pen, _rect.Value);
                }
            }
        }
 
        #endregion
 
        #region gridView
        //删除点
        private void gridView1_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {
            if (e.Column == this.colDelete)
            {
                if (this.gridView1.GetRow(e.RowHandle) is CurrentViewModel row)
                {
                    _allClickPoints.Remove(row);
                    ReloadPickPoints();
                    DrawPictureShape();
                }
            }
        }
        #endregion
 
        /// <summary>
        /// 得到图片曲线上的点
        /// </summary> 
        /// <param name="clickPoints">返回获取点</param> 
        /// <returns></returns>
        public bool GetPicPoints(out List<Yw.Geometry.Point2d> clickPoints)
        {
            clickPoints = null;
            if (_allClickPoints == null)
                return false;
 
            clickPoints = _allClickPoints.Select(x => new Yw.Geometry.Point2d(x.X, x.Y)).ToList();
 
            List<Yw.Geometry.Point2d> curvePoints = null;
            if (!_isCaptureEuqualCurve)
            {
                curvePoints = clickPoints.GetFitPointList(12);
                curvePoints?.OrderBy(x => x.X);
            }
            else
            {
                /*
                 * 暂时只用完全通过点的方法,因为有时垂直很厉害时DrawFitPoint现状不好看
                 * 有增减性 就可以用拟合的方法
                 * 只用通过点的方法 
                */
                curvePoints = clickPoints;
            }
 
            _allClickPoints.Clear();
            ReloadPickPoints();
            DrawPictureShape();
            return true;
        }
 
        //刷新抓起点列表
        private void ReloadPickPoints()
        {
            this.bindingSource1.ResetBindings(false);
            var pickPoints = _allClickPoints?.Select(x => new Yw.Geometry.Point2d(x.X, x.Y)).ToList();
            this.ReloadPickPointsEvent?.Invoke(pickPoints);
        }
 
    }
}