using dict_py_Inner;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http.Headers;
using System.Net.NetworkInformation;
using System.Numerics;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using static CloudWaterNetwork.Repeater;
using static System.Windows.Forms.LinkLabel;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TrackBar;
namespace CloudWaterNetwork
{
[Serializable]
public partial class Network
{
#region PropertyForm
#endregion
public string Name;
public Node StartPoint;
public Node EndPoint;
///
/// 核心承载字段
///
public List Nodes=new List();
///
/// 核心承载字段
///
public List Links = new List();
public List MapObjects
{
get
{
List objects = new List();
objects.AddRange(Nodes);
objects.AddRange(Links);
return objects;
}
}
public List junctions
{
get
{
return Nodes.FindAll(n=>n is Junction).Select(n=>n as Junction).ToList();
}
}
public List reservoirs
{
get
{
return Nodes.FindAll(n => n is Reservoir).Select(n => n as Reservoir).ToList();
}
}
public List tanks
{
get
{
return Nodes.FindAll(n => n is Tank).Select(n => n as Tank).ToList();
}
}
public List meters
{
get
{
return Nodes.FindAll(n => n is Meter).Select(n => n as Meter).ToList();
}
}
public List pipes
{
get
{
return Links.FindAll(n => n is Pipe).Select(n => n as Pipe).ToList();
}
}
public List valves
{
get
{
return Links.FindAll(n => n is Valve).Select(n => n as Valve).ToList();
}
}
public List repeaters
{
get
{
return Links.FindAll(n => n is Repeater).Select(n => n as Repeater).ToList();
}
}
public List pumps
{
get
{
return Links.FindAll(n => n is Pump).Select(n => n as Pump).ToList();
}
}
public List areas = new List();
public Junction AddJunction(PointF position,float z=0)
{
Junction j = new Junction();
int i = 0;
string ID = $"{Default.GetPreString(j)}{i}";
while (Nodes.Find(p0 => p0.ID == ID) != null)
{
i++;
ID = $"{Default.GetPreString(j)}{i}";
}
j.ID = ID;
j.Elev = 0;
j.Demand = 0;
j.Pattern = "";
j.Elev = z;
j.X = position.X;
j.Y = position.Y;
Nodes.Add(j);
return j;
}
public Meter AddMeter(PointF position)
{
Meter j = new Meter();
int i = 0;
string ID = $"{Default.GetPreString(j)}{i}";
while (Nodes.Find(p0 => p0.ID == ID) != null)
{
i++;
ID = $"{Default.GetPreString(j)}{i}";
}
j.ID = ID;
j.Elev = 0;
j.Demand = 0;
j.Pattern = "";
j.X = position.X;
j.Y = position.Y;
Nodes.Add(j);
meters.Add(j);
return j;
}
public Reservoir AddReservoir(PointF position)
{
Reservoir j = new Reservoir();
int i = 0;
string ID = $"{Default.GetPreString(j)}{i}";
while (Nodes.Find(p0 => p0.ID == ID) != null)
{
i++;
ID = $"{Default.GetPreString(j)}{i}";
}
j.ID = ID;
j.Elev = 0;
j.Demand = 0;
j.Pattern = "";
j.Head = 100;
j.X = position.X;
j.Y = position.Y;
Nodes.Add(j);
reservoirs.Add(j);
return j;
}
public Tank AddTank(PointF position)
{
Tank j = new Tank();
int i = 0;
string ID = $"{Default.GetPreString(j)}{i}";
while (Nodes.Find(p0 => p0.ID == ID) != null)
{
i++;
ID = $"{Default.GetPreString(j)}{i}";
}
j.ID = ID;
j.Elev = 0;
j.Demand = 0;
j.Pattern = "";
j.X = position.X;
j.Y = position.Y;
Nodes.Add(j);
tanks.Add(j);
return j;
}
public Pipe AddPipe(Node node1, Node node2)
{
Pipe pipe = new Pipe();
int i = 0;
string ID = $"{Default.GetPreString(pipe)}{i}";
while (Links.Find(p0 => p0.ID == ID) != null)
{
i++;
ID = $"{Default.GetPreString(pipe)}{i}";
}
pipe.ID = ID;
pipe.Diameter = 100;
pipe.MinorLoss = 0;
pipe.Roughness = 100;
pipe.Status = "1";
pipe.StartNode = node1;
pipe.EndNode = node2;
Links.Add(pipe);
return pipe;
}
public Valve AddValve(Node node1, Node node2)
{
Valve valve = new Valve();
int i = 0;
string ID = $"{Default.GetPreString(valve)}{i}";
while (Links.Find(p0 => p0.ID == ID) != null)
{
i++;
ID = $"{Default.GetPreString(valve)}{i}";
}
valve.ID = ID;
valve.Diameter = 100;
valve.Status = "1";
valve.StartNode = node1;
valve.EndNode = node2;
Links.Add(valve);
valves.Add(valve);
return valve;
}
public Repeater AddRepeater(Node node1, Node node2)
{
Repeater repeater = new Repeater();
int i = 0;
string ID = $"{Default.GetPreString(repeater)}{i}";
while (Links.Find(p0 => p0.ID == ID) != null)
{
i++;
ID = $"{Default.GetPreString(repeater)}{i}";
}
repeater.ID = ID;
repeater.StartNode = node1;
repeater.EndNode = node2;
Links.Add(repeater);
repeaters.Add(repeater);
return repeater;
}
public void ChangePoint(Node source,Node newPoint)
{
Links.ForEach(l =>
{
if (l.StartNode == source)
{
l.StartNode = newPoint;
}
else if (l.EndNode == source)
{
l.EndNode = newPoint;
}
});
source.Visible = false;
//Nodes.Remove(source);
}
public string GetValidID(MapObject obj)
{
string DefaultString = Default.GetPreString(obj);
int i = 0;
string ID = obj.ID;
List objs;
while ((objs = MapObjects.FindAll(p0 => p0.ID == ID)).Count>1 && objs[0]!=obj)
{
ID = $"{DefaultString}{i}";
i++;
}
return ID;
}
public Network CreateNew(PointF3D basepoint)
{
Network net = this.DeepCopy();
var BasePos = StartPoint.ToPointF3D();
net.Nodes.ForEach(n =>
{
n.X += basepoint.X- BasePos.X;
n.Y += basepoint.Y - BasePos.Y;
n.Elev += basepoint.Z - BasePos.Z;
});
return net;
}
public void Add(Network network)
{
if (Nodes.Count == 0)
{
Nodes.AddRange(network.Nodes);
Links.AddRange(network.Links);
StartPoint=network.StartPoint;
EndPoint=network.EndPoint;
}
else
{
ChangePoint(EndPoint, network.StartPoint);
Nodes.AddRange(network.Nodes);
Links.AddRange(network.Links);
//MapObjects.ForEach(n => n.ID = GetValidID(n));
EndPoint = network.EndPoint;
}
}
public void Rename()
{
MapObjects.ForEach(o => o.ID = $"{Name}_{o.ID}");
}
public void Add(Repeater repeater)
{
ChangePoint(repeater.StartNode, repeater.StartNode_inner);
MoveDownNetwork(repeater);
ChangePoint(repeater.EndNode, repeater.EndNode_inner);
Nodes.AddRange(repeater.netList.Nodes);
Links.AddRange(repeater.netList.Links);
}
public void LoadRepeaters(int MaxLevel,dict param, Dictionary viewModel,bool ViewMode=true)
{
//repeaters.ForEach(r =>
//{
// r.Load();
// Add(r);
//});
repeaters.ForEach(r =>
{
if ( !r.Load(MaxLevel,param, viewModel, ViewMode))
{
r.Status = Repeater.RepeatStatus.收起;
r.Visible= true;
}
else
{
Add(r);
r.Visible = false;
}
//if (r.Status == Repeater.RepeatStatus.显示)
//{
//}
});
}
public void MoveDownNetwork(Repeater repeater)
{
visited = new Dictionary();
visited.Add(repeater.StartNode,true);
var p1 = repeater.EndNode_inner;
var p2 = repeater.EndNode;
PointF3D p = new PointF3D(p1.X-p2.X,p1.Y-p2.Y,p1.Elev-p2.Elev);
BFS(this, repeater.EndNode,new Vector3(p.X,p.Y,p.Z));
}
// 定义 visited 字典记录已访问过的节点和待访问的节点队列
Dictionary visited;//= new Dictionary();
public void BFS(Network net, Node startNode,Vector3 vector)
{
Queue queue = new Queue();
// 标记起始节点为已访问,并加入队列
visited[startNode] = true;
queue.Enqueue(startNode);
// 不断从队列中取出节点进行访问,直到队列为空
while (queue.Count > 0)
{
// 取出队首节点并输出
Node node = queue.Dequeue();
node.Move(vector);
// 遍历当前节点的所有未访问邻居节点,并标记为已访问
foreach (Link link in net.Links)
{
if (link.StartNode == node && !visited.ContainsKey(link.EndNode))
{
visited[link.EndNode] = true;
queue.Enqueue(link.EndNode);
}
else if (link.EndNode == node && !visited.ContainsKey(link.StartNode))
{
visited[link.StartNode] = true;
queue.Enqueue(link.StartNode);
}
}
}
}
public List GetNode(string node)
{
return Nodes.FindAll(n => n.ID == node);
}
}
}