Spaces:
Running
Running
File size: 4,959 Bytes
6cd9596 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
'use strict';
/**
* THREE.GCodeLoader is used to load gcode files usually used for 3D printing or CNC applications.
*
* Gcode files are composed by commands used by machines to create objects.
*
* @class THREE.GCodeLoader
* @param {Manager} manager Loading manager.
* @author tentone
* @author joewalnes
*/
THREE.GCodeLoader = function ( manager ) {
this.manager = ( manager !== undefined ) ? manager : THREE.DefaultLoadingManager;
this.splitLayer = false;
};
THREE.GCodeLoader.prototype.load = function ( url, onLoad, onProgress, onError ) {
var self = this;
var loader = new THREE.FileLoader( self.manager );
loader.setPath( self.path );
loader.load( url, function ( text ) {
onLoad( self.parse( text ) );
}, onProgress, onError );
};
THREE.GCodeLoader.prototype.setPath = function ( value ) {
this.path = value;
return this;
};
THREE.GCodeLoader.prototype.parse = function ( data ) {
var state = { x: 0, y: 0, z: 0, e: 0, f: 0, extruding: false, relative: false };
var layers = [];
var currentLayer = undefined;
var pathMaterial = new THREE.LineBasicMaterial( { color: 0xFF0000 } );
pathMaterial.name = 'path';
var extrudingMaterial = new THREE.LineBasicMaterial( { color: 0x00FF00 } );
extrudingMaterial.name = 'extruded';
function newLayer( line ) {
currentLayer = { vertex: [], pathVertex: [], z: line.z };
layers.push( currentLayer );
}
//Create lie segment between p1 and p2
function addSegment( p1, p2 ) {
if ( currentLayer === undefined ) {
newLayer( p1 );
}
if ( line.extruding ) {
currentLayer.vertex.push( p1.x, p1.y, p1.z );
currentLayer.vertex.push( p2.x, p2.y, p2.z );
} else {
currentLayer.pathVertex.push( p1.x, p1.y, p1.z );
currentLayer.pathVertex.push( p2.x, p2.y, p2.z );
}
}
function delta( v1, v2 ) {
return state.relative ? v2 : v2 - v1;
}
function absolute( v1, v2 ) {
return state.relative ? v1 + v2 : v2;
}
var lines = data.replace( /;.+/g, '' ).split( '\n' );
for ( var i = 0; i < lines.length; i ++ ) {
var tokens = lines[ i ].split( ' ' );
var cmd = tokens[ 0 ].toUpperCase();
//Argumments
var args = {};
tokens.splice( 1 ).forEach( function ( token ) {
if ( token[ 0 ] !== undefined ) {
var key = token[ 0 ].toLowerCase();
var value = parseFloat( token.substring( 1 ) );
args[ key ] = value;
}
} );
//Process commands
//G0/G1 – Linear Movement
if ( cmd === 'G0' || cmd === 'G1' ) {
var line = {
x: args.x !== undefined ? absolute( state.x, args.x ) : state.x,
y: args.y !== undefined ? absolute( state.y, args.y ) : state.y,
z: args.z !== undefined ? absolute( state.z, args.z ) : state.z,
e: args.e !== undefined ? absolute( state.e, args.e ) : state.e,
f: args.f !== undefined ? absolute( state.f, args.f ) : state.f,
};
//Layer change detection is or made by watching Z, it's made by watching when we extrude at a new Z position
if ( delta( state.e, line.e ) > 0 ) {
line.extruding = delta( state.e, line.e ) > 0;
if ( currentLayer == undefined || line.z != currentLayer.z ) {
newLayer( line );
}
}
addSegment( state, line );
state = line;
} else if ( cmd === 'G2' || cmd === 'G3' ) {
//G2/G3 - Arc Movement ( G2 clock wise and G3 counter clock wise )
//console.warn( 'THREE.GCodeLoader: Arc command not supported' );
} else if ( cmd === 'G90' ) {
//G90: Set to Absolute Positioning
state.relative = false;
} else if ( cmd === 'G91' ) {
//G91: Set to state.relative Positioning
state.relative = true;
} else if ( cmd === 'G92' ) {
//G92: Set Position
var line = state;
line.x = args.x !== undefined ? args.x : line.x;
line.y = args.y !== undefined ? args.y : line.y;
line.z = args.z !== undefined ? args.z : line.z;
line.e = args.e !== undefined ? args.e : line.e;
state = line;
} else {
//console.warn( 'THREE.GCodeLoader: Command not supported:' + cmd );
}
}
function addObject( vertex, extruding ) {
var geometry = new THREE.BufferGeometry();
geometry.addAttribute( 'position', new THREE.Float32BufferAttribute( vertex, 3 ) );
var segments = new THREE.LineSegments( geometry, extruding ? extrudingMaterial : pathMaterial );
segments.name = 'layer' + i;
object.add( segments );
}
var object = new THREE.Group();
object.name = 'gcode';
if ( this.splitLayer ) {
for ( var i = 0; i < layers.length; i ++ ) {
var layer = layers[ i ];
addObject( layer.vertex, true );
addObject( layer.pathVertex, false );
}
} else {
var vertex = [], pathVertex = [];
for ( var i = 0; i < layers.length; i ++ ) {
var layer = layers[ i ];
vertex = vertex.concat( layer.vertex );
pathVertex = pathVertex.concat( layer.pathVertex );
}
addObject( vertex, true );
addObject( pathVertex, false );
}
object.quaternion.setFromEuler( new THREE.Euler( - Math.PI / 2, 0, 0 ) );
return object;
};
|