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("提示", "初始化成功!");
|
|
}
|
}
|
}
|
}
|