lixiaojun
2 天以前 531e3b2f09ebf2e6df44e6803e7fa06073ba2c3d
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
namespace Yw.WpfUI.Hydro
{
    /// <summary>
    /// 绘制小管网辅助类
    /// </summary>
    internal class DrawSmallHelper
    {
        /// <summary>
        /// 
        /// </summary>
        public DrawSmallHelper
            (
                HelixViewport3D viewport,
                DrawInitialHelper initialHelper,
                DrawHighlightHelper highlightHelper,
                DrawSelectionHelper selectionHelper,
                DrawMoudingHelper moudingHelper
            )
        {
            _viewport = viewport;
            _initialHelper = initialHelper;
            _highlightHelper = highlightHelper;
            _selectionHelper = selectionHelper;
            _moudingHelper = moudingHelper;
            Attach();
        }
 
        #region 事件集合
 
        /// <summary>
        /// 小管网改变事件
        /// </summary>
        public event Action<List<VisualDraw3D>> SmallChangedEvent;
 
        #endregion
 
        private readonly HelixViewport3D _viewport = null;//三维组件
        private readonly DrawInitialHelper _initialHelper = null;//初始化辅助类
        private readonly DrawHighlightHelper _highlightHelper = null;//高亮辅助类
        private readonly DrawSelectionHelper _selectionHelper = null;//选择辅助类
        private readonly DrawMoudingHelper _moudingHelper = null;//牟定辅助类
        private bool _isWorking = false;//是否工作中
        private bool? _lastHighlightEnabled = false;//上次高亮是否启用
        private bool? _lastSelectionEnabled = false;//上次选择是否启用
        private bool? _lastMoudingEnabled = false;//上次牟定是否启用
        private SmallInputL3d _input;//输入参数
        private SmallDraw3D _small3d;//小管网绘制件
 
        /// <summary>
        /// 是否初始化
        /// </summary>
        public bool Initialized
        {
            get { return _initialized; }
            private set { _initialized = value; }
        }
        private bool _initialized = false;
 
        //初始化
        private void Initialize()
        {
            if (!_initialHelper.Initialized)
            {
                return;
            }
            _initialized = true;
        }
 
        //处理初始化完成
        private void OnInitialCompleted()
        {
            Initialize();
        }
 
        /// <summary>
        /// 开始
        /// </summary>
        public bool Start(SmallInputL3d input)
        {
            if (!Initialized)
            {
                return false;
            }
            if (!input.Verify())
            {
                return false;
            }
            _initialHelper.RegistDrawStatus(eDrawStatusL3d.Small, End);
            _input = input;
            _lastHighlightEnabled = _highlightHelper.Enabled;
            _lastSelectionEnabled = _selectionHelper.Enabled;
            _lastMoudingEnabled = _moudingHelper.Enabled;
            _highlightHelper.Enabled = false;
            _selectionHelper.Enabled = false;
            _moudingHelper.Enabled = false;
            _isWorking = true;
            return true;
        }
 
        /// <summary>
        /// 结束
        /// </summary>
        public bool End()
        {
            if (!Initialized)
            {
                return false;
            }
            _isWorking = false;
            _highlightHelper.Enabled = _lastHighlightEnabled ?? false;
            _selectionHelper.Enabled = _lastSelectionEnabled ?? false;
            _moudingHelper.Enabled = _lastMoudingEnabled ?? false;
            if (_small3d != null)
            {
                _small3d.Close();
                _viewport.Children.Remove(_small3d);
                _small3d = null;
            }
            _initialHelper.ResetDrawStatus();
            return true;
        }
 
        //处理鼠标按下
        private void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (!_isWorking)
            {
                return;
            }
        }
 
        //处理鼠标移动
        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (!_isWorking)
            {
                return;
            }
            var pt = e.GetPosition(_viewport);
            pt = new Point(pt.X + 3, pt.Y - 3);
            var sp = _viewport.Viewport.UnProject(pt);
            if (!sp.HasValue)
            {
                return;
            }
            if (_small3d == null)
            {
                var input = _input.CloneC();
                var drawMode = _initialHelper.GetDrawMode();
                _small3d = new SmallDraw3D(input, drawMode);
                _viewport.Children.Add(_small3d);
            }
            _small3d.UpdateTransform(sp.Value);
            _moudingHelper.Mouding(pt, _small3d.Visual3ds);
        }
 
        //处理鼠标弹起
        private void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            if (!_isWorking)
            {
                return;
            }
            if (e.ChangedButton != MouseButton.Left)
            {
                return;
            }
            if (_small3d == null)
            {
                return;
            }
            var pt = e.GetPosition(_viewport);
            var snapNode = _moudingHelper.SnapNearestNode(pt, _small3d.Visual3ds);
            if (snapNode != null)
            {
                var allVisual3ds = _small3d.Close(snapNode);
                allVisual3ds.ForEach(x =>
                {
                    _initialHelper.AddVisual3D(x);
                });
                _viewport.Children.Remove(_small3d);
                _small3d = null;
                OnSmallChanged(allVisual3ds);
            }
        }
 
        //处理小管网改变
        private void OnSmallChanged(List<VisualDraw3D> visual3ds)
        {
            if (visual3ds == null || visual3ds.Count < 1)
            {
                return;
            }
            this.SmallChangedEvent?.Invoke(visual3ds);
        }
 
        //附加
        private void Attach()
        {
            _initialHelper.InitialCompletedEvent += OnInitialCompleted;
            _viewport.MouseDown += OnMouseDown;
            _viewport.MouseUp += OnMouseUp;
            _viewport.MouseMove += OnMouseMove;
        }
 
        //分离
        private void Detach()
        {
            _initialHelper.InitialCompletedEvent -= OnInitialCompleted;
            _viewport.MouseDown -= OnMouseDown;
            _viewport.MouseUp -= OnMouseUp;
            _viewport.MouseMove -= OnMouseMove;
        }
 
        /// <summary>
        /// 释放
        /// </summary>
        public void Dispose()
        {
            Detach();
        }
 
    }
}