GCodeLoader.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import {
  2. BufferGeometry,
  3. FileLoader,
  4. Float32BufferAttribute,
  5. Group,
  6. LineBasicMaterial,
  7. LineSegments,
  8. Loader
  9. } from 'three';
  10. /**
  11. * A loader for the GCode format.
  12. *
  13. * GCode files are usually used for 3D printing or CNC applications.
  14. *
  15. * ```js
  16. * const loader = new GCodeLoader();
  17. * const object = await loader.loadAsync( 'models/gcode/benchy.gcode' );
  18. * scene.add( object );
  19. * ```
  20. *
  21. * @augments Loader
  22. */
  23. class GCodeLoader extends Loader {
  24. /**
  25. * Constructs a new GCode loader.
  26. *
  27. * @param {LoadingManager} [manager] - The loading manager.
  28. */
  29. constructor( manager ) {
  30. super( manager );
  31. /**
  32. * Whether to split layers or not.
  33. *
  34. * @type {boolean}
  35. * @default false
  36. */
  37. this.splitLayer = false;
  38. }
  39. /**
  40. * Starts loading from the given URL and passes the loaded GCode asset
  41. * to the `onLoad()` callback.
  42. *
  43. * @param {string} url - The path/URL of the file to be loaded. This can also be a data URI.
  44. * @param {function(Group)} onLoad - Executed when the loading process has been finished.
  45. * @param {onProgressCallback} onProgress - Executed while the loading is in progress.
  46. * @param {onErrorCallback} onError - Executed when errors occur.
  47. */
  48. load( url, onLoad, onProgress, onError ) {
  49. const scope = this;
  50. const loader = new FileLoader( scope.manager );
  51. loader.setPath( scope.path );
  52. loader.setRequestHeader( scope.requestHeader );
  53. loader.setWithCredentials( scope.withCredentials );
  54. loader.load( url, function ( text ) {
  55. try {
  56. onLoad( scope.parse( text ) );
  57. } catch ( e ) {
  58. if ( onError ) {
  59. onError( e );
  60. } else {
  61. console.error( e );
  62. }
  63. scope.manager.itemError( url );
  64. }
  65. }, onProgress, onError );
  66. }
  67. /**
  68. * Parses the given GCode data and returns a group with lines.
  69. *
  70. * @param {string} data - The raw Gcode data as a string.
  71. * @return {Group} The parsed GCode asset.
  72. */
  73. parse( data ) {
  74. let state = { x: 0, y: 0, z: 0, e: 0, f: 0, extruding: false, relative: false };
  75. const layers = [];
  76. let currentLayer = undefined;
  77. const pathMaterial = new LineBasicMaterial( { color: 0xFF0000 } );
  78. pathMaterial.name = 'path';
  79. const extrudingMaterial = new LineBasicMaterial( { color: 0x00FF00 } );
  80. extrudingMaterial.name = 'extruded';
  81. function newLayer( line ) {
  82. currentLayer = { vertex: [], pathVertex: [], z: line.z };
  83. layers.push( currentLayer );
  84. }
  85. //Create lie segment between p1 and p2
  86. function addSegment( p1, p2 ) {
  87. if ( currentLayer === undefined ) {
  88. newLayer( p1 );
  89. }
  90. if ( state.extruding ) {
  91. currentLayer.vertex.push( p1.x, p1.y, p1.z );
  92. currentLayer.vertex.push( p2.x, p2.y, p2.z );
  93. } else {
  94. currentLayer.pathVertex.push( p1.x, p1.y, p1.z );
  95. currentLayer.pathVertex.push( p2.x, p2.y, p2.z );
  96. }
  97. }
  98. function delta( v1, v2 ) {
  99. return state.relative ? v2 : v2 - v1;
  100. }
  101. function absolute( v1, v2 ) {
  102. return state.relative ? v1 + v2 : v2;
  103. }
  104. const lines = data.replace( /;.+/g, '' ).split( '\n' );
  105. for ( let i = 0; i < lines.length; i ++ ) {
  106. const tokens = lines[ i ].split( ' ' );
  107. const cmd = tokens[ 0 ].toUpperCase();
  108. //Arguments
  109. const args = {};
  110. tokens.splice( 1 ).forEach( function ( token ) {
  111. if ( token[ 0 ] !== undefined ) {
  112. const key = token[ 0 ].toLowerCase();
  113. const value = parseFloat( token.substring( 1 ) );
  114. args[ key ] = value;
  115. }
  116. } );
  117. //Process commands
  118. //G0/G1 – Linear Movement
  119. if ( cmd === 'G0' || cmd === 'G1' ) {
  120. const line = {
  121. x: args.x !== undefined ? absolute( state.x, args.x ) : state.x,
  122. y: args.y !== undefined ? absolute( state.y, args.y ) : state.y,
  123. z: args.z !== undefined ? absolute( state.z, args.z ) : state.z,
  124. e: args.e !== undefined ? absolute( state.e, args.e ) : state.e,
  125. f: args.f !== undefined ? absolute( state.f, args.f ) : state.f,
  126. };
  127. //Layer change detection is or made by watching Z, it's made by watching when we extrude at a new Z position
  128. if ( delta( state.e, line.e ) > 0 ) {
  129. state.extruding = delta( state.e, line.e ) > 0;
  130. if ( currentLayer == undefined || line.z != currentLayer.z ) {
  131. newLayer( line );
  132. }
  133. }
  134. addSegment( state, line );
  135. state = line;
  136. } else if ( cmd === 'G2' || cmd === 'G3' ) {
  137. //G2/G3 - Arc Movement ( G2 clock wise and G3 counter clock wise )
  138. //console.warn( 'THREE.GCodeLoader: Arc command not supported' );
  139. } else if ( cmd === 'G90' ) {
  140. //G90: Set to Absolute Positioning
  141. state.relative = false;
  142. } else if ( cmd === 'G91' ) {
  143. //G91: Set to state.relative Positioning
  144. state.relative = true;
  145. } else if ( cmd === 'G92' ) {
  146. //G92: Set Position
  147. const line = state;
  148. line.x = args.x !== undefined ? args.x : line.x;
  149. line.y = args.y !== undefined ? args.y : line.y;
  150. line.z = args.z !== undefined ? args.z : line.z;
  151. line.e = args.e !== undefined ? args.e : line.e;
  152. } else {
  153. //console.warn( 'THREE.GCodeLoader: Command not supported:' + cmd );
  154. }
  155. }
  156. function addObject( vertex, extruding, i ) {
  157. const geometry = new BufferGeometry();
  158. geometry.setAttribute( 'position', new Float32BufferAttribute( vertex, 3 ) );
  159. const segments = new LineSegments( geometry, extruding ? extrudingMaterial : pathMaterial );
  160. segments.name = 'layer' + i;
  161. object.add( segments );
  162. }
  163. const object = new Group();
  164. object.name = 'gcode';
  165. if ( this.splitLayer ) {
  166. for ( let i = 0; i < layers.length; i ++ ) {
  167. const layer = layers[ i ];
  168. addObject( layer.vertex, true, i );
  169. addObject( layer.pathVertex, false, i );
  170. }
  171. } else {
  172. const vertex = [],
  173. pathVertex = [];
  174. for ( let i = 0; i < layers.length; i ++ ) {
  175. const layer = layers[ i ];
  176. const layerVertex = layer.vertex;
  177. const layerPathVertex = layer.pathVertex;
  178. for ( let j = 0; j < layerVertex.length; j ++ ) {
  179. vertex.push( layerVertex[ j ] );
  180. }
  181. for ( let j = 0; j < layerPathVertex.length; j ++ ) {
  182. pathVertex.push( layerPathVertex[ j ] );
  183. }
  184. }
  185. addObject( vertex, true, layers.length );
  186. addObject( pathVertex, false, layers.length );
  187. }
  188. object.rotation.set( - Math.PI / 2, 0, 0 );
  189. return object;
  190. }
  191. }
  192. export { GCodeLoader };
粤ICP备19079148号