qin
2024-09-28 e358beb08f5be49703009b64f058ecfbcfeefbd9
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
using Autodesk.Revit.UI;
using HStation.RevitDev.Model.ModelEnum;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Entity;
using HStation.RevitDev.RevitDataExport.Utility;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Button = System.Windows.Controls.Button;
 
namespace HStation.RevitDev.RevitDataExport.Forms
{
    /// <summary>
    /// WPF_FamilyPanel.xaml 的交互逻辑
    /// </summary>
    public partial class Wpf_FamilyPanel : UserControl, IDockablePaneProvider
    {
        //外部事件
        ExternalEvent_CreateInstance m_createInstanceEvent = null;
        ExternalEvent_CreatePipe m_createPipeEvent = null;
        ExternalEvent m_createInstanceHandler = null;
        ExternalEvent m_createPipeHandler = null;
 
        private string m_familyFileDir;
        private RevitType m_type;
 
        //面板上的按钮缓存
        private List<Button> m_displayFamilys = new List<Button>();
        private List<Button> m_realFamilys = new List<Button>();
 
        public Wpf_FamilyPanel(RevitType type)
        {
            m_type = type;
            m_familyFileDir = Path.Combine(GlobalResource.FamilysDirectory, type.GetDescription());
            InitializeComponent();
 
            if (type == RevitType.RFT_Pipe)
            {
                InitializePipeButton();
 
            }
            else
            {
                InitializeCostomControls();
            }
 
            m_createInstanceEvent = new ExternalEvent_CreateInstance();
            m_createPipeEvent = new ExternalEvent_CreatePipe();
  
            m_createInstanceHandler = ExternalEvent.Create(m_createInstanceEvent);
            m_createPipeHandler = ExternalEvent.Create(m_createPipeEvent);
        }
 
        private void InitializePipeButton()
        {
            var pipeDir = Path.Combine(GlobalResource.FamilysDirectory, RevitType.RFT_Pipe.GetDescription());
            var filePaths = Directory.GetFiles(pipeDir);
            foreach (var filePath in filePaths)
            {
                Button btn = CreatePipeButton(filePath);
                m_realFamilys.Add(btn);
                m_displayFamilys.Add(btn);
                wrapPanel_family2.Children.Add(btn);
            }
        }
 
        private Button CreatePipeButton(string filePath)
        {
            var fileName = Path.GetFileName(filePath);
            Button btn = new Button();
            btn.Height = GlobalResource.ThumbnailSize;
            btn.Width = GlobalResource.ThumbnailSize;
            btn.Content = fileName;
            btn.VerticalContentAlignment = VerticalAlignment.Bottom;
            btn.ToolTip = fileName;
 
            var bitmapPath = Path.Combine(GlobalResource.FamilysDirectory, RevitType.RFT_Pipe.GetDescription(), "默认.png");
            var bitmap = new Bitmap(bitmapPath);
            ImageBrush imageBrush = new ImageBrush();
            imageBrush.ImageSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
            btn.Background = imageBrush;
 
            btn.Click += PipeBtnClick;
            return btn;
        }
 
        private void PipeBtnClick(object sender, RoutedEventArgs e)
        {
            GlobalResource.PlacingType = m_type;
            m_createPipeHandler.Raise();
        }
 
        private void InitializeCostomControls()
        {
            string[] filePaths = Directory.GetFiles(m_familyFileDir, "*.rfa", SearchOption.TopDirectoryOnly);
            m_realFamilys.Clear();
            m_displayFamilys.Clear();
            this.wrapPanel_family2.Children.Clear();
            foreach (string rfaPath in filePaths)
            {
                if (RevitUtil.IsBackUpFile(rfaPath))
                {
                    continue;
                }
                AddToFamilyPanel(rfaPath);
            }
        }
 
        private void AddToFamilyPanel(string rfaPath)
        {
            Bitmap bitmap = null;
            var dir = Path.GetDirectoryName(rfaPath);
            var pngName = Path.GetFileNameWithoutExtension(rfaPath);
            var pngPath = Path.Combine(dir, pngName + ".png");
 
            if (!File.Exists(pngPath))
            {
                bitmap = ThumbnailUtils.GetThumbnailWithoutRevit(rfaPath);
            }
            else
            {
                bitmap = Bitmap.FromFile(pngPath) as Bitmap; 
            }
 
            ContextMenu menu = CreateInstanceDeleteMenu();
            Button btn = CreateInstanceButton(rfaPath, bitmap);
            btn.ContextMenu = menu;
            menu.Tag = btn;
 
            m_realFamilys.Add(btn);
            m_displayFamilys.Add(btn);
            this.wrapPanel_family2.Children.Add(btn);
        }
 
