ningshuxia
9 天以前 018bfb9c78088d9cd7b9371edcd2102abd594b4d
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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
using HydroUI;
 
namespace PBS.WinFrmUI.Hydro
{
    public partial class ModelEditPage : DocumentPage
    {
        public ModelEditPage() 
        { 
            InitializeComponent();
            this.facilityTreeListCtrl1.SelectFacilityEvent += FacilityTreeListCtrl1_SelectFacilityEvent;
            this.facilityTreeListCtrl1.RefreshDataEvent += () => { RefreshData(); }; 
        }
 
        
        private FacilityVmo _facility = null;
        private Template _template = null;
        private MapViewer _mapView;
        private PropertyForm _propertyForm;
        private bool _isInitial = false;
 
 
        /// <summary>
        /// 初始化数据源
        /// </summary>
        public override async void InitialDataSource()
        {
            var overlay = this.ShowOverlay();
            base.InitialDataSource();
            if (!_isInitial)
            {
                InitialMapViewer();
                _isInitial = true;
            }
 
            var allFacilityList = await BLLFactory<BLL.Facility>.Instance.GetAll();
            this.facilityTreeListCtrl1.SetBindingData(allFacilityList);
 
            overlay.Close();
        }
 
        //选择设施
        private void FacilityTreeListCtrl1_SelectFacilityEvent(FacilityVmo obj)
        {
            InitialData(obj);
        }
 
 
 
        /// <summary>
        /// 初始化数据
        /// </summary> 
        public void InitialData(FacilityVmo obj)
        {
            if (obj == null)
            {
                _facility = null;
                _template = null;
                _mapView.Clear();
                return;
            }
 
            if (string.IsNullOrEmpty(obj.ModelPath))
            {
                var fullPath = Path.Combine(Directory.GetCurrentDirectory(), "Data\\Inp\\" + obj.ID + ".inp");
                var directoryPath = Path.GetDirectoryName(fullPath);
                if (!Directory.Exists(directoryPath))
                    Directory.CreateDirectory(directoryPath);
                File.Create(fullPath).Close();
                obj.ModelPath = fullPath;
            }
 
 
            if (string.IsNullOrEmpty(obj.ModelInfo))
            {
                var templateTemp = new Template()
                {
                    ID = obj.ID.ToString(),
                    Name = obj.Name,
                    Type = eModelTemplateType.Custom,
                    filePath = obj.ModelPath,
                };
                obj.ModelInfo = Yw.JsonHelper.Object2Json(templateTemp);
 
            }
 
            _facility = obj;
            _template = Yw.JsonHelper.Json2Object<Template>(obj.ModelInfo);
 
 
            var template = _template;
            template.network = new MapViewNetWork();
            template.network.BuildFromInp(Path.Combine(Directory.GetCurrentDirectory(), _template.filePath));
            _mapView.SetData(template);
 
        }
 
 
        //初始化视图
        private void InitialMapViewer()
        {
            _mapView = new MapViewer();
            _mapView.ShowPropertyForm = false;
            _mapView.Dock = DockStyle.Fill;
            _mapView.mapOption.isShowPic = true;
            _mapView.mapOption.IsOrtho = false;
            this.panelControl1.Controls.Add(_mapView);
 
            _propertyForm = new PropertyForm();
            _propertyForm.Dock = DockStyle.Fill;
            this.dockPanelModelProperty.Controls.Add(_propertyForm);
 
            GlobalObject.PropertyForm = _propertyForm;
            GlobalObject.map = _mapView;
 
        }
 
 
        /// <summary>
        /// 刷新数据源
        /// </summary>
        public override void RefreshData()
        {
            base.RefreshData();
            InitialDataSource();
        }
 
