test_archive_nozzle_temperature_of_the_plate.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. """An archive's nozzle temperature is that of the filament its plate prints (PL-C11).
  2. ``project_settings.config`` lists one nozzle temperature per filament of the
  3. whole project. Reading the first entry gave a plate that prints only
  4. filament 3 (PETG at 250 °C) the temperature of filament 1 (PLA at 220 °C).
  5. """
  6. import json
  7. import zipfile
  8. from pathlib import Path
  9. from backend.app.services.archive import ThreeMFParser
  10. def _3mf(path: Path, plates: dict[int, list[int]], unused: tuple[int, ...] = ()) -> Path:
  11. """A 3MF whose plate N prints the filaments listed for it (1-based ids);
  12. ``unused`` ones are listed on every plate with 0 g."""
  13. plate_xml = "".join(
  14. "<plate>"
  15. f"<metadata key='index' value='{index}'/>"
  16. + "".join(f"<filament id='{fid}' type='PLA' color='#FFFFFF' used_g='0'/>" for fid in unused)
  17. + "".join(f"<filament id='{fid}' type='PLA' color='#FFFFFF' used_g='5'/>" for fid in filaments)
  18. + "</plate>"
  19. for index, filaments in plates.items()
  20. )
  21. settings = {"nozzle_temperature": ["220", "230", "250"], "nozzle_diameter": ["0.4"]}
  22. with zipfile.ZipFile(path, "w") as zf:
  23. zf.writestr("Metadata/slice_info.config", f"<?xml version='1.0'?><config>{plate_xml}</config>")
  24. zf.writestr("Metadata/project_settings.config", json.dumps(settings))
  25. zf.writestr("3D/3dmodel.model", "<model/>")
  26. return path
  27. def _nozzle_temperature(path: Path, plate_number: int | None = None) -> int | None:
  28. return ThreeMFParser(path, plate_number=plate_number).parse().get("nozzle_temperature")
  29. def test_a_plate_printing_only_filament_3_has_its_temperature(tmp_path):
  30. assert _nozzle_temperature(_3mf(tmp_path / "a.3mf", {1: [3]})) == 250
  31. def test_the_printed_plate_of_several_decides(tmp_path):
  32. path = _3mf(tmp_path / "a.3mf", {1: [1], 2: [2]})
  33. assert _nozzle_temperature(path, plate_number=2) == 230
  34. assert _nozzle_temperature(path, plate_number=1) == 220
  35. def test_a_plate_of_several_filaments_takes_the_first_it_uses(tmp_path):
  36. assert _nozzle_temperature(_3mf(tmp_path / "a.3mf", {1: [3, 2]})) == 230
  37. def test_a_filament_listed_but_not_used_does_not_count(tmp_path):
  38. assert _nozzle_temperature(_3mf(tmp_path / "a.3mf", {1: [3]}, unused=(1,))) == 250