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

原文链接: 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 );
}


  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值