BIM建筑网
更专业的BIM技术学习网站!


Revit二次开发_WPF的使用

免费的Ai上线啦!
BIM建筑网自主研发,你想要Ai功能都在这里!
Ai·提升效率 技术答疑

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

 

微信公众号:xuebim
关注建筑行业BIM发展、研究建筑新技术,汇集建筑前沿信息!
← 微信扫一扫,关注我们+
赞(0) 打赏
BIM建筑网 » Revit二次开发_WPF的使用

评论 抢沙发

评论前必须登录!

 

BIM建筑网,更专业的BIM技术学习网站!

关注建筑新动态,分享建筑新技术

联系我们关于BIM建筑网

觉得文章有用就打赏一下小编吧

非常感谢你的打赏,我们将继续提供更多优质内容,让我们一起创建更加美好的网络世界!

支付宝扫一扫打赏

微信扫一扫打赏

扫码登录

微信「关注」,快捷登录
扫码关注后会自动登录
注册登录代表您已同意《用户许可协议》
账号登录 | 其他登录

|登录

找回密码

|账号登录注册