Files
RookieStation/RookieStation/CommonTools/Views/WpfFamilyDockablePane.xaml.cs
2021-09-09 12:39:12 +08:00

168 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using Autodesk.Revit.UI;
using HandyControl.Tools;
using RookieStation.CommonTools.ExtHandler;
using RookieStation.CommonTools.Models;
using RookieStation.Properties;
using RookieStation.Utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
namespace RookieStation.CommonTools.Views
{
/// <summary>
/// WpfFamilyDockablePane.xaml 的交互逻辑
/// </summary>
public partial class WpfFamilyDockablePane : Page, IDockablePaneProvider
{
internal LoadFamilyHandler LoadHandler = new LoadFamilyHandler();
public ExternalEvent LoadFamilyExEvent { get; set; }
public string FamilyFolder;
private ObservableCollection<FamilyInfo> FamilyInfos = null;
public WpfFamilyDockablePane()
{
FamilyFolder = Settings.Default.FamilyFolder;
LoadFamilyExEvent = ExternalEvent.Create(LoadHandler);
//if (IsContain)
//{
// FamilyInfos = GetFamilyFiles(FamilyFolder, SearchOption.AllDirectories);
//}
//else
//{
// FamilyInfos = GetFamilyFiles(FamilyFolder, SearchOption.TopDirectoryOnly);
//}
//cbbLibrary.DataContext
InitializeComponent();
btnSelectPath.ToolTip = FamilyFolder;
cbbLibrary.ItemsSource = GetTopDirectoryFolder(FamilyFolder);
}
public void SetupDockablePane(DockablePaneProviderData data)
{
data.FrameworkElement = this as FrameworkElement;
data.InitialState = new DockablePaneState
{
DockPosition = DockPosition.Left
};
}
private Dictionary<string, string> GetTopDirectoryFolder(string folder)
{
DirectoryInfo directory = new DirectoryInfo(folder);
var dirs = directory.GetDirectories("*", SearchOption.TopDirectoryOnly);
//ComboBox Key显示Value路径下的文件夹路径
Dictionary<string, string> dictFolderInfo = new Dictionary<string, string>();
for (int i = 0; i < dirs.Count(); i++)
{
dictFolderInfo.Add(dirs[i].Name, dirs[i].FullName);
}
return dictFolderInfo;
}
private ObservableCollection<FamilyInfo> GetFamilyInfos(string folder)
{
var fileinfos = new ObservableCollection<FamilyInfo>();
DirectoryInfo directory = new DirectoryInfo(folder);
var files = directory.GetFiles("*.rfa");
for (int i = 0; i < files.Count(); i++)
{
FamilyInfo fi = new FamilyInfo(files[i].FullName);
fileinfos.Add(fi);
}
return fileinfos;
}
private void cbbLibrary_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var selected = cbbLibrary.SelectedValue;
FamilyInfos = GetFamilyInfos(selected.ToString());
lbFamilyLib.ItemsSource = FamilyInfos;
//lbFamilyLib.DataContext =;
}
private void BtnLoadFamily_Click(object sender, RoutedEventArgs e)
{
try
{
System.Windows.Controls.Button btn = sender as System.Windows.Controls.Button;
LoadHandler.FamilyInfo = btn.DataContext as FamilyInfo;
LoadFamilyExEvent.Raise();
}
catch (Exception ex)
{
Log.WriteLog(ex.Message);
}
}
private void BtnOpenFile_Click(object sender, RoutedEventArgs e)
{
System.Windows.Controls.Button btn = sender as System.Windows.Controls.Button;
FamilyInfo file = btn.DataContext as FamilyInfo;
ExploreFile(file.FullFileName);
}
/// <summary>
/// 定位文件
/// </summary>
/// <param name="filePath"></param>
public static void ExploreFile(string filePath)
{
Process proc = new Process();
proc.StartInfo.FileName = "explorer";
//打开资源管理器
proc.StartInfo.Arguments = @"/select," + filePath;
proc.Start();
}
private void btnSelectPath_Click(object sender, RoutedEventArgs e)
{
VistaFolderBrowserDialog folderBrowserDialog = new VistaFolderBrowserDialog()
{
Multiselect = false,
};
if (folderBrowserDialog.ShowDialog())
{
FamilyFolder = folderBrowserDialog.SelectedPath;
btnSelectPath.ToolTip = FamilyFolder;
cbbLibrary.DataContext = GetTopDirectoryFolder(FamilyFolder);
Settings.Default.FamilyFolder = FamilyFolder;
Settings.Default.Save();
}
}
private void btnSearch_Click(object sender, RoutedEventArgs e)
{
if (tbSearch.Text == "" || lbFamilyLib.ItemsSource == null)
{
return;
}
ObservableCollection<FamilyInfo> searchfamily = new ObservableCollection<FamilyInfo>();
foreach (FamilyInfo item in FamilyInfos)
{
if (item.Title.Contains(tbSearch.Text.Trim()))
{
searchfamily.Add(item);
}
}
lbFamilyLib.ItemsSource = searchfamily;
}
private void TbSearch_TextChanged(object sender, TextChangedEventArgs e)
{
if (tbSearch.Text == "")
{
lbFamilyLib.ItemsSource = FamilyInfos;
}
}
}
}