lixiaojun
2024-12-02 dbcef6befb123066754141adef6dc3ef6653b6ce
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
using DevExpress.XtraEditors;
using NPOI.HSSF.UserModel;
using System.IO;
 
namespace Yw.WinFrmUI.Phart
{
    public partial class PumpImportByExcelCtrl : DevExpress.XtraEditors.XtraUserControl
    {
        public PumpImportByExcelCtrl()
        {
            InitializeComponent();
 
            this.gridView1.SetDefaultEditView();
            this.gridView1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
 
            _all_bind_list = new List<CurrentViewModel>();
            this.bindingSource1.DataSource = _all_bind_list;
        }
 
        #region CurrentViewModel
 
        public class CurrentViewModel
        {
            public enum eSortType
            {
                ID,
                Q,
                H,
                E,
                P
            }
 
            public CurrentViewModel()
            { }
 
            public CurrentViewModel(int id, double q, double h, double e, double p)
            {
                ID = id;
                Q = q;
                H = h;
                E = e;
                P = p;
            }
 
            public CurrentViewModel(CurrentViewModel rhs) : this(rhs.ID, rhs.Q, rhs.H, rhs.E, rhs.P)
            {
            }
 
            public int ID { get; set; }
            public double Q { get; set; }
            public double H { get; set; }
            public double E { get; set; }
            public double P { get; set; }
 
            public CurrentViewModel RoundAll(int decimals)
            {
                Q = Math.Round(Q, decimals);
                H = Math.Round(H, decimals);
                E = Math.Round(E, decimals);
                P = Math.Round(P, decimals);
                return this;
            }
 
            public CurrentViewModel RoundAll_Small(int decimal_places, double small = 2.0)
            {
                Q = Round_Small(Q, decimal_places, small);
                H = Round_Small(H, decimal_places, small);
                E = Round_Small(E, decimal_places, small);
                P = Round_Small(P, decimal_places, small);
                return this;
            }
 
            private double Round_Small(double value, int num, double small = 2.0)
            {
                if (value < small)
                    value = Math.Round(value, 3);
                else
                    value = Math.Round(value, num);
                return value;
            }
        }
 
        #endregion CurrentViewModel
 
        private List<CurrentViewModel> _all_bind_list;
 
        private Yw.Geometry.CubicSpline2d
            _qh = null,
            _qe = null,
            _qp = null;
 
        private List<Yw.Geometry.Point2d>
            _qh_pt_list = null,
            _qe_pt_list = null,
            _qp_pt_list = null;
 
        //选择Excel文件
        private void btnExcelFilePath_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            var dlg = new System.Windows.Forms.OpenFileDialog();
            dlg.Filter = "EXCEL 文件(*.xls)|*.xls";
            dlg.CheckFileExists = true;
            if (dlg.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                return;
            this.btnExcelFilePath.Text = dlg.FileName;
            LoadExcel(dlg.FileName);
        }
 
