fix: 取消PP钻带外框13/15孔规则 — GetPpGenerationRule仅返回Legacy 3孔规则;删除外框规则及专属辅助死代码(SelectPpDrillTapeLocations/ParsePpHolePoints/SelectMiddlePoint/PpHolePoint/PpSideCoordinateTolerance)

This commit is contained in:
2026-08-12 10:25:14 +08:00
parent 5365791acd
commit 2335894be7

View File

@@ -47,7 +47,6 @@ namespace DrillTools
private const int SvsiFocused = 0x10; private const int SvsiFocused = 0x10;
private const int SelectFileFlags = SvsiSelect | SvsiDeselectOthers | SvsiEnsureVisible | SvsiFocused; private const int SelectFileFlags = SvsiSelect | SvsiDeselectOthers | SvsiEnsureVisible | SvsiFocused;
private static readonly double[] PpSourceDiameters = { 3.203, 4.503 }; private static readonly double[] PpSourceDiameters = { 3.203, 4.503 };
private const int PpSideCoordinateTolerance = 1500;
/// <summary> /// <summary>
/// PP 钻带来源孔径集合(3.203 PP 定位孔 / 4.503 铆钉孔)。 /// PP 钻带来源孔径集合(3.203 PP 定位孔 / 4.503 铆钉孔)。
@@ -1220,11 +1219,7 @@ M30";
if (!HasOriginalFile || OriginalTools.Count == 0) if (!HasOriginalFile || OriginalTools.Count == 0)
return null; return null;
var legacyRule = GetLegacyPpGenerationRule(); return GetLegacyPpGenerationRule();
if (legacyRule != null)
return legacyRule;
return GetOuterPpGenerationRule();
} }
private PpGenerationRule? GetLegacyPpGenerationRule() private PpGenerationRule? GetLegacyPpGenerationRule()
@@ -1253,120 +1248,6 @@ M30";
: null; : null;
} }
private PpGenerationRule? GetOuterPpGenerationRule()
{
var sourceTool = OriginalTools.FirstOrDefault(tool =>
IsPpSourceDiameter(tool.Diameter)
&& (tool.TotalHoles == 13 || tool.TotalHoles == 15)
&& tool.HoleLocations != null
&& tool.HoleLocations.Count >= 4
&& CanSelectPpDrillTapeLocations(tool));
return sourceTool == null
? null
: new PpGenerationRule("T01C4.000", SelectPpDrillTapeLocations(sourceTool));
}
/// <summary>
/// 从 PP 来源孔径(3.203 PP 定位孔 / 4.503 铆钉孔)外框中按右、上、左、下四边各取一个中位孔,并保持原钻带坐标文本。
/// </summary>
private static List<string> SelectPpDrillTapeLocations(ToolItem sourceTool)
{
var points = ParsePpHolePoints(sourceTool.HoleLocations);
if (points.Count < 4)
throw new InvalidOperationException("PP 来源孔径刀具坐标不足,无法生成 PP 钻带");
int minX = points.Min(point => point.X);
int maxX = points.Max(point => point.X);
int minY = points.Min(point => point.Y);
int maxY = points.Max(point => point.Y);
var selected = new[]
{
SelectMiddlePoint(points.Where(point => Math.Abs(point.X - maxX) <= PpSideCoordinateTolerance), sortByX: false, useUpperMiddle: true),
SelectMiddlePoint(points.Where(point => Math.Abs(point.Y - maxY) <= PpSideCoordinateTolerance), sortByX: true, useUpperMiddle: false),
SelectMiddlePoint(points.Where(point => Math.Abs(point.X - minX) <= PpSideCoordinateTolerance), sortByX: false, useUpperMiddle: true),
SelectMiddlePoint(points.Where(point => Math.Abs(point.Y - minY) <= PpSideCoordinateTolerance), sortByX: true, useUpperMiddle: false)
};
if (selected.Any(point => point == null))
throw new InvalidOperationException("PP 来源孔径刀具未形成完整的上下左右四边孔位,无法生成 PP 钻带");
var selectedTexts = selected
.Select(point => point!.Text)
.Distinct(StringComparer.Ordinal)
.ToHashSet(StringComparer.Ordinal);
if (selectedTexts.Count != 4)
throw new InvalidOperationException("PP 来源孔径刀具四边中位孔存在重复,无法生成 PP 钻带");
// 输出顺序沿用原 PP 来源孔径刀具(3.203/4.503)中的孔位顺序,避免改变已有钻带坐标排序习惯。
return points
.Where(point => selectedTexts.Contains(point.Text))
.Select(point => point.Text)
.ToList();
}
private static bool CanSelectPpDrillTapeLocations(ToolItem sourceTool)
{
try
{
return SelectPpDrillTapeLocations(sourceTool).Count == 4;
}
catch (InvalidOperationException)
{
return false;
}
}
private static List<PpHolePoint> ParsePpHolePoints(IEnumerable<string> holeLocations)
{
var points = new List<PpHolePoint>();
foreach (var location in holeLocations)
{
if (string.IsNullOrWhiteSpace(location))
continue;
var match = Regex.Match(location.Trim(), @"X([+-]?\d+)Y([+-]?\d+)");
if (!match.Success)
continue;
points.Add(new PpHolePoint(
location.Trim(),
int.Parse(match.Groups[1].Value),
int.Parse(match.Groups[2].Value)));
}
return points;
}
private static PpHolePoint? SelectMiddlePoint(IEnumerable<PpHolePoint> sourcePoints, bool sortByX, bool useUpperMiddle)
{
var points = sortByX
? sourcePoints.OrderBy(point => point.X).ThenBy(point => point.Y).ToList()
: sourcePoints.OrderBy(point => point.Y).ThenBy(point => point.X).ToList();
if (points.Count == 0)
return null;
int index = useUpperMiddle ? points.Count / 2 : (points.Count - 1) / 2;
return points[index];
}
private sealed class PpHolePoint
{
public PpHolePoint(string text, int x, int y)
{
Text = text;
X = x;
Y = y;
}
public string Text { get; }
public int X { get; }
public int Y { get; }
}
private sealed class PpGenerationRule private sealed class PpGenerationRule
{ {
public PpGenerationRule(string toolDefinition, List<string> locations) public PpGenerationRule(string toolDefinition, List<string> locations)