123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- using Autodesk.Revit.DB;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using FWindSoft.SystemExtensions;
- namespace FWindSoft.Revit
- {
- /// <summary>
- /// 点的扩展相关
- /// </summary>
- public static class XYZExtension
- {
- /// <summary>
- /// 向量是否平行
- /// </summary>
- /// <param name="baseDirection"></param>
- /// <param name="direction"></param>
- /// <returns></returns>
- public static bool IsParallel(this XYZ baseDirection, XYZ direction)
- {
- return baseDirection.CrossProduct(direction).GetLength().IsZero();
- }
- /// <summary>
- /// 向量是否垂直
- /// </summary>
- /// <param name="baseDirection"></param>
- /// <param name="direction"></param>
- /// <returns></returns>
- public static bool IsVertical(this XYZ baseDirection, XYZ direction)
- {
- return baseDirection.DotProduct(direction).IsZero();
- }
- /// <summary>
- /// 判断向量是否同向
- /// </summary>
- /// <param name="firstDirection"></param>
- /// <param name="secondDirection"></param>
- /// <returns></returns>
- public static bool IsSameDirection(this XYZ firstDirection, XYZ secondDirection)
- {
- if(firstDirection.IsParallel(secondDirection))
- {
- return firstDirection.DotProduct(secondDirection).MoreEqual(0);
- }
- return false;
- }
- /// <summary>
- /// 向量方向相反
- /// </summary>
- /// <param name="firstDirection"></param>
- /// <param name="secondDirection"></param>
- /// <returns></returns>
- public static bool IsOppositeDirection(this XYZ firstDirection, XYZ secondDirection)
- {
- if (firstDirection.IsParallel(secondDirection))
- {
- return secondDirection.DotProduct(secondDirection).LessEqual(0);
- }
- return false;
- }
-
- /// <summary>
- /// 偏移指定向量
- /// </summary>
- /// <param name="origin"></param>
- /// <param name="vector"></param>
- /// <returns></returns>
- public static XYZ Offset(this XYZ origin,XYZ vector)
- {
- return origin + vector;
- }
- /// <summary>
- /// 判断两个点是否重合
- /// </summary>
- /// <param name="first"></param>
- /// <param name="second"></param>
- /// <param name="tolerance"></param>
- /// <returns></returns>
- public static bool IsEqual(this XYZ first, XYZ second, double tolerance)
- {
- return first.X.IsEqual(second.X, tolerance)
- && first.Y.IsEqual(second.Y, tolerance)
- && first.Z.IsEqual(second.Z, tolerance);
- }
- /// <summary>
- /// 判断两个点是否重合
- /// </summary>
- /// <param name="first"></param>
- /// <param name="second"></param>
- /// <returns></returns>
- public static bool IsEqual(this XYZ first, XYZ second)
- {
- return first.X.IsEqual(second.X, DoubleExtensions.Precision)
- && first.Y.IsEqual(second.Y, DoubleExtensions.Precision)
- && first.Z.IsEqual(second.Z, DoubleExtensions.Precision);
- }
- /// <summary>
- /// 判断两个点是否重合
- /// </summary>
- /// <param name="first"></param>
- /// <param name="second"></param>
- /// <returns></returns>
- public static bool IsEqual2(this XYZ first, XYZ second)
- {
- return first.X.IsEqual(second.X, DoubleExtensions.Precision)
- && first.Y.IsEqual(second.Y, DoubleExtensions.Precision);
- }
- /// <summary>
- /// 按指定方向的轴,在原点位置旋转向量
- /// </summary>
- /// <param name="vec"></param>
- /// <param name="axis"></param>
- /// <param name="angle"></param>
- /// <returns></returns>
- public static XYZ RotateVector(this XYZ vec, XYZ axis, double angle)
- {
- var transform = Transform.CreateRotationAtPoint(axis, angle, XYZ.Zero);
- return transform.OfVector(vec);
- }
- /// <summary>
- /// 计算点到指定平面的投影
- /// </summary>
- /// <param name="input"></param>
- /// <param name="normal"></param>
- /// <param name="origin"></param>
- /// <returns></returns>
- public static XYZ GetPlaneProject(this XYZ input, XYZ normal, XYZ origin)
- {
- if (!normal.IsUnitLength())
- {
- normal = normal.Normalize();
- }
- return input + normal * origin.Subtract(input).DotProduct(normal);
- }
- /// <summary>
- /// 计算点到指定直线的投影
- /// </summary>
- /// <param name="input"></param>
- /// <param name="direction"></param>
- /// <param name="origin"></param>
- /// <returns></returns>
- public static XYZ GetLineProject(this XYZ input, XYZ direction, XYZ origin)
- {
- if (!direction.IsUnitLength())
- {
- direction = direction.Normalize();
- }
- return origin + direction * input.Subtract(origin).DotProduct(direction);
- }
- /// <summary>
- /// 获取任意一个与input垂直的向量
- /// </summary>
- /// <param name="input"></param>
- /// <returns></returns>
- public static XYZ GetAnyVerticalVector(this XYZ input)
- {
- if (!XYZ.BasisX.IsParallel(input))
- {
- return input.CrossProduct(XYZ.BasisX).Normalize();
- }
- else
- {
- return input.CrossProduct(XYZ.BasisY).Normalize();
- }
- }
- /// <summary>
- /// 判断点的投影是否在直线段内
- /// </summary>
- /// <param name="input"></param>
- /// <param name="line"></param>
- /// <returns></returns>
- public static bool IsProjectInLine(this XYZ input, Line line)
- {
- if (!line.IsBound)
- {
- return true;
- }
- var project =line.GetProject(input);
- return line.InCurve(project);
- }
- /// <summary>
- /// 使用Input在line上的投影点分割指定直线
- /// </summary>
- /// <param name="input"></param>
- /// <param name="line"></param>
- /// <returns>如果点在线外侧,则返回一个较大的向量,另一个去掉。如果在线上,则返回两个向量</returns>
- public static List<XYZ> SplitVector(this XYZ input, Line line)
- {
- if (!line.IsBound)
- {
- return new List<XYZ>() { line.Direction * Double.MaxValue, line.Direction * Double.MaxValue, };
- }
- var project = line.GetProject(input);
- var startVector = line.StartPoint() - project;
- var endVector = line.EndPoint() - project;
- if (startVector.DotProduct(endVector).MoreEqual(0))
- {
- //两个向量同向,去较大的返回
- if (startVector.GetLength().More(endVector.GetLength()))
- {
- return new List<XYZ>() { startVector };
- }
- else
- {
- return new List<XYZ>() { endVector };
- }
- }
- return new List<XYZ>() { startVector, endVector };
- }
- }
- }
|