GCodeLoader.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 'use strict';
  2. /**
  3. * THREE.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 THREE.GCodeLoader
  8. * @param {Manager} manager Loading manager.
  9. * @author tentone
  10. * @author joewalnes
  11. */
  12. THREE.GCodeLoader = function ( manager ) {
  13. this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
  14. this.splitLayer = false;
  15. };
  16. THREE.GCodeLoader.prototype.load = function ( url, onLoad, onProgress, onError ) {
  17. var self = this;
  18. var loader = new THREE.FileLoader( self.manager );
  19. loader.setPath( self.path );
  20. loader.load( url, function ( text ) {
  21. onLoad( self.parse( text ) );
  22. }, onProgress, onError );
  23. };
  24. THREE.GCodeLoader.prototype.setPath = function ( value ) {
  25. this.path = value;
  26. return this;
  27. };
  28. THREE.GCodeLoader.prototype.parse = function ( data ) {
  29. var state = { x: 0, y: 0, z: 0, e: 0, f: 0, extruding: false, relative: false };
  30. var layers = [];
  31. var currentLayer = undefined;
  32. var pathMaterial = new THREE.LineBasicMaterial( { color: 0x0d5ed8,linewidth: 3.0 } );
  33. pathMaterial.name = 'path';
  34. var extrudingMaterial = new THREE.LineBasicMaterial( { color: 0xd8c70d,linewidth: 3.0} );
  35. extrudingMaterial.name = 'extruded';
  36. function newLayer( line ) {
  37. currentLayer = { vertex: [], pathVertex: [], z: line.z };
  38. layers.push( currentLayer );
  39. }
  40. //Create lie segment between p1 and p2
  41. function addSegment( p1, p2 ) {
  42. if ( currentLayer === undefined ) {
  43. newLayer( p1 );
  44. }
  45. if ( line.extruding ) {
  46. currentLayer.vertex.push( p1.x, p1.y, p1.z );
  47. currentLayer.vertex.push( p2.x, p2.y, p2.z );
  48. } else {
  49. currentLayer.pathVertex.push( p1.x, p1.y, p1.z );
  50. currentLayer.pathVertex.push( p2.x, p2.y, p2.z );
  51. }
  52. }
  53. function delta( v1, v2 ) {
  54. return state.relative ? v2 : v2 - v1;
  55. }
  56. function absolute( v1, v2 ) {
  57. return state.relative ? v1 + v2 : v2;
  58. }
  59. var lines = data.replace( /;.+/g, '' ).split( '\n' );
  60. for ( var i = 0; i < lines.length; i ++ ) {
  61. var tokens = lines[ i ].split( ' ' );
  62. var cmd = tokens[ 0 ].toUpperCase();
  63. //Argumments
  64. var args = {};
  65. tokens.splice( 1 ).forEach( function ( token ) {
  66. if ( token[ 0 ] !== undefined ) {
  67. var key = token[ 0 ].toLowerCase();
  68. var value = parseFloat( token.substring( 1 ) );
  69. args[ key ] = value;
  70. }
  71. } );
  72. //Process commands
  73. //G0/G1 – Linear Movement
  74. if ( cmd === 'G0' || cmd === 'G1' ) {
  75. var line = {
  76. x: args.x !== undefined ? absolute( state.x, args.x ) : state.x,
  77. y: args.y !== undefined ? absolute( state.y, args.y ) : state.y,
  78. z: args.z !== undefined ? absolute( state.z, args.z ) : state.z,
  79. e: args.e !== undefined ? absolute( state.e, args.e ) : state.e,
  80. f: args.f !== undefined ? absolute( state.f, args.f ) : state.f,
  81. };
  82. //Layer change detection is or made by watching Z, it's made by watching when we extrude at a new Z position
  83. if ( delta( state.e, line.e ) > 0 ) {
  84. line.extruding = delta( state.e, line.e ) > 0;
  85. if ( currentLayer == undefined || line.z != currentLayer.z ) {
  86. newLayer( line );
  87. }
  88. }
  89. addSegment( state, line );
  90. state = line;
  91. } else if ( cmd === 'G2' || cmd === 'G3' ) {
  92. //G2/G3 - Arc Movement ( G2 clock wise and G3 counter clock wise )
  93. console.warn( 'THREE.GCodeLoader: Arc command not supported' );
  94. } else if ( cmd === 'G90' ) {
  95. //G90: Set to Absolute Positioning
  96. state.relative = false;
  97. } else if ( cmd === 'G91' ) {
  98. //G91: Set to state.relative Positioning
  99. state.relative = true;
  100. } else if ( cmd === 'G92' ) {
  101. //G92: Set Position
  102. var line = state;
  103. line.x = args.x !== undefined ? args.x : line.x;
  104. line.y = args.y !== undefined ? args.y : line.y;
  105. line.z = args.z !== undefined ? args.z : line.z;
  106. line.e = args.e !== undefined ? args.e : line.e;
  107. state = line;
  108. } else {
  109. console.warn( 'THREE.GCodeLoader: Command not supported:' + cmd );
  110. }
  111. }
  112. function addObject( vertex, extruding ) {
  113. var geometry = new THREE.BufferGeometry();
  114. geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( vertex, 3 ) );
  115. var segments = new THREE.LineSegments( geometry, extruding ? extrudingMaterial : pathMaterial );
  116. segments.name = 'layer' + i;
  117. object.add( segments );
  118. }
  119. var object = new THREE.Group();
  120. object.name = 'gcode';
  121. if ( this.splitLayer ) {
  122. for ( var i = 0; i < layers.length; i ++ ) {
  123. var layer = layers[ i ];
  124. addObject( layer.vertex, true );
  125. addObject( layer.pathVertex, false );
  126. }
  127. } else {
  128. var vertex = [], pathVertex = [];
  129. for ( var i = 0; i < layers.length; i ++ ) {
  130. var layer = layers[ i ];
  131. vertex = vertex.concat( layer.vertex );
  132. pathVertex = pathVertex.concat( layer.pathVertex );
  133. }
  134. addObject( vertex, true );
  135. addObject( pathVertex, false );
  136. }
  137. object.quaternion.setFromEuler( new THREE.Euler( - Math.PI / 2, 0, 0 ) );
  138. return object;
  139. };