@@ -56,18 +56,11 @@ type singleLetter =
5656 | 'Z' ;
5757type CommandParams = { [ key in singleLetter ] ?: number } ;
5858
59- type MoveCommandParamName = 'x' | 'y' | 'z' | 'r' | 'e' | 'f' | 'i' | 'j' ;
60- type MoveCommandParams = {
61- [ key in MoveCommandParamName ] ?: number ;
62- } ;
63-
6459export enum Code {
6560 G0 = 'G0' ,
6661 G1 = 'G1' ,
6762 G2 = 'G2' ,
6863 G3 = 'G3' ,
69- G20 = 'G20' ,
70- G21 = 'G21' ,
7164 G28 = 'G28' ,
7265 T0 = 'T0' ,
7366 T1 = 'T1' ,
@@ -125,54 +118,15 @@ export class GCodeCommand {
125118 }
126119}
127120
128- export class MoveCommand extends GCodeCommand {
129- constructor (
130- src : string ,
131- gcode : string ,
132- public params : MoveCommandParams ,
133- comment ?: string
134- ) {
135- super ( src , gcode , params , comment ) ;
136- }
137- }
138-
139121type Metadata = { thumbnails : Record < string , Thumbnail > } ;
140122
141- export class Layer {
142- constructor (
143- public layer : number ,
144- public commands : GCodeCommand [ ] ,
145- public lineNumber : number ,
146- public height : number = 0
147- ) { }
148- }
149-
150123export class Parser {
151124 lines : string [ ] = [ ] ;
152125 commands : GCodeCommand [ ] = [ ] ;
153126
154- /**
155- * @experimental GCode commands before extrusion starts.
156- */
157- preamble = new Layer ( - 1 , [ ] , 0 ) ; // TODO: remove preamble and treat as a regular layer? Unsure of the benefit
158- layers : Layer [ ] = [ ] ;
159- curZ = 0 ;
160- maxZ = 0 ;
161127 metadata : Metadata = { thumbnails : { } } ;
162- tolerance = 0 ; // The higher the tolerance, the fewer layers are created, so performance will improve.
163-
164- /**
165- * Create a new Parser instance.
166- *
167- * @param minLayerThreshold - If specified, the minimum layer height to be considered a new layer. If not specified, the default value is 0.
168- * @returns A new Parser instance.
169- */
170- constructor ( minLayerThreshold : number ) {
171- this . tolerance = minLayerThreshold ?? this . tolerance ;
172- }
173128
174129 parseGCode ( input : string | string [ ] ) : {
175- layers : Layer [ ] ;
176130 metadata : Metadata ;
177131 commands : GCodeCommand [ ] ;
178132 } {
@@ -182,15 +136,13 @@ export class Parser {
182136
183137 this . commands = this . lines2commands ( lines ) ;
184138
185- // this.groupIntoLayers(this.commands);
186-
187139 // merge thumbs
188140 const thumbs = this . parseMetadata ( this . commands . filter ( ( cmd ) => cmd . comment ) ) . thumbnails ;
189141 for ( const [ key , value ] of Object . entries ( thumbs ) ) {
190142 this . metadata . thumbnails [ key ] = value ;
191143 }
192144
193- return { layers : this . layers , metadata : this . metadata , commands : this . commands } ;
145+ return { metadata : this . metadata , commands : this . commands } ;
194146 }
195147
196148 private lines2commands ( lines : string [ ] ) {
@@ -210,19 +162,7 @@ export class Parser {
210162
211163 const gcode = ! parts . length ? '' : `${ parts [ 0 ] ?. toLowerCase ( ) } ${ parts [ 1 ] } ` ;
212164 const params = this . parseParams ( parts . slice ( 2 ) ) ;
213- switch ( gcode ) {
214- case 'g0' :
215- case 'g00' :
216- case 'g1' :
217- case 'g01' :
218- case 'g2' :
219- case 'g02' :
220- case 'g3' :
221- case 'g03' :
222- return new MoveCommand ( line , gcode , params , comment ) ;
223- default :
224- return new GCodeCommand ( line , gcode , params , comment ) ;
225- }
165+ return new GCodeCommand ( line , gcode , params , comment ) ;
226166 }
227167
228168 private isAlpha ( char : string | singleLetter ) : char is singleLetter {
@@ -243,39 +183,6 @@ export class Parser {
243183 } , { } ) ;
244184 }
245185
246- private groupIntoLayers ( commands : GCodeCommand [ ] ) : Layer [ ] {
247- for ( let lineNumber = 0 ; lineNumber < commands . length ; lineNumber ++ ) {
248- const cmd = commands [ lineNumber ] ;
249-
250- if ( cmd instanceof MoveCommand ) {
251- const params = cmd . params ;
252-
253- // update current z?
254- if ( params . z ) {
255- this . curZ = params . z ; // abs mode
256- }
257-
258- if (
259- ( params . e ?? 0 ) > 0 && // extruding?
260- ( params . x != undefined || params . y != undefined ) && // moving?
261- Math . abs ( this . curZ - ( this . maxZ || - Infinity ) ) > this . tolerance // new layer?
262- ) {
263- const layerHeight = Math . abs ( this . curZ - this . maxZ ) ;
264- this . maxZ = this . curZ ;
265- this . layers . push ( new Layer ( this . layers . length , [ ] , lineNumber , layerHeight ) ) ;
266- }
267- }
268-
269- this . maxLayer . commands . push ( cmd ) ;
270- }
271-
272- return this . layers ;
273- }
274-
275- get maxLayer ( ) : Layer {
276- return this . layers [ this . layers . length - 1 ] ?? this . preamble ;
277- }
278-
279186 parseMetadata ( metadata : GCodeCommand [ ] ) : Metadata {
280187 const thumbnails : Record < string , Thumbnail > = { } ;
281188
0 commit comments