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;
|
using Hydro.MapView;
|
using static Hydro.MapView.MapViewEnum;
|
using CommonBase;
|
|
namespace Hydro.MapUI
|
{
|
public partial class 模板管理 : Form
|
{
|
TemplateList templateList = new TemplateList();
|
public string TemplateID { get; set; } = null;
|
private List<Template> _templates
|
{
|
get
|
{
|
return TemplateList.GetTemplates();
|
}
|
|
}
|
MapViewer map;
|
string _configfilePath = "";
|
|
public 模板管理(bool isSelect=false)
|
{
|
InitializeComponent();
|
if (isSelect)
|
{
|
this.splitContainer3.SplitterDistance = 690;
|
this.FormBorderStyle = FormBorderStyle.None;
|
}
|
else
|
{
|
this.splitContainer3.SplitterDistance = 734;
|
this.panelControl1.Visible = false;
|
}
|
}
|
|
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 (selectedType==TemplateType.全部 || 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;
|
|
// 更新propertygrid_template
|
propertyGrid1.SelectedObject = selectedTemplate;
|
}
|
Template _selectTemp = null;
|
private void SetMapData()
|
{
|
// 获取当前选中的模板
|
_selectTemp = propertyGrid1.SelectedObject as Template;
|
|
if (_selectTemp!=null)
|
{
|
// 更新propertygrid_template
|
var temp = _selectTemp;
|
|
//MapViewNetWork network = new MapViewNetWork();
|
//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("读取地图失败");
|
//}
|
|
}
|
}
|
|
void RefreshAll()
|
{
|
RefreshListBox();
|
RefreshPropertyGrid();
|
SetMapData();
|
}
|
|
string getTempInpPath(Template temp)
|
{
|
return temp.FullPath;
|
}
|
private void combobox_type_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
RefreshListBox();
|
RefreshPropertyGrid();
|
|
}
|
|
private void listbox_templates_SelectedIndexChanged(object sender, EventArgs e)
|
{
|
RefreshPropertyGrid();
|
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);
|
// templateList = JsonConvert.DeserializeObject<TemplateList>(jsonString);
|
// //TemplateList.LoadFromFile(_configfilePath);
|
//}
|
//else
|
//{
|
|
//}
|
|
map = new MapViewer(true);
|
map.Location = new System.Drawing.Point(0, 0);
|
//map.Size = new Size(500, 500);
|
map.Dock = DockStyle.Fill;
|
map.Lock2DView=true;
|
//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;
|
|
}
|
if (!TemplateList.Inited) TemplateList.Init();
|
// 初始化combobox_type
|
combobox_type.DataSource = Enum.GetValues(typeof(TemplateType));
|
combobox_type.SelectedItem = TemplateType.楼层模板;
|
combobox_type.Visible = false;
|
//combobox_type.Items.Add(TemplateType.楼层模板);
|
RefreshListBox();
|
RefreshPropertyGrid();
|
|
}
|
|
private void button1_Click(object sender, EventArgs e)
|
{
|
textBox1.Text = "0";
|
}
|
|
private void textBox1_TextChanged(object sender, EventArgs e)
|
{
|
double d = 0;
|
if (!double.TryParse(textBox1.Text, out d)) return;
|
trackBar1.Value = (int)d;
|
map.SetRotation(d);
|
}
|
|
private void trackBar1_Scroll(object sender, EventArgs e)
|
{
|
textBox1.TextChanged -= textBox1_TextChanged;
|
textBox1.Text = trackBar1.Value.ToString();
|
textBox1.TextChanged += textBox1_TextChanged;
|
map.SetRotation(trackBar1.Value);
|
}
|
|
private void checkBox1_CheckedChanged(object sender, EventArgs e)
|
{
|
double jiaodu = 45;
|
List<RadioButton> radioButtons = new List<RadioButton>() { radioButton1, radioButton2, radioButton4, radioButton5,radioButton3 };
|
radioButtons.ForEach(r =>
|
{
|
if (r.Checked)
|
{
|
jiaodu=double.Parse(r.Text);
|
}
|
});
|
if (checkBox1.Checked == false) jiaodu = 90;
|
map.Set3DView(checkBox1.Checked, jiaodu);
|
}
|
|
|
|
private void 模板管理_FormClosing(object sender, FormClosingEventArgs e)
|
{
|
saveAll();
|
}
|
|
private void 导入形状ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
//_selectTemp = propertyGrid1.SelectedObject as Template;
|
if (_selectTemp == null) return;
|
string CurDir = Directory.GetCurrentDirectory().ToLower();
|
OpenFileDialog openFileDialog = new OpenFileDialog();
|
openFileDialog.Filter = "Temporary Files (*.inp)|*.inp";
|
openFileDialog.InitialDirectory =Path.Combine(CurDir, $@"\template\{combobox_type.SelectedItem}");
|
|
if (openFileDialog.ShowDialog() == DialogResult.OK)
|
{
|
string filePath = openFileDialog.FileName.ToLower();
|
// 使用 filePath 变量来打开文件
|
|
if (filePath.IndexOf(CurDir)==0)
|
_selectTemp.filePath = filePath.Replace(CurDir,"");
|
else
|
_selectTemp.filePath = filePath;
|
}
|
RefreshAll();
|
}
|
|
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);
|
|
|
saveAll();
|
}
|
public void saveAll()
|
{
|
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);
|
var pathName=$@"template\{newTemp.Type}\{newTemp.名称}.inp";
|
newTemp.filePath = pathName;
|
File.Copy(_selectTemp.FullPath, newTemp.FullPath,true);
|
|
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 设为默认视角ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if (_selectTemp!=null)
|
{
|
if (_selectTemp.view == null) _selectTemp.view = new MapDimensions();
|
_selectTemp.view.Center = map.MapCenter;
|
_selectTemp.view.zoom = map.zoom;
|
_selectTemp.view.rotation = map.Rotation;
|
_selectTemp.view.rotationF = map.RotationF;
|
MessageBox.Show("设置成功");
|
RefreshAll();
|
}
|
|
}
|
|
private void 恢复默认视角ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
if(_selectTemp!=null && _selectTemp.view != null)
|
map.MapCenter = _selectTemp.view.Center;
|
map.zoom = _selectTemp.view.zoom;
|
map.Rotation = _selectTemp.view.rotation;
|
map.RotationF = _selectTemp.view.rotationF;
|
map.SetInvalidated();
|
}
|
|
private void 属性栏ToolStripMenuItem_Click(object sender, EventArgs e)
|
{
|
GlobalObject.PropertyForm.Show();
|
}
|
|
private void btn_OK_Click(object sender, EventArgs e)
|
{
|
this.TemplateID = _selectTemp.ID;
|
this.DialogResult=DialogResult.OK;
|
this.Close();
|
}
|
|
private void btn_Cancel_Click(object sender, EventArgs e)
|
{
|
this.DialogResult = DialogResult.Cancel;
|
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;
|
}
|
}
|
|
|
|
|
|
}
|