duheng
2025-03-05 0f831db8df9c2e4adc7feca636967a0fb1cd5e29
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
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using DevExpress.Drawing;
using DevExpress.Utils.Extensions;
using HStation.RevitDev.Model.ModelEnum;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Entity;
using HStation.RevitDev.RevitDataExport.Utility;
using System;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
 
namespace HStation.RevitDev.RevitDataExport.Forms
{
    public partial class Form_FamilyManager : System.Windows.Forms.Form
    {
        ExternalCommandData _data;
        private ExternalEvent_Execute externalEvent_Execute;
        private ExternalEvent externalEvent;
 
        public Form_FamilyManager(ExternalCommandData data)
        {
            _data = data;
            InitializeComponent();
            InitializeControls();
            externalEvent_Execute = new ExternalEvent_Execute("Ex");
            externalEvent = ExternalEvent.Create(externalEvent_Execute);
        }
 
        private void InitializeControls()
        {
            InitTreeView();
            InitFamilyPanel();
        }
 
        private void InitFamilyPanel()
        {
 
        }
 
        private void InitTreeView()
        {
            var values = Enum.GetValues(typeof(RevitType));
            foreach (var value in values)
            {
                var revitType = (RevitType)value;
                if (revitType.IsRequired())
                {
                    switch (revitType)
                    {
                        case RevitType.RFT_FireHydrant:
                            {
                                if (GlobalResource.ConfigSettingModel.SystemType == ConfigHelper.SystemType.All || GlobalResource.ConfigSettingModel.SystemType == ConfigHelper.SystemType.Fire)
                                {
                                    TreeNode node = new TreeNode(revitType.GetDescription());
                                    node.Tag = revitType;
                                    this.treeView_categorys.Nodes.Add(node);
                                }
                                break;
                            }
                        case RevitType.RFT_HeatExchanger:
                            {
                                if (GlobalResource.ConfigSettingModel.SystemType == ConfigHelper.SystemType.All || GlobalResource.ConfigSettingModel.SystemType == ConfigHelper.SystemType.CirculatingWater)
                                {
                                    TreeNode node = new TreeNode(revitType.GetDescription());
                                    node.Tag = revitType;
                                    this.treeView_categorys.Nodes.Add(node);
                                }
                                break;
                            }
                        case RevitType.RFT_Shower:
                            {
                                if (GlobalResource.ConfigSettingModel.SystemType == ConfigHelper.SystemType.All || GlobalResource.ConfigSettingModel.SystemType == ConfigHelper.SystemType.Fire)
                                {
                                    TreeNode node = new TreeNode(revitType.GetDescription());
                                    node.Tag = revitType;
                                    this.treeView_categorys.Nodes.Add(node);
                                }
                                break;
                            }
                        default:
                            {
                                TreeNode node = new TreeNode(revitType.GetDescription());
                                node.Tag = revitType;
                                this.treeView_categorys.Nodes.Add(node);
                                break;
                            }
                    }
 
                }
            }
            this.treeView_categorys.NodeMouseClick += TreeView_categorys_NodeMouseClick;
        }
 
        private void TreeView_categorys_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            var node = e.Node;
            if (node == null) { return; }
 
            var revitType = (RevitType)node.Tag;
            if (!revitType.IsRequired()) { return; }
            var dscribe = revitType.GetDescription();
            var dir = Path.Combine(GlobalResource.FamilysDirectory, dscribe);
            if (!Directory.Exists(dir))
            {
                TaskDialog.Show("错误", $"未找到{dscribe}目录");
                return;
            }
            string[] filePaths = Directory.GetFiles(dir, "*.rfa", SearchOption.TopDirectoryOnly);
            this.flowLayoutPanel_familys.Controls.Clear();
            var addBtn = CreateAddButton(revitType);
            flowLayoutPanel_familys.Controls.Add(addBtn);
            foreach (string rfaPath in filePaths)
            {
                if (RevitUtil.IsBackUpFile(rfaPath))
                {
                    continue;
                }
                Bitmap bitmap = GetRfaThumbnail(dir, rfaPath);
                Button button = CreateButton(bitmap, rfaPath, revitType);
                flowLayoutPanel_familys.Controls.Add(button);
            }
        }
 
        private Button CreateAddButton(RevitType type)
        {
            var pngPath = Path.Combine(GlobalResource.ImageDirectory, "添加.png");
            var bitmap = Bitmap.FromFile(pngPath) as Bitmap;
            Button btn = new Button();
            btn.Height = GlobalResource.ThumbnailSize;
            btn.Tag = type;
            btn.Width = GlobalResource.ThumbnailSize;
            ToolTip toolTip = new ToolTip();
            toolTip.SetToolTip(btn, "添加族文件");
            btn.BackgroundImage = bitmap;
            btn.BackgroundImageLayout = ImageLayout.Stretch;
            btn.Click += Btn_Click;
            return btn;
        }
 
