using Yw.Geometry;
namespace HStation.WinFrmUI.PhartRelation
{
public partial class PumpSerialParallelChartDlg : XtraForm
{
public PumpSerialParallelChartDlg()
{
InitializeComponent();
this.Text = "曲线视图";
this.WindowState = FormWindowState.Maximized;
this.pumpSerialParallelInfoCtrl1.SetEvent += (id, qh, qe, qp) =>
{
this.pumpSerialParallelChart.Set(id, qh, qe, qp);
var list = this.pumpSerialParallelInfoCtrl1.GetList();
SetSerialParallel(list, _design_pt, _is_parallel);
};
this.pumpSerialParallelInfoCtrl1.SetInfoEvent += (id, name, color) =>
{
this.pumpSerialParallelChart.SetInfo(id, name, color);
};
this.pumpSerialParallelInfoCtrl1.SetDesignPointEvent += (q, h) =>
{
_design_pt = new Point2d(q, h);
this.pumpSerialParallelChart.SetDesignPt(new Point2d(q, h));
};
this.pumpSerialParallelChart.OnCalcQueryPoint += (id, pt) =>
{
this.pumpSerialParallelInfoCtrl1.SetQueryInfo(id, pt);
};
}
private string _serial_parallel_id= "serial_parallel";
private Yw.Geometry.Point2d _design_pt;
private bool _is_parallel;
///
/// 设置
///
///
///
///
///
public string SetBindingData(
List list,
Yw.Geometry.Point2d design_pt,
bool is_parallel = true)
{
if (list == null || !list.Any())
{
return "数据为空!";
}
_is_parallel = is_parallel;
_design_pt = design_pt;
var vm_list = new List();
for (int i = 0; i < list.Count; i++)
{
var x = list[i];
var vm = new Yw.WinFrmUI.Phart.PumpSerialParallelInfoViewModel(x);
vm.Color = GetRandomColor(i);
vm.Calc();
vm_list.Add(vm);
}
this.pumpSerialParallelInfoCtrl1.SetBindingData(vm_list, _design_pt);
this.pumpSerialParallelChart.Add(vm_list, _design_pt);
var msg = SetSerialParallel(vm_list, _design_pt, _is_parallel);
if (!string.IsNullOrEmpty(msg))
return msg;
return string.Empty;
}
private string SetSerialParallel(List list, Yw.Geometry.Point2d design_pt, bool is_parallel)
{
this.pumpSerialParallelChart.Delete(_serial_parallel_id);
if (list == null || !list.Any())
{
return "无数据!";
}
var vm_serial_parallel = CalcSerialParallel(list, is_parallel);
if (vm_serial_parallel == null)
{
return "无法计算串并联曲线!";
}
var qh = vm_serial_parallel.Qh;
var min_h = qh.GetPointY(qh.MinX);
var max_h = qh.GetPointY(qh.MaxX);
if (design_pt != null)
{
if (design_pt.X > qh.MaxX || design_pt.X < qh.MinX)
{
return "设计流量无效!";
}
if (design_pt.Y > max_h * 2 || design_pt.Y < min_h * 0.5)
{
return "设计扬程无效!";
}
}
else
{
var design_q = qh.MaxX / 2;
var design_h = qh.GetPointY(design_q);
design_q = Math.Round(design_q, 1);
design_h = Math.Round(design_h, 1);
design_pt = new Point2d(design_q, design_h);
_design_pt = design_pt;
this.pumpSerialParallelInfoCtrl1.SetDesignPoint(design_pt);
}
this.pumpSerialParallelChart.Add(vm_serial_parallel, design_pt);
return string.Empty;
}
private Yw.WinFrmUI.Phart.PumpSerialParallelInfoViewModel CalcSerialParallel(List list, bool is_parallel)
{
if (list == null || !list.Any())
{
return default;
}
var calc_bol = false;
var line_name = "";
List calc_pt_qh_list;
List calc_pt_qe_list;
List calc_pt_qp_list;
var helper = new Yw.WinFrmUI.Phart.PumpParallelConnectionHelper();
list.ForEach(x => helper.Add(x.QhCalc, x.QpCalc));
if (is_parallel)
{
line_name = "并联曲线";
calc_bol = helper.CalculateParallel(out calc_pt_qh_list, out calc_pt_qe_list, out calc_pt_qp_list);
}
else
{
line_name = "串联曲线";
calc_bol = helper.CalculateSeries(out calc_pt_qh_list, out calc_pt_qe_list, out calc_pt_qp_list);
}
if (!calc_bol)
{
return default;
}
var vm_sp = new Yw.WinFrmUI.Phart.PumpSerialParallelInfoViewModel();
vm_sp.Id = _serial_parallel_id;
vm_sp.Name = line_name;
vm_sp.Qh = new Yw.Geometry.CubicSpline2d(calc_pt_qh_list);
vm_sp.Qe = new Yw.Geometry.CubicSpline2d(calc_pt_qe_list);
vm_sp.Qp = new Yw.Geometry.CubicSpline2d(calc_pt_qp_list);
vm_sp.QhCalc = vm_sp.Qh;
vm_sp.QeCalc = vm_sp.Qe;
vm_sp.QpCalc = vm_sp.Qp;
vm_sp.Color = Color.Black;
vm_sp.IsBp = true;
vm_sp.IsDefault = true;
return vm_sp;
}
#region Color
private List _color_array = new List()
{
Color.Red, Color.Blue, Color.Green,Color.DodgerBlue,
Color.Fuchsia, Color.MidnightBlue, Color.Maroon, Color.Aquamarine,
Color.Bisque,Color.BurlyWood
};
///
/// 获取随机颜色
///
///
private Color GetRandomColor(int count)
{
if (count < _color_array.Count)
{
return _color_array[count];
}
var _random = new Random();
int r = _random.Next(1, 256);
int g = _random.Next(1, 256);
int b = _random.Next(1, 256);
return Color.FromArgb(r, g, b);
}
#endregion
}
}