lixiaojun
2025-04-18 28ba54f194f1301c45aa30b44cd7b612855b8963
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
namespace Yw.WpfUI.Hydro
{
    /// <summary>
    /// 抽象管理器
    /// </summary>
    internal class LogicalManager
    {
        public LogicalManager(HelixViewport3D viewport)
        {
            _viewport = viewport;
            _stateHelper = new LogicalStateHelper();
            _materialHelper = new LogicalMaterialHelper();
            _overrideColorHelper = new LogicalOverrideColorHelper();
            _overrideOpacityHelper = new LogicalOverrideOpacityHelper();
            _overrideVisibleHelper = new LogicalOverrideVisibleHelper();
            _backgroundHelper = new LogicalBackgroudHelper(_viewport);
            _gridLinesHelper = new LogicalGridLineHelper(_viewport);
            _highlightHelper = new LogicalHighlightHelper(_viewport);
            _selectionHelper = new LogicalSelectionHelper(_viewport);
            _zoomHelper = new LogicalZoomHelper(_viewport);
            _billboardTextHelper = new LogicalBillboardLeadLabelHelper(_viewport);
        }
 
        #region 事件集合
 
        /// <summary>
        /// 选择改变事件
        /// </summary>
        public event Action<List<VisualL3d>> SelectionChangedEvent;
 
        #endregion
 
        #region 固定资源
 
        protected readonly HelixViewport3D _viewport = null;//三维组件
        protected readonly LogicalStateHelper _stateHelper = null;//状态辅助类
        protected readonly LogicalMaterialHelper _materialHelper = null;//材质辅助类
        protected readonly LogicalOverrideColorHelper _overrideColorHelper = null;//覆盖颜色辅助类
        protected readonly LogicalOverrideOpacityHelper _overrideOpacityHelper = null;//覆盖透明度辅助类
        protected readonly LogicalOverrideVisibleHelper _overrideVisibleHelper = null;//覆盖可见性辅助类
        protected readonly LogicalBackgroudHelper _backgroundHelper = null;//背景辅助类
        protected readonly LogicalGridLineHelper _gridLinesHelper = null;//网格线辅助类
        protected readonly LogicalHighlightHelper _highlightHelper = null;//高亮辅助类
        protected readonly LogicalSelectionHelper _selectionHelper = null;//选择辅助类
        protected readonly LogicalZoomHelper _zoomHelper = null;//缩放辅助类
        protected readonly LogicalBillboardLeadLabelHelper _billboardTextHelper = null;//公告版文字辅助类
 
        #endregion
 
        #region 私有字段
 
        protected NetworkL3d _nw = null;
        protected Dictionary<string, VisualL3d> _allVisualL3dDict;
        protected Dictionary<VisualL3d, LogicalVisual3D> _allVisualLogicalDict;
 
        #endregion
 
        #region 判断字段
 
        /// <summary>
        /// 是否启用缩放
        /// </summary>
        public virtual bool IsZoomEnabled
        {
            get { return _viewport.IsZoomEnabled; }
            set { _viewport.IsZoomEnabled = value; }
        }
 
        /// <summary>
        /// 是否启用平移
        /// </summary>
        public virtual bool IsPanEnabled
        {
            get { return _viewport.IsPanEnabled; }
            set { _viewport.IsPanEnabled = value; }
        }
 
        /// <summary>
        /// 是否启用旋转
        /// </summary>
        public virtual bool IsRotationEnabled
        {
            get { return _viewport.IsRotationEnabled; }
            set { _viewport.IsRotationEnabled = value; }
        }
 
        /// <summary>
        /// 是否启用高亮
        /// </summary>
        public virtual bool IsHighlightEnabled
        {
            get { return _isHighlightEnabled; }
            set { _isHighlightEnabled = value; }
        }
        protected bool _isHighlightEnabled = true;
 
        /// <summary>
        /// 是否启用选择
        /// </summary>
        public virtual bool IsSelectionEnabled
        {
            get => _isSelectionEnabled;
            set => _isSelectionEnabled = value;
        }
        protected bool _isSelectionEnabled = true;
 
        /// <summary>
        /// 是否启用缩放适应
        /// </summary>
        public virtual bool IsZoomExtentsEnabled
        {
            get => _isZoomExtentsEnabled;
            set => _isZoomExtentsEnabled = value;
        }
        protected bool _isZoomExtentsEnabled = true;
 
