using System;
|
using System.Collections.Generic;
|
using System.ComponentModel;
|
using System.Data;
|
using System.Drawing;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
using System.Windows.Forms;
|
using Yw.WinFrmUI.Q3d;
|
using Yw.EPAnet;
|
using System.IO;
|
using Newtonsoft.Json;
|
|
namespace Hydro.ClientTool
|
{
|
public partial class Form3 : Form
|
{
|
public NetworkViewModel _viewnet { get; set; }
|
|
public Network _network { get; set; }
|
public Dictionary<string, Node> dictNodes { get; set; }
|
public Dictionary<string, Link> dictLinks { get; set; }
|
public Form3()
|
{
|
InitializeComponent();
|
}
|
|
private void 打开OToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
//打开文件对话框,选择inp类型的文件
|
var ofd = new OpenFileDialog();
|
ofd.Filter = "json文件|*.json|inp文件|*.inp";
|
if (ofd.ShowDialog() == DialogResult.OK)
|
{
|
|
//string inptxt = File.ReadAllText(ofd.FileName);
|
//如果是inp文件,调用InpInteropHelper.FromInpString方法,将inp文件转换为Network对象
|
//如果是json文件,直接调用JsonConvert.DeserializeObject方法,将json文件转换为Network对象
|
if (ofd.FileName.EndsWith(".inp"))
|
//_network = InpInteropHelper.FromInpString(ofd.FileName);
|
_network = InpInteropHelper.FromInpString(ofd.FileName);
|
else
|
{
|
string inptxt = File.ReadAllText(ofd.FileName);
|
_network = JsonConvert.DeserializeObject<Network>(inptxt);
|
//_network.GetAllNodes().ForEach(n =>
|
//{
|
// n.Links = new List<Link>();
|
//});
|
dictNodes = _network.GetAllNodes().ToDictionary(n => n.Id);
|
_network.GetAllLinks().ForEach(l =>
|
{
|
l.StartNode = dictNodes[l.StartNode.Id];
|
l.EndNode = dictNodes[l.EndNode.Id];
|
|
});
|
dictLinks = _network.GetAllLinks().ToDictionary(l => l.Id);
|
|
_network.GetAllLinks().ForEach(l =>
|
{
|
if (l.StartNode.Links == null) l.StartNode.Links = new List<Link>();
|
if (l.EndNode.Links == null) l.EndNode.Links = new List<Link>();
|
l.StartNode.Links.Add(l);
|
l.EndNode.Links.Add(l);
|
});
|
|
|
|
}
|
|
|
_viewnet = ConvertNetworkToNetworkViewModel(_network);
|
map.SetData(_viewnet);
|
map.SelectedObjectsChanged += (oo, ee) =>
|
{
|
if (map.selectedObjs.Count > 1)
|
{
|
propertyGrid1.SelectedObjects = map.selectedObjs.ToArray();
|
}
|
else if (map.selectedObjs.Count == 1)
|
{
|
propertyGrid1.SelectedObject = map.selectedObjs[0];
|
}
|
else
|
{
|
propertyGrid1.SelectedObject = null;
|
}
|
};
|
}
|
|
|
|
}
|
public NetworkViewModel ConvertNetworkToNetworkViewModel(Network network)
|
{
|
NetworkViewModel net = new NetworkViewModel();
|
network.GetAllLinks().ForEach(l =>
|
{
|
if (l is Pipe)
|
{
|
Pipe pipe = l as Pipe;
|
net.Links.Add(new PipeViewModel()
|
{
|
ID = pipe.Id,
|
Node1 = pipe.StartNode.Id,
|
Node2 = pipe.EndNode.Id,
|
Length = (float)pipe.Length,
|
Diameter = (float)pipe.Diameter,
|
Roughness = (float)pipe.Roughness,
|
Status = pipe.LinkStatus == null ? ObjectEnum.StatusType.DEFAULT : (ObjectEnum.StatusType)Enum.Parse(typeof(ObjectEnum.StatusType), pipe.LinkStatus.ToUpper()),
|
});
|
}
|
else if (l is Pump)
|
{
|
Pump pump = l as Pump;
|
net.Links.Add(new PumpViewModel()
|
{
|
ID = pump.Id,
|
Node1 = pump.StartNode.Id,
|
Node2 = pump.EndNode.Id,
|
HeadCurve = pump.CurveQH,
|
Status = pump.LinkStatus == null ? ObjectEnum.StatusType.DEFAULT : (ObjectEnum.StatusType)Enum.Parse(typeof(ObjectEnum.StatusType), pump.LinkStatus.ToUpper()),
|
});
|
}
|
else if (l is Valve)
|
{
|
Valve valve = l as Valve;
|
net.Links.Add(new ValveViewModel()
|
{
|
ID = valve.Id,
|
Node1 = valve.StartNode.Id,
|
Node2 = valve.EndNode.Id,
|
Diameter = (float)valve.Diameter,
|
Setting = valve.ValveSetting,
|
Status = valve.LinkStatus == null ? ObjectEnum.StatusType.DEFAULT : (ObjectEnum.StatusType)Enum.Parse(typeof(ObjectEnum.StatusType), valve.LinkStatus.ToUpper()),
|
});
|
}
|
else if (l is Exchanger)
|
{
|
Exchanger valve = l as Exchanger;
|
net.Links.Add(new HeatExchanger()
|
{
|
ID = valve.Id,
|
Node1 = valve.StartNode.Id,
|
Node2 = valve.EndNode.Id,
|
Diameter = (float)valve.Diameter,
|
Setting = valve.CurveQL,
|
Status = (ObjectEnum.StatusType)Enum.Parse(typeof(ObjectEnum.StatusType), valve.LinkStatus.ToUpper()),
|
});
|
}
|
else if (l is Compressor)
|
{
|
Compressor valve = l as Compressor;
|
net.Links.Add(new AirCompressor()
|
{
|
ID = valve.Id,
|
Node1 = valve.StartNode.Id,
|
Node2 = valve.EndNode.Id,
|
Diameter = (float)valve.Diameter,
|
Setting = valve.CurveQL,
|
Status = (ObjectEnum.StatusType)Enum.Parse(typeof(ObjectEnum.StatusType), valve.LinkStatus.ToUpper()),
|
});
|
}
|
|
});
|
|
network.GetAllNodes().ForEach(n =>
|
{
|
if (n is Junction)
|
{
|
var junction = (Junction)n;
|
net.Nodes.Add(new JunctionViewModel()
|
{
|
ID = junction.Id,
|
X = (float)junction.Position.X,
|
Y = (float)junction.Position.Y,
|
Z = (float)junction.Elev,
|
Demand = (float)junction.Demand,
|
PatternID = junction.DemandPattern,
|
});
|
}
|
else if (n is Tank)
|
{
|
var tank = (Tank)n;
|
net.Nodes.Add(new TankViewModel()
|
{
|
ID = tank.Id,
|
X = (float)tank.Position.X,
|
Y = (float)tank.Position.Y,
|
Z = (float)tank.PoolElev,
|
InitLevel = (float)tank.InitLevel,
|
MinLevel = (float)tank.MinLevel,
|
MaxLevel = (float)tank.MaxLevel,
|
Diameter = (float)tank.Diameter,
|
});
|
}
|
else if (n is Reservoir)
|
{
|
var reservoir = (Reservoir)n;
|
net.Nodes.Add(new ReservoirViewModel()
|
{
|
ID = reservoir.Id,
|
X = (float)reservoir.Position.X,
|
Y = (float)reservoir.Position.Y,
|
Z = (float)(reservoir.PoolElev ?? 0),
|
Head = (float)reservoir.Head,
|
});
|
}
|
else if (n is Emitter)
|
{
|
var emitter = (Emitter)n;
|
net.Nodes.Add(new NozzleViewModel()
|
{
|
ID = emitter.Id,
|
X = (float)emitter.Position.X,
|
Y = (float)emitter.Position.Y,
|
Z = (float)emitter.Elev,
|
FlowCoefficient = (float)emitter.Coefficient,
|
});
|
}
|
else if (n is Meter)
|
{
|
var meter = (Meter)n;
|
net.Nodes.Add(new MeterViewModel()
|
{
|
ID = meter.Id,
|
X = (float)meter.Position.X,
|
Y = (float)meter.Position.Y,
|
Z = (float)meter.Elev,
|
Demand = (float)meter.Demand,
|
PatternID = meter.DemandPattern,
|
});
|
}
|
});
|
net.BuildRelation();
|
return net;
|
}
|
|
private void Form3_Load(object sender, EventArgs e)
|
{
|
|
}
|
|
|
|
private void 退出XToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
this.Close();
|
}
|
|
|
|
private void 简单计算ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
CalcuResult result = _network.Calcu(CalcuMode.Simple);
|
//将计算结果建立node字典和link字典
|
Dictionary<string, CalcuNode> dictNodes = result.NodeList.ToDictionary(n => n.Id);
|
Dictionary<string, CalcuLink> dictLinks = result.LinkList.ToDictionary(l => l.Id);
|
//将计算结果显示在_net中
|
_viewnet.Links.ForEach(l =>
|
{
|
if (dictLinks.ContainsKey(l.ID))
|
{
|
l.EN_FLOW = (float)dictLinks[l.ID].Flow;
|
l.EN_VELOCITY = (float)dictLinks[l.ID].Velocity;
|
l.EN_HEADLOSS = (float)dictLinks[l.ID].Headloss;
|
|
}
|
});
|
_viewnet.Nodes.ForEach(n =>
|
{
|
if (dictNodes.ContainsKey(n.ID))
|
{
|
n.EN_HEAD = (float)dictNodes[n.ID].Head;
|
n.EN_PRESSURE = (float)dictNodes[n.ID].Press;
|
n.EN_DEMAND = (float)dictNodes[n.ID].Demand;
|
|
}
|
});
|
|
|
|
|
|
}
|
|
private void 局部损失计算ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
var result = _network.Calcu(CalcuMode.MinorLoss);
|
}
|
|
private void 下游路径分析ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (_network == null)
|
{
|
MessageBox.Show("请先打开文件");
|
return;
|
}
|
if (map.selectedObjs.Count > 0 && map.selectedObjs[0] is NodeViewModel node)
|
{
|
var calcResult = _network.Calcu(CalcuMode.MinorLoss);
|
var Snode = _network.GetAllNodes().FirstOrDefault(n => n.Id == node.ID);
|
var links = _network.AnalyzeDownstreamPath(Snode, calcResult);
|
if (links == null || links.Count <= 0) return;
|
map.SetSelectObj(links.Select(l => l.Id).ToList());
|
|
}
|
|
|
}
|
private void 沿程纵断面分析ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (_network == null)
|
{
|
MessageBox.Show("请先打开文件");
|
return;
|
}
|
if (map.selectedObjs.Count > 0 && map.selectedObjs[0] is NodeViewModel node)
|
{
|
var calcResult = _network.Calcu(CalcuMode.MinorLoss);
|
var Snode = _network.GetAllNodes().FirstOrDefault(n => n.Id == node.ID);
|
var links = _network.AnalyzeDownstreamPath(Snode, calcResult);
|
if (links == null || links.Count <= 0) return;
|
|
map.SetSelectObj(links.Select(l => l.Id).ToList());
|
var result = _network.GetChartNodeByPathLinks(links, calcResult);
|
|
}
|
|
}
|
|
private void iNPToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
//打开文件对话框,将 _network.ToInpString();导出
|
var sfd = new SaveFileDialog();
|
sfd.Filter = "inp文件|*.inp";
|
if (sfd.ShowDialog() == DialogResult.OK)
|
{
|
File.WriteAllText(sfd.FileName, _network.ToInpString());
|
}
|
}
|
}
|
}
|