        private void Btn_Click(object sender, EventArgs e)
        {
            try
            {
                Button button = sender as Button;
                var openFileDialog = new OpenFileDialog();
                openFileDialog.FileName = "选择族文件";
                openFileDialog.Filter = "族文件(*.rfa)|*.rfa";
                openFileDialog.Title = "选择族文件";
                openFileDialog.Multiselect = true;
                if (openFileDialog.ShowDialog() != DialogResult.OK)
                {
                    return;
                }
 
                var selectPaths = openFileDialog.FileNames;
 
                var revitType = (RevitType)button.Tag;
                var dscribe = revitType.GetDescription();
                var dir = Path.Combine(GlobalResource.FamilysDirectory, dscribe);
                var validFiles = RevitUtil.ValidateFileVersion(selectPaths).ToList();
                for (int i = 0; i < validFiles.Count(); i++)
                {
                    string path = validFiles[i];
                    CopyToImageDirectory(ref path, revitType);
                    Bitmap bitmap = GetRfaThumbnail(dir, path);
                    Button btn = CreateButton(bitmap, path, revitType);
                    CreateParams(path, revitType);
                    flowLayoutPanel_familys.Controls.Add(btn);
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("错误", $"{ex.Message}");
            }
            TaskDialog.Show("提示", "添加成功!");
        }
 
        private void CreateParams(string path, RevitType revitType)
        {
            if (File.Exists(path))
            {
                //if (externalEvent != null)
                //{
                //    externalEvent_Execute.ExecuteAction = new Action<UIApplication>((app) =>
                //    {
                var doc = _data.Application.ActiveUIDocument.Document;
                var fdoc = doc.Application.OpenDocumentFile(path);
                var fm = fdoc.FamilyManager;
                var ls = GlobalResource.GetParamsList().Where(c => c.FamilyType.Equals(revitType.GetDescriptioin()));
                if (ls != null && ls.Any())
                {
                    using (var trans = new Transaction(fdoc, "addParameters"))
                    {
                        trans.Start();
                        foreach (FamilyType ft in fm.Types)
                        {
                            fm.CurrentType = ft;
                            ls.ForEach(c =>
                            {
                                var ps = fm.get_Parameter(c.ParamsName);
 
                                if (ps == null)
                                {
                                    fm.AddParameter(c.ParamsName, Autodesk.Revit.DB.BuiltInParameterGroup.PG_GENERAL, Autodesk.Revit.DB.ParameterType.Text, true);
                                }
                                if (!string.IsNullOrEmpty(c.DefaultValue))
                                {
                                    var sp = fm.get_Parameter(c.ParamsName);
                                    //if (sp != null)
                                    //{
                                        fm.Set(sp, c.DefaultValue);
                                   // }
                                }
                            });
                        }
                        trans.Commit();
                    }
                }
                fdoc.Close();
                //    });
                //    externalEvent.Raise();
                //}
            }
        }
 
        
 
        private void CopyToImageDirectory(ref string path, RevitType revitType)
        {
            var fileName = Path.GetFileName(path);
            var newPath = Path.Combine(GlobalResource.FamilysDirectory + $@"/{revitType.GetDescription()}", fileName);
            try
            {
                if (path != newPath)
                {
 
                    File.Copy(path, newPath, true);
                    File.SetAttributes(newPath, FileAttributes.Normal);
                    path = newPath;
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show(path, $"文件复制失败,{ex.Message}");
            }
        }
 
        private Button CreateButton(Bitmap bitmap, string rfaPath, RevitType revitType)
        {
            string fileName = Path.GetFileName(rfaPath);
            ContextMenu menu = CreateContextMenu();
 
            Button btn = new Button();
            btn.ContextMenu = menu;
            btn.Tag = revitType;
            menu.Tag = btn;
            btn.Text = fileName;
            btn.TextAlign = ContentAlignment.BottomCenter;
            ToolTip toolTip = new ToolTip();
            toolTip.SetToolTip(btn, fileName);
            btn.Height = GlobalResource.ThumbnailSize;
            btn.Width = GlobalResource.ThumbnailSize;
            btn.BackgroundImage = bitmap;
            btn.BackgroundImageLayout = ImageLayout.Stretch;
            btn.Click += Box_Click;
            return btn;
        }
 
        private ContextMenu CreateContextMenu()
        {
            ContextMenu menu = new ContextMenu();
            MenuItem item = new MenuItem();
            item.Text = "删除";
            item.Click += ItemRightBtnClick;
            menu.MenuItems.Add(item);
 
            MenuItem item2 = new MenuItem();
            item2.Text = "修改图片";
            item2.Click += ItemRightBtnModifyClick;
            menu.MenuItems.Add(item2);
            return menu;
        }
 
        private void ItemRightBtnModifyClick(object sender, EventArgs e)
        {
            try
            {
                var dlg = new OpenFileDialog();
                dlg.Filter = "图片文件(*.png;)|*.png";
                dlg.Title = "注意使用.png格式图片,长宽是120像素";
                dlg.ValidateNames = true;
                dlg.CheckPathExists = true;
                dlg.CheckFileExists = true;
                if (dlg.ShowDialog() != DialogResult.OK)
                    return;
                var filePath = dlg.FileName;
 
                var item = sender as MenuItem;
                Button btn = (Button)item.Parent.Tag;
                RevitType revitType = (RevitType)btn.Tag;
                var dir = Path.Combine(GlobalResource.FamilysDirectory, revitType.GetDescriptioin());
                var path = Path.Combine(
                    GlobalResource.FamilysDirectory,
                    revitType.GetDescription(),
                    Path.GetFileName(btn.Text));
                var bitmap = ModifyRfaThumbnail(dir, path, filePath);
                btn.BackgroundImage = bitmap;
                btn.Refresh();
            }
            catch (Exception ex)
            {
                TaskDialog.Show("错误", ex.Message);
            }
        }
 
        private void ItemRightBtnClick(object sender, EventArgs e)
        {
            try
            {
                var dlg = MessageBox.Show("确认要删除族吗?", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dlg == DialogResult.Yes)
                {
                    var item = sender as MenuItem;
                    Button btn = (Button)item.Parent.Tag;
                    RevitType revitType = (RevitType)btn.Tag;
                    var path = Path.Combine(
                        GlobalResource.FamilysDirectory,
                        revitType.GetDescription(),
                        Path.GetFileName(btn.Text));
                    btn.Dispose();//.BackgroundImage = null;
                    this.flowLayoutPanel_familys.Controls.Remove(btn);
                    File.SetAttributes(path, FileAttributes.Normal);
                    File.Delete(path);
 
                    try
                    {
                        var pngName = path.Replace(".rfa", "");
                        var pngPath = Path.Combine(pngName + ".png");
                        File.Delete(pngPath);
                    }
                    catch (Exception ex)
                    {
 
                    }
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("错误", ex.Message);
            }
        }
 
        private void Box_Click(object sender, EventArgs e)
        {
            //TaskDialog.Show("提示", "点击了图标");
        }
 
        private static Bitmap GetRfaThumbnail(string dir, string rfaPath)
        {
            Bitmap bitmap = null;
            var pngName = Path.GetFileNameWithoutExtension(rfaPath);
            var pngPath = Path.Combine(dir, pngName + ".png");
 
            if (!File.Exists(pngPath))
            {
                bitmap = ThumbnailUtils.GetThumbnailWithoutRevit(rfaPath);
            }
            else
            {
                var fs = new FileStream(pngPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                var fs2 = fs;
                bitmap = Bitmap.FromStream(fs2) as Bitmap;
                fs.Close();
            }
            return bitmap;
        }
 
        private static Bitmap ModifyRfaThumbnail(string dir, string rfaPath, string imagePath)
        {
            Bitmap bitmap = null;
            var pngName = Path.GetFileNameWithoutExtension(rfaPath);
            var pngPath = Path.Combine(dir, pngName + ".png");
 
            if (!File.Exists(pngPath))
            {
                bitmap = ThumbnailUtils.GetThumbnailWithoutRevit(rfaPath);
            }
            else
            {
                File.Copy(imagePath, pngPath, true);
                File.SetAttributes(pngPath, FileAttributes.Normal);
                var fs = new FileStream(pngPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                bitmap = Bitmap.FromStream(fs) as Bitmap;
                fs.Close();
            }
            return bitmap;
        }
 
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            InitParams();
        }
 
        private void InitParams()
        {
            var values = Enum.GetValues(typeof(RevitType));
            if (externalEvent != null)
            {
                externalEvent_Execute.ExecuteAction = new Action<UIApplication>((app) =>
                {
                    foreach (var value in values)
                    {
                        var revitType = (RevitType)value;
 
                        var path = GlobalResource.FamilysDirectory + $@"/{revitType.GetDescription()}";
                        var files = Directory.GetFiles(path);
                        foreach (var file in files)
                        {
 
                            var doc = _data.Application.ActiveUIDocument.Document;
                            var fdoc = doc.Application.OpenDocumentFile(path);
                            var fm = fdoc.FamilyManager;
                            var ls = GlobalResource.GetParamsList().Where(c => c.FamilyType.Equals(revitType.GetDescriptioin()));
                            if (ls != null && ls.Any())
                            {
                                using (var trans = new Transaction(fdoc, "addParameters"))
                                {
                                    trans.Start();
                                    ls.ForEach(c =>
                                    {
                                        var ps = fm.get_Parameter(c.ParamsName);
                                        if (ps == null)
                                        {
                                            fm.AddParameter(c.ParamsName, Autodesk.Revit.DB.BuiltInParameterGroup.PG_GENERAL, Autodesk.Revit.DB.ParameterType.Text, true);
                                        }
                                        if (!string.IsNullOrEmpty(c.DefaultValue))
                                        {
                                            var sp = fm.get_Parameter(c.ParamsName);
                                            if (sp != null)
                                            {
                                                fm.Set(sp, c.DefaultValue);
                                            }
                                        }
                                    });
 
                                    trans.Commit();
                                }
                            }
                            fdoc.Close();
                        };
                    }
                });
                externalEvent.Raise();
                TaskDialog.Show("提示", "初始化成功!");
 
            }
        }
    }
}