Skip to content

Commit

Permalink
GCodeProcessor: add option to allow unknown generators
Browse files Browse the repository at this point in the history
  • Loading branch information
miklschmidt committed Dec 17, 2024
1 parent 1106cbb commit 7891861
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
12 changes: 12 additions & 0 deletions configuration/klippy/ratos.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def __init__(self, config):
self.name = config.get_name()
self.last_processed_file_result = None
self.allow_unsupported_slicer_versions = False
self.allow_unknown_generator = False
self.use_legacy_post_processor = False
self.enable_post_processing = False
self.gcode = self.printer.lookup_object('gcode')
Expand Down Expand Up @@ -77,6 +78,11 @@ def register_commands(self):
self.gcode.register_command('PROCESS_GCODE_FILE', self.cmd_PROCESS_GCODE_FILE, desc=(self.desc_PROCESS_GCODE_FILE))
self.gcode.register_command('BEACON_APPLY_SCAN_COMPENSATION', self.cmd_BEACON_APPLY_SCAN_COMPENSATION, desc=(self.desc_BEACON_APPLY_SCAN_COMPENSATION))
self.gcode.register_command('TEST_PROCESS_GCODE_FILE', self.cmd_TEST_PROCESS_GCODE_FILE, desc=(self.desc_TEST_PROCESS_GCODE_FILE))
self.gcode.register_command('ALLOW_UNKNOWN_GENERATOR', self.cmd_ALLOW_UNKNOWN_GENERATOR, desc=(self.desc_ALLOW_UNKNOWN_GENERATOR))

desc_ALLOW_UNKNOWN_GENERATOR = "Allow gcode from generators that cannot be identified by the postprocessor"
def cmd_ALLOW_UNKNOWN_GENERATOR(self, gcmd):
self.allow_unknown_generator = True

desc_TEST_PROCESS_GCODE_FILE = "Test the G-code post-processor for IDEX and RMMU, only for debugging purposes"
def cmd_TEST_PROCESS_GCODE_FILE(self, gcmd):
Expand Down Expand Up @@ -227,6 +233,12 @@ def _interpret_output(data):
if data['result'] == 'error' and 'message' in data:
self.last_processed_file_result = None
self.console_echo("Error: " + data['title'], 'alert', data['message'])
if data['code'] == 'UNKNOWN_GCODE_GENERATOR':
self.console_echo(
'Do you want to allow gcode from unknown generators/slicers?', 'info',
'You can allow gcode from unknown generators by running ALLOW_UNKNOWN_GENERATOR in the console before starting a print._N_' +
'Keep in mind that this may cause unexpected behaviour, but it can be useful for calibration prints ' +
'such as the ones found in <a href="https://ellis3dp.com/Print-Tuning-Guide/">Ellis\' Print Tuning Guide</a>.')
if data['result'] == 'warning' and 'message' in data:
self.console_echo("Warning: " + data['title'], 'warning', data['message'])
if data['result'] == 'success':
Expand Down
30 changes: 27 additions & 3 deletions src/cli/commands/postprocessor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ import { z, ZodError } from 'zod';
import { getLogger } from '@/cli/logger';
import { WarningCodes } from '@/server/gcode-processor/WarningCodes';
import { getRealPath, loadEnvironment } from '@/cli/util';
import { GCodeError, GCodeProcessorError, SlicerNotSupported } from '@/server/gcode-processor/errors';
import {
GCodeError,
GCodeProcessorError,
GeneratorIdentificationNotFound,
SlicerNotSupported,
} from '@/server/gcode-processor/errors';
import { formatZodError } from '@schema-hub/zod-error-formatter';
import { Printability } from '@/server/gcode-processor/Printability';
import { promisify } from 'util';
Expand Down Expand Up @@ -100,11 +105,20 @@ const GcodeInfoZod = z.object({
.optional(),
});

const ErrorCodes = z.enum([
'UNKNOWN_GCODE_GENERATOR',
'UNSUPPORTED_SLICER_VERSION',
'FILE_NOT_FOUND',
'G_CODE_ERROR',
'UNKNOWN_ERROR',
]);

export const PostProcessorCLIOutput = z.discriminatedUnion('result', [
z.object({
result: z.literal('error'),
title: z.string().optional(),
message: z.string(),
code: ErrorCodes.default('UNKNOWN_ERROR'),
}),
z.object({
result: z.literal('warning'),
Expand All @@ -130,9 +144,10 @@ export const PostProcessorCLIOutput = z.discriminatedUnion('result', [
}),
]);

export type PostProcessorCLIOutput = z.infer<typeof PostProcessorCLIOutput>;
export type PostProcessorCLIOutput = z.output<typeof PostProcessorCLIOutput>;
export type PostProcessorCLIInput = z.input<typeof PostProcessorCLIOutput>;

export const toPostProcessorCLIOutput = (obj: PostProcessorCLIOutput): void => {
export const toPostProcessorCLIOutput = (obj: PostProcessorCLIInput): void => {
try {
echo(JSON.stringify(PostProcessorCLIOutput.parse(obj)));
} catch (e) {
Expand All @@ -143,6 +158,7 @@ export const toPostProcessorCLIOutput = (obj: PostProcessorCLIOutput): void => {
JSON.stringify({
result: 'error',
title: 'An error occurred while serializing postprocessor output',
code: 'UNKNOWN_ERROR',
message: `This is likely caused by loading a gcode file that was processed by a legacy version of the RatOS postprocessor.\n\n${formatZodError(e, obj).message}`,
} satisfies PostProcessorCLIOutput),
);
Expand Down Expand Up @@ -377,20 +393,27 @@ export const postprocessor = (program: Command) => {
let errorMessage =
'An unexpected error occurred while processing the file, please download a debug-zip and report this issue.';
let errorTitle = 'An unexpected error occurred during post-processing';
let errorCode: z.input<typeof ErrorCodes> = 'UNKNOWN_ERROR';
if (e instanceof GCodeProcessorError) {
errorTitle = 'G-code could not be processed';
errorMessage = e.message;
if (e instanceof SlicerNotSupported) {
errorTitle = 'Unsupported slicer version';
errorCode = 'UNSUPPORTED_SLICER_VERSION';
}
if (e instanceof GCodeError && e.lineNumber) {
errorTitle += ` (line ${e.lineNumber})`;
errorMessage += `\n\nLine ${e.lineNumber}: ${e.line}`;
errorCode = 'G_CODE_ERROR';
}
if (e instanceof GeneratorIdentificationNotFound) {
errorCode = 'UNKNOWN_GCODE_GENERATOR';
}
} else if (e instanceof Error) {
if ('code' in e && e.code === 'ENOENT' && 'path' in e) {
errorTitle = 'File not found';
errorMessage = `File ${e.path} not found`;
errorCode = 'FILE_NOT_FOUND';
} else {
getLogger().error(e, 'Unexpected error while processing gcode file');
}
Expand All @@ -404,6 +427,7 @@ export const postprocessor = (program: Command) => {
result: 'error',
message: errorMessage,
title: errorTitle,
code: errorCode,
});
}
process.exit(1);
Expand Down

0 comments on commit 7891861

Please sign in to comment.