preheatFilamentTargets.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Shared frontend helpers for the per-filament chamber-target map (#1468).
  2. // Mirrors backend `PrintScheduler.DEFAULT_PREHEAT_FILAMENT_TARGETS`. Values
  3. // are chamber-temperature recommendations from BambuStudio's bundled filament
  4. // profiles; users can override the whole map via the Settings → Workflow card.
  5. import { MAX_CHAMBER_TEMP_C } from './printer';
  6. export const DEFAULT_PREHEAT_FILAMENT_TARGETS: Record<string, number> = {
  7. PLA: 0,
  8. PETG: 0,
  9. 'PETG-CF': 40,
  10. ABS: 45,
  11. ASA: 45,
  12. PA: 50,
  13. 'PA-CF': 55,
  14. PC: 50,
  15. 'PC-FR': 50,
  16. TPU: 0,
  17. PVA: 0,
  18. default: 0,
  19. };
  20. // Stable display order for the editor and any preview UI. Matches the order
  21. // users will most likely tune (engineering filaments first, then commodity).
  22. export const PREHEAT_FILAMENT_ORDER: readonly string[] = [
  23. 'PA-CF',
  24. 'PA',
  25. 'PC',
  26. 'PC-FR',
  27. 'ABS',
  28. 'ASA',
  29. 'PETG-CF',
  30. 'PETG',
  31. 'PLA',
  32. 'TPU',
  33. 'PVA',
  34. 'default',
  35. ];
  36. export function parsePreheatFilamentTargets(raw: string): Record<string, number> {
  37. if (!raw) return { ...DEFAULT_PREHEAT_FILAMENT_TARGETS };
  38. try {
  39. const parsed = JSON.parse(raw);
  40. if (parsed && typeof parsed === 'object' && !Array.isArray(parsed)) {
  41. const out: Record<string, number> = {};
  42. for (const [key, value] of Object.entries(parsed)) {
  43. const num = typeof value === 'number' ? value : Number(value);
  44. if (Number.isFinite(num)) {
  45. out[key] = Math.max(0, Math.min(MAX_CHAMBER_TEMP_C, Math.round(num)));
  46. }
  47. }
  48. if (out.default === undefined) out.default = DEFAULT_PREHEAT_FILAMENT_TARGETS.default;
  49. return out;
  50. }
  51. } catch {
  52. // fall through to defaults
  53. }
  54. return { ...DEFAULT_PREHEAT_FILAMENT_TARGETS };
  55. }
  56. export function serializePreheatFilamentTargets(map: Record<string, number>): string {
  57. // Re-encode in the canonical display order so the on-disk JSON stays
  58. // diff-stable. Strip 0 entries that match the default to keep the payload
  59. // small — except `default` itself, which we always keep for predictability.
  60. const out: Record<string, number> = {};
  61. for (const key of PREHEAT_FILAMENT_ORDER) {
  62. if (map[key] !== undefined) out[key] = map[key];
  63. }
  64. // Preserve any custom keys the user added that aren't in our canonical order.
  65. for (const [key, value] of Object.entries(map)) {
  66. if (!(key in out)) out[key] = value;
  67. }
  68. return JSON.stringify(out);
  69. }