Building Coder(Revit 二次开发) - UIView 和 Windows 设备坐标

来源:互联网 发布:linux svn改登录账号 编辑:程序博客网 时间:2024/06/03 10:18
原文链接:UIView and Windows Device Coordinates

Revit 2013 API 中新提供了一个新的视图 API,具体地说是提供一个新类 UIView。UIView 代表一个视图窗口,让我们可以通过程序来移动、缩放和决定窗口平铺时的大小等等一系列操作。也为我们从其它可视化环境切换回视图提供了可能。


UIView 目前只公开了三个方法:
1. GetWindowRectangle:使用窗口坐标系返回视图的绘图区域的矩形坐标;
2. GetZoomCorners:使用 Revit 模型坐标系返回视图矩形的四个角坐标;
3. ZoomAndCreateRectangle:将视图缩放并居中到一个指定的矩形区域中

UIView 是 Revit API 第一次提供了窗口坐标和 Revit 坐标之间的关联,开启了 Revit 全新的交互方式。比方说:在 Idling 事件中显示自定义的 Tooltip ……

值得一提的是,目前 Revit API 只能通过 UIDocument.GetOpenUIViews() 方法获取 UIView 对象。由于每个 UIView 都是和一个视图绑定的,所以我们可以通过 UIView.ViewId 来判断某个 UIView 是属于哪个视图的。


我创建了一个简单的外部命令 WinCoords 来测试 UIView。它的功能如下:
- 获取当前视图
- 获取当前文档的所有 UIView 对象
- 找到属于当前视图的 UIView 对象
- 读取当前 UIView 的窗口和 Revit 坐标
- 调用 ZoomAndCenterRectangle 将 Revit 模型放大 10%

public Result Execute( ExternalCommandData commandData, ref string message, ElementSet elements ){UIApplication uiapp = commandData.Application;UIDocument uidoc = uiapp.ActiveUIDocument;Document doc = uidoc.Document;View view = doc.ActiveView;UIView uiview = null;IList<UIView> uiviews = uidoc.GetOpenUIViews();foreach( UIView uv in uiviews ){if( uv.ViewId.Equals( view.Id ) ){uiview = uv;break;}}Rectangle rect = uiview.GetWindowRectangle();IList<XYZ> corners = uiview.GetZoomCorners();XYZ p = corners[0];XYZ q = corners[1];string msg = string.Format( "UIView Windows rectangle: {0}; zoom corners: {1}-{2}; click Close to zoom in by 10%.", RectangleString( rect ), PointString( p ), PointString( q ) );TaskDialog.Show( "WinCoords", msg );// Calculate new zoom corners to // zoom in by 10%, i.e. the two corners// end up at 0.45 of their previous// distance to the centre point.XYZ v = q - p;XYZ center = p + 0.5 * v;v *= 0.45;p = center - v;q = center + v;uiview.ZoomAndCenterRectangle( p, q );return Result.Succeeded;}

注意:窗口坐标系中 Y 方向和通常的坐标系中相反。显示矩形坐标的 RectangleString() 方法代码如下:

/// <summary>/// 返回一个表示矩形区域的字符串,格式是(左上角)-(右下角)/// 窗口坐标系的 Y 方向是相反方向/// </summary>static string RectangleString( Rectangle r ){return string.Format( "({0},{1})-({2},{3})", r.Left, r.Top, r.Right, r.Bottom );}


原创粉丝点击