using Autodesk.Revit.DB; using FWindSoft.SystemExtensions; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FWindSoft.Revit { public static class LineExtension { /// /// 两直线平行 /// /// /// /// public static bool IsParallel(this Line line1, Line line2) { return line1.Direction.IsParallel(line2.Direction); } /// /// 获取线的端点与指定点 最远的点 /// /// /// /// public static XYZ GetFarnessPoint(this Line line, XYZ point) { XYZ start = line.StartPoint(); XYZ end = line.EndPoint(); return start.DistanceTo(point).Less(end.DistanceTo(point)) ? end : start; } /// /// 获取线的端点与指定点 最近的点 /// /// /// /// public static XYZ GetNearnessPoint(this Line line, XYZ point) { XYZ start = line.StartPoint(); XYZ end = line.EndPoint(); return start.DistanceTo(point).Less(end.DistanceTo(point)) ? start : end; } /// /// 获取空间直线的垂足点 /// /// /// /// public static XYZ GetSpatialIntersection(this Line baseLine, Line line2) { //两条法向量构建,和baseLine上一点构建平面,line2中的点在平面内的投影线。求投影线和baseLine1两条直线的夹角 XYZ basePoint = null; do { if (baseLine.IsParallel(line2)) { break; } var normal = baseLine.Direction.CrossProduct(line2.Direction); var origin = baseLine.Origin; var start = line2.Origin; var end = start + 10 * line2.Direction; if (line2.IsBound) { start = line2.StartPoint(); end = line2.EndPoint(); } //取投影点 XYZ point1 = start.GetPlaneProject(normal, origin); XYZ point2 = end.GetPlaneProject(normal, origin); if (point1.IsEqual(point2)) { basePoint = point1; } else { var unBoundLine1 = baseLine.CloneUnbound(); var unboundLine2 = point1.NewLine(point2).CloneUnbound(); basePoint = unBoundLine1.GetIntersection(unboundLine2); } } while (false); return basePoint; } /// /// 是否共线 /// /// /// /// public static bool IsCollinear(this Line baseLine,Line line2) { return baseLine.IsParallel(line2) && baseLine.Direction.IsParallel(line2.Origin - baseLine.Origin); } /// /// 获取输入点在直线上的投影点 /// /// /// /// public static XYZ GetProject(this Line line, XYZ input) { var origin = line.Origin; var direction = line.Direction; var useVector = input - origin; var project = useVector.DotProduct(direction) * direction + origin; return project; } } }