        //设置背景图
        private void SetMapBackground(string backgroundUrl, float width, float height)
        {
            _mapView.mapOption.isShowPic = true;
            _mapView.mapOption.isAutoBackgroundImage = true;
            var img = Image.FromFile(backgroundUrl);
            var imgWidth = img.Width;
            var imgHeight = img.Height;
 
            var w = imgWidth / width;
            var h = imgHeight / height;
            var zoom = w > h ? w : h;
 
            _mapView.MapCenter = _mapView.mapOption.Center = new PointF(width / 2, height / 2);
            _mapView.zoom = _mapView.mapOption.zoom = zoom;
 
            var temp = _mapView._Template;
            var directory = Path.GetDirectoryName(temp.BackGroundImg_FullPath);
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }
            File.Copy(backgroundUrl, temp.BackGroundImg_FullPath, true);
            temp.BackGroundImg_FullPath = temp.BackGroundImg_FullPath;
            temp.BackGroundImgWidth = width;
            temp.BackGroundImgHeight = height;
            temp.BackGroundImgRotaAngle = 0;
            temp.BackGroundPoint1 = new PointF(0, 0);
            _mapView._Template = temp;
            _mapView.SetMapInvalidate();
 
        }
 
 
 
        #region Event
 
        #region Draw
 
        private void toolboxControlDraw_ItemClick(object sender, DevExpress.XtraToolbox.ToolboxItemClickEventArgs e)
        {
            if (e.Item.Tag is not string tag)
                return;
            switch (tag)
            {
                case "Select":
                    {
                        _mapView.toolStripButton_普通_Click(1, new EventArgs());
                    }
                    break;
                case "AddNode":
                    {
                        _mapView.toolStripButton_新建节点_Click(1, new EventArgs());
                    }
                    break;
                case "AddVerticalPipe":
                    {
                        _mapView.toolStripButton_新建立管_Click(1, new EventArgs());
                    }
                    break;
                case "AddHorizontalPipe":
                    {
                        _mapView.toolStripButton_新建管线_Click(1, new EventArgs());
                    }
                    break;
                case "AddValve":
                    {
                        _mapView.toolStripButton_添加阀门_Click(1, new EventArgs());
                    }
                    break;
                case "AddPump":
                    {
                        _mapView.toolStripButton_添加水泵_Click(1, new EventArgs());
                    }
                    break;
                case "AddWaterMeter":
                    {
                        _mapView.toolStripButton_添加水表_Click(1, new EventArgs());
                    }
                    break;
                case "AddTank":
                    {
                        _mapView.toolStripButton_添加水池_Click(1, new EventArgs());
                    }
                    break;
                case "AddReservoir":
                    {
                        _mapView.toolStripButton_添加水库_Click(1, new EventArgs());
                    }
                    break;
                case "Copy":
                    {
                        _mapView.复制ToolStripMenuItem_Click(1, new EventArgs());
                    }
                    break;
                case "Paste":
                    {
                        _mapView.粘贴ToolStripMenuItem1_Click(1, new EventArgs());
                    }
                    break;
                case "Delete":
                    {
                        _mapView.删除ToolStripMenuItem_Click(1, new EventArgs());
                    }
                    break;
 
                case "Undo":
                    {
                        _mapView.buttonUndo_Click(1, new EventArgs());
                    }
                    break;
                case "Redo":
                    {
                        _mapView.buttonRedo_Click(1, new EventArgs());
                    }
                    break;
                case "HorizontalAlign":
                    {
                        _mapView.东西对齐ToolStripMenuItem_Click(1, new EventArgs());
                    }
                    break;
                case "VerticalAlign":
                    {
                        _mapView.南北对齐ToolStripMenuItem_Click(1, new EventArgs());
                    }
                    break;
                case "TopBottomAlign":
                    {
                        _mapView.竖直对齐ToolStripMenuItem_Click(1, new EventArgs());
                    }
                    break;
                case "AutoAlign":
                    {
                        _mapView.自动对齐ToolStripMenuItem_Click(1, new EventArgs());
                    }
                    break;
                default:
                    break;
            }
 
 
        }
 
 
 
