cloudflight
2023-12-02 c0f9915265878e56e91ee97f7f8d925db1e12626
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
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;
        }
    }
 
    
 
 
    
}