[RevitSDK解读]UIAPI->PreviewModel:在自己的界面显示Revit视图

2017-08-11 20:20:04来源:CSDN作者:AdijeShen人点击

分享

PreviewModel的主要作用是介绍了PreviewControl这个WPF控件,用于在自己的窗口展示revit元素。

效果如下:


因为要获取application与document,所以运行一定要保证在有打开一个族文件或者项目文件的情况下。

首先看一下Execute函数:

 public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)      {         _dbdocument = commandData.Application.ActiveUIDocument.Document;         TransactionGroup outerGroup = new TransactionGroup(_dbdocument, "preview control");         outerGroup.Start();         try         {            PreviewModel form = new PreviewModel(commandData.Application.Application, new ElementId(-1));            form.ShowDialog();         }         catch (Exception e)         {            throw e;         }         finally         {            outerGroup.RollBack();         }         return Result.Succeeded;      }
重要的部分就是创建了一个PreviewModel,那么PreviewModel在构造函数中做了什么呢?

        public PreviewModel(RApplication application, ElementId viewId)        {            InitializeComponent();            _application = application;            _uiApplication = new UIApplication(application);            _dbDocument = _uiApplication.ActiveUIDocument.Document;            updateDocumentList(_dbDocument);            updateViewsList(_uiApplication.ActiveUIDocument.ActiveView.Id);        }
从这段代码中不难看出,首先是记录了一些项目和文档信息,然后通过这些信息来更新Documents和Views的ComboBox列表

updateDocumentList函数和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;            foreach (Autodesk.Revit.DB.View dbView in secs)            {                if (viewId == null || viewId.IntegerValue < 0)                {                    activeItem = new DBViewItem(dbView, _dbDocument);                    viewId = dbView.Id;                }                if (dbView.Id == viewId)                {                    activeItem = new DBViewItem(dbView, _dbDocument);                    _cbViews.Items.Add(activeItem);                }                else                    _cbViews.Items.Add(new DBViewItem(dbView, _dbDocument));            }            _cbViews.SelectedItem = activeItem;        }        private void updateDocumentList(Document selectedDocument)        {            // fill the documents to the comboxbox _cbDocuments.            DBDocumentItem activeItem = null;            _cbDocuments.Items.Clear();            DocumentSetIterator docIter = _application.Documents.ForwardIterator();            docIter.Reset();            while (docIter.MoveNext())            {                Document dbDoc = docIter.Current as Document;                String documentName = null;                DBDocumentItem item = null;                if (dbDoc != null)                {                    if (dbDoc.IsFamilyDocument)                    {                        item = new DBDocumentItem(dbDoc.PathName, dbDoc);                    }                    else                    {                        String projName = dbDoc.ProjectInformation.Name;                        if (String.IsNullOrEmpty(projName) || projName.ToLower().CompareTo("project name") == 0)                        {                            if (String.IsNullOrEmpty(dbDoc.PathName))                                documentName = projName;                            else                                documentName = new System.IO.FileInfo(dbDoc.PathName).Name;                        }                        else                            documentName = projName;                        item = new DBDocumentItem(documentName, dbDoc);                    }                    if (dbDoc.Equals(selectedDocument))                    {                        _dbDocument = selectedDocument;                        activeItem = item;                    }                    _cbDocuments.Items.Add(item);                }            }            _cbDocuments.Items.Add(new DBDocumentItem());            _cbDocuments.SelectedItem = activeItem;        }
很奇怪的一点是这两个函数中都没有调用PreviewControl,那么他是如何显示预览视图的呢?

可以看到updateView中最后一句代码

            _cbViews.SelectedItem = activeItem;
将ComboBox的选项改为了当前的item

那么就好办了,看一下ComboBox的EventHandler:

        private void cbViews_SelIdxChanged(object sender, EventArgs e)        {            System.Windows.Forms.ComboBox cb = sender as System.Windows.Forms.ComboBox;            if (cb == null)                return;            DBViewItem dbItem = cb.SelectedItem as DBViewItem;            if (dbItem == null)                return;            PreviewControl vc = _elementHostWPF.Child as PreviewControl;            if (vc != null)                vc.Dispose();            _elementHostWPF.Child = new PreviewControl(_dbDocument, dbItem.Id);            _currentDBViewId = dbItem.Id;        }
其中_elementHostWPF为一个elementHost类,用于在WF中调用WPF控件。因为PreviewControl是一个WPF的控件,所以这里这么使用。

然后看下方

            PreviewControl vc = _elementHostWPF.Child as PreviewControl;            if (vc != null)                vc.Dispose();
这里这段代码是因为一个插件只能调用一个PreviewControl,所以要将上一个Control清空。

然后PreviewControl的创建需要传的参数为一个UIDocument以及一个ElementId。

以上。

            _cbViews.SelectedItem = activeItem;

最新文章

123

最新摄影

闪念基因

微信扫一扫

第七城市微信公众平台