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
{
///
/// 判断两条线是否相交
///
///
///
///
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;
}
///
/// 是否共线
///
///
///
///
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;
}
///
/// 判断是否是同一根线
///
///
///
///
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;
}
///
/// 设置z轴参数
///
///
///
///
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));
}
///
/// 点到线的距离
///
///
///
///
///
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;
}
///
/// 两平行线求中线(中线长度按最长条)
///
///
///
///
///
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;
}
}
}