2 回答
TA贡献1796条经验 获得超10个赞
这会起作用
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贡献1878条经验 获得超4个赞
这应该适合你
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);
}
添加回答
举报