733 if (commands) {
734 for (let commandIndex = 0; commandIndex < commands.length; commandIndex++) {
735 //console.log(`index ${commandIndex} command ${commands[commandIndex]}`);
736 switch (commands[commandIndex]) { 737 case 'G0': 738 case 'G1': 739 case 'G00': 740 case 'G01': 741 this.g0g1(tokenString, lineNumber, filePosition, renderLine, commands); 742 break; 743 case 'G2': 744 case 'G3': 745 case 'G02': 746 case 'G03': 747 this.g2g3(tokenString, lineNumber, filePosition, renderLine); 748 break; 749 case 'G10': 750 this.firmwareRetraction = true; 751 break; 752 case 'G11': 753 this.firmwareRetraction = false; 754 break; 755 case 'G17': 756 this.arcPlane = 'XY'; 757 break; 758 case 'G18': 759 this.arcPlane = 'XZ'; 760 break; 761 case 'G19': 762 this.arcPlane = 'YZ'; 763 break; 764 case 'G20': 765 this.inches = true; 766 break; 767 case 'G28': 768 //Home 769 tokens = tokenString.split(/(?=[GXYZ])/); 770 if (tokens.length === 1 || tokenString === 'G28 W') { 771 //G28 W is due to PrusaSlicer command 772 // this.currentPosition = new Vector3(0, 0, 0); 773 this.currentPosition = new Vector3(this.workplaceOffsets[this.currentWorkplace].x, this.workplaceOffsets[this.currentWorkplace].z, this.workplaceOffsets[this.currentWorkplace].y); 774 } else { 775 if (tokens.some((t) => t.trim() === 'X')) { 776 // this.currentPosition.x = 0; 777 this.currentPosition.x = this.workplaceOffsets[this.currentWorkplace].x; 778 } 779 if (tokens.some((t) => t.trim() === 'Y')) { 780 // this.currentPosition.z = 0; 781 this.currentPosition.z = this.workplaceOffsets[this.currentWorkplace].y; 782 } 783 if (tokens.some((t) => t.trim() === 'Z')) { 784 //this.currentPosition.y = 0; 785 this.currentPosition.y = this.workplaceOffsets[this.currentWorkplace].z; 786 } 787 } 788 break; 789 case 'G53': 790 //Machine movement - need to think through this one. 791 break; 792 case 'G54': 793 case 'G55': 794 case 'G56': 795 case 'G57': 796 case 'G58': 797 case 'G59': 798 this.currentWorkplace = 54 - Number(commands[commandIndex].substring(1)); 799 this.currentPosition = this.workplaceOffsets[this.currentWorkplace].clone(); 800 break; 801 case 'G59.1': 802 case 'G59.2': 803 case 'G59.2': 804 this.currentWorkplace = (58.6 - Number(commands[commandIndex].substring(1))) * 10; 805 this.currentPosition = this.workplaceOffsets[this.currentWorkplace].clone(); 806 break; 807 case 'G90': 808 this.absolute = true; 809 break; 810 case 'G91': 811 this.absolute = false; 812 break; 813 case 'G92': 814 //this resets positioning, typically for extruder, probably won't need 815 break; 816 case 'S': 817 this.hasSpindle = true; 818 break; 819 case 'M3': 820 case 'M4': 821 { 822 const tokens = tokenString.split(/(?=[SM])/); 823 let spindleSpeed = tokens.filter((speed) => speed.startsWith('S')); 824 spindleSpeed = spindleSpeed[0] ? Number(spindleSpeed[0].substring(1)) : 0; 825 if (spindleSpeed > 0) { 826 this.hasSpindle = true; 827 } 828 } 829 break; 830 case 'M567': { 831 this.hasMixing = true; 832 this.m567(tokenString); 833 break; 834 } 835 case 'M600': 836 { 837 try { 838 this.currentTool++; 839 if (this.currentTool >= this.tools.length) { 840 this.currentTool = 0; 841 } 842 if (this.colorMode !== ColorMode.Feed) { 843 this.currentColor = this.tools[this.currentTool].color.clone(); 844 } 845 } catch (ex) { 846 console.log(ex); 847 } 848 } 849 break; 850 } 851 this.lastCommand = commands;
852 }
853 } else {
598 if (this.colorMode === ColorMode.Feed) return;
599 for (let tokenIdx = 1; tokenIdx < tokens.length; tokenIdx++) {
600 const token = tokens[tokenIdx];
601 switch (token[0]) { 602 case 'E': 603 this.extruderPercentage = token.substring(1).split(':'); 604 break; 605 } 606 }
607 for (let extruderIdx = 0; extruderIdx < this.extruderPercentage.length; extruderIdx++) {
608 finalColors[0] -= (1 - this.tools[extruderIdx].color.r) * this.extruderPercentage[extruderIdx];
364
365 for (let tokenIdx = 0; tokenIdx < tokens.length; tokenIdx++) {
366 let token = tokens[tokenIdx];
367 switch (token[0]) { 368 case 'X': 369 if (this.zBelt) { 370 this.currentPosition.x = Number(token.substring(1)); 371 } else { 372 this.currentPosition.x = this.absolute ? Number(token.substring(1)) + this.workplaceOffsets[this.currentWorkplace].x : this.currentPosition.x + Number(token.substring(1)); 373 } 374 hasXYMove = true; 375 break; 376 case 'Y': 377 if (this.zBelt) { 378 this.currentPosition.y = Number(token.substring(1)) * this.hyp; 379 this.currentPosition.z = this.currentZ + this.currentPosition.y * this.adj; 380 } else { 381 this.currentPosition.z = this.absolute ? Number(token.substring(1)) + this.workplaceOffsets[this.currentWorkplace].y : this.currentPosition.z + Number(token.substring(1)); 382 } 383 hasXYMove = true; 384 break; 385 case 'Z': 386 if (this.zBelt) { 387 this.currentZ = -Number(token.substring(1)); 388 this.currentPosition.z = this.currentZ + this.currentPosition.y * this.adj; 389 hasXYMove = true; 390 } else { 391 this.currentPosition.y = this.absolute ? Number(token.substring(1)) + this.workplaceOffsets[this.currentWorkplace].z : this.currentPosition.y + Number(token.substring(1)); 392 393 if (!this.lastY || this.lastY !== this.currentPosition.y) { 394 this.lastY = this.currentPosition.y; 395 if (this.lastY === undefined) this.lastY = 0; 396 } 397 if (this.currentPosition.y < this.minHeight) { 398 this.minHeight = this.currentPosition.y; 399 } 400 if (this.spreadLines) { 401 this.currentPosition.y *= this.spreadLineAmount; 402 } 403 } 404 break; 405 case 'E': 406 //Do not count retractions as extrusions 407 if (Number(token.substring(1)) > 0) { 408 line.extruding = true; 409 this.maxHeight = this.currentPosition.y; //trying to get the max height of the model. 410 } 411 break; 412 case 'F': 413 this.currentFeedRate = Number(token.substring(1)); 414 line.feedRate = this.currentFeedRate; 415 if (this.currentFeedRate > this.maxFeedRate) { 416 this.maxFeedRate = this.currentFeedRate; 417 } 418 if (this.currentFeedRate < this.minFeedRate) { 419 this.minFeedRate = this.currentFeedRate; 420 } 421 422 if (this.colorMode === ColorMode.Feed) { 423 let ratio = (this.currentFeedRate - this.minColorRate) / (this.maxColorRate - this.minColorRate); 424 if (ratio >= 1) { 425 this.currentColor = this.maxFeedColor; 426 } else if (ratio <= 0) { 427 this.currentColor = this.minFeedColor; 428 } else { 429 this.currentColor = Color4.Lerp(this.minFeedColor, this.maxFeedColor, ratio); 430 } 431 } 432 433 break; 434 } 435 }
436
437 if (this.zBelt) {
278
279 for (let renderModeIdx = renderStartIndex; renderModeIdx < 4; renderModeIdx++) {
280 let vertextMultiplier;
281 switch (renderModeIdx) { 282 case 1: 283 vertextMultiplier = 24; 284 break; 285 case 2: 286 vertextMultiplier = 2; 287 break; 288 case 3: 289 vertextMultiplier = 1; 290 break; 291 } 292
293 for (let idx = this.everyNthRow; idx <= maxNRow; idx++) {
294 if (this.debug) {
481 z = position.y;
482 } else {
483 for (var index = 0; index < position.length; index++) {
484 switch (position[index].axes) { 485 case 'X': 486 { 487 x = position[index].position; 488 } 489 break; 490 case 'Y': 491 { 492 y = position[index].position; 493 } 494 break; 495 case 'Z': 496 { 497 z = position[index].position * (this.gcodeProcessor.spreadLines ? this.gcodeProcessor.spreadLineAmount : 1); 498 } 499 break; 500 } 501 }
502 }
503
Some code conventions require that all switch statements have a default case, even if the default case is empty. The idea is to always explicitly state what the default behavior should be so that it's clear that the developer didn't forget to handle the default fallback case.
switch (a) {
case 1:
/* code */
break;
}
switch (a) {
case 1:
/* code */
break;
default:
/* code */
break;
}
switch (a) {
case 1:
/* code */
break;
// no default
}
switch (a) {
case 1:
/* code */
break;
// No Default
}