        private Button CreateInstanceButton(string rfaPath, Bitmap bitmap)
        {
            Button btn = new Button();
            btn.Height = GlobalResource.ThumbnailSize;
            btn.Width = GlobalResource.ThumbnailSize;
            btn.Tag = rfaPath;
            btn.Content = Path.GetFileName(rfaPath);
            btn.VerticalContentAlignment = VerticalAlignment.Bottom;
            btn.ToolTip = btn.Content;
 
            ImageBrush imageBrush = new ImageBrush();
            imageBrush.ImageSource = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
 
            btn.Background = imageBrush;
            btn.Click += FamilyBtnClick;
            return btn;
        }
 
        private ContextMenu CreateInstanceDeleteMenu()
        {
            ContextMenu menu = new ContextMenu();
            try
            {
                menu.StaysOpen = true;
                var item = new MenuItem();
                item.Click += (s, e) =>
                {
                    var result = TaskDialog.Show("提示", "是否确定删除?", TaskDialogCommonButtons.Yes | TaskDialogCommonButtons.No);
                    if (result == TaskDialogResult.Yes)
                    {
                        MenuItem mItem = s as MenuItem;
                        ContextMenu cMenu = mItem.Parent as ContextMenu;
                        DeleteButton((Button)(cMenu.Tag));
                    }
                };
                item.Header = "删除";
                item.Background = new SolidColorBrush(System.Windows.Media.Color.FromRgb(200, 0, 0));
                menu.Items.Add(item);
            }
            catch (Exception ex)
            {
                TaskDialog.Show("错误", $"{ex.Message}");
            }
            return menu;
        }
 
        private void DeleteButton(Button btn)
        {
            try
            {
                File.Delete(btn.Tag.ToString());
            }
            catch (UnauthorizedAccessException ex)
            {
                TaskDialog.Show("错误", ex.Message + ":请检查文件属性是否可写!");
                return;
            }
            m_displayFamilys.Remove(btn);
            m_realFamilys.Remove(btn);
            this.wrapPanel_family2.Children.Remove(btn);
        }
 
        private void FamilyBtnClick(object sender, RoutedEventArgs e)
        {
            GlobalResource.PlacingType = m_type;
            Button btn = sender as Button;
            m_createInstanceEvent.RfaPath = btn.Tag.ToString();
            m_createInstanceHandler.Raise();
        }
 
        public void SetupDockablePane(DockablePaneProviderData data)
        {
            data.FrameworkElement = this;
            DockablePaneState state = new DockablePaneState();
            state.DockPosition = DockPosition.Right;
            state.MinimumWidth = GlobalResource.ThumbnailSize;
            state.MinimumHeight = GlobalResource.ThumbnailSize;
            data.InitialState = state;
        }
 
        private void SearchBtnClick(object sender, RoutedEventArgs e)
        {
            //InitializePipeButton();
            InitializeCostomControls();
            if (string.IsNullOrEmpty(searchBox.Text))
            {
                ShowAllFamilys();
                return;
            }
 
            m_displayFamilys.Clear();
            foreach (var child in this.m_realFamilys)
            {
                if (child is Button familyBtn)
                {
                    string filePath = familyBtn.Tag.ToString();
                    string fileName = Path.GetFileName(filePath);
                    if (fileName.ToUpper().Contains(searchBox.Text.ToUpper()))
                    {
                        m_displayFamilys.Add(familyBtn);
                    }
                }
            }
            wrapPanel_family2.Children.Clear();
            for (int i = 0; i < m_displayFamilys.Count; i++)
            {
                wrapPanel_family2.Children.Add(m_displayFamilys[i]);
            }
        }
 
        private void ShowAllFamilys()
        {
            wrapPanel_family2.Children.Clear();
            for (int i = 0; i < m_realFamilys.Count; i++)
            {
                wrapPanel_family2.Children.Add(m_realFamilys[i]);
            }
        }
 
        private void searchBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                SearchBtnClick(null, null);
            }
        }
 
        private void FamilyAddBtnClick(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Windows.Forms.OpenFileDialog openFileDialog = new System.Windows.Forms.OpenFileDialog();
                openFileDialog.FileName = "选择族文件";
                openFileDialog.Filter = "族文件(*.rfa)|*.rfa";
                openFileDialog.Title = "选择族文件";
                openFileDialog.Multiselect = true;
                if (openFileDialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
                {
                    return;
                }
 
                var selectPaths = openFileDialog.FileNames;
                var validPaths = RevitUtil.ValidateFileVersion(selectPaths).ToList();
                for (int i = 0; i < validPaths.Count(); i++)
                {
                    string path = validPaths[i];
                    CopyToInstanceDirectory(ref path);
                    AddToFamilyPanel(path);
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("错误", $"{ex.Message}");
            }
            TaskDialog.Show("提示", "添加成功!");
        }
 
        private void CopyToInstanceDirectory(ref string path)
        {
            var fileName = Path.GetFileName(path);
            var newPath = Path.Combine(GlobalResource.FamilysDirectory + $@"/{m_type.GetDescription()}", fileName);
            try
            {
                File.Copy(path, newPath, true);
                path = newPath;
            }
            catch (Exception ex)
            {
                TaskDialog.Show(path, $"文件复制失败,{ex.Message}");
            }
        }
    }
}