Revit二次开发_WPF的使用

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

窗口:

Revit二次开发_WPF的使用_第1张图片

WPF标签:


    
        
            
            
        
        
        
            
            
        
    

WPF代码:

public partial class FamilyManagerWPF : Window
   {
       //族库路径
       DirectoryInfo dirInfo = new DirectoryInfo(@"C:\ProgramData\Autodesk\RVT 2016\Libraries\China");
       //载入族路径
       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二次开发)