123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- 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
- {
- /// <summary>
- /// 两直线平行
- /// </summary>
- /// <param name="line1"></param>
- /// <param name="line2"></param>
- /// <returns></returns>
- public static bool IsParallel(this Line line1, Line line2)
- {
- return line1.Direction.IsParallel(line2.Direction);
- }
- /// <summary>
- /// 获取线的端点与指定点 最远的点
- /// </summary>
- /// <param name="line"></param>
- /// <param name="point"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取线的端点与指定点 最近的点
- /// </summary>
- /// <param name="line"></param>
- /// <param name="point"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 获取空间直线的垂足点
- /// </summary>
- /// <param name="baseLine"></param>
- /// <param name="line2"></param>
- /// <returns></returns>
- 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;
- }
- /// <summary>
- /// 是否共线
- /// </summary>
- /// <param name="baseLine"></param>
- /// <param name="line2"></param>
- /// <returns></returns>
- public static bool IsCollinear(this Line baseLine,Line line2)
- {
- return baseLine.IsParallel(line2) && baseLine.Direction.IsParallel(line2.Origin - baseLine.Origin);
- }
- /// <summary>
- /// 获取输入点在直线上的投影点
- /// </summary>
- /// <param name="line"></param>
- /// <param name="input"></param>
- /// <returns></returns>
- 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;
- }
- }
- }
|