GCodeLoader.js 5.3 KB

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