ningshuxia
2025-03-28 ba369f7a7b260c356b1b63fdbe603f44b15cfec5
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
using HydroUI;
 
namespace PBS.WinFrmUI.Hydro
{
    public partial class ModelTemplatePage : DocumentPage
    {
        public ModelTemplatePage()
        {
            InitializeComponent();
            this.modelTemplateTreeListCtrl1.SelectModelTemplateEvent += ModelTemplateTreeListCtrl1_SelectModelTemplateEvent;
            this.modelTemplateTreeListCtrl1.RefreshDataEvent += () => { RefreshData(); };
        }
 
 
        private ModelTemplateVmo _modelTemplate = null;
        private Template _template = null;
        private MapViewer _mapView;
        private PropertyForm _propertyForm;
 
        /// <summary>
        /// 初始化数据源
        /// </summary>
        public override void InitialDataSource()
        {
            base.InitialDataSource();
            InitialMapViewer();
            InitialData();
        }
 
        //初始化数据
        private async void InitialData()
        {
            var overlay = this.ShowOverlay();
            var allModelTemplateList = await BLLFactory<BLL.ModelTemplate>.Instance.GetAll();
            this.modelTemplateTreeListCtrl1.SetBindingData(allModelTemplateList);
 
            overlay.Close();
        }
 
        //初始化视图
        private void InitialMapViewer()
        {
            _mapView = new MapViewer();
            _mapView.Lock2DView = true;
            _mapView.ShowPropertyForm = false;
            _mapView.Location = new System.Drawing.Point(0, 0);
            _mapView.Dock = DockStyle.Fill;
 
            this.panelControl1.Controls.Add(_mapView);
 
            _propertyForm = new PropertyForm();
            _propertyForm.Dock = DockStyle.Fill;
            this.dockPanelModelProperty.Controls.Add(_propertyForm);
 
            GlobalObject.PropertyForm = _propertyForm;
            GlobalObject.map = _mapView;
 
        }
 
 
        private void ModelTemplateTreeListCtrl1_SelectModelTemplateEvent(ModelTemplateVmo obj)
        {
 
            if (obj == null)
            {
                _modelTemplate = null;
                _template = null;
                _mapView.Clear();
                return;
            }
 
            if (string.IsNullOrEmpty(obj.ModelPath))
            {
                var filePath = "template\\" + obj.TemplateType + "\\" + obj.ID + ".inp";
                var fullPath = Path.Combine(Directory.GetCurrentDirectory(), filePath);
 
                var directoryPath = Path.GetDirectoryName(fullPath);
                if (!Directory.Exists(directoryPath))
                    Directory.CreateDirectory(directoryPath);
                if (!File.Exists(fullPath))
                    File.Create(fullPath).Close();
 
                obj.ModelPath = fullPath;
            }
 
            if (string.IsNullOrEmpty(obj.ModelInfo))
            {
                obj.ModelInfo = Yw.JsonHelper.Object2Json(new Template()
                {
                    ID = obj.ID.ToString(),
                    Name = obj.Name,
                    Type = obj.TemplateType,
                    filePath = obj.ModelPath,
                });
            }
 
            _modelTemplate = 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);
 
        }
 
 
        public override void RefreshData()
        {
            base.RefreshData();
            InitialData();
        }
 
        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 dict = Path.GetDirectoryName(temp.BackGroundImg_FullPath);
            if (!Directory.Exists(dict))
            {
                Directory.CreateDirectory(dict);
            }
            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 ModelTemplate
        private void barBtnAddModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.modelTemplateTreeListCtrl1.AddModelTemplate();
        }
 
        private void barBtnEditModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.modelTemplateTreeListCtrl1.UpdateModelTemplate();
        }
 
        private void barBtnDeleteModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.modelTemplateTreeListCtrl1.DeleteModelTemplate();
        }
 
 
        /// <summary>
        /// 保存
        /// </summary> 
        private void SaveModelTemplate()
        {
            if (_modelTemplate == null)
            {
                TipFormHelper.ShowWarn("请选择模板");
                return;
            }
            if (_template == null)
            {
                TipFormHelper.ShowWarn("请选择模板");
                return;
            }
            SaveView();
            SetZoom();
 
            _modelTemplate.ModelInfo = Yw.JsonHelper.Object2Json(_template);
            this._mapView.toolStripButton_save_ButtonClick(1, new EventArgs());
            this.modelTemplateTreeListCtrl1.SaveModelTemplate(_modelTemplate);
        }
 
        
 
 
        private void barBtnSaveModelTemplate_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            SaveModelTemplate();
        }
 
        #endregion
 
        #region Import
 
        private void ImportInp()
        {
            if (_modelTemplate == 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
 
 
     
    }
 
}