lixiaojun
2025-04-03 2e52f10a2cccb1471859b05e0d0e9dd9649859c8
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
using System.Collections.Generic;
 
namespace Yw.WpfUI.Hydro
{
    internal class SimpleViewManager
    {
        public SimpleViewManager(HelixViewport3D viewport)
        {
            _viewport = viewport;
            _materialManager = new SimpleMaterialManager();
            _geometryManager = new SimpleGeometryManager();
            _highlightManager = new SimpleHighlightManager(viewport);
            _highlightManager.StateChangedEvent += _highlightManager_StateChangedEvent;
            _selectionManager = new SimpleSelectionManager(viewport);
            _selectionManager.StateChangedEvent += _selectionManager_StateChangedEvent;
            _selectionManager.SelectionChangedEvent += _selectionManager_SelectionChangedEvent;
            _zoomManger = new ZoomManager(viewport);
            _stateManager = new VisualStateManager();
 
            _highlightColor = ConstParas.SimpleHighlightColor.ToMediaColor();
            _highlightFactor = ConstParas.SimpleHighlightFactor;
            _selectionColor = ConstParas.SimpleSelectionColor.ToMediaColor();
            _selectionFactor = ConstParas.SimpleSelectionFactor;
        }
 
        #region 固定资源
 
        private readonly HelixViewport3D _viewport = null;
        private readonly SimpleMaterialManager _materialManager = null;
        private readonly SimpleGeometryManager _geometryManager = null;
 
 
        private readonly ColorOverrideManager _colorOverrideManager = null;
        private readonly VisualStateManager _stateManager = null;
        private readonly ZoomManager _zoomManger = null;
        private readonly Color _highlightColor;
        private readonly double _highlightFactor;
        private readonly Color _selectionColor;
        private readonly double _selectionFactor;
 
        #endregion
 
        #region 私有字段
 
        private NetworkL3d _nw = null;
        private Dictionary<string, VisualL3d> _allVisualL3dDict;
        private Dictionary<VisualL3d, Visual3D> _allVisualModelDict;
 
        #endregion
 
        #region 初始化
 
        /// <summary>
        /// 是否初始化
        /// </summary>
        public bool Initialized
        {
            get
            {
                if (_nw == null)
                {
                    return false;
                }
                if (_allVisualL3dDict == null)
                {
                    return false;
                }
                if (_allVisualModelDict == null)
                {
                    return false;
                }
                return true;
            }
        }
 
        /// <summary>
        /// 初始化
        /// </summary>
        public void Initial(NetworkL3d nw)
        {
            InitialNetwork(nw);
            InitialViewport();
            InitialManager();
        }
 
        //加载管网
        private void InitialNetwork(NetworkL3d nw)
        {
            _nw = nw;
            if (_nw == null)
            {
                return;
            }
            _allVisualL3dDict = _nw.Visuals.ToDictionary(x => x.Id);
            _allVisualModelDict = new Dictionary<VisualL3d, Visual3D>();
            _nw.Links.ForEach(x =>
            {
                var visual3d = CreateVisual3D(x);
                _allVisualModelDict.Add(x, visual3d);
            });
            _nw.Nodes.ForEach(x =>
            {
                var visual3d = CreateVisual3D(x);
                _allVisualModelDict.Add(x, visual3d);
            });
        }
 
        //初始化Vieport
        private void InitialViewport()
        {
            _viewport.Children.Clear();
            _viewport.Children.Add(new DefaultLights());
            _viewport.ViewCubeBackText = "后";
            _viewport.ViewCubeBottomText = "下";
            _viewport.ViewCubeFrontText = "前";
            _viewport.ViewCubeLeftText = "左";
            _viewport.ViewCubeRightText = "右";
            _viewport.ViewCubeTopText = "上";
            //_viewport.CameraController.ZoomAroundMouseDownPoint = true;
            //_viewport.CameraController.ZoomSensitivity = 1.1; // 设置缩放灵敏度
            _allVisualModelDict?.Values.ToList().ForEach(x => _viewport.Children.Add(x));
            _viewport.ZoomExtents();
        }
 
        //初始化管理器
        private void InitialManager()
        {
            _stateManager.Initial(_allVisualL3dDict?.Values.ToList());
        }
 
 
 
 
 
        //选择改变事件
        private void _selectionManager_SelectionChangedEvent(List<Visual3D> obj)
        {
 
        }
 
        #endregion
 
        #region 高亮
 
        //高亮管理器
        private readonly SimpleHighlightManager _highlightManager = null;
 
        /// <summary>
        /// 高亮显示
        /// </summary>
        public void Highlight(Point pt)
        {
            if (!Initialized)
            {
                return;
            }
            _highlightManager.Highlight(pt);
        }
 
