@@ -39,11 +39,11 @@ THREE.GCodeLoader.prototype.parse = function ( data ) {
3939 var currentLayer = undefined ;
4040
4141 var box = new THREE . Box3 ( ) ;
42-
43- var pathMaterial = new THREE . LineBasicMaterial ( { color : 0xFFFF00 } ) ;
42+
43+ var pathMaterial = new THREE . LineBasicMaterial ( { color : 0xFF0000 } ) ;
4444 pathMaterial . name = 'path' ;
4545
46- var extrudingMaterial = new THREE . LineBasicMaterial ( { color : 0xFFFFFF } ) ;
46+ var extrudingMaterial = new THREE . LineBasicMaterial ( { color : 0x00FF00 } ) ;
4747 extrudingMaterial . name = 'extruded' ;
4848
4949 function newLayer ( line ) {
@@ -62,33 +62,23 @@ THREE.GCodeLoader.prototype.parse = function ( data ) {
6262
6363 }
6464
65- if ( line . extruding ) {
65+ if ( line . extruding ) {
6666
67- currentLayer . vertex . push ( p1 . x ) ;
68- currentLayer . vertex . push ( p1 . y ) ;
69- currentLayer . vertex . push ( p1 . z ) ;
70- currentLayer . vertex . push ( p2 . x ) ;
71- currentLayer . vertex . push ( p2 . y ) ;
72- currentLayer . vertex . push ( p2 . z ) ;
67+ currentLayer . vertex . push ( p1 . x , p1 . y , p1 . z ) ;
68+ currentLayer . vertex . push ( p2 . x , p2 . y , p2 . z ) ;
7369
74- }
70+ } else {
7571
76- else {
72+ currentLayer . pathVertex . push ( p1 . x , p1 . y , p1 . z ) ;
73+ currentLayer . pathVertex . push ( p2 . x , p2 . y , p2 . z ) ;
7774
78- currentLayer . pathVertex . push ( p1 . x ) ;
79- currentLayer . pathVertex . push ( p1 . y ) ;
80- currentLayer . pathVertex . push ( p1 . z ) ;
81- currentLayer . pathVertex . push ( p2 . x ) ;
82- currentLayer . pathVertex . push ( p2 . y ) ;
83- currentLayer . pathVertex . push ( p2 . z ) ;
84-
8575 }
8676
8777 if ( line . extruding ) {
8878
8979 box . min . set ( Math . min ( box . min . x , p2 . x ) , Math . min ( box . min . y , p2 . y ) , Math . min ( box . min . z , p2 . z ) ) ;
9080 box . max . set ( Math . max ( box . max . x , p2 . x ) , Math . max ( box . max . y , p2 . y ) , Math . max ( box . max . z , p2 . z ) ) ;
91-
81+
9282 }
9383
9484 }
@@ -108,22 +98,23 @@ THREE.GCodeLoader.prototype.parse = function ( data ) {
10898 var lines = data . replace ( / ; .+ / g, '' ) . split ( '\n' ) ;
10999
110100 for ( var i = 0 ; i < lines . length ; i ++ ) {
101+
111102 var tokens = lines [ i ] . split ( ' ' ) ;
112- var cmd = tokens [ 0 ] . toUpperCase ( ) ;
103+ var cmd = tokens [ 0 ] . toUpperCase ( ) ;
113104
114105 //Argumments
115106 var args = { } ;
116- tokens . splice ( 1 ) . forEach ( function ( token ) {
107+ tokens . splice ( 1 ) . forEach ( function ( token ) {
117108
118- if ( token [ 0 ] !== undefined ) {
109+ if ( token [ 0 ] !== undefined ) {
119110
120- var key = token [ 0 ] . toLowerCase ( ) ;
121- var value = parseFloat ( token . substring ( 1 ) ) ;
111+ var key = token [ 0 ] . toLowerCase ( ) ;
112+ var value = parseFloat ( token . substring ( 1 ) ) ;
122113 args [ key ] = value ;
123114
124115 }
125116
126- } ) ;
117+ } ) ;
127118
128119 //Process commands
129120 //G0/G1 – Linear Movement
@@ -152,57 +143,55 @@ THREE.GCodeLoader.prototype.parse = function ( data ) {
152143
153144 addSegment ( state , line ) ;
154145 state = line ;
155- }
156- //G2/G3 - Arc Movement ( G2 clock wise and G3 counter clock wise )
157- else if ( cmd === 'G2' || cmd === 'G3' ) {
158146
147+ } else if ( cmd === 'G2' || cmd === 'G3' ) {
148+
149+ //G2/G3 - Arc Movement ( G2 clock wise and G3 counter clock wise )
159150 console . warn ( 'THREE.GCodeLoader: Arc command not supported' ) ;
160- }
161- //G90: Set to Absolute Positioning
162- else if ( cmd === 'G90' ) {
163151
152+ } else if ( cmd === 'G90' ) {
153+
154+ //G90: Set to Absolute Positioning
164155 state . relative = false ;
165156
166- }
167- //G91: Set to state.relative Positioning
168- else if ( cmd === 'G91' ) {
157+ } else if ( cmd === 'G91' ) {
169158
159+ //G91: Set to state.relative Positioning
170160 state . relative = true ;
171161
172- }
173- //G92: Set Position
174- else if ( cmd === 'G92' ) {
162+ } else if ( cmd === 'G92' ) {
175163
164+ //G92: Set Position
176165 var line = state ;
177166 line . x = args . x !== undefined ? args . x : line . x ;
178167 line . y = args . y !== undefined ? args . y : line . y ;
179168 line . z = args . z !== undefined ? args . z : line . z ;
180169 line . e = args . e !== undefined ? args . e : line . e ;
181170 state = line ;
182171
183- }
184- else {
172+ } else {
185173
186174 console . warn ( 'THREE.GCodeLoader: Command not supported:' + cmd ) ;
187175
188176 }
177+
189178 }
190179
191- function addObject ( vertex , extruding ) {
180+ function addObject ( vertex , extruding ) {
192181
193182 var geometry = new THREE . BufferGeometry ( ) ;
194- geometry . addAttribute ( 'position' , new THREE . BufferAttribute ( new Float32Array ( vertex ) , 3 ) ) ;
183+ geometry . addAttribute ( 'position' , new THREE . Float32BufferAttribute ( vertex , 3 ) ) ;
195184
196185 var segments = new THREE . LineSegments ( geometry , extruding ? extrudingMaterial : pathMaterial ) ;
197186 segments . name = 'layer' + i ;
198187 object . add ( segments ) ;
199188
200189 }
201190
202- var object = new THREE . Object3D ( ) ;
191+ var object = new THREE . Group ( ) ;
203192 object . name = 'gcode' ;
204193
205- if ( this . splitLayer ) {
194+ if ( this . splitLayer ) {
206195
207196 for ( var i = 0 ; i < layers . length ; i ++ ) {
208197
@@ -212,25 +201,26 @@ THREE.GCodeLoader.prototype.parse = function ( data ) {
212201
213202 }
214203
215- }
216- else {
204+ } else {
217205
218206 var vertex = [ ] , pathVertex = [ ] ;
219207
220208 for ( var i = 0 ; i < layers . length ; i ++ ) {
221209
222210 var layer = layers [ i ] ;
223211
224- vertex = vertex . concat ( layer . vertex ) ;
225- pathVertex = pathVertex . concat ( layer . pathVertex ) ;
212+ vertex = vertex . concat ( layer . vertex ) ;
213+ pathVertex = pathVertex . concat ( layer . pathVertex ) ;
226214
227215 }
228216
229217 addObject ( vertex , true ) ;
230218 addObject ( pathVertex , false ) ;
219+
231220 }
232221
233- object . rotation . set ( - Math . PI / 2 , 0 , 0 ) ;
222+ object . rotation . set ( - Math . PI / 2 , 0 , 0 ) ;
234223
235224 return object ;
225+
236226} ;
0 commit comments