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;
|
}
|
}
|
}
|