Revit二次开发之屏幕坐标到Revit视图坐标的转换

有时我们需要对屏幕坐标进行转换,也就是我们的鼠标点击的屏幕位置转换到视图坐标里,这个功能很有必要,本测试代码是点击窗体按钮,在按钮相对视图的位置生成两个参照平面,相交点为鼠标点击的那一点——上代码喽!


Revit二次开发之屏幕坐标到Revit视图坐标的转换Revit二次开发之屏幕坐标到Revit视图坐标的转换

 

using Autodesk.Revit.UI;

using Autodesk.Revit.DB;

using Autodesk.Revit.Attributes;

using Autodesk.Revit.UI.Selection;

using Autodesk.Revit.DB.Plumbing;

using Autodesk.Revit.DB.Mechanical;

using System.Xml;

using Autodesk.Revit.UI.Events;

using System.Windows.Forms;

 

namespace HelloWorld

{

    [Transaction(TransactionMode.Manual)]

    [Regeneration(RegenerationOption.Manual)]

    public class Test : IExternalCommand

    {  

        public  Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)

        {

            UIApplication uiApp = commandData.Application;

            UIDocument uiDoc = uiApp.ActiveUIDocument;

            Document Doc = uiDoc.Document;

            Form1 frm = new Form1();

            frm.ShowDialog();

            if (frm.DialogResult == DialogResult.OK)

            {

                System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;

                Autodesk.Revit.DB.View view = Doc.ActiveView;

                UIView uiview = null;

                IList uiviews = uiDoc.GetOpenUIViews();

                foreach (UIView uv in uiviews)

                {

                    if (uv.ViewId.Equals(view.Id))

                    {

                        uiview = uv;

                        break;

                    }

                }

                Rectangle rect = uiview.GetWindowRectangle();

                //屏幕比例

                double CountWidth = rect.Right - rect.Left;

                double CountHeight = rect.Bottom - rect.Top;

                double k = (point.X - rect.Left) / CountWidth;

                double h = (rect.Bottom - point.Y) / CountHeight;

 

                IList corners = uiview.GetZoomCorners();

                XYZ p1 = corners[0];

                XYZ p2 = corners[1];

                double UIWidth = p2.X - p1.X;

                double UIHeight = p2.Y - p1.Y;

                double Wdistance = UIWidth * k;

                double Hdistance = UIHeight * h;

                XYZ xyz = new XYZ(p1.X + Wdistance, p1.Y + Hdistance, 0);

 

                XYZ pt1 = new XYZ(xyz.X - 5, xyz.Y, xyz.Z);

                XYZ pt2 = new XYZ(xyz.X + 5, xyz.Y, xyz.Z);

                XYZ pt3 = new XYZ(xyz.X, xyz.Y + 5, xyz.Z);

                XYZ pt4 = new XYZ(xyz.X, xyz.Y - 5, xyz.Z);

                XYZ vec = XYZ.BasisZ;

                Transaction ts = new Transaction(Doc, "BIM");

                ts.Start();

                ReferencePlane refPlane = Doc.Create.NewReferencePlane(pt1, pt2, vec, Doc.ActiveView);

                ReferencePlane refPlane2 = Doc.Create.NewReferencePlane(pt3, pt4, vec, Doc.ActiveView);

                ts.Commit();

 

            }

            return Result.Succeeded;

        }

       

    }

    

}

  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值