fix(posts): keep CI Fiber material codes case-insensitive after reload
System.Text.Json rebuilds MaterialCodes with the default ordinal comparer, so a saved config matched 'Mild Steel' but not 'mild steel' and silently fell back to the default code. The setter now re-keys assigned maps case-insensitively.
This commit is contained in:
@@ -309,4 +309,24 @@ public class CIFiberPostProcessorTests
|
|||||||
Assert.Contains("G0X50Y50", output);
|
Assert.Contains("G0X50Y50", output);
|
||||||
Assert.Contains("V.E.R3=1", output);
|
Assert.Contains("V.E.R3=1", output);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void MaterialCodes_MatchIgnoringCase_AfterJsonReload()
|
||||||
|
{
|
||||||
|
var options = new System.Text.Json.JsonSerializerOptions();
|
||||||
|
var json = System.Text.Json.JsonSerializer.Serialize(
|
||||||
|
new CIFiberPostConfig
|
||||||
|
{
|
||||||
|
MaterialCodes = new() { ["Stainless"] = "SSN" },
|
||||||
|
DefaultMaterialCode = "MSN",
|
||||||
|
},
|
||||||
|
options
|
||||||
|
);
|
||||||
|
|
||||||
|
var config = System.Text.Json.JsonSerializer.Deserialize<CIFiberPostConfig>(json, options)!;
|
||||||
|
|
||||||
|
Assert.Equal("SSN", config.ResolveMaterialCode("stainless"));
|
||||||
|
Assert.Equal("SSN", config.ResolveMaterialCode(" STAINLESS "));
|
||||||
|
Assert.Equal("MSN", config.ResolveMaterialCode("Aluminum"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -139,12 +139,36 @@ namespace OpenNest.Posts.CincinnatiCIFiber
|
|||||||
[PostSetting(MacrosSection, 6)]
|
[PostSetting(MacrosSection, 6)]
|
||||||
public string ProgramEndMacro { get; set; } = "PROGRAMEND.NC";
|
public string ProgramEndMacro { get; set; } = "PROGRAMEND.NC";
|
||||||
|
|
||||||
/// <summary>Material name (case-insensitive) to machine material code map for V.E.MATERIAL.</summary>
|
/// <summary>
|
||||||
|
/// Material name (case-insensitive) to machine material code map for
|
||||||
|
/// V.E.MATERIAL. The setter re-keys any assigned map (including one
|
||||||
|
/// deserialized from JSON, which is case-sensitive) case-insensitively;
|
||||||
|
/// if names collide by case, the later entry wins.
|
||||||
|
/// </summary>
|
||||||
[DisplayName("Material codes")]
|
[DisplayName("Material codes")]
|
||||||
[Description("Material name (not case-sensitive) and the code written to V.E.MATERIAL.")]
|
[Description("Material name (not case-sensitive) and the code written to V.E.MATERIAL.")]
|
||||||
[PostSetting(MaterialSection, 0, KeyHeader = "Material name", ValueHeader = "Machine code")]
|
[PostSetting(MaterialSection, 0, KeyHeader = "Material name", ValueHeader = "Machine code")]
|
||||||
public Dictionary<string, string> MaterialCodes { get; set; } =
|
public Dictionary<string, string> MaterialCodes
|
||||||
new(StringComparer.OrdinalIgnoreCase) { ["Mild Steel"] = "MSN" };
|
{
|
||||||
|
get => _materialCodes;
|
||||||
|
set => _materialCodes = IgnoreCase(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private Dictionary<string, string> _materialCodes = new(StringComparer.OrdinalIgnoreCase)
|
||||||
|
{
|
||||||
|
["Mild Steel"] = "MSN",
|
||||||
|
};
|
||||||
|
|
||||||
|
private static Dictionary<string, string> IgnoreCase(Dictionary<string, string> map)
|
||||||
|
{
|
||||||
|
if (map == null || map.Comparer == StringComparer.OrdinalIgnoreCase)
|
||||||
|
return map;
|
||||||
|
|
||||||
|
var copy = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
||||||
|
foreach (var entry in map)
|
||||||
|
copy[entry.Key] = entry.Value;
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Fallback V.E.MATERIAL code when the material name has no mapping.</summary>
|
/// <summary>Fallback V.E.MATERIAL code when the material name has no mapping.</summary>
|
||||||
[DisplayName("Default material code")]
|
[DisplayName("Default material code")]
|
||||||
|
|||||||
Reference in New Issue
Block a user