Revit二次开发_WPF的使用

来源:建筑界编辑:黄子俊发布时间:2020-03-22 19:39:32

[摘要] Revit中如果要获取用户输入,除了Winform,还可以使用WPF。在这里记录一个使用WPF编写的简单窗口例子。例子中读取了Revit的自带族库路径

Revit中如果要获取用户输入,除了Winform,还可以使用WPF。
在这里记录一个使用WPF编写的简单窗口例子。例子中读取了Revit的自带族库路径,然后根据族库的结构生成树状列表,用户可以在树状列表中选择族载入到项目中。
不知为何,即使是一样的界面,但觉得WPF做出来的窗体比Winform的顺眼些…

窗口:

Revit二次开发_WPF的使用

WPF标签:

<Window x:Class="ClassLibrary2.FamilyManagerWPF"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:local="clr-namespace:ClassLibrary2"mc:Ignorable="d"d:DesignHeight="300"d:DesignWidth="300"Height="500"Width="300"> <Grid> <Grid.RowDefinitions> <RowDefinition></RowDefinition> <RowDefinition Height="auto"></RowDefinition> </Grid.RowDefinitions> <TreeView Name="FamilyTreeList"Margin="10"Loaded="FamilyTreeList_Loaded"TreeViewItem.Expanded="FamilyTreeList_Expanded"TreeViewItem.Selected="FamilyTreeList_Selected"></TreeView> <StackPanel Grid.Row="1"HorizontalAlignment="Right"Orientation="Horizontal"> <Button Name="Btn_Load"Margin="10,10,2,10"IsEnabled="False"Click="Btn_Load_Click">载入</Button> <Button Name="Btn_Cancel"Margin="2,10,10,10"IsCancel="True"Click="Btn_Cancel_Click">取消</Button> </StackPanel> </Grid></Window>

WPF代码:

public partial class FamilyManagerWPF : Window { //族库路径 DirectoryInfo dirInfo = new DirectoryInfo(@"C:ProgramDataAutodeskRVT 2016LibrariesChina"); //载入族路径 string familyFilePath; public FamilyManagerWPF() { InitializeComponent(); } //树控件载入事件 private void FamilyTreeList_Loaded(object sender, RoutedEventArgs e) { //遍历文件夹 foreach(DirectoryInfo di in dirInfo.GetDirectories()) { //创建子项 TreeViewItem item = new TreeViewItem(); item.Tag = di; item.Header = di.Name; //占位符 if (di.GetDirectories().Length > 0 || di.GetFiles("*.rfa").Length > 0) item.Items.Add("*"); //添加子项 FamilyTreeList.Items.Add(item); } //遍历族文件 CreateFamilyItems(dirInfo, FamilyTreeList); } //节点展开事件 private void FamilyTreeList_Expanded(object sender, RoutedEventArgs e) { TreeViewItem item = (TreeViewItem)e.OriginalSource; item.Items.Clear(); //遍历文件夹 DirectoryInfo di = (DirectoryInfo)item.Tag; foreach(DirectoryInfo subDi in di.GetDirectories()) { //创建子项 TreeViewItem subItem = new TreeViewItem(); subItem.Tag = subDi; subItem.Header = subDi.Name; //占位符 if (subDi.GetDirectories().Length > 0 || subDi.GetFiles("*.rfa").Length > 0) subItem.Items.Add("*"); //添加子项 item.Items.Add(subItem); } //遍历族文件 CreateFamilyItems(di, item); } //节点选择事件 private void FamilyTreeList_Selected(object sender, RoutedEventArgs e) { TreeViewItem item = (TreeViewItem)e.Source; if (item.Tag is FileInfo) { Btn_Load.IsEnabled = true; } else { Btn_Load.IsEnabled = false; } } //载入按钮 private void Btn_Load_Click(object sender, RoutedEventArgs e) { if (FamilyTreeList.SelectedItem != null) { TreeViewItem item = (TreeViewItem)FamilyTreeList.SelectedItem; FileInfo familyFi = (FileInfo)item.Tag; familyFilePath = familyFi.FullName; DialogResult = true; } Close(); } //取消按钮 private void Btn_Cancel_Click(object sender, RoutedEventArgs e) { DialogResult = false; Close(); } //创建对应族文件的子项 private void CreateFamilyItems(DirectoryInfo directoryInfo,Control control) { //遍历族文件 foreach(FileInfo fi in directoryInfo.GetFiles("*.rfa")) { //创建子项 TreeViewItem item = new TreeViewItem(); item.Tag = fi; item.Header = fi.Name; //添加子项 if (control is TreeView) { ((TreeView)control).Items.Add(item); continue; } if (control is TreeViewItem) { ((TreeViewItem)control).Items.Add(item); } } } public string FamilyFilePath => familyFilePath; }

调用窗体代码:

FamilyManagerWPF form = new FamilyManagerWPF();if (form.ShowDialog() == true){ using(Transaction tran=new Transaction(doc,"载入族")) { tran.Start(); Family family; doc.LoadFamily(form.FamilyFilePath, UIDocument.GetRevitUIFamilyLoadOptions(),out family); tran.Commit(); }}

 

Revit,_WPF

延展阅读

相关文章


热门资讯推荐