Skip to content

Commit b6cbb2a

Browse files
committed
Improve the paths subsets of jobs (#220)
* Introduce the traveltype indexer * Add the layer indexer * don't clear for all types * Fix the calls to the getters * Cleaner error management * Fix all tests
1 parent a3d9d26 commit b6cbb2a

File tree

8 files changed

+344
-149
lines changed

8 files changed

+344
-149
lines changed

demo/js/app.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ export const app = (window.app = createApp({
7070
const { thumbnails } = parser.metadata;
7171

7272
thumbnail.value = thumbnails['220x124']?.src;
73-
layerCount.value = job.layers()?.length;
73+
layerCount.value = job.layers?.length;
7474
const colors = extrusionColor instanceof Array ? extrusionColor : [extrusionColor];
7575
const currentSettings = {
76-
maxLayer: job.layers()?.length,
77-
endLayer: job.layers()?.length,
76+
maxLayer: job.layers?.length,
77+
endLayer: job.layers?.length,
7878
singleLayerMode,
7979
renderTravel,
8080
travelColor: '#' + travelColor.getHexString(),
@@ -93,7 +93,7 @@ export const app = (window.app = createApp({
9393
};
9494

9595
Object.assign(settings.value, currentSettings);
96-
preview.endLayer = job.layers()?.length;
96+
preview.endLayer = job.layers?.length;
9797
};
9898

9999
const loadGCodeFromServer = async (filename) => {
@@ -121,12 +121,12 @@ export const app = (window.app = createApp({
121121
const render = async () => {
122122
debounce(async () => {
123123
if (loadProgressive) {
124-
if (preview.job.layers() === null) {
124+
if (preview.job.layers === null) {
125125
console.warn('Job is not planar');
126126
preview.render();
127127
return;
128128
}
129-
await preview.renderAnimated(Math.ceil(preview.job.layers().length / 60));
129+
await preview.renderAnimated(Math.ceil(preview.job.layers?.length / 60));
130130
} else {
131131
preview.render();
132132
}

src/__tests__/interpreter.ts

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ test('.G0 starts a path if the job has none', () => {
7474

7575
const job = interpreter.execute([command]);
7676

77-
expect(job.paths.length).toEqual(1);
78-
expect(job.paths[0].vertices.length).toEqual(6);
79-
expect(job.paths[0].vertices[0]).toEqual(0);
80-
expect(job.paths[0].vertices[1]).toEqual(0);
81-
expect(job.paths[0].vertices[2]).toEqual(0);
82-
expect(job.paths[0].vertices[3]).toEqual(1);
83-
expect(job.paths[0].vertices[4]).toEqual(2);
84-
expect(job.paths[0].vertices[5]).toEqual(0);
77+
expect(job.paths.length).toEqual(0);
78+
expect(job.inprogressPath).not.toBeNull();
79+
expect(job.inprogressPath?.vertices.length).toEqual(6);
80+
expect(job.inprogressPath?.vertices[0]).toEqual(0);
81+
expect(job.inprogressPath?.vertices[1]).toEqual(0);
82+
expect(job.inprogressPath?.vertices[2]).toEqual(0);
83+
expect(job.inprogressPath?.vertices[3]).toEqual(1);
84+
expect(job.inprogressPath?.vertices[4]).toEqual(2);
85+
expect(job.inprogressPath?.vertices[5]).toEqual(0);
8586
});
8687

8788
test('.G0 starts a path if the job has none, starting at the job current state', () => {
@@ -94,12 +95,12 @@ test('.G0 starts a path if the job has none, starting at the job current state',
9495

9596
interpreter.execute([command], job);
9697

97-
expect(job.paths.length).toEqual(1);
98-
expect(job.paths[0].vertices.length).toEqual(6);
99-
expect(job.paths[0].vertices[0]).toEqual(3);
100-
expect(job.paths[0].vertices[1]).toEqual(4);
101-
expect(job.paths[0].vertices[2]).toEqual(0);
102-
expect(job.paths[0].tool).toEqual(5);
98+
expect(job.paths.length).toEqual(0);
99+
expect(job.inprogressPath?.vertices.length).toEqual(6);
100+
expect(job.inprogressPath?.vertices[0]).toEqual(3);
101+
expect(job.inprogressPath?.vertices[1]).toEqual(4);
102+
expect(job.inprogressPath?.vertices[2]).toEqual(0);
103+
expect(job.inprogressPath?.tool).toEqual(5);
103104
});
104105

105106
test('.G0 continues the path if the job has one', () => {
@@ -113,11 +114,11 @@ test('.G0 continues the path if the job has one', () => {
113114

114115
interpreter.G0(command2, job);
115116

116-
expect(job.paths.length).toEqual(1);
117-
expect(job.paths[0].vertices.length).toEqual(9);
118-
expect(job.paths[0].vertices[6]).toEqual(3);
119-
expect(job.paths[0].vertices[7]).toEqual(4);
120-
expect(job.paths[0].vertices[8]).toEqual(5);
117+
expect(job.paths.length).toEqual(0);
118+
expect(job.inprogressPath?.vertices.length).toEqual(9);
119+
expect(job.inprogressPath?.vertices[6]).toEqual(3);
120+
expect(job.inprogressPath?.vertices[7]).toEqual(4);
121+
expect(job.inprogressPath?.vertices[8]).toEqual(5);
121122
});
122123

123124
test(".G0 assigns the travel type if there's no extrusion", () => {
@@ -127,8 +128,8 @@ test(".G0 assigns the travel type if there's no extrusion", () => {
127128

128129
interpreter.G0(command, job);
129130

130-
expect(job.paths.length).toEqual(1);
131-
expect(job.paths[0].travelType).toEqual(PathType.Travel);
131+
expect(job.paths.length).toEqual(0);
132+
expect(job.inprogressPath?.travelType).toEqual(PathType.Travel);
132133
});
133134

134135
test(".G0 assigns the extrusion type if there's extrusion", () => {
@@ -138,8 +139,8 @@ test(".G0 assigns the extrusion type if there's extrusion", () => {
138139

139140
interpreter.G0(command, job);
140141

141-
expect(job.paths.length).toEqual(1);
142-
expect(job.paths[0].travelType).toEqual('Extrusion');
142+
expect(job.paths.length).toEqual(0);
143+
expect(job.inprogressPath?.travelType).toEqual('Extrusion');
143144
});
144145

145146
test('.G0 assigns the travel type if the extrusion is a retraction', () => {
@@ -149,8 +150,19 @@ test('.G0 assigns the travel type if the extrusion is a retraction', () => {
149150

150151
interpreter.G0(command, job);
151152

152-
expect(job.paths.length).toEqual(1);
153-
expect(job.paths[0].travelType).toEqual('Travel');
153+
expect(job.paths.length).toEqual(0);
154+
expect(job.inprogressPath?.travelType).toEqual('Travel');
155+
});
156+
157+
test('.G0 assigns the travel type if the extrusion is a retraction', () => {
158+
const command = new GCodeCommand('G0 E-2', 'g0', { e: -2 });
159+
const interpreter = new Interpreter();
160+
const job = new Job();
161+
162+
interpreter.G0(command, job);
163+
164+
expect(job.paths.length).toEqual(0);
165+
expect(job.inprogressPath?.travelType).toEqual('Travel');
154166
});
155167

156168
test('.G0 starts a new path if the travel type changes from Travel to Extrusion', () => {
@@ -162,9 +174,8 @@ test('.G0 starts a new path if the travel type changes from Travel to Extrusion'
162174

163175
interpreter.G0(command2, job);
164176

165-
expect(job.paths.length).toEqual(2);
166-
expect(job.paths[0].travelType).toEqual('Travel');
167-
expect(job.paths[1].travelType).toEqual('Extrusion');
177+
expect(job.paths.length).toEqual(1);
178+
expect(job.inprogressPath?.travelType).toEqual('Extrusion');
168179
});
169180

170181
test('.G0 starts a new path if the travel type changes from Extrusion to Travel', () => {
@@ -176,9 +187,8 @@ test('.G0 starts a new path if the travel type changes from Extrusion to Travel'
176187

177188
interpreter.G0(command2, job);
178189

179-
expect(job.paths.length).toEqual(2);
180-
expect(job.paths[0].travelType).toEqual('Extrusion');
181-
expect(job.paths[1].travelType).toEqual('Travel');
190+
expect(job.paths.length).toEqual(1);
191+
expect(job.inprogressPath?.travelType).toEqual('Travel');
182192
});
183193

184194
test('.G1 is an alias to .G0', () => {

0 commit comments

Comments
 (0)