        //分析文件
        private void LoadExcel(string fileName)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                XtraMessageBox.Show("路径无效!");
                return;
            }
            int line = 0;
            try
            {
                //初始化文件
                HSSFWorkbook theBook = null;
                using (FileStream file = new FileStream(fileName, FileMode.Open, FileAccess.Read))
                {
                    theBook = new HSSFWorkbook(file);
                }
 
                //检查表格是否符合
                NPOI.SS.UserModel.ISheet sheet1 = theBook.GetSheet("Sheet1");
                if (sheet1 == null)
                {
                    sheet1 = theBook.GetSheetAt(0);
                    if (sheet1 == null)
                    {
                        XtraMessageBox.Show("请将数据放在Sheet1中");
                        return;
                    }
                }
 
                //标题行
                int title_line_index = 0;
                //流量列
                int col_index_q = 1;
                //扬程列
                int col_index_h = 2;
                //效率列
                int col_index_e = 3;
                //功率列
                int col_index_p = 4;
 
                var row_title = sheet1.GetRow(title_line_index);
                if (row_title == null)
                {
                    XtraMessageBox.Show("第一行第一列不能空,");
                    return;
                }
 
                //开始读取的行
                int start_line = title_line_index + 1;
                var cell_0 = row_title.GetCell(0);
                if (cell_0 == null)
                {
                    XtraMessageBox.Show("无法读取表头文件");
                    return;
                }
                else if (cell_0.StringCellValue.Contains("序号"))
                {
                    col_index_q = 1;
                    col_index_h = 2;
                    col_index_e = 3;
                    col_index_p = 4;
                }
                else if (cell_0.StringCellValue.Contains("流量"))
                {
                    col_index_q = 0;
                    col_index_h = 1;
                    col_index_e = 2;
                    col_index_p = 3;
                }
 
                _all_bind_list.Clear();
 
                double q, h, eta, power;
                NPOI.SS.UserModel.IRow rowtemp = null;
                NPOI.SS.UserModel.ICell cell;
 
                var index = 1;
                for (line = start_line; line < 1000; line++)
                {
                    q = h = eta = power = -1;
                    rowtemp = sheet1.GetRow(line);
                    if (rowtemp == null)
                        break;
 
                    cell = rowtemp.GetCell(col_index_q);
                    if (cell == null)
                        break;
                    if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                        q = cell.NumericCellValue;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        q = Convert.ToDouble(cell.StringCellValue);
                    else
                        continue;
 
                    if (q < 0)
                        break;
 
                    cell = rowtemp.GetCell(col_index_h);
                    if (cell == null)
                        break;
                    if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                        h = cell.NumericCellValue;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        h = Convert.ToDouble(cell.StringCellValue);
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.Blank)
                        break;
                    if (h < 0)
                        break;
 
                    cell = rowtemp.GetCell(col_index_e);
                    if (cell == null)
                        break;
                    if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                        eta = cell.NumericCellValue;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        eta = Convert.ToDouble(cell.StringCellValue);
                    else
                        eta = -1;
 
                    cell = rowtemp.GetCell(col_index_p);
                    if (cell == null)
                        break;
                    if (cell.CellType == NPOI.SS.UserModel.CellType.Numeric)
                        power = cell.NumericCellValue;
                    else if (cell.CellType == NPOI.SS.UserModel.CellType.String)
                        power = Convert.ToDouble(cell.StringCellValue);
                    else
                        power = -1;
 
                    if (eta > 0)
                    {
                        power = Yw.Pump.CalculationHelper.CalcuP(q, h, eta);
                    }
                    else
                    {
                        eta = Yw.Pump.CalculationHelper.CalcuE(q, h, power);
                    }
 
                    if (eta > 99)
                    {
                        throw new Exception("效率大于100%");
                    }
                    _all_bind_list.Add(new CurrentViewModel(index++, q, h, eta, power));
                }
 
                if (_all_bind_list.Count < 4)
                {
                    this.xtrPerform2dChart1.InitialChartData();
                    XtraMessageBox.Show("导入点过少,请手动添加点,点数至少4个点");
                    return;
                }
                this.bindingSource1.ResetBindings(false);
                loadChart();
            }
            catch (Exception err)
            {
                XtraMessageBox.Show(string.Format("读取Excel出错!错误原因:{0},行号: {1}", err.Message, line), "提示信息",
                    System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
            }
        }
 
        //加载曲线
        private bool loadChart()
        {
            if (_all_bind_list == null || _all_bind_list.Count < 4)
            {
                this.xtrPerform2dChart1.InitialChartData();
                return false;
            }
 
            _qh_pt_list = new List<Yw.Geometry.Point2d>();
            _qe_pt_list = new List<Yw.Geometry.Point2d>();
            _qp_pt_list = new List<Yw.Geometry.Point2d>();
            for (int i = 0; i < _all_bind_list.Count; i++)
            {
                var model = _all_bind_list[i];
                if (model.H > 0)
                    _qh_pt_list.Add(new Yw.Geometry.Point2d() { X = model.Q, Y = model.H });
                if (model.E >= 0)
                    _qe_pt_list.Add(new Yw.Geometry.Point2d() { X = model.Q, Y = model.E });
 
                if (model.P > 0)
                    _qp_pt_list.Add(new Yw.Geometry.Point2d() { X = model.Q, Y = model.P });
            }
 
            _qh = new Yw.Geometry.CubicSpline2d(_qh_pt_list);
            _qe = new Yw.Geometry.CubicSpline2d(_qe_pt_list);
            _qp = new Yw.Geometry.CubicSpline2d(_qp_pt_list);
 
            var test_pt_list = _qe.GetPointList();
            this.xtrPerform2dChart1.SetBindingData(_qh, _qe, _qp, string.Empty, true);
            return true;
        }
 
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <returns></returns>
        public bool Get(out string other_name, out Yw.Pump.CurveQH qh, out Yw.Pump.CurveQE qe, out Yw.Pump.CurveQP qp)
        {
            other_name = this.txtCurveCode.Text.Trim();
            qh = null;
            qe = null;
            qp = null;
 
            if (!loadChart())
                return false;
 
            if (string.IsNullOrEmpty(other_name))
            {
                XtraMessageBox.Show("请输入曲线名称!");
                return false;
            }
            qh = new Pump.CurveQH(Yw.Pump.eFeatType.Cubic, _qh_pt_list);
            qe = new Pump.CurveQE(Yw.Pump.eFeatType.Cubic, _qe_pt_list);
            qp = new Pump.CurveQP(Yw.Pump.eFeatType.Cubic, _qp_pt_list);
 
            return true;
        }
    }
}