ComputerGraphics/20210129/zzx-20210129作业/Method/LineMethod.cs
2021-02-25 15:53:31 +08:00

139 lines
5.3 KiB
C#

using Autodesk.Revit.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GraphicsStudy
{
public static class LineMethod
{
/// <summary>
/// 判断两条线是否相交
/// </summary>
/// <param name="l1"></param>
/// <param name="l2"></param>
/// <returns></returns>
public static bool IsIntersect(this Line l1, Line l2)
{
bool result = false;
Line l1LengthenLine = Line.CreateBound(l1.GetEndPoint(0), l1.GetEndPoint(1));
l1LengthenLine.MakeUnbound();
var proResult = l1LengthenLine.Project(l2.Evaluate(0.5, true));
if (proResult.XYZPoint.IsOnLine(l1) && proResult.XYZPoint.IsOnLine(l2))
{
result = true;
}
return result;
}
/// <summary>
/// 是否共线
/// </summary>
/// <param name="line1"></param>
/// <param name="line2"></param>
/// <returns></returns>
public static bool IsCollinear(this Autodesk.Revit.DB.Line line1, Autodesk.Revit.DB.Line line2, double distance = 0.05)
{
return line1.Direction.IsParallel(line2.Direction) && line2.DisToPoint(line1.Evaluate(0.5, true), out XYZ direction) < distance;
}
/// <summary>
/// 判断是否是同一根线
/// </summary>
/// <param name="l1"></param>
/// <param name="l2"></param>
/// <returns></returns>
public static bool IsSameLine(this Autodesk.Revit.DB.Line l1, Autodesk.Revit.DB.Line l2)
{
if (l1.GetEndPoint(0).DistanceTo(l2.GetEndPoint(0)) < 1 / 304.8 && l1.GetEndPoint(1).DistanceTo(l2.GetEndPoint(1)) < 1 / 304.8)
return true;
else if (l1.GetEndPoint(1).DistanceTo(l2.GetEndPoint(0)) < 1 / 304.8 && l1.GetEndPoint(0).DistanceTo(l2.GetEndPoint(1)) < 1 / 304.8)
return true;
else
return false;
}
/// <summary>
/// 设置z轴参数
/// </summary>
/// <param name="line"></param>
/// <param name="z"></param>
/// <returns></returns>
public static Autodesk.Revit.DB.Line SetZ(this Autodesk.Revit.DB.Line line, double z = 0)
{
if (line.Direction.IsParallel(XYZ.BasisZ))
throw new ArgumentException("线无法进行Z值设置");
if (line.GetEndPoint(0).SetZ(z).DistanceTo(line.GetEndPoint(1).SetZ(z)) < 1 / 304.8)
throw new ArgumentException("线太短");
return Autodesk.Revit.DB.Line.CreateBound(line.GetEndPoint(0).SetZ(z), line.GetEndPoint(1).SetZ(z));
}
public static Curve SetZ(this Curve curve, double z = 0)
{
var line = curve as Autodesk.Revit.DB.Line;
if (line.Direction.IsParallel(XYZ.BasisZ))
throw new ArgumentException("线无法进行Z值设置");
if (line.GetEndPoint(0).SetZ(z).DistanceTo(line.GetEndPoint(1).SetZ(z)) < 1 / 304.8)
throw new ArgumentException("线太短");
return Autodesk.Revit.DB.Line.CreateBound(line.GetEndPoint(0).SetZ(z), line.GetEndPoint(1).SetZ(z));
}
/// <summary>
/// 点到线的距离
/// </summary>
/// <param name="line"></param>
/// <param name="point"></param>
/// <param name="direction"></param>
/// <returns></returns>
public static double DisToPoint(this Autodesk.Revit.DB.Line line, XYZ point, out XYZ direction)
{
var nwLine = line.SetZ();
nwLine.MakeUnbound();
XYZ projectPoint = nwLine.Project(point.SetZ()).XYZPoint;
double distance = point.SetZ().DistanceTo(projectPoint);
direction = (point.SetZ() - projectPoint).Normalize();
return distance;
}
public static double DisToPoint(this Autodesk.Revit.DB.Line line, XYZ point)
{
var nwLine = line.SetZ();
nwLine.MakeUnbound();
XYZ projectPoint = nwLine.Project(point.SetZ()).XYZPoint;
double distance = point.SetZ().DistanceTo(projectPoint);
return distance;
}
/// <summary>
/// 两平行线求中线(中线长度按最长条)
/// </summary>
/// <param name="curve1"></param>
/// <param name="curve2"></param>
/// <param name="distance"></param>
/// <returns></returns>
public static Line GetMidLine(this Line curve1, Line curve2, out double distance)
{
distance = double.MaxValue;
if (curve1.Length < curve2.Length)
{
var tempLine = curve1;
curve1 = curve2;
curve2 = tempLine;
}
Line midline;
XYZ startPoint = curve1.GetEndPoint(0);
XYZ endPoint = curve1.GetEndPoint(1);
Line line2 = Line.CreateBound(curve2.GetEndPoint(0), curve2.GetEndPoint(1));
line2.MakeUnbound();
midline = Line.CreateBound((startPoint + line2.Project(startPoint).XYZPoint) / 2, (endPoint + line2.Project(endPoint).XYZPoint) / 2);
distance = startPoint.DistanceTo(line2.Project(startPoint).XYZPoint).ToMM();
return midline;
}
}
}