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