zhangyuekai
2024-08-17 ac588abe1499785d2c4840bc79e3cdc42e08560a
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
using Autodesk.Revit.UI;
using HStation.RevitDev.Model.ModelEnum;
using HStation.RevitDev.RevitDataExport.Common;
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;
 
        public Form_FamilyManager(ExternalCommandData data)
        {
            _data = data;
            InitializeComponent();
            InitializeControls();
        }
 
        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())
                {
                    TreeNode node = new TreeNode(revitType.GetDescription());
                    node.Tag = revitType;
                    this.treeView_categorys.Nodes.Add(node);
                }
            }
            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);
                    flowLayoutPanel_familys.Controls.Add(btn);
                }
            }
            catch (Exception ex)
            {
                TaskDialog.Show("错误", $"{ex.Message}");
            }
            TaskDialog.Show("提示", "添加成功!");
        }
 
        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);
                    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);
            return menu;
        }
 
        private void ItemRightBtnClick(object sender, EventArgs e)
        {
            try
            {
                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));
                this.flowLayoutPanel_familys.Controls.Remove(btn);
                File.SetAttributes(path, FileAttributes.Normal);
                File.Delete(path);
            }
            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
            {
                bitmap = Bitmap.FromFile(pngPath) as Bitmap;
            }
            return bitmap;
        }
    }
}