using DevExpress.Utils;
|
using DevExpress.XtraEditors;
|
using DevExpress.XtraGrid.Views.Grid;
|
using NPOI.HSSF.UserModel;
|
using System;
|
using System.Collections.Generic;
|
using System.Drawing;
|
using System.IO;
|
using System.Windows.Forms;
|
|
|
//编辑原始的性能曲线
|
namespace AStation.WinFrmUI.Chart
|
{
|
public partial class ImportCurveByExcelCtrl : DevExpress.XtraEditors.XtraUserControl
|
{
|
public ImportCurveByExcelCtrl()
|
{
|
InitializeComponent();
|
this.gridView1.OptionsFind.FindNullPrompt = "检索";
|
this.gridView1.OptionsSelection.MultiSelect = false;
|
this.gridView1.OptionsMenu.EnableColumnMenu = true;
|
|
this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;
|
this.gridView1.Appearance.HeaderPanel.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
|
this.gridView1.Appearance.Row.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Center;
|
this.gridView1.FocusRectStyle = DrawFocusRectStyle.RowFullFocus;
|
this.gridView1.OptionsCustomization.AllowFilter = true;
|
this.gridView1.OptionsCustomization.AllowSort = true;
|
this.gridView1.OptionsCustomization.AllowQuickHideColumns = false;
|
this.gridView1.OptionsView.ShowAutoFilterRow = false;
|
this.gridView1.OptionsView.ShowFilterPanelMode = DevExpress.XtraGrid.Views.Base.ShowFilterPanelMode.Never;
|
this.gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
|
this.gridView1.OptionsView.ShowGroupPanel = false;
|
this.gridView1.OptionsClipboard.AllowCopy = DefaultBoolean.True;
|
this.gridView1.OptionsBehavior.Editable = true;
|
this.gridView1.OptionsBehavior.ReadOnly = false;
|
|
this.gridView1.OptionsView.EnableAppearanceOddRow = true;
|
this.gridView1.OptionsView.EnableAppearanceEvenRow = true;
|
this.gridView1.Appearance.OddRow.BackColor = Color.White;
|
this.gridView1.Appearance.EvenRow.BackColor = Color.FromArgb(244, 248, 251);
|
|
int height = 30;
|
this.gridView1.RowHeight = height;
|
this.gridView1.ColumnPanelRowHeight = height;
|
this.gridView1.GroupRowHeight = height;
|
this.gridView1.BestFitColumns();
|
|
this.gridView1.OptionsView.ShowIndicator = true;
|
this.gridView1.IndicatorWidth = height;
|
this.gridView1.CustomDrawRowIndicator += (sender, e) =>
|
{
|
if (e.Info.IsRowIndicator && e.RowHandle >= 0)
|
{
|
e.Info.DisplayText = (e.RowHandle + 1).ToString().Trim();
|
e.Appearance.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
|
}
|
};
|
|
this.gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.True;
|
this.gridView1.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom;
|
this.gridView1.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.NoBorder;
|
|
this.imgCmbCurveSourceFrom.Properties.AddEnum(typeof(AStation.Chart.eSourceWay), false);
|
this.imgCmbCurveSourceFrom.EditValue = AStation.Chart.eSourceWay.现场测试;
|
|
this.imgCmbQHFitType.Properties.AddEnum(typeof(AStation.Curve.eFitType), false);
|
this.imgCmbQHFitType.EditValue = _qhFitType;
|
|
this.imgCmbQEFitType.Properties.AddEnum(typeof(AStation.Curve.eFitType), false);
|
this.imgCmbQEFitType.EditValue = _qeFitType;
|
|
this.imgCmbQPFitType.Properties.AddEnum(typeof(AStation.Curve.eFitType), false);
|
this.imgCmbQPFitType.EditValue = _qpFitType;
|
|
_allBindList = new List<CurrentViewModel>();
|
this.bindingSource1.DataSource = _allBindList;
|
this.curveExpressChart1.Enabled = false;
|
}
|
|
#region 页面类
|
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
|
|
AStation.Curve.eFitType _qhFitType = AStation.Curve.eFitType.CubicCurve;
|
AStation.Curve.eFitType _qeFitType = AStation.Curve.eFitType.CubicCurve;
|
AStation.Curve.eFitType _qpFitType = AStation.Curve.eFitType.CubicCurve;
|
|
private List<CurrentViewModel> _allBindList;
|
|
private AStation.Curve.CurveExpress QhCurve = null, QeCurve = null, QpCurve = null;
|
List<AStation.Curve.CurvePoint> QhPoints = null, QePoints = null, QpPoints = 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() != DialogResult.OK)
|
return;
|
this.btnExcelFilePath.Text = dlg.FileName;
|
AnaSelectFile(dlg.FileName);
|
}
|
|
|
private void imgCmbQHFitType_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
loadCurve();
|
}
|
|
private void imgCmbQEFitType_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
loadCurve();
|
}
|
|
private void imgCmbQPFitType_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
loadCurve();
|
}
|
|
//分析文件
|
private void AnaSelectFile(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)
|
{
|
MessageBox.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)
|
{
|
MessageBox.Show("第一行第一列不能空,");
|
return;
|
}
|
|
//开始读取的行
|
int start_line = title_line_index + 1;
|
var cell_0 = row_title.GetCell(0);
|
if (cell_0 == null)
|
{
|
MessageBox.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;
|
}
|
|
_allBindList.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 = AStation.Curve.PumpCalculateHelper.CalculateP(q, h, eta);
|
}
|
else
|
{
|
eta = AStation.Curve.PumpCalculateHelper.CalculateE(q, h, power);
|
}
|
|
if (eta > 99)
|
{
|
throw new Exception("效率大于100%");
|
}
|
_allBindList.Add(new CurrentViewModel(index++, q, h, eta, power));
|
}
|
|
if (_allBindList.Count < 4)
|
{
|
this.curveExpressChart1.InitialChartData();
|
MessageBox.Show("导入点过少,请手动添加点,点数至少4个点");
|
return;
|
}
|
loadCurve();
|
this.bindingSource1.ResetBindings(false);
|
}
|
catch (Exception err)
|
{
|
MessageBox.Show(string.Format("读取Excel出错!错误原因:{0},行号: {1}", err.Message, line), "提示信息",
|
MessageBoxButtons.OK, MessageBoxIcon.Information);
|
}
|
}
|
|
//加载曲线
|
private bool loadCurve()
|
{
|
if (_allBindList == null || _allBindList.Count < 4)
|
{
|
this.curveExpressChart1.Enabled = false;
|
this.curveExpressChart1.InitialChartData();
|
return false;
|
|
}
|
|
this.curveExpressChart1.Enabled = true;
|
_qhFitType = (AStation.Curve.eFitType)this.imgCmbQHFitType.EditValue;
|
_qeFitType = (AStation.Curve.eFitType)this.imgCmbQEFitType.EditValue;
|
_qpFitType = (AStation.Curve.eFitType)this.imgCmbQPFitType.EditValue;
|
|
QhPoints = new List<AStation.Curve.CurvePoint>();
|
QePoints = new List<AStation.Curve.CurvePoint>();
|
QpPoints = new List<AStation.Curve.CurvePoint>();
|
for (int i = 0; i < _allBindList.Count; i++)
|
{
|
var model = _allBindList[i];
|
if (model.H > 0)
|
QhPoints.Add(new AStation.Curve.CurvePoint() { X = model.Q, Y = model.H });
|
if (model.E >= 0)
|
QePoints.Add(new AStation.Curve.CurvePoint() { X = model.Q, Y = model.E });
|
|
if (model.P > 0)
|
QpPoints.Add(new AStation.Curve.CurvePoint() { X = model.Q, Y = model.P });
|
}
|
|
QhCurve = AStation.Curve.FitHelper.BuildCurveExpress(QhPoints, _qhFitType);
|
QeCurve = AStation.Curve.FitHelper.BuildCurveExpress(QePoints, _qeFitType);
|
QpCurve = AStation.Curve.FitHelper.BuildCurveExpress(QpPoints, _qpFitType);
|
|
this.curveExpressChart1.SetBindingData(QhCurve, QeCurve, QpCurve);
|
return true;
|
}
|
|
/// <summary>
|
/// 获取数据
|
/// </summary>
|
/// <param name="curveCode"></param>
|
/// <param name="eCurveSourceFrom"></param>
|
/// <param name="featCurvePointGroup"></param>
|
/// <param name="featCurveExpressGroup"></param>
|
/// <returns></returns>
|
public bool GetCurve(out string curveCode, out AStation.Chart.eSourceWay eCurveSourceFrom, out Model.PumpCurveInfo featCurveExpressGroup)
|
{
|
curveCode = this.txtCurveCode.Text.Trim();
|
eCurveSourceFrom = (AStation.Chart.eSourceWay)this.imgCmbCurveSourceFrom.EditValue;
|
|
featCurveExpressGroup = null;
|
if (!loadCurve())
|
return false;
|
|
if (string.IsNullOrEmpty(curveCode))
|
{
|
XtraMessageBox.Show("请输入曲线名称!");
|
return false;
|
}
|
eCurveSourceFrom = (AStation.Chart.eSourceWay)this.imgCmbCurveSourceFrom.EditValue;
|
featCurveExpressGroup = new Model.PumpCurveInfo(QhCurve, QeCurve, QpCurve);
|
return true;
|
}
|
|
}
|
}
|