本文演示视图相关的操作。
1.用Revit绘制模型
2.获取视图ID
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
using System.Windows.Forms;
namespace RevitAddin4
{
[TransactionAttribute(TransactionMode.Manual)]
public class RevitAddin : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
IList<UIView> views = uiDoc.GetOpenUIViews();
foreach (var item in views)
{
if (item.ViewId != uiDoc.ActiveView.Id)
continue;
MessageBox.Show("当前视图的ID = " + item.ViewId.ToString(), "信息");
}
return Result.Succeeded;
}
}
}
执行结果:
3.切换视图
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using System.Collections.Generic;
namespace RevitAddin4
{
[TransactionAttribute(TransactionMode.Manual)]
public class RevitAddin : IExternalCommand
{
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{
UIDocument uiDoc = commandData.Application.ActiveUIDocument;
Document doc = uiDoc.Document;
IList<UIView> views = uiDoc.GetOpenUIViews();
foreach (var item in views)
{
if (item.ViewId != uiDoc.ActiveView.Id)
{
View view = doc.GetElement(item.ViewId) as View;
uiDoc.RequestViewChange(view);
}
}
return Result.Succeeded;
}
}
}
执行结果:依次显示所有打开的视图。
将语句
uiDoc.RequestViewChange(view);
替换为
uiDoc.ActiveView = view;
效果相同。