GCodeLoader.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. ( function () {
  2. /**
  3. * GCodeLoader is used to load gcode files usually used for 3D printing or CNC applications.
  4. *
  5. * Gcode files are composed by commands used by machines to create objects.
  6. *
  7. * @class GCodeLoader
  8. * @param {Manager} manager Loading manager.
  9. */
  10. class GCodeLoader extends THREE.Loader {
  11. constructor( manager ) {
  12. super( manager );
  13. this.splitLayer = false;
  14. }
  15. load( url, onLoad, onProgress, onError ) {
  16. const scope = this;
  17. const loader = new THREE.FileLoader( scope.manager );
  18. loader.setPath( scope.path );
  19. loader.setRequestHeader( scope.requestHeader );
  20. loader.setWithCredentials( scope.withCredentials );
  21. loader.load( url, function ( text ) {
  22. try {
  23. onLoad( scope.parse( text ) );
  24. } catch ( e ) {
  25. if ( onError ) {
  26. onError( e );
  27. } else {
  28. console.error( e );
  29. }
  30. scope.manager.itemError( url );
  31. }
  32. }, onProgress, onError );
  33. }
  34. parse( data ) {
  35. let state = {
  36. x: 0,
  37. y: 0,
  38. z: 0,
  39. e: 0,
  40. f: 0,
  41. extruding: false,
  42. relative: false
  43. };
  44. const layers = [];
  45. let currentLayer = undefined;
  46. const pathMaterial = new THREE.LineBasicMaterial( {
  47. color: 0xFF0000
  48. } );
  49. pathMaterial.name = 'path';
  50. const extrudingMaterial = new THREE.LineBasicMaterial( {
  51. color: 0x00FF00
  52. } );
  53. extrudingMaterial.name = 'extruded';
  54. function newLayer( line ) {
  55. currentLayer = {
  56. vertex: [],
  57. pathVertex: [],
  58. z: line.z
  59. };
  60. layers.push( currentLayer );
  61. } //Create lie segment between p1 and p2
  62. function addSegment( p1, p2 ) {
  63. if ( currentLayer === undefined ) {
  64. newLayer( p1 );
  65. }
  66. if ( state.extruding ) {
  67. currentLayer.vertex.push( p1.x, p1.y, p1.z );
  68. currentLayer.vertex.push( p2.x, p2.y, p2.z );
  69. } else {
  70. currentLayer.pathVertex.push( p1.x, p1.y, p1.z );
  71. currentLayer.pathVertex.push( p2.x, p2.y, p2.z );
  72. }
  73. }
  74. function delta( v1, v2 ) {
  75. return state.relative ? v2 : v2 - v1;
  76. }
  77. function absolute( v1, v2 ) {
  78. return state.relative ? v1 + v2 : v2;
  79. }
  80. const lines = data.replace( /;.+/g, '' ).split( '\n' );
  81. for ( let i = 0; i < lines.length; i ++ ) {
  82. const tokens = lines[ i ].split( ' ' );
  83. const cmd = tokens[ 0 ].toUpperCase(); //Argumments
  84. const args = {};
  85. tokens.splice( 1 ).forEach( function ( token ) {
  86. if ( token[ 0 ] !== undefined ) {
  87. const key = token[ 0 ].toLowerCase();
  88. const value = parseFloat( token.substring( 1 ) );
  89. args[ key ] = value;
  90. }
  91. } ); //Process commands
  92. //G0/G1 – Linear Movement
  93. if ( cmd === 'G0' || cmd === 'G1' ) {
  94. const line = {
  95. x: args.x !== undefined ? absolute( state.x, args.x ) : state.x,
  96. y: args.y !== undefined ? absolute( state.y, args.y ) : state.y,
  97. z: args.z !== undefined ? absolute( state.z, args.z ) : state.z,
  98. e: args.e !== undefined ? absolute( state.e, args.e ) : state.e,
  99. f: args.f !== undefined ? absolute( state.f, args.f ) : state.f
  100. }; //Layer change detection is or made by watching Z, it's made by watching when we extrude at a new Z position
  101. if ( delta( state.e, line.e ) > 0 ) {
  102. state.extruding = delta( state.e, line.e ) > 0;
  103. if ( currentLayer == undefined || line.z != currentLayer.z ) {
  104. newLayer( line );
  105. }
  106. }
  107. addSegment( state, line );
  108. state = line;
  109. } else if ( cmd === 'G2' || cmd === 'G3' ) { //G2/G3 - Arc Movement ( G2 clock wise and G3 counter clock wise )
  110. //console.warn( 'THREE.GCodeLoader: Arc command not supported' );
  111. } else if ( cmd === 'G90' ) {
  112. //G90: Set to Absolute Positioning
  113. state.relative = false;
  114. } else if ( cmd === 'G91' ) {
  115. //G91: Set to state.relative Positioning
  116. state.relative = true;
  117. } else if ( cmd === 'G92' ) {
  118. //G92: Set Position
  119. const line = state;
  120. line.x = args.x !== undefined ? args.x : line.x;
  121. line.y = args.y !== undefined ? args.y : line.y;
  122. line.z = args.z !== undefined ? args.z : line.z;
  123. line.e = args.e !== undefined ? args.e : line.e;
  124. state = line;
  125. } else { //console.warn( 'THREE.GCodeLoader: Command not supported:' + cmd );
  126. }
  127. }
  128. function addObject( vertex, extruding, i ) {
  129. const geometry = new THREE.BufferGeometry();
  130. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertex, 3 ) );
  131. const segments = new THREE.LineSegments( geometry, extruding ? extrudingMaterial : pathMaterial );
  132. segments.name = 'layer' + i;
  133. object.add( segments );
  134. }
  135. const object = new THREE.Group();
  136. object.name = 'gcode';
  137. if ( this.splitLayer ) {
  138. for ( let i = 0; i < layers.length; i ++ ) {
  139. const layer = layers[ i ];
  140. addObject( layer.vertex, true, i );
  141. addObject( layer.pathVertex, false, i );
  142. }
  143. } else {
  144. const vertex = [],
  145. pathVertex = [];
  146. for ( let i = 0; i < layers.length; i ++ ) {
  147. const layer = layers[ i ];
  148. const layerVertex = layer.vertex;
  149. const layerPathVertex = layer.pathVertex;
  150. for ( let j = 0; j < layerVertex.length; j ++ ) {
  151. vertex.push( layerVertex[ j ] );
  152. }
  153. for ( let j = 0; j < layerPathVertex.length; j ++ ) {
  154. pathVertex.push( layerPathVertex[ j ] );
  155. }
  156. }
  157. addObject( vertex, true, layers.length );
  158. addObject( pathVertex, false, layers.length );
  159. }
  160. object.quaternion.setFromEuler( new THREE.Euler( - Math.PI / 2, 0, 0 ) );
  161. return object;
  162. }
  163. }
  164. THREE.GCodeLoader = GCodeLoader;
  165. } )();