Keep G-code matched to imported AC or BC machine type

This commit is contained in:
2026-09-21 19:52:31 +08:00
parent 86cfa2b7f0
commit 0de96bbc72
20 changed files with 315 additions and 4 deletions

View File

@@ -0,0 +1,30 @@
// This is a machine-family guard, not a replacement for the LinuxCNC parser.
// Ignore comments, named parameters, O labels and bracket expression contents;
// inspect address words at block level, including zero and parameter values.
export function programMachineMismatch(source, coordinates, path = "") {
if (!["XYZAC", "XYZBC"].includes(coordinates) || typeof source !== "string") return "";
const family = String(path).match(/xyz[ab]c/ig)?.map((s) => s.toUpperCase()) ?? [];
if (family.some((value) => value !== coordinates)) return `程序机型不匹配:${path} 不适用于 ${coordinates}`;
let comment = 0, bracket = 0, semicolon = false, named = false, code = "";
for (const char of source) {
if (char === '\n' || char === '\r') { semicolon = false; code += '\n'; continue; }
if (semicolon) continue;
if (char === '(') { comment++; continue; }
if (comment) { if (char === ')') comment--; continue; }
if (char === ';') { semicolon = true; continue; }
if (char === '<') { named = true; code += ' '; continue; }
if (named) { if (char === '>') named = false; continue; }
if (char === '[') { if (!bracket) code += '['; bracket++; continue; }
if (bracket) { if (char === ']') bracket--; continue; }
code += char;
}
const forbidden = coordinates === "XYZBC" ? "A" : "B";
const pattern = new RegExp(`${forbidden}\\s*(?=[+\\-\\d.#\\[])`, "i");
const lines = code.split('\n');
const index = lines.findIndex((line) => pattern.test(line));
return index < 0 ? "" : `程序机型不匹配:${coordinates} 不支持 ${forbidden} 轴指令(第 ${index + 1} 行),请选择对应机型的 G 代码`;
}
export function assertProgramMachine(source, coordinates, path = "") {
const error = programMachineMismatch(source, coordinates, path);
if (error) throw new Error(error);
}