This is little bit complicated:
- You need to traverse the <Solid> of <Duct> elements.
- Get all <Edge> objects, then tessellate all feature poins along the Edge.GetCurve() curve objects.
- Get all feature points, then sort them along a vector.
- Calculate the "Distance Along Vector" from the near point to far point.
public static Pair<XYZ, XYZ> ClosestFarthestPoints(IEnumerable<XYZ> points, XYZ dir)
{
XYZ closest = null, farthest = null;
foreach (var pnt in points)
{
if (pnt != null)
{
if (closest == null || farthest == null)
{
closest = farthest = pnt;
continue;
}
else
{
XYZ fromClosest = pnt - closest;
if (fromClosest.DotProduct(dir) < 0)
{
closest = pnt;
}
else
{
XYZ fromFarthest = pnt - farthest;
if (fromFarthest.DotProduct(dir) > 0)
{
farthest = pnt;
}
else
{
// 调试断点
}
}
}
}
}
if (closest == null || farthest == null)
throw ExceptionHelper.CollectionEmpty(nameof(points));
else
return Pair.Create(closest, farthest);
}
/// <summary>
/// 指定一个方向向量,求出沿着这个方向向量上的实体“最近、最远”点
/// </summary>
public static Pair<XYZ, XYZ> ClosestFarthestPoints(Solid solid, XYZ dir, int num)
{
solid.AssertValidSolid();
dir.AssertNotZeroVector(nameof(dir));
if (num <= 0)
{
throw ExceptionHelper.ArgumentMustBePositive(nameof(num), num.ToString());
}
var edgePoints = GetParamPoints(solid, num);
var toReturn = XyzPointUtils.ClosestFarthestPoints(edgePoints, dir); // 如果点集为空,则这里会引发异常
return toReturn;
}
public static IList<XYZ> GetParamPoints(Solid solid, int num)
{
solid.AssertValidSolid();
if (num <= 0)
{
throw ExceptionHelper.ArgumentMustBePositive(nameof(num), num.ToString());
}
var toReturn = new List<XYZ>();
foreach (Edge solidEdge in solid.Edges)
{
Curve edgeCurve = solidEdge.AsCurve();
if (edgeCurve != null)
{
#if DEBUG
edgeCurve.AssertBound(nameof(edgeCurve));
edgeCurve.Length.AssertPositive(nameof(edgeCurve) + '.' + nameof(edgeCurve.Length));
#endif
if (edgeCurve is Line edgeLine)
{
var pnts = CurveUtils.GetParamPoints(edgeLine, 1);
toReturn.AddRange(pnts);
}
else
{
var pnts = CurveUtils.GetParamPoints(edgeCurve, num);
toReturn.AddRange(pnts);
}
}
}
return toReturn;
}
单身狗;代码狗;健身狗;jolinpiggy@hotmail.com