GCodeLoader.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. }
  62. //Create lie segment between p1 and p2
  63. function addSegment( p1, p2 ) {
  64. if ( currentLayer === undefined ) {
  65. newLayer( p1 );
  66. }
  67. if ( state.extruding ) {
  68. currentLayer.vertex.push( p1.x, p1.y, p1.z );
  69. currentLayer.vertex.push( p2.x, p2.y, p2.z );
  70. } else {
  71. currentLayer.pathVertex.push( p1.x, p1.y, p1.z );
  72. currentLayer.pathVertex.push( p2.x, p2.y, p2.z );
  73. }
  74. }
  75. function delta( v1, v2 ) {
  76. return state.relative ? v2 : v2 - v1;
  77. }
  78. function absolute( v1, v2 ) {
  79. return state.relative ? v1 + v2 : v2;
  80. }
  81. const lines = data.replace( /;.+/g, '' ).split( '\n' );
  82. for ( let i = 0; i < lines.length; i ++ ) {
  83. const tokens = lines[ i ].split( ' ' );
  84. const cmd = tokens[ 0 ].toUpperCase();
  85. //Argumments
  86. const args = {};
  87. tokens.splice( 1 ).forEach( function ( token ) {
  88. if ( token[ 0 ] !== undefined ) {
  89. const key = token[ 0 ].toLowerCase();
  90. const value = parseFloat( token.substring( 1 ) );
  91. args[ key ] = value;
  92. }
  93. } );
  94. //Process commands
  95. //G0/G1 – Linear Movement
  96. if ( cmd === 'G0' || cmd === 'G1' ) {
  97. const line = {
  98. x: args.x !== undefined ? absolute( state.x, args.x ) : state.x,
  99. y: args.y !== undefined ? absolute( state.y, args.y ) : state.y,
  100. z: args.z !== undefined ? absolute( state.z, args.z ) : state.z,
  101. e: args.e !== undefined ? absolute( state.e, args.e ) : state.e,
  102. f: args.f !== undefined ? absolute( state.f, args.f ) : state.f
  103. };
  104. //Layer change detection is or made by watching Z, it's made by watching when we extrude at a new Z position
  105. if ( delta( state.e, line.e ) > 0 ) {
  106. state.extruding = delta( state.e, line.e ) > 0;
  107. if ( currentLayer == undefined || line.z != currentLayer.z ) {
  108. newLayer( line );
  109. }
  110. }
  111. addSegment( state, line );
  112. state = line;
  113. } else if ( cmd === 'G2' || cmd === 'G3' ) {
  114. //G2/G3 - Arc Movement ( G2 clock wise and G3 counter clock wise )
  115. //console.warn( 'THREE.GCodeLoader: Arc command not supported' );
  116. } else if ( cmd === 'G90' ) {
  117. //G90: Set to Absolute Positioning
  118. state.relative = false;
  119. } else if ( cmd === 'G91' ) {
  120. //G91: Set to state.relative Positioning
  121. state.relative = true;
  122. } else if ( cmd === 'G92' ) {
  123. //G92: Set Position
  124. const line = state;
  125. line.x = args.x !== undefined ? args.x : line.x;
  126. line.y = args.y !== undefined ? args.y : line.y;
  127. line.z = args.z !== undefined ? args.z : line.z;
  128. line.e = args.e !== undefined ? args.e : line.e;
  129. } else {
  130. //console.warn( 'THREE.GCodeLoader: Command not supported:' + cmd );
  131. }
  132. }
  133. function addObject( vertex, extruding, i ) {
  134. const geometry = new THREE.BufferGeometry();
  135. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertex, 3 ) );
  136. const segments = new THREE.LineSegments( geometry, extruding ? extrudingMaterial : pathMaterial );
  137. segments.name = 'layer' + i;
  138. object.add( segments );
  139. }
  140. const object = new THREE.Group();
  141. object.name = 'gcode';
  142. if ( this.splitLayer ) {
  143. for ( let i = 0; i < layers.length; i ++ ) {
  144. const layer = layers[ i ];
  145. addObject( layer.vertex, true, i );
  146. addObject( layer.pathVertex, false, i );
  147. }
  148. } else {
  149. const vertex = [],
  150. pathVertex = [];
  151. for ( let i = 0; i < layers.length; i ++ ) {
  152. const layer = layers[ i ];
  153. const layerVertex = layer.vertex;
  154. const layerPathVertex = layer.pathVertex;
  155. for ( let j = 0; j < layerVertex.length; j ++ ) {
  156. vertex.push( layerVertex[ j ] );
  157. }
  158. for ( let j = 0; j < layerPathVertex.length; j ++ ) {
  159. pathVertex.push( layerPathVertex[ j ] );
  160. }
  161. }
  162. addObject( vertex, true, layers.length );
  163. addObject( pathVertex, false, layers.length );
  164. }
  165. object.quaternion.setFromEuler( new THREE.Euler( - Math.PI / 2, 0, 0 ) );
  166. return object;
  167. }
  168. }
  169. THREE.GCodeLoader = GCodeLoader;
  170. } )();