Skip to content

Commit 3e83d28

Browse files
committed
Remove some layer references
1 parent c94a66e commit 3e83d28

File tree

3 files changed

+8
-106
lines changed

3 files changed

+8
-106
lines changed

src/dev-gui.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,10 @@ class DevGUI {
116116
parser.onOpenClose(() => {
117117
this.saveOpenFolders();
118118
});
119-
parser.add(this.watchedObject.parser, 'curZ').listen();
120-
parser.add(this.watchedObject.parser, 'maxZ').listen();
121-
parser.add(this.watchedObject.parser, 'tolerance').listen();
122-
parser.add(this.watchedObject.parser.layers, 'length').name('layers.count').listen();
123-
parser.add(this.watchedObject.parser.lines, 'length').name('lines.count').listen();
119+
// parser.add(this.watchedObject.parser, 'curZ').listen();
120+
// parser.add(this.watchedObject.parser, 'maxZ').listen();
121+
// parser.add(this.watchedObject.parser, 'tolerance').listen();
122+
// parser.add(this.watchedObject.parser.lines, 'length').name('lines.count').listen();
124123
}
125124

126125
private setupBuildVolumeFolder(): void {

src/gcode-parser.ts

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -56,18 +56,11 @@ type singleLetter =
5656
| 'Z';
5757
type 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-
6459
export 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-
139121
type 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-
150123
export 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

src/webgl-preview.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ export type GCodePreviewOptions = {
4646
lineWidth?: number;
4747
lineHeight?: number;
4848
nonTravelMoves?: string[];
49-
minLayerThreshold?: number;
5049
renderExtrusion?: boolean;
5150
renderTravel?: boolean;
5251
startLayer?: number;
@@ -65,7 +64,6 @@ export type GCodePreviewOptions = {
6564
};
6665

6766
export class WebGLPreview {
68-
minLayerThreshold = 0.05;
6967
parser: Parser;
7068
scene: Scene;
7169
camera: PerspectiveCamera;
@@ -97,7 +95,6 @@ export class WebGLPreview {
9795
static readonly defaultExtrusionColor = new Color('hotpink');
9896
private _extrusionColor: Color | Color[] = WebGLPreview.defaultExtrusionColor;
9997
private animationFrameId?: number;
100-
private renderLayerIndex = 0;
10198
private _geometries: Record<number, BufferGeometry[]> = {};
10299
interpreter: Interpreter = new Interpreter();
103100
virtualMachine: Machine = new Machine();
@@ -118,8 +115,7 @@ export class WebGLPreview {
118115
private devGui?: DevGUI;
119116

120117
constructor(opts: GCodePreviewOptions) {
121-
this.minLayerThreshold = opts.minLayerThreshold ?? this.minLayerThreshold;
122-
this.parser = new Parser(this.minLayerThreshold);
118+
this.parser = new Parser();
123119
this.scene = new Scene();
124120
this.scene.background = this._backgroundColor;
125121
if (opts.backgroundColor !== undefined) {
@@ -316,7 +312,7 @@ export class WebGLPreview {
316312
// reset parser & processing state
317313
clear(): void {
318314
this.resetState();
319-
this.parser = new Parser(this.minLayerThreshold);
315+
this.parser = new Parser();
320316
this.virtualMachine = new Machine();
321317
}
322318

0 commit comments

Comments
 (0)