Revit二次开发_WPF的使用

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

窗口:

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:\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 二次开发使用 WPF 的相关信息 在 Revit 二次开发领域,WPF 是一种常用的 UI 技术,用于创建自定义对话框、窗口以及更复杂的交互界面。以下是有关如何在 Revit 中利用 WPF 进行二次开发的一些关键点: #### 使用 WPF 创建自定义窗口 通过 WPF 可以为用户提供更好的用户体验和灵活的界面设计能力。下面是一个基于引用的内容来说明如何实现这一目标。 根据已知的信息,在某些情况下可能需要加载外部网页或显示动态内容。例如,可以通过 `WebView2` 来嵌入浏览器控件[^3]。这使得开发者能够轻松集成 HTML 页面或其他在线资源到 Revit 插件中。 ```csharp // XAML 定义部分 <Window x:Class="TestCmd.WebWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestCmd" xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf" Title="WebWindow" Height="650" Width="1000"> <Grid> <wv2:WebView2 Name="webView"/> </Grid> </Window> // C# 后端逻辑初始化 WebView 控件并导航至指定 URL public partial class WebWindow : Window { public WebWindow() { InitializeComponent(); InitializeWebView("http://www.baidu.com"); } // 初始化 WebView 控件的方法 private async void InitializeWebView(string url) { await webView.EnsureCoreWebView2Async(null); webView.CoreWebView2.Navigate(url); } } ``` 上述代码展示了如何将一个简单的 WebView 组件嵌入到 WPF 应用程序中,并设置其初始页面地址为百度首页[^3]。 #### 利用 WPF 实现预览功能 除了基本的窗体外,还可以借助 WPF 提供的功能模块完成更多高级任务,比如构建实时渲染器或者参数化模型编辑器等复杂组件。对于涉及图形展示的应用场景来说尤为重要的是——能够在不离开当前工作流的前提下即时观察更改效果的能力[^2]。 当涉及到族文件管理时,通常会希望向用户呈现所选选项对应的三维视图以便确认最终成果是否满足预期需求。此时就可以考虑采用类似于以下方式的设计思路:先获取所需几何形状的数据结构形式(即 Element),接着将其转换成适配特定框架所需的格式最后传递给前端进行绘制处理。 ```csharp private void UpdatePreview(Element element){ var geometry = GetGeometryFromElement(element); previewControl.Model = ConvertToModel(geometry); } private ModelData ConvertToModel(GeometryObject geomObj){ ... } ``` 以上伪代码片段示意了更新某个控件上的可视化表示的过程,其中包含了从选定构件提取必要信息并通过适当算法映射为目标平台可识别的对象实例这两个主要阶段。 --- ### 问题
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值