using Autodesk.Revit.UI;
using HStation.RevitDev.RevitDataExport.Common;
using HStation.RevitDev.RevitDataExport.Entity;
using HStation.RevitDev.RevitDataExport.Enum;
using HStation.RevitDev.RevitDataExport.Utility;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace HStation.RevitDev.RevitDataExport.Forms
{
///
/// WPF_FamilyPanel.xaml 的交互逻辑
///
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 YWFamilyType m_type;
private List m_displayFamilys = new List();
private List m_realFamilys = new List();
public Wpf_FamilyPanel(YWFamilyType type)
{
m_type = type;
m_familyFileDir = Path.Combine(GlobalResource.FamilysDirectory, type.GetDescription());
InitializeComponent();
if (type == YWFamilyType.YWFT_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, YWFamilyType.YWFT_Pipe.GetDescription());
var filePaths = Directory.GetFiles(pipeDir);
foreach (var filePath in filePaths)
{
Button btn = CreateButton(filePath);
m_realFamilys.Add(btn);
m_displayFamilys.Add(btn);
wrapPanel_family2.Children.Add(btn);
}
}
private Button CreateButton(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, YWFamilyType.YWFT_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.PipePlacing = true;
m_createPipeHandler.Raise();
}
private void InitializeCostomControls()
{
string[] filePaths = Directory.GetFiles(m_familyFileDir, "*.rfa", SearchOption.TopDirectoryOnly);
foreach (string rfaPath in filePaths)
{
AddToFamilyPanel(rfaPath);
}
}
private void AddToFamilyPanel(string rfaPath)
{
Bitmap bitmap = ThumbnailUtils.GetThumbnailWithoutRevit(rfaPath);
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;
m_realFamilys.Add(btn);
m_displayFamilys.Add(btn);
this.wrapPanel_family2.Children.Add(btn);
}
private void FamilyBtnClick(object sender, RoutedEventArgs e)
{
GlobalResource.InstancePlacing = true;
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)
{
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 = ValidateFileVersion(selectPaths);
foreach (string path in validPaths)
{
CopyToFamilyDirectory(path);
AddToFamilyPanel(path);
}
}
catch (Exception ex)
{
TaskDialog.Show("错误", $"{ex.Message}");
}
TaskDialog.Show("提示", "添加成功!");
}
private void CopyToFamilyDirectory(string path)
{
var fileName = Path.GetFileName(path);
var newPath = Path.Combine(GlobalResource.FamilysDirectory + $@"/{m_type.GetDescription()}", fileName);
try
{
File.Copy(path, newPath, true);
}
catch (Exception ex)
{
TaskDialog.Show(path, $"文件复制失败,{ex.Message}");
}
}
private IEnumerable ValidateFileVersion(string[] selectPaths)
{
var result = new List();
foreach ( var selectPath in selectPaths)
{
var fileVersion = VersionUtil.GetVersion(selectPath);
if (fileVersion == null)
{
TaskDialog.Show("警告", "无法检测文件版本,载入族文件失败!");
}
if ( fileVersion.CompareTo(GlobalResource.CurrentRevitVersion) > 0)
{
TaskDialog.Show("警告","文件版本高于当前Revit版本,无法载入族文件!");
}
else
{
result.Add(selectPath);
}
}
return result;
}
}
}