        #endregion
 
        #region 初始化
 
        /// <summary>
        /// 是否初始化
        /// </summary>
        public virtual bool Initialized
        {
            get
            {
                if (_nw == null)
                {
                    return false;
                }
                if (_allVisualL3dDict == null)
                {
                    return false;
                }
                if (_allVisualLogicalDict == null)
                {
                    return false;
                }
                return true;
            }
        }
 
        /// <summary>
        /// 初始化
        /// </summary>
        public virtual void Initial(NetworkL3d nw)
        {
            _nw = nw;
            InitialNetwork();
            InitialViewport();
            RegisterEvents();
        }
 
        //加载管网
        protected virtual void InitialNetwork()
        {
            if (_nw == null)
            {
                return;
            }
            _allVisualL3dDict = _nw.Visuals.ToDictionary(keySelector: x => x.Id);
            _allVisualLogicalDict = new Dictionary<VisualL3d, LogicalVisual3D>();
            _nw.Visuals?.ForEach(x =>
            {
                var logicalVisual = LogicalCreateHelper.Create(x, _stateHelper, _materialHelper, _overrideColorHelper, _overrideOpacityHelper, _overrideVisibleHelper);
                if (logicalVisual != null)
                {
                    _allVisualLogicalDict.Add(x, logicalVisual);
                }
            });
        }
 
        //初始化Vieport
        protected virtual void InitialViewport()
        {
            InitialViewportInput();
            InitialViewportCube();
            InitialViewportData();
        }
 
        //初始化Viewport立方体
        protected virtual void InitialViewportCube()
        {
            if (_viewport == null)
            {
                return;
            }
            _viewport.ViewCubeBackText = "后";
            _viewport.ViewCubeBottomText = "下";
            _viewport.ViewCubeFrontText = "前";
            _viewport.ViewCubeLeftText = "左";
            _viewport.ViewCubeRightText = "右";
            _viewport.ViewCubeTopText = "上";
        }
 
        //初始化Viewport交互
        protected virtual void InitialViewportInput()
        {
            if (_viewport == null)
            {
                return;
            }
            //当前操作不做任何处理,是为了避免鼠标操作冲突
        }
 
        //初始化Viewport数据
        protected virtual void InitialViewportData()
        {
            if (_viewport == null)
            {
                return;
            }
            _viewport.Children.Clear();
            _viewport.Children.Add(new SunLight());
            _allVisualLogicalDict?.Values.ToList().ForEach(x => _viewport.Children.Add(x));
            _viewport.ZoomExtents();
        }
 
 
        #endregion
 
        #region 事件注册
 
        //注册事件
        protected virtual void RegisterEvents()
        {
            RegisterViewportEvents();
            RegisterOperationEvents();
        }
 
        #region Viewport
 
        //注册事件
        protected virtual void RegisterViewportEvents()
        {
            if (_viewport == null)
            {
                return;
            }
            _viewport.MouseDown += Viewport_MouseDown;
            _viewport.MouseUp += Viewport_MouseUp;
            _viewport.MouseMove += Viewport_MouseMove;
            _viewport.MouseWheel += Viewport_MouseWheel;
            _viewport.MouseDoubleClick += Viewport_MouseDoubleClick;
            _viewport.MouseLeftButtonDown += Viewport_MouseLeftButtonDown;
            _viewport.PreviewMouseDown += _viewport_PreviewMouseDown;
        }
 
        private void _viewport_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            OnPreviewMouseDown(e);
        }
 