        //高亮状态改变事件
        private void _highlightManager_StateChangedEvent(Visual3D visual3d, eHighlightType highlightType)
        {
            if (visual3d == null)
            {
                return;
            }
            var visual = Visual3DProperties.GetTag(visual3d);
            if (visual == null)
            {
                return;
            }
            switch (highlightType)
            {
                case eHighlightType.Load:
                    {
                        _stateManager.LoadState(visual, eVisualState.Highlight);
                    }
                    break;
                case eHighlightType.Unload:
                    {
                        _stateManager.UnloadState(visual, eVisualState.Highlight);
                    }
                    break;
                default: break;
            }
            UpdateVisual3D(visual, visual3d);
        }
 
        #endregion
 
        #region 选择
 
        //选择管理器
        private readonly SimpleSelectionManager _selectionManager = null;
 
        /// <summary>
        /// 处理单个选择
        /// </summary>
        public void HandleSingleSelection(Point pt)
        {
            if (!Initialized)
            {
                return;
            }
            _selectionManager.HandleSingle(pt);
        }
 
        //选择状态改变事件
        private void _selectionManager_StateChangedEvent(Visual3D visual3d, eSelectionType selectionType)
        {
            if (visual3d == null)
            {
                return;
            }
            var visual = Visual3DProperties.GetTag(visual3d);
            if (visual == null)
            {
                return;
            }
            switch (selectionType)
            {
                case eSelectionType.Load:
                    {
                        _stateManager.LoadState(visual, eVisualState.Selection);
                    }
                    break;
                case eSelectionType.Unload:
                    {
                        _stateManager.UnloadState(visual, eVisualState.Selection);
                    }
                    break;
                default: break;
            }
            UpdateVisual3D(visual, visual3d);
        }
 
 
        #endregion
 
        #region 绘制逻辑
 
        //更新Visual3D
        private void UpdateVisual3D(VisualL3d visual, Visual3D visual3d)
        {
            var state = _stateManager.GetEffectState(visual);
            var model3d = (visual3d as ModelVisual3D).Content as GeometryModel3D;
            model3d.Material = GetMaterial(visual, state);
            model3d.Geometry = GetGeometry(visual, state);
        }
 
        //创建Visual3D
        private Visual3D CreateVisual3D(VisualL3d visual)
        {
            var visual3d = new ModelVisual3D()
            {
                Content = CreateModel3D(visual)
            };
            Visual3DProperties.SetTag(visual3d, visual);
            return visual3d;
        }
 
        //创建Model3D
        private Model3D CreateModel3D(VisualL3d visual)
        {
            var state = _stateManager.GetEffectState(visual);
 
            return new GeometryModel3D()
            {
                Geometry = GetGeometry(visual, state),
                Material = GetMaterial(visual, state)
            };
        }
 
        //获取材质
        private Material GetMaterial(VisualL3d visual, eVisualState state)
        {
            var color = GetVisual3DColor(visual, state);
            return _materialManager.GetMaterial(color);
        }
 
        //获取几何体
        private Geometry3D GetGeometry(VisualL3d visual, eVisualState state)
        {
            var size = GetVisual3DSize(visual, state);
            var factor = GetVisual3DFactor(visual, state);
            return _geometryManager.GetGeometry(visual, size, factor);
        }
 
        //获取颜色
        private Color GetVisual3DColor(VisualL3d visual, eVisualState state)
        {
            Color color;
            switch (state)
            {
                case eVisualState.Highlight:
                    {
                        color = _highlightColor;
                    }
                    break;
                case eVisualState.Selection:
                    {
                        color = _selectionColor;
                    }
                    break;
                case eVisualState.Override:
                    {
                        var cv = _colorOverrideManager.GetColor(visual);
                        if (cv.HasValue)
                        {
                            color = cv.Value;
                        }
                    }
                    break;
                default:
                    {
                        color = visual.SimpleStyle.HtmlColor.ToMediaColor();
                    }
                    break;
            }
            return color;
        }
 
        //获取尺寸
        private double GetVisual3DSize(VisualL3d visual, eVisualState state)
        {
            if (visual is NodeL3d node)
            {
                return node.SimpleStyle.Radiu;
            }
            if (visual is LinkL3d link)
            {
                return link.SimpleStyle.Diameter;
            }
            return default;
        }
 
        //获取因子
        private double GetVisual3DFactor(VisualL3d visual, eVisualState state)
        {
            double factor = 1.0d;
            switch (state)
            {
                case eVisualState.Highlight:
                    {
                        factor = _highlightFactor;
                    }
                    break;
                case eVisualState.Selection:
                    {
                        factor = _selectionFactor;
                    }
                    break;
                case eVisualState.Override:
                    {
 
                    }
                    break;
                default:
                    {
                        factor = 1.0d;
                    }
                    break;
            }
            return factor;
        }
 
        #endregion
 
 
    }
}