lixiaojun
2025-04-07 33efd807b5f5aac98ce736ccec002e190e5dd175
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
namespace Yw.WpfUI.Hydro
{
    /// <summary>
    /// 选择管理器
    /// </summary>
    internal class SimpleSelectionManager
    {
        /// <summary>
        /// 
        /// </summary>
        public SimpleSelectionManager(HelixViewport3D viewport)
        {
            _viewport = viewport;
        }
 
        /// <summary>
        /// 选择改变事件
        /// </summary>
        public event Action<List<Visual3D>> SelectionChangedEvent;
        /// <summary>
        /// 状态改变事件
        /// </summary>
        public event Action<Visual3D, eSelectionType> StateChangedEvent;
 
        private readonly HelixViewport3D _viewport;//控件
        private readonly List<Visual3D> _selection = new();//选择集合
 
        #region 内部实现
 
        //添加选择
        private void AddToSelection(Visual3D visual)
        {
            if (_selection.Contains(visual))
            {
                return;
            }
            _selection.Add(visual);
            this.StateChangedEvent?.Invoke(visual, eSelectionType.Load);
        }
 
        //从选择中移除
        private void RemoveFromSelection(Visual3D visual)
        {
            if (!_selection.Contains(visual))
            {
                return;
            }
            _selection.Remove(visual);
            this.StateChangedEvent?.Invoke(visual, eSelectionType.Unload);
        }
 
        //清理选择
        private void ClearSelection()
        {
            if (_selection.Count < 1)
            {
                return;
            }
            _selection.ForEach(x => this.StateChangedEvent?.Invoke(x, eSelectionType.Unload));
            _selection.Clear();
        }
 
        #endregion
 
        /// <summary>
        /// 处理单个选择
        /// </summary>
        public void HandleSingle(Point pt)
        {
            var visual = _viewport.FindNearestVisual(pt);
            if (visual == null)
            {
                if (_selection.Count > 0)
                {
                    ClearSelection();
                    this.SelectionChangedEvent?.Invoke(null);
                }
                return;
            }
            if (_selection.Count == 1 && _selection[0] == visual)
            {
                return;
            }
            ClearSelection();
            AddToSelection(visual);
            this.SelectionChangedEvent?.Invoke(_selection);
        }
 
        /// <summary>
        /// 处理多个选择
        /// </summary>
        public void HandleMulti(Point pt)
        {
            var visual = _viewport.FindNearestVisual(pt);
            if (visual == null)
            {
                return;
            }
            if (_selection.Contains(visual))
            {
                return;
            }
            AddToSelection(visual);
            this.SelectionChangedEvent?.Invoke(_selection);
        }
 
        /// <summary>
        /// 选择Visual
        /// 不触发选择改变事件
        /// </summary>
        public void SelectVisual(Visual3D visual)
        {
            ClearSelection();
            if (visual == null)
            {
                return;
            }
            AddToSelection(visual);
        }
 
        /// <summary>
        /// 选择Visuals
        /// </summary>
        public void SelectVisuals(List<Visual3D> visuals)
        {
            ClearSelection();
            if (visuals == null || visuals.Count < 1)
            {
                return;
            }
            visuals.ForEach(AddToSelection);
        }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
    }
}