2 回答

TA貢獻(xiàn)1796條經(jīng)驗(yàn) 獲得超10個(gè)贊
這會(huì)起作用
function processPathLettersIntoArray(source) {
const result = source.replace(/M/g, ";M").replace(/L/g, ";L").replace(/z/g, ";z").split(';').slice(1);
return result.map(mapGroup);
}
function gcodeXY(source) {
const [xVal, yVal] = source.split(",");
return `X${xVal} Y${yVal}`;
}
let start="X0 Y0"; //another side effect :-(
function mapGroup(grp) {
let s = grp.split(" ");
if (s[0]=="M"){
start = gcodeXY(s[1]);
last=s[1];
return "G0 F200 "+start;
}
else if (s[0]=="L"){
start=gcodeXY(s[1]);
last=s[1];
return "G1 F100 "+start;
}
else if (s[0]=="z"){
last=s[1];
return "G1 F100 "+start;
}
return grp;
}
function path2gcode() {
const source = 'M 1,2 L 2,2 L 3,3 z';
return processPathLettersIntoArray(source);
}

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊
這應(yīng)該適合你
function processPathLettersIntoArray(source){
source = source.split("M").join(";M");
source = source.split("L").join(";L");
source = source.split("z").join(";z");
source = source.split(";");
source = source.slice(1);
output = [];
for(i=0;i<source.length;i++) {
output.push(mapGroup(source[i]));
}
return output;
}
或者,.map可以使用
function processPathLettersIntoArray(source){
source = source.split("M").join(";M");
source = source.split("L").join(";L");
source = source.split("z").join(";z");
source = source.split(";");
source = source.slice(1);
return source.map(mapGroup);
}
添加回答
舉報(bào)