        private void toolTipController1_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e)
        {
            var hi = this.toolboxControlDraw.CalcHitInfo(e.ControlMousePosition);
            if (hi.IsInItem)
            {
                var toolItem = hi.ItemInfo.Item;
                this.toolTipController1.ShowHint(toolItem.Caption, MousePosition);
            }
            else
            {
                this.toolTipController1.HideHint();
            }
        }
 
        #endregion
 
        #region QuickModeling
 
        private void barBtnModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new SelectModelTemplateDlg();
            dlg.SetBindingData();
            dlg.ReloadDataEvent += (template) =>
            {
                _mapView.InsertNet(template);
            };
            dlg.ShowDialog();
        }
 
        private void barBtnConnectivityCheck_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _mapView.btn_拓扑检查_Click(1, new EventArgs());
        }
 
        private void barBtnVerticalPipeDiameter_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new InputDlg();
            dlg.SetBindingData("输入口径");
            dlg.ReloadDataEvent += (value) =>
            {
                _mapView._Template.network.Links.ForEach(l =>
                {
                    if (l.StartNode != null && l.EndNode != null)
                    {
                        if (l.StartNode.Position == l.EndNode.Position)
                        {
                            l.Diameter = (float)value;
                        }
                    }
                });
            };
        }
 
        private void barBtnHorizontalPipeDiameter_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new InputDlg();
            dlg.SetBindingData("输入口径");
            dlg.ReloadDataEvent += (value) =>
            {
                _mapView._Template.network.Links.ForEach(l =>
                {
                    if (l.StartNode.Elev == 0 && l.EndNode.Elev == 0)
                    {
                        l.Diameter = (float)value;
                    }
                });
            };
 
        }
 
        private void barBtnElevationTranslation_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            var dlg = new InputDlg();
            dlg.SetBindingData("输入标高偏移量");
            dlg.ReloadDataEvent += (value) =>
            {
                _mapView._Template.network.Nodes.ForEach(n =>
                {
                    if (n.Elev > 0)
                    {
                        n.Elev = n.Elev + (float)value;
                    }
                });
            };
            dlg.ShowDialog();
 
        }
 
        private void HydraulicCalc()
        {
            _template.network.CalcLinkMinorLoss();
 
        }
 
        private void barBtnHydraulicCalc_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            HydraulicCalc();
        }
 
 
        /// <summary>
        /// 保存
        /// </summary> 
        private async void SaveModelTemplate()
        {
            if (_facility == null)
            {
                TipFormHelper.ShowWarn("请选择模板");
                return;
            }
            if (_template == null)
            {
                TipFormHelper.ShowWarn("请选择模板");
                return;
            }
            SaveView();
            SetZoom();
 
            var filePath = "Data\\Inp\\" + _facility.ID + ".inp";
            var fullPath = Path.Combine(Directory.GetCurrentDirectory(), filePath);
            if (_facility.ModelPath.Contains("temp"))
            {
                var tempInpFile = _facility.ModelPath;
                File.Copy(tempInpFile, fullPath, true);
                if (File.Exists(tempInpFile))
                    File.Delete(tempInpFile);
            }
            _facility.ModelPath = filePath;
            _template.filePath = filePath; 
 
            _template.network.CalcLinkMinorLoss(); 
            _facility.ModelInfo = Yw.JsonHelper.Object2Json(_template);
            this._mapView.toolStripButton_save_ButtonClick(1, new EventArgs());
 
            if (!await BLLFactory<BLL.Facility>.Instance.Update(_facility))
            {
                TipFormHelper.ShowError("保存模型信息失败!");
                return;
            }
 
            TipFormHelper.ShowSucceed("保存模型信息成功!");
        }
         
 
        private void barBtnSaveModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveModelTemplate();
        }
 
        private void barBtnResetModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            RefreshData();
        }
 
 
        #endregion
 
        #region Import
 
        private void ImportInp()
        {
            if (_facility == null)
            {
                return;
            }
            if (_template == null)
            {
                return;
            }
            var openFileDlg = new OpenFileDialog();
            openFileDlg.Filter = "INP文件|*.inp";
            if (openFileDlg.ShowDialog() != DialogResult.OK)
                return;
 
            _mapView.Clear();
 
            var inpFilePath = openFileDlg.FileName;
            var templateFilePath = _template.FullPath;
 
            HelperC.ClearFileReadOnly(templateFilePath);
            File.Copy(inpFilePath, templateFilePath, true);
            _template.network.BuildFromInp(templateFilePath);
 
            _mapView.SetData(_template);
 
        }
 
 
        private void barBtnImportInp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            ImportInp();
        }
 
        #endregion
 
        #region BaseMap
 
        private void SetBaseMap()
        {
            var dlg = new SetMapBaseMapDlg();
            dlg.ReloadDataEvent += (filePath, widht, height) =>
            {
                SetMapBackground(filePath, (float)widht, (float)height);
            };
            dlg.ShowDialog();
        }
 
        private void barBtnAddBaseMap_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetBaseMap();
        }
 
        private void barBtnSetBaseMap_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _mapView.设置底图ToolStripMenuItem_Click(1, null);
        }
 
        private void barBtnClearBaseMap_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _mapView.清除底图ToolStripMenuItem_Click(1, null);
        }
 
        private void barCekBaseMapVisible_CheckedChanged(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _mapView.显示隐藏底图ToolStripMenuItem_Click(1, null);
        }
 
 
        #endregion
 
        #region View
 
        private void SetDefaultView()
        {
            var mapOption0 = _mapView.mapOption.Copy();
            var p = PointF.Empty;
            float x0 = 99999999999f, y0 = 99999999999f, x1 = -99999999999f, y1 = -99999999999f;
 
            foreach (NodeViewModel junction in _mapView._Nodes)
            {
                p.X += (float)junction.X;
                p.Y += (float)junction.Y;
                if (x0 > junction.X) x0 = junction.X;
                if (y0 > junction.Y) y0 = junction.Y;
                if (x1 < junction.X) x1 = junction.X;
                if (y1 < junction.Y) y1 = junction.Y;
            }
 
            float x_span = x1 - x0, y_span = y1 - y0;
 
            _mapView.zoom = Math.Min(this._mapView.Width / x_span, this._mapView.Height / y_span);
            _mapView.zoom = Math.Max(_mapView.zoom, 0.1f);
            _mapView.zoom = Math.Min(_mapView.zoom, 1000.0f);
 
            _mapView.Rotation = 0;
            _mapView.RotationF = 90;
 
 
            if (_mapView._Nodes.Count > 0)
            {
                p.X /= _mapView._Nodes.Count;
                p.Y /= _mapView._Nodes.Count;
            }
            _mapView.MapCenter = p;
            _mapView.RotationF = 45;
            _mapView.Rotation = -45;
            MapObjectExtensions.AddCommand(_mapView.mapOption, "Map", mapOption0, _mapView.mapOption);
 
            _mapView.SetMapInvalidate();
        }
 
        private void barBtnDefaultView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetDefaultView();
        }
 
        private void barBtnFullView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _mapView.重置视角ToolStripMenuItem_Click(1, new EventArgs());
        }
 
        private void SetTopView()
        {
            var mapOption0 = _mapView.mapOption.Copy();
            _mapView.RotationF = 90;
            MapObjectExtensions.AddCommand(_mapView.mapOption, "Map", mapOption0, _mapView.mapOption);
 
            _mapView.SetMapInvalidate();
        }
 
        private void barBtnTopView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetTopView();
        }
 
        private void SetFrontView()
        {
            var mapOption0 = _mapView.mapOption.Copy();
            _mapView.RotationF = 0;
            MapObjectExtensions.AddCommand(_mapView.mapOption, "Map", mapOption0, _mapView.mapOption);
 
            _mapView.SetMapInvalidate();
        }
 
        private void barBtnFrontView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SetFrontView();
        }
 
        private void barBtnResetView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _mapView.重置视角ToolStripMenuItem_Click(1, new EventArgs());
        }
 
 
        private void SetZoom()
        {
            if (_template == null) return;
            _template.view ??= new MapDimensions();
            _template.view.Center = _mapView.MapCenter;
            _template.view.zoom = _mapView.zoom;
            _template.view.rotation = _mapView.Rotation;
            _template.view.rotationF = _mapView.RotationF;
 
        }
 
        private void SaveView()
        {
            if (_template == null) return;
            _template.view ??= new MapDimensions();
            _template.view.Center = _mapView.MapCenter;
            _template.view.zoom = _mapView.zoom;
            _template.view.rotation = _mapView.Rotation;
            _template.view.rotationF = _mapView.RotationF;
 
        }
 
 
        private void barBtnSaveView_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveView();
        }
        #endregion
 
        #endregion
 
 
   
    }
 
}