using System;
|
using System.Collections.Generic;
|
using System.Windows.Forms;
|
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 System.IO;
|
using System.Xml;
|
using Newtonsoft.Json;
|
using Formatting = Newtonsoft.Json.Formatting;
|
using System.Diagnostics;
|
using System.Runtime.Serialization.Formatters.Binary;
|
using System.Threading;
|
using Newtonsoft.Json.Serialization;
|
using System.Reflection;
|
|
namespace CloudWaterNetwork
|
{
|
public partial class Form_selectTemplate : Form
|
{
|
TemplateList templateList = new TemplateList();
|
|
private List<Template> _templates
|
{
|
get
|
{
|
return TemplateList.GetTemplates();
|
}
|
|
}
|
MapViewer map;
|
string _configfilePath = "";
|
public string TemplateID { get; set; }
|
public Form_selectTemplate()
|
{
|
InitializeComponent();
|
|
|
|
// 初始化combobox_type
|
combobox_type.DataSource = Enum.GetValues(typeof(TemplateType));
|
combobox_type.SelectedItem = TemplateType.楼层模板;
|
combobox_type.Visible = false;
|
}
|
public Form_selectTemplate(string TemplateID)
|
{
|
InitializeComponent();
|
this.TemplateID = TemplateID;
|
|
|
|
// 初始化combobox_type
|
combobox_type.DataSource = Enum.GetValues(typeof(TemplateType));
|
combobox_type.SelectedItem = TemplateType.楼层模板;
|
}
|
private void RefreshListBox()
|
{
|
// 获取当前选中的模板类型
|
TemplateType selectedType = (TemplateType)combobox_type.SelectedItem;
|
|
Template temp = listbox_templates.SelectedItem as Template;
|
// 更新listbox_templates
|
listbox_templates.Items.Clear();
|
foreach (var template in _templates)
|
{
|
if (template.Type == selectedType)
|
{
|
listbox_templates.Items.Add(template);
|
}
|
}
|
if (temp != null && listbox_templates.Items.Contains(temp)) listbox_templates.SelectedItem = temp;
|
if (listbox_templates.Items.Count > 0 && listbox_templates.SelectedIndex < 0)
|
listbox_templates.SelectedIndex = 0;
|
}
|
|
private void RefreshPropertyGrid()
|
{
|
// 获取当前选中的模板
|
Template selectedTemplate = (Template)listbox_templates.SelectedItem;
|
_selectTemp = (Template)listbox_templates.SelectedItem;
|
// 更新propertygrid_template
|
propertyGrid1.SelectedObject = selectedTemplate;
|
}
|
Template _selectTemp = null;
|
private void SetMapData()
|
{
|
// 获取当前选中的模板
|
_selectTemp = propertyGrid1.SelectedObject as Template;
|
|
if (_selectTemp!=null)
|
{
|
// 更新propertygrid_template
|
var temp = _selectTemp;
|
|
//Network network = new Network();
|
//temp.network = network;
|
// 创建打开文件对话框
|
string filePath = getTempInpPath(temp);
|
map.SetData(temp);
|
//if (temp.network.loadInpFile(filePath, temp.最高级数))
|
//{
|
// temp.network.LoadRepeaters();
|
|
// map.SetStartEndPoint(temp.Node1, temp.Node2);
|
|
//}
|
//else
|
//{
|
// MessageBox.Show("读取地图失败");
|
//}
|
|
}
|
}
|
string getTempInpPath(Template temp)
|
{
|
return temp.FullPath;//Path.Combine(Directory.GetCurrentDirectory(), temp.filePath);
|
}
|
private void combobox_type_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
RefreshListBox();
|
RefreshPropertyGrid();
|
|
}
|
|
private void listbox_templates_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
RefreshPropertyGrid();
|
if (_selectTemp!=null)
|
TemplateID = _selectTemp.ID;
|
SetMapData();
|
}
|
|
private async void 模板管理_Load(object sender, EventArgs e)
|
{
|
|
//从文件中读取json格式的模板数据
|
|
_configfilePath = Path.Combine(Directory.GetCurrentDirectory(), "templates.json");
|
|
if (File.Exists(_configfilePath))
|
{
|
string jsonString = File.ReadAllText(_configfilePath + ".txt");
|
templateList = JsonConvert.DeserializeObject<TemplateList>(jsonString);
|
//TemplateList.LoadFromFile(_configfilePath);
|
|
}
|
else
|
{
|
// 添加测试数据
|
_templates.Add(new Template(null,"小区模板", @"template\小区模板\", TemplateType.小区模板));
|
_templates.Add(new Template(null, "分区模板", @"template\小区分区模板\", TemplateType.单元分区模板));
|
_templates.Add(new Template(null, "单元模板", @"template\单元模板\", TemplateType.单元模板));
|
_templates.Add(new Template(null, "楼层1", @"template\楼层模板\楼层1.inp", TemplateType.楼层模板));
|
}
|
|
map = new MapViewer();
|
map.Location = new System.Drawing.Point(0, 0);
|
//map.Size = new Size(500, 500);
|
map.Dock = DockStyle.Fill;
|
|
//gen();
|
|
splitContainer2.Panel2.Controls.Add(map);
|
|
//if (GlobalObject.PropertyForm==null)
|
//{
|
// GlobalObject.PropertyForm = new PropertyForm();
|
// GlobalObject.PropertyForm.SetWindows(this);
|
// GlobalObject.PropertyForm.Show();
|
|
//}
|
if (GlobalObject.map == null)
|
{
|
GlobalObject.map = map;
|
|
}
|
//Global.TemplateList = templateList;
|
if (TemplateID!=null)
|
{
|
var tp = _templates.Find(t => t.ID == TemplateID);
|
if (tp != null)
|
{
|
combobox_type.SelectedItem = tp.Type;
|
listbox_templates.SelectedItem = tp;
|
}
|
|
|
}
|
RefreshListBox();
|
RefreshPropertyGrid();
|
|
}
|
private void 模板管理_FormClosing(object sender, FormClosingEventArgs e)
|
{
|
|
}
|
|
private void 导入形状ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
//_selectTemp = propertyGrid1.SelectedObject as Template;
|
if (_selectTemp == null) return;
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
openFileDialog.Filter = "Temporary Files (*.inp)|*.inp";
|
openFileDialog.InitialDirectory = Directory.GetCurrentDirectory() + $@"\template\{combobox_type.SelectedItem}";
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
string filePath = openFileDialog.FileName;
|
// 使用 filePath 变量来打开文件
|
_selectTemp.filePath = filePath;
|
|
}
|
}
|
private void 导出形状ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
//var temp = propertyGrid1.SelectedObject as Template;
|
if (_selectTemp == null) return;
|
SaveFileDialog saveFileDialog = new SaveFileDialog();
|
|
saveFileDialog.Filter = "Inp Files (*.inp)|*.inp";
|
saveFileDialog.InitialDirectory = Directory.GetCurrentDirectory() + $@"\template\{combobox_type.SelectedItem}";
|
saveFileDialog.FileName = _selectTemp.名称;
|
if (saveFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
string filePath = saveFileDialog.FileName;
|
// 使用 filePath 变量来保存文件
|
//temp.路径 = filePath;
|
_selectTemp.Export(filePath);
|
}
|
}
|
|
private void 新增模板ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
|
}
|
|
private void toolStripMenuItem1_Click(object sender, EventArgs e)
|
{
|
//var temp = propertyGrid1.SelectedObject as Template;
|
if (_selectTemp == null) return;
|
Process.Start(@"epanetH\Epanet2wH.exe", getTempInpPath(_selectTemp));
|
}
|
|
private void 模板管理_Move(object sender, EventArgs e)
|
{
|
//if (GlobalObject.PropertyForm!=null) GlobalObject.PropertyForm.SetWindows(this);
|
|
}
|
|
private void 曲线拟合ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
//Form_Curve fc = new Form_Curve();
|
//fc.Show();
|
}
|
private void 保存ToolStripMenuItem1_Click(object sender, EventArgs e)
|
{
|
//TemplateList.SaveToFile(_configfilePath);
|
JsonSerializerSettings settings = new JsonSerializerSettings
|
{
|
Formatting = Formatting.Indented,
|
ContractResolver = new ShouldSerializeContractResolver(),
|
};
|
string json = JsonConvert.SerializeObject(templateList, Formatting.Indented, settings);
|
File.WriteAllText(_configfilePath + ".txt", json);
|
}
|
|
private void 序列化ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
JsonSerializerSettings settings = new JsonSerializerSettings
|
{
|
Formatting = Formatting.Indented,
|
ContractResolver = new ShouldSerializeContractResolver(),
|
};
|
string json = JsonConvert.SerializeObject(templateList, Formatting.Indented,settings);
|
File.WriteAllText(_configfilePath+".txt", json);
|
}
|
|
private void 序列化读取ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
|
//从文件中读取json格式的模板数据
|
string jsonString = File.ReadAllText(_configfilePath + ".txt");
|
templateList = JsonConvert.DeserializeObject<TemplateList>(jsonString);
|
RefreshListBox();
|
RefreshPropertyGrid();
|
}
|
|
private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
|
{
|
RefreshListBox();
|
RefreshPropertyGrid();
|
}
|
|
private void toolStripButton_新增_Click(object sender, EventArgs e)
|
{
|
// 获取当前选中的模板类型
|
TemplateType selectedType = (TemplateType)combobox_type.SelectedItem;
|
int i = 0;
|
while (true)
|
{
|
if (_templates.Find(t => t.名称 == $"{combobox_type.SelectedItem}_{i}") == null) break;
|
i++;
|
}
|
_templates.Add(new Template(Guid.NewGuid().ToString(), $"{combobox_type.SelectedItem}_{i}", $@"\template\{combobox_type.SelectedItem}\{combobox_type.SelectedItem}_{i}.inp", selectedType));
|
|
RefreshListBox();
|
RefreshPropertyGrid();
|
}
|
|
private void toolStripButton_删除_Click(object sender, EventArgs e)
|
{
|
if (message.show("确认删除吗?","删除提醒"))
|
_templates.Remove(_selectTemp);
|
}
|
|
private void 保存ToolStripMenuItem_Click_1(object sender, EventArgs e)
|
{
|
if (_selectTemp == null) return;
|
_selectTemp.Export(getTempInpPath(_selectTemp));
|
MessageBox.Show("保存成功");
|
}
|
|
private void 模板管理_SizeChanged(object sender, EventArgs e)
|
{
|
//if (GlobalObject.PropertyForm != null) GlobalObject.PropertyForm.SetWindows(this);
|
}
|
|
private void toolStripButton_复制_Click(object sender, EventArgs e)
|
{
|
if (_selectTemp == null) return;
|
// 获取当前选中的模板类型
|
TemplateType selectedType = (TemplateType)combobox_type.SelectedItem;
|
var newTemp = new Template(Guid.NewGuid().ToString(), $"{_selectTemp.名称}_复制", _selectTemp.filePath, _selectTemp.Type);
|
newTemp.MaxLevel = _selectTemp.MaxLevel;
|
newTemp.Node1 = _selectTemp.Node1;
|
newTemp.Node2 = _selectTemp.Node2;
|
_templates.Add(newTemp);
|
|
RefreshListBox();
|
RefreshPropertyGrid();
|
}
|
|
private void toolStripButton_刷新_Click(object sender, EventArgs e)
|
{
|
//map.SetInvalidated();
|
map.LoadData();
|
}
|
|
private void propertyGrid1_PropertyValueChanged_1(object s, PropertyValueChangedEventArgs e)
|
{
|
map.LoadData();
|
}
|
|
private void button1_Click(object sender, EventArgs e)
|
{
|
this.DialogResult = DialogResult.OK;
|
this.Close();
|
}
|
|
private void button2_Click(object sender, EventArgs e)
|
{
|
this.Close();
|
}
|
}
|
|
public class ShouldSerializeContractResolver : DefaultContractResolver
|
{
|
protected override JsonProperty CreateProperty(MemberInfo member, MemberSerialization memberSerialization)
|
{
|
JsonProperty property = base.CreateProperty(member, memberSerialization);
|
|
if (property.DeclaringType == typeof(Template) && member.Name == "network")
|
{
|
property.ShouldSerialize = instance => false;
|
}
|
|
return property;
|
}
|
}
|
|
|
|
|
|
}
|