        //鼠标按下
        private void Viewport_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            OnMouseDown(e);
        }
 
        //鼠标弹起
        private void Viewport_MouseUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            OnMouseUp(e);
        }
 
        //鼠标移动
        private void Viewport_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            OnMouseMove(e);
        }
 
        //鼠标滚轮
        private void Viewport_MouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
        {
            OnMouseWheel(e);
        }
 
        //鼠标双击
        private void Viewport_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            OnMouseDoubleClick(e);
        }
 
        //鼠标左键按下
        private void Viewport_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            OnMouseLeftButtonDown(e);
        }
 
        //处理鼠标按下事件
        protected virtual void OnMouseDown(System.Windows.Input.MouseButtonEventArgs e)
        {
 
        }
 
        //处理鼠标弹起事件
        protected virtual void OnMouseUp(System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!Initialized)
            {
                return;
            }
            if (this.IsSelectionEnabled)
            {
                _selectionHelper.HandleSingle(e.GetPosition(_viewport));
            }
        }
 
        //处理鼠标移动事件
        protected virtual void OnMouseMove(System.Windows.Input.MouseEventArgs e)
        {
            if (!Initialized)
            {
                return;
            }
            if (this.IsHighlightEnabled)
            {
                _highlightHelper.Highlight(e.GetPosition(_viewport));
            }
        }
 
        //处理鼠标滚轮事件
        protected virtual void OnMouseWheel(System.Windows.Input.MouseWheelEventArgs e)
        {
 
        }
 
        //处理鼠标双击事件
        protected virtual void OnMouseDoubleClick(System.Windows.Input.MouseButtonEventArgs e)
        {
            if (!Initialized)
            {
                return;
            }
            if (this.IsZoomExtentsEnabled)
            {
                _viewport.ZoomExtents();
            }
        }
 
        //处理鼠标
        protected virtual void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
 
        }
 
        protected virtual void OnPreviewMouseDown(MouseButtonEventArgs e)
        {
 
        }
 
        #endregion
 
        #region Operation
 
        //注册操作事件
        protected virtual void RegisterOperationEvents()
        {
            if (_highlightHelper != null)
            {
                _highlightHelper.StateChangedEvent += HighlightHelper_StateChangedEvent;
            }
            if (_selectionHelper != null)
            {
                _selectionHelper.StateChangedEvent += SelectionHelper_StateChangedEvent;
                _selectionHelper.SelectionChangedEvent += SelectionHelper_SelectionChangedEvent;
            }
        }
 
        //高亮状态改变事件
        private void HighlightHelper_StateChangedEvent(LogicalVisual3D logicalVisual, bool operation)
        {
            if (logicalVisual == null)
            {
                return;
            }
            if (operation)
            {
                _stateHelper.LoadState(logicalVisual.Vmo, eLogicalState.Highlight);
            }
            else
            {
                _stateHelper.UnloadState(logicalVisual.Vmo, eLogicalState.Highlight);
            }
            logicalVisual.UpdateVisual();
        }
 
        //选择状态改变事件
        private void SelectionHelper_StateChangedEvent(LogicalVisual3D logicalVisual, bool operation)
        {
            if (logicalVisual == null)
            {
                return;
            }
            if (operation)
            {
                _stateHelper.LoadState(logicalVisual.Vmo, eLogicalState.Selection);
            }
            else
            {
                _stateHelper.UnloadState(logicalVisual.Vmo, eLogicalState.Selection);
            }
            logicalVisual.UpdateVisual();
        }
 
        //选择改变事件
        private void SelectionHelper_SelectionChangedEvent(List<LogicalVisual3D> obj)
        {
            var visuals = obj?.Select(x => x.Vmo).ToList();
            this.SelectionChangedEvent?.Invoke(visuals);
        }
 
 
        #endregion
 
        #endregion
 
        #region 视角
 
        /// <summary>
        /// 设置上视图
        /// </summary>
        public virtual void SetTopView()
        {
            if (!Initialized)
            {
                return;
            }
            var lookDirection = new Vector3D(0, 0, -1); // 向下看
            var upDirection = new Vector3D(0, 1, 0);// Y轴朝上
            _viewport.FitView(lookDirection, upDirection);
        }
 
        /// <summary>
        /// 设置下视图
        /// </summary>
        public virtual void SetBottomView()
        {
            if (!Initialized)
            {
                return;
            }
            var lookDirection = new Vector3D(0, 0, 1);   // 向上看
            var upDirection = new Vector3D(0, 1, 0);// Y轴朝上
            _viewport.FitView(lookDirection, upDirection);
        }
 
        /// <summary>
        /// 设置左视图
        /// </summary>
        public virtual void SetLeftView()
        {
            if (!Initialized)
            {
                return;
            }
            var lookDirection = new Vector3D(1, 0, 0);   // 向右看
            var upDirection = new Vector3D(0, 0, 1);// Z轴朝上
            _viewport.FitView(lookDirection, upDirection);
        }
 
 
        /// <summary>
        /// 设置右视图
        /// </summary>
        public virtual void SetRightView()
        {
            if (!Initialized)
            {
                return;
            }
            var lookDirection = new Vector3D(-1, 0, 0);   // 向左看
            var upDirection = new Vector3D(0, 0, 1);// Z轴朝上
            _viewport.FitView(lookDirection, upDirection);
        }
 
        /// <summary>
        /// 设置前视图
        /// </summary>
        public virtual void SetFrontView()
        {
            if (!Initialized)
            {
                return;
            }
            var lookDirection = new Vector3D(0, 1, 0);   // 向后看
            var upDirection = new Vector3D(0, 0, 1);// Z轴朝上
            _viewport.FitView(lookDirection, upDirection);
        }
 
        /// <summary>
        /// 设置后视图
        /// </summary>
        public virtual void SetBackView()
        {
            if (!Initialized)
            {
                return;
            }
            var lookDirection = new Vector3D(0, -1, 0);   // 向前看
            var upDirection = new Vector3D(0, 0, 1);// Z轴朝上
            _viewport.FitView(lookDirection, upDirection);
        }
 
        #endregion
 
        #region 背景
 
        /// <summary>
        /// 显示背景
        /// </summary>
        public virtual void ShowBackgroud()
        {
            if (!Initialized)
            {
                return;
            }
            _backgroundHelper.Show();
        }
 
        /// <summary>
        /// 隐藏背景
        /// </summary>
        public virtual void HideBackgroud()
        {
            if (!Initialized)
            {
                return;
            }
            _backgroundHelper.Hide();
        }
 
        /// <summary>
        /// 背景可见性
        /// </summary>
        public virtual bool BackgroudVisible
        {
            get
            {
                if (!Initialized)
                {
                    return false;
                }
                return _backgroundHelper.Visible;
            }
            set
            {
                if (!Initialized)
                {
                    return;
                }
                _backgroundHelper.Visible = value;
            }
        }
 
        /// <summary>
        /// 设置背景
        /// </summary>
        public virtual void SetBackgroud(string url)
        {
            if (!Initialized)
            {
                return;
            }
            _backgroundHelper.Set(url);
        }
 
        /// <summary>
        /// 设置背景
        /// </summary>
        public virtual void SetBackgroud(string url, double width, double height)
        {
            if (!Initialized)
            {
                return;
            }
            _backgroundHelper.Set(url, width, height);
        }
 
        #endregion
 
        #region 网格线
 
        /// <summary>
        /// 显示网格线
        /// </summary>
        public virtual void ShowGridLines()
        {
            if (!Initialized)
            {
                return;
            }
            _gridLinesHelper.Show();
        }
 
        /// <summary>
        /// 隐藏网格线
        /// </summary>
        public virtual void HideGridLines()
        {
            if (!Initialized)
            {
                return;
            }
            _gridLinesHelper.Hide();
        }
 
        /// <summary>
        /// 网格线可见性
        /// </summary>
        public virtual bool GridLinesVisible
        {
            get
            {
                if (!Initialized)
                {
                    return false;
                }
                return _gridLinesHelper.Visible;
            }
            set
            {
                if (!Initialized)
                {
                    return;
                }
                _gridLinesHelper.Visible = value;
            }
        }
 
        #endregion
 
        #region 公告板文本
 
        /// <summary>
        /// 设置公告板文本
        /// </summary>
        public virtual void SetBillboardText(List<LogicalTextL3d> items)
        {
            if (!Initialized)
            {
                return;
            }
            _billboardTextHelper.Set(items);
        }
 
        /// <summary>
        /// 更新公告板文本
        /// </summary>
        public virtual void UpdateBillboardText(LogicalTextL3d item)
        {
            if (!Initialized)
            {
                return;
            }
            _billboardTextHelper.Update(item);
        }
 
        /// <summary>
        /// 更新公告板文本
        /// </summary>
        public virtual void UpdateBillboardText(List<LogicalTextL3d> items)
        {
            if (!Initialized)
            {
                return;
            }
            _billboardTextHelper.Update(items);
        }
 
        /// <summary>
        /// 清理公告板文本
        /// </summary>
        public virtual void ClearBillboardText()
        {
            if (!Initialized)
            {
                return;
            }
            _billboardTextHelper.Clear();
        }
 
 
        #endregion
 
 
 
 
 
 
    }
}