[Revit二次开发]在WPF显示revit的三维视图

来源:互联网 发布:如何制作淘宝水印 编辑:程序博客网 时间:2024/06/05 07:28

在revit插件开发过程中,有时候需要在自己的页面上展示某些元素的模型,关于WF上如何实现在RevitSDK中已经存在着代码,对于初学者来说,要搬运到WPF并不太容易,于是本篇文章模仿了PreviewModel的实现讲解如何在WPF上显示revit的三维视图。

不知道PreviewModel的可以先看一下PreviewModel的解读http://blog.csdn.net/adijeshen/article/details/77099581

这是运行效果

我的实现差不多就是把SDK中的PreviewModel稍微删掉一点

那么来看代码,

首先,看一下PreviewPage的xaml

<Page x:Class="RevitWPFAddin.PreViewPage"      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:RevitWPFAddin"      mc:Ignorable="d"      d:DesignHeight="800" d:DesignWidth="1000"      Title="PreViewPage">    <Grid>        <Grid.RowDefinitions>            <RowDefinition/>            <RowDefinition Height="Auto"/>        </Grid.RowDefinitions>        <Grid Name="previewControlGrid" Grid.Row="0">        </Grid>        <StackPanel Height="80" Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">            <StackPanel Width="80" Margin="70,0">                <Label>Views</Label>                <ComboBox Name="_cbViews" SelectionChanged="cbViews_SelIdxChanged"></ComboBox>            </StackPanel>        </StackPanel>    </Grid></Page>
previewControlGrid就是之后用于存放PreviewControl的地方。

定义一些要用到的变量

using RApplication = Autodesk.Revit.ApplicationServices.Application;using RView = Autodesk.Revit.DB.View;private ElementId _currentDBViewId = null;private Document _dbDocument = null;private RApplication _application = null;private UIApplication _uiApplication = null;private ElementId elementId;private PreviewControl pc = null;
那么PreviewControl就是最主要的部分了。

仿照PreviewModel中的代码,先是构造函数:

        public PreViewPage(RApplication application, ElementId viewId, string dir)        {            InitializeComponent();            _application = application;            _uiApplication = new UIApplication(application);            _dbDocument = _application.OpenDocumentFile(dir);            updateViewsList(elementId);        }
可以看到与PreviewModel中的代码不同之处在于我没有updateDocumentsList函数,这是根据功能决定的。

那么看一下updateViewsList:

private void updateViewsList(ElementId viewId)        {            // fill the combobox with printable views <name + id>            FilteredElementCollector collecotr = new FilteredElementCollector(_dbDocument);            collecotr.OfClass(typeof(Autodesk.Revit.DB.View));            IEnumerable<Autodesk.Revit.DB.View> secs = from Element f in collecotr where (f as Autodesk.Revit.DB.View).CanBePrinted == true select f as Autodesk.Revit.DB.View;            _cbViews.Items.Clear();            DBViewItem activeItem = null;            bool isEmpty = true;            foreach (Autodesk.Revit.DB.View dbView in secs)            {                elementId = dbView.Id;                isEmpty = false;                activeItem = new DBViewItem(dbView, _dbDocument);                _cbViews.Items.Add(activeItem);            }            if(isEmpty == true)            {                RView view = View3D.CreateIsometric(_dbDocument, elementId);                activeItem = new DBViewItem(view, _dbDocument);                _cbViews.Items.Add(activeItem);            }            _cbViews.SelectedItem = activeItem;        }
其实就是讲打开的文档中的视图加入了名为_cdViews的ComboBox控件。

最后一个语句改变了SelectedItem,触发indexchange

        private void cbViews_SelIdxChanged(object sender, SelectionChangedEventArgs e)        {            System.Windows.Controls.ComboBox cb = sender as System.Windows.Controls.ComboBox;            if (cb == null)                return;            DBViewItem dbItem = cb.SelectedItem as DBViewItem;            if (dbItem == null)                return;            if (previewControlGrid.Children != null)            {                previewControlGrid.Children.Clear();            }            if (pc != null)            {                pc.Dispose();            }            pc = new PreviewControl(_dbDocument, dbItem.Id);            previewControlGrid.Children.Add(pc);            _currentDBViewId = dbItem.Id;        }
值得注意的地方是pc!=null的if语句判断,因为revitAPI只允许存在一个previewControl,所以在切换视图的时候务必要将上一个进行析构。

项目源代码已上传 http://download.csdn.net/download/adijeshen/9939701

以上。

阅读全文
0 0
原创粉丝点击