1
0

GCodeLoader.js 5